FileDocCategorySizeDatePackage
ApplicationsConfigMBeanTest.javaAPI DocGlassfish v2 API14126Fri May 04 22:24:18 BST 2007com.sun.enterprise.admin.mbeans

ApplicationsConfigMBeanTest

public class ApplicationsConfigMBeanTest extends TestCase

Fields Summary
private JMXConnector
connector
private static final String
HOST
private static final String
PORT
private static final String
ADMIN_USER
private static final String
ADMIN_PASSWORD
private static final String
APPLICATION
Constructors Summary
public ApplicationsConfigMBeanTest(String name)

    
         
        super(name);
    
Methods Summary
public voidcheckDeploymentPassed(java.lang.String appName, java.lang.String[] targets)

        final MBeanServerConnection server = getMBeanServer();
        try{
            ObjectName clusters = 
                new ObjectName("com.sun.appserv:type=clusters,category=config");
            for(int i = 0; i < targets.length; i++) {
                ObjectName name = (ObjectName)server.invoke(clusters, 
                    "getClusterByName", new Object[]{targets[i]}, 
                    new String[]{"java.lang.String"});
                if(name != null) {
                    System.out.println("What is the objectname: " + name);
                    String clusterName = 
                        (String)server.getAttribute(name, "name");
                    ObjectName cluster = new ObjectName(
                        "com.sun.appserv:type=cluster,name=" + 
                        clusterName + ",category=config");
                    ObjectName[] deployedObjs = (ObjectName[])server.invoke(
                        cluster,"getApplicationRef", 
                        new Object[]{}, new String[]{}); 
                    for(int z = 0; z < deployedObjs.length; z++) {
                        String objName = 
                           (String)server.getAttribute(deployedObjs[z], "name");
                        if(appName.equals(objName)) {
                            System.out.println("Application deployed " + 
                                "successfully on cluster: " + clusterName);
                        }
                    }
                } else {
                    System.out.println("No clusters found from target list!");
                }
            }//end first for loop
        }catch(Exception e){
            e.printStackTrace();
        }
    
public voiddeployApplication(java.io.File appArchive, java.lang.String[] targets)

        final MBeanServerConnection server = getMBeanServer();
        java.util.Properties myProps = getDeploymentProperties();
        try {
             String methodToInvoke = "deploy";
             ObjectName objectName = 
                    new ObjectName(
                        "com.sun.appserv:type=applications,category=config");       
             printMBeanTestInfo(objectName);
             Object deployResult = server.invoke(objectName,  
                methodToInvoke, new Object[]{myProps, targets},
                new String[]{"java.util.Properties", "[Ljava.lang.String;"});
        } catch(Throwable e) {
            System.out.println("Something Failed!");
            e.printStackTrace();
        }
    
public voidgetClusters()

       final MBeanServerConnection server = getMBeanServer();
       try {
             String methodToInvoke = "getCluster";
             ObjectName objectName = 
                    new ObjectName(
                        "com.sun.appserv:type=clusters,category=config"); 
             ObjectName[] result = (ObjectName[])server.invoke(objectName,  
                methodToInvoke, new Object[]{},
                new String[]{});
             System.out.println("\n");
             for(int i = 0; i < result.length; i++) {
                System.out.println("Cluster: " + result[i]);
             }
        } catch(Throwable e) {
            System.out.println("Something failed when getting all targets!");
            e.printStackTrace();
        }
    
public java.util.PropertiesgetDeploymentProperties()

        java.util.Properties props = new java.util.Properties();
        props.setProperty(DeploymentProperties.ARCHIVE_NAME, APPLICATION);
        props.setProperty(DeploymentProperties.VERIFY, "false");
        props.setProperty(DeploymentProperties.PRECOMPILE_JSP, "false");
        props.setProperty(DeploymentProperties.ENABLE, "true");
        props.setProperty(DeploymentProperties.FORCE, "true");
        return props;
    
private javax.management.MBeanServerConnectiongetMBeanServer()

        MBeanServerConnection conn = null;
        try {
            conn = connector.getMBeanServerConnection();
        } catch(Exception e) {
            System.out.println("Could not retrieve connection! " 
                + e.getMessage());
            e.printStackTrace();
        }
        return conn;
    
private javax.management.ObjectNamegetSampleObjectName()

        ObjectName name = null;
        try {
            name = 
                new ObjectName("com.sun.appserv:type=jdbc-connection-pool," + 
                    "name=__TimerPool,category=config");
        } catch(Exception e) {
            e.printStackTrace();
        }
        return name;
    
public voidgetServers()

       final MBeanServerConnection server = getMBeanServer();
       try {
             String methodToInvoke = "getServer";
             ObjectName objectName = 
                    new ObjectName(
                        "com.sun.appserv:type=servers,category=config"); 
             ObjectName[] result = (ObjectName[])server.invoke(objectName,  
                methodToInvoke, new Object[]{},
                new String[]{});
             System.out.println("\n");
             for(int i = 0; i < result.length; i++) {
                System.out.println("Server: " + result[i]);
             }
        } catch(Throwable e) {
            System.out.println("Something failed when getting all targets!");
            e.printStackTrace();
        }
    
public java.lang.String[]getTargets()

       final MBeanServerConnection server = getMBeanServer();
       String[] targets = null;
       System.out.println("Testing getTargets!");
       try {
             String methodToInvoke = "getTargets";
             ObjectName objectName = 
                    new ObjectName(
                        "com.sun.appserv:type=applications,category=config"); 
             targets = (String[])server.invoke(objectName,  
                methodToInvoke, new Object[]{},
                new String[]{});
             System.out.println("\n");
             for(int i = 0; i < targets.length; i++) {
                System.out.println("Target: " + targets[i]);
             }
            
        } catch(Throwable e) {
            System.out.println("Something failed when getting all targets!");
            e.printStackTrace();
        }
       return targets;
    
private voidinitConnector()

        try {
            connector = JMXConnectorFactory.connect(
                new JMXServiceURL("service:jmx:s1ashttp://" + HOST + 
                    ":" + PORT), initConnectorEnvironment());
        } catch (Exception ex) {
            System.out.println("Remote Connect Failed!!!");
            System.out.println(ex.getMessage());
            throw new RuntimeException(ex);
        }
    
private java.util.MapinitConnectorEnvironment()

        final java.util.Map env = new java.util.HashMap();
        final String PKGS = "com.sun.enterprise.admin.jmx.remote.protocol";
        env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, PKGS);
        env.put(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME, ADMIN_USER);
        env.put(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME, ADMIN_PASSWORD);
        env.put(DefaultConfiguration.HTTP_AUTH_PROPERTY_NAME,
                DefaultConfiguration.DEFAULT_HTTP_AUTH_SCHEME);
        return (env);
    
public static voidmain(java.lang.String[] args)

        final TestRunner runner= new TestRunner();
        final TestResult result = runner.doRun(ApplicationsConfigMBeanTest.suite(), false);
        System.exit(result.errorCount() + result.failureCount());
    
public voidprintAllDeployedComponents()

       final MBeanServerConnection server = getMBeanServer();
       try {
             String methodToInvoke = "getAllDeployedComponents";
             ObjectName objectName = 
                    new ObjectName(
                        "com.sun.appserv:type=applications,category=config"); 
             ObjectName[] result = (ObjectName[])server.invoke(objectName,  
                methodToInvoke, new Object[]{},
                new String[]{});
             for(int i = 0; i < result.length; i++) {
                System.out.println("Deployed: " + result[i]);
             }
        } catch(Throwable e) {
            System.out.println("Something failed when getting all deployed components!");
            e.printStackTrace();
        }
    
private voidprintMBeanTestInfo(javax.management.ObjectName objectName)

        System.out.println("Testing mbean invocation on <" + objectName + ">");
    
protected voidsetUp()

        initConnector();
    
public static junit.framework.Testsuite()

        TestSuite suite = new TestSuite(ApplicationsConfigMBeanTest.class);
        return suite;
    
protected voidtearDown()

    
public voidtestTargetAwareDeployment()
This test is used as a driver to test the deployment of an application to multiple targets against a running instance of the application server. Uncomment the lines within this method and set the appropriate class member variables to administer the test.

author
Rob Ruyak

        final java.io.File appArchive = 
            new java.io.File(APPLICATION);
        printAllDeployedComponents();
        //deployApplication(appArchive,getTargets());
        printAllDeployedComponents();
        //checkDeploymentPassed("stateless-simple", getTargets());
        //undeployApplication(appArchive, getTargets());
    
public voidundeployApplication(java.io.File appArchive, java.lang.String[] targets)

        final MBeanServerConnection server = getMBeanServer();
        java.util.Properties myProps = getDeploymentProperties();
        try {
             String methodToInvoke = "undeploy";
             ObjectName objectName = 
                    new ObjectName(
                        "com.sun.appserv:type=applications,category=config"); 
             Object undeployResult = server.invoke(objectName,  
                methodToInvoke, new Object[]{myProps, targets},
                new String[]{"java.util.Properties", "[Ljava.lang.String;"});
        } catch(Throwable e) {
            System.out.println("Something failed when undeploying!");
            e.printStackTrace();
        }