HP Integrity NonStop H-Series User Manual
Page 106

* Constructor - Instantiates the NSASJ Client Object. The NSASJ Client Objec is used to connect
* to host controller, execute commands and disconnect from HC
*
* @param - properties file that contains the connection params
* */
public NSASJClient(String propFile) {
Properties connProp;
try {
connProp = Utility.getClientProperties(propFile);
if (connProp.containsKey("host") && connProp.getProperty("host") != null)
this.host = connProp.getProperty("host");
if (connProp.containsKey("port") && connProp.getProperty("port") != null)
this.port = Integer.valueOf(connProp.getProperty("port"));
if (connProp.containsKey("realm"))
this.RealmName = connProp.getProperty("realm");
if (connProp.containsKey("user"))
this.userid = connProp.getProperty("user");
if (connProp.containsKey("password")) {
this.password = connProp.getProperty("password");
} else {
System.out.println("Password not specified. Hence exiting.....");
System.exit(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* method - disconnectFromHC(). Used to disconnect from the HostController NSASJ Client is closed.
*/
protected void disconnectFromHC() {
this.logger.info("Disconnecting from HC ......");
try {
if (this.client != null)
this.client.close();
} catch (Exception e) {
this.logger.error("Exception: " + e.getMessage());
e.printStackTrace();
System.exit(0);
}
}
/**
* method - executeCommand() is an abstract method, which the inheriting classes will override to provide
their
* functionality
*/
public abstract void executeCommand();
/**
* method - connectToHC(). Used to connect to the HostController NSASJ Client is created which can be used
by subsequent
* commands to communicate with the HostController and pass on the commands.
*/
protected void connectToHC() {
try {
this.logger.info("Connecting to HC @ " + host + ":" + port);
this.client = createClient(InetAddress.getByName(host), port, userid, password.toCharArray(),
RealmName);
this.logger.trace("Got the client: " + client);
} catch (Exception e) {
this.logger.error("Unable to connect to HC @ " + host + ":" + port);
this.logger.error("Exception: " + e.getMessage());
if (this.client != null)
this.disconnectFromHC();
System.exit(0);
}
}
/**
* method - createClient(). The method creates the client and connects to the host and port of the Controller
and
* authenticates using the credentials provided in the properties file.
*
* @param host- IP Address of the Host Controller port -The native management port where the Host Controller
is listening
* username - user name to be used for the connection password - to be used for the connection
securityRealm - The
* security Realm to which the user belongs to (preferably the ManagementRealm).
*/
private ModelControllerClient createClient(final InetAddress host, final int port, final String username,
final char[] password, final String securityRealmName) {
final CallbackHandler callbackHandler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback current : callbacks) {
if (current instanceof NameCallback) {
NameCallback ncb = (NameCallback) current;
logger.trace("\n\t\tncb.setName() = " + username);
106 Programmatic Management of NSASJ using API