Tuesday, June 14, 2011

Using API to get WebSphere Application Server Information--Connect to WebSphere Application Server

In general, we connect to WebSphere Application Server Instance using jython or jacl scripts . IBM WebSphere Application Server also provide various APIs to let the user to connect to the server instance and get the server information. The following is the code to use API to connect to WAS server application.

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.Session;
import com.ibm.websphere.management.configservice.ConfigService;
import com.ibm.websphere.management.configservice.ConfigServiceHelper;
import com.ibm.websphere.management.configservice.ConfigServiceProxy;
import com.ibm.websphere.management.exception.ConfigServiceException;
import com.ibm.websphere.management.exception.ConnectorException;
import com.ibm.websphere.management.exception.AdminException;
import com.ibm.websphere.management.application.*;
import com.ibm.websphere.pmi.*;
import com.ibm.websphere.pmi.stat.*;

public static void connectToServer(){

Properties props = new Properties();
props.put("type", "SOAP");
props.put("host", hostName);
props.put("port", "8880");
props.put("username", "username");
props.put("password", "password");

//***********************************************************
// if global security enabled. you need the following configurations
//***********************************************************

props.put(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
props.put("com.ibm.ssl.keyStoreFileBased", "true");
props.put("com.ibm.ssl.trustStoreFileBased", "true");
props.put("com.ibm.ssl.keyStore", keyStoreFile);
props.put("javax.net.ssl.keyStore", keyStoreFile);
props.put("com.ibm.ssl.keyStorePassword", keyPassword);
props.put("javax.net.ssl.keyStorePassword", keyPassword);
if (keyStoreFile.endsWith(".p12") || keyStoreFile.endsWith(".P12"))
{
props.put("com.ibm.ssl.keyStoreType", "PKCS12");
props.put("javax.net.ssl.keyStoreType", "PKCS12");
} else
{
props.put("com.ibm.ssl.keyStoreType", "JKS");
props.put("javax.net.ssl.keyStoreType", "JKS");
}
props.put("com.ibm.ssl.trustStore", trustStoreFile);
props.put("javax.net.ssl.trustStore", trustStoreFile);
props.put("com.ibm.ssl.trustStorePassword", trustPassword);
props.put("javax.net.ssl.trustStorePassword", trustPassword);
if (trustStoreFile.endsWith(".p12") || trustStoreFile.endsWith(".P12"))
{
props.put("com.ibm.ssl.trustStoreType", "PKCS12");
props.put("javax.net.ssl.trustStoreType", "PKCS12");
} else
{
props.put("com.ibm.ssl.trustStoreType", "JKS");
props.put("javax.net.ssl.trustStoreType", "JKS");
}
try
{
adminClient = AdminClientFactory.createAdminClient(props);
System.out.println("Connect to "+hostName+":"+portNumber+" Successfully");
}
catch (ConnectorException e)
{
System.out.println("Failed to Connect to "+hostName+":"+portNumber);
System.out.println("Exception creating admin client: " + e);
e.printStackTrace();
System.exit(-1);
}
}

it is better to use IBM WebSphere Application java  to run the application. I see some exceptions if we use SUN Java.

2 comments:

  1. catch(com.ibm.websphere.management.exception.ConnectorException ConnectorException ){

    System.out.println("Failed to Connect to "+hostName+":"+portNumber);
    System.out.println("Exception creating admin client: " + ConnectorException);
    e.printStackTrace();
    System.exit(-1);

    }

    but at catch it says "incompatible types: ConnectorException cannot be converted to Throwable"

    please answer as soon as possible. thanks

    ReplyDelete
  2. and i have downloaded API from http://www.java2s.com/Code/Jar/a/Downloadadminjar.htm

    ReplyDelete