FileDocCategorySizeDatePackage
AppServerContextImpl.javaAPI DocGlassfish v2 API15339Fri May 04 22:30:24 BST 2007com.sun.enterprise.jbi

AppServerContextImpl

public class AppServerContextImpl extends Object implements AppServerContext
The JBI Runtime needs information on the servers/clusters in a domain, state of an instance etc. This is the interface to get this application server context information.
author
Sun Microsystems, Inc.

Fields Summary
private static String
SERVER
private static Logger
_logger
private Method
irMethod
private final String
INSTANCE_REGISTRY
Constructors Summary
public AppServerContextImpl()

    
     
    
        try {
                irMethod = (Method) java.security.AccessController.doPrivileged
                (new java.security.PrivilegedExceptionAction() {
                    public java.lang.Object run() throws Exception {
                        Class cls = Class.forName(INSTANCE_REGISTRY);
                        return cls.getMethod("getInstanceConnection", 
                        new Class[] {String.class});
                    }
                });
        } catch (Exception e) {
             _logger.log(Level.SEVERE, e.getMessage(), e);
             throw new RuntimeException(e.getMessage());
        }    
    
Methods Summary
private java.lang.StringgetClusterForInstance(java.lang.String instanceName)

return
the name of the cluster the instance belongs to. If the instance does not belong to a cluster a null is returned.

        String clusterName = null;
        if ( isInstanceClustered(instanceName) )
        {
        	Cluster 
            	cluster = ClusterHelper.getClusterForInstance(getConfigContext(), instanceName);
        	clusterName = cluster.getName();
        }
        return clusterName;
    
private java.lang.String[]getClusterNames(com.sun.enterprise.config.serverbeans.Cluster[] clusters)

return
a String array containing the cluster names.

        String[] domClusters = new String[clusters.length];
        for (int i = 0; i < clusters.length; i++) 
        {
            domClusters[i] = clusters[i].getName();
        }
        return domClusters;
    
public java.lang.String[]getClusterNames()
Get the names of all the clsuters in the domain.

return
an array of names of all the clusters in the domain. If the domain has zero clusters then an empty array is returned.

        try
        {
            Cluster[] clusters = ClusterHelper.getClustersInDomain(getConfigContext());
            return getClusterNames(clusters);
        }
        catch (Exception ex)
        {
            _logger.log(Level.WARNING, ex.getMessage(), ex);
            return new String[0];
        }
    
private com.sun.enterprise.config.ConfigContextgetConfigContext()

return
the ConfigContext based on whether this is DAS or non-DAS instance.

        ConfigContext ctx = null;
        if (isDAS())
        {
            if ( AdminService.getAdminService() != null )
            {
                ctx = AdminService.getAdminService().getAdminContext().getAdminConfigContext();   
            }
        }
        else
        {
            if (ApplicationServer.getServerContext() != null)
            {
                ctx = ApplicationServer.getServerContext().getConfigContext();   
            }
        }
        return ctx;
    
public java.lang.StringgetInstanceName()
Get the name of the server instance. In case this is the central administartion server then instance name = "server".

return
the name of the glassfish instance.

        return System.getProperty("com.sun.aas.instanceName");
    
public javax.management.MBeanServerConnectiongetMBeanServerConnection(java.lang.String instanceName)
Get the MBeanServerConnection for a specific instance.

param
instanceName - instance name, the MBeanServerConnection for which is to be obtained.
return
the MBeanServerConnection to a given instance.
throws
Exception on errors

            // If the instance name is the same as the local instance return the
            // local MBeanServer
            if ( getInstanceName().equals(instanceName))
            {
                return getPlatformMBeanServer();
            }
            
            if ( isDAS() && !multipleServersSupported() )
            {
                // Developer profile
                throw new Exception("Developer profile does not support multiple server instances");
            }
            else
            {
                return (MBeanServerConnection) java.security.AccessController.doPrivileged
                    (new java.security.PrivilegedExceptionAction() {
                     public java.lang.Object run() throws Exception {
                         return irMethod.invoke(null, new Object[] {instanceName});
                     }
                });
             }
    
private javax.management.MBeanServergetPlatformMBeanServer()

return
the DAS MBeanServer

        return java.lang.management.ManagementFactory.getPlatformMBeanServer();
    
private java.lang.String[]getServerNames(com.sun.enterprise.config.serverbeans.Server[] servers)

return
a Set containing the server names.

        String[] ucServers = new String[servers.length];
        for (int i = 0; i < servers.length; i++) 
        {
            ucServers[i] = servers[i].getName();
        }
        return ucServers;
    
public java.lang.String[]getServersInCluster(java.lang.String clusterName)
Get the names of all the member servers in the specified cluster.

return
an array of names of all the servers in the cluster. If the clusterName is a non-existent cluster or does not have any member instances then an empty array is returned

        try
        {
            Server[] servers = ServerHelper.getServersInCluster(getConfigContext(), 
                clusterName);
            return getServerNames(servers);
        }
        catch (Exception ex)
        {
            _logger.log(Level.WARNING, ex.getMessage(), ex);
            return new String[0];
        }  
    
public java.lang.String[]getStandaloneServerNames()
Get the names of all the non-clustered standalone servers in the domain.

return
an array of names of all the standalone servers in the domain.

        try
        {
            Server[] servers = ServerHelper.getUnclusteredServers(getConfigContext(), false);
            return getServerNames(servers);
        }
        catch (Exception ex)
        {
            _logger.log(Level.WARNING, ex.getMessage(), ex);
            return new String[0];
        }
    
public java.lang.StringgetTargetName(java.lang.String instanceName)
Get the "target" for the specified instance.
  • is the central admininistration server instance then target = "server"
  • is a clustered instance then target = [cluster-name]
  • is a standalone instance then target = [server-name]

param
instanceName - name of the instance, whose target is to be determined
return
the "target" for the specific instance

        String targetName = instanceName;
        if ( isInstanceClustered(instanceName) )
        {
            // -- Target is the cluster name
            targetName  = getClusterForInstance(instanceName);
        }
        return targetName;
    
public java.lang.StringgetTargetName()
Get the "target" for this instance. If this instance :
  • is the central admininistration server instance then target = "server"
  • is a clustered instance then target = [cluster-name]
  • is a standalone instance then target = [server-name]

This is equivalent to calling getTargetName(getInstanceName()).

return
the "target" for this instance.

        String instanceName = getInstanceName();
        return getTargetName(instanceName);
    
public booleanisDAS()
Determine if this instance is the central administration server.

return
true if this instance is the central administration server.

        AdminService adminSvc = AdminService.getAdminService();
        boolean isDAS = false;
        if (adminSvc != null)
        {
            isDAS =  adminSvc.isDas();
        }
        return isDAS;
    
public booleanisInstanceClustered(java.lang.String instanceName)

return
true if the instance is clustered
param
instanceName - instance name

        boolean isClustered = false;
        ConfigContext ctx = getConfigContext();
        if ( ctx != null )
        {
            isClustered = ServerHelper.isServerClustered(ctx, instanceName);
        }
        return isClustered;
    
public booleanisInstanceUp(java.lang.String instanceName)
Determine the runtime state of an instance.

param
instanceName - name of the instance whose state is to be determined.
return
true if the specified instance is running false is it is shutdown

        boolean isRunning = false;
        if ( isDAS() )
        {
            if ( SERVER.equals(instanceName))
            {
                // -- If DAS is running, so is the "server" instance.
                isRunning = true;
            }
            else
            {
                String  
                    instanceObjName = "amx:J2EEServer=" + instanceName + ",j2eeType=JVM,*";
                    
                ObjectName objName = null;
                try
                {
                    objName =  new ObjectName(instanceObjName);
                }
                catch(javax.management.MalformedObjectNameException mex)
                {
                    _logger.log(Level.SEVERE, mex.getMessage(), mex);
                    return false;
                }

                java.util.Set<ObjectName> nameSet = getPlatformMBeanServer().queryNames(objName, null);

                if ( (!nameSet.isEmpty()) )
                {
                    isRunning = true;
                }
            }
        }
        return isRunning;
    
public booleanmultipleServersSupported()
Determine if the central administraion server supports multiple servers.

return
true if this administration server supports multiple servers.

        if ( isDAS())
        {
            MBeanServer mbeanServer = java.lang.management.ManagementFactory.getPlatformMBeanServer();
            DomainRoot domainRoot = ProxyFactory.getInstance(mbeanServer).getDomainRoot();

            return domainRoot.getSystemInfo().supportsFeature(
                SystemInfo.MULTIPLE_SERVERS_FEATURE);
        }
        else
        {
            return false;
        }