FileDocCategorySizeDatePackage
TargetBuilder.javaAPI DocGlassfish v2 API14933Fri May 04 22:34:10 BST 2007com.sun.enterprise.admin.target

TargetBuilder

public class TargetBuilder extends Object

Fields Summary
public static final TargetBuilder
INSTANCE
private static final com.sun.enterprise.util.i18n.StringManager
strMgr
i18n strings manager object
private static final String
XPATH_CONFIG_PREFIX
private static final String
XPATH_SERVER_PREFIX
private static final String
XPATH_CLUSTER_PREFIX
private static final String
XPATH_NODE_AGENT_PREFIX
private static final String
XPATH_DOMAIN_PREFIX
private static final String
XPATH_APPLICATIONS_PREFIX
private static final String
XPATH_RESOURCES_PREFIX
Constructors Summary
public TargetBuilder()


     
    
    
Methods Summary
private voidcheckArg(java.lang.Object o, java.lang.Object name)

        
        if (null == o)
        {
            throw new IllegalArgumentException(
                strMgr.getString("target.cant_be_null", name.toString()));
        }
    
public TargetcreateTarget(java.lang.String name, com.sun.enterprise.config.ConfigContext domainContext)

        return createTarget(null, getDefaultValidTargets(), name, domainContext);
    
public TargetcreateTarget(TargetType[] validTypes, java.lang.String name, com.sun.enterprise.config.ConfigContext domainContext)

        return createTarget(null, validTypes, name, domainContext);
    
public TargetcreateTarget(java.lang.String defaultTargetName, java.lang.String name, com.sun.enterprise.config.ConfigContext domainContext)

        return createTarget(defaultTargetName, getDefaultValidTargets(), name, domainContext);
    
public TargetcreateTarget(java.lang.String defaultTargetName, TargetType[] validTypes, java.lang.String name, com.sun.enterprise.config.ConfigContext domainContext)

        checkArg(domainContext, strMgr.getString("target.config_context"));        
        if (name == null || "".equals(name) || "null".equals(name)) { 
            if (defaultTargetName == null || "".equals(defaultTargetName)) {                
                name = getDefaultTarget(domainContext); 
            } else {
                name = defaultTargetName;
            }
        }
        TargetType type = getTargetType(name, domainContext);
        validateTargetType(validTypes, type, name, domainContext);
        
        Target target = null;
        if (type == TargetType.DAS) {
            target = new DASTarget(name, domainContext);
        } else if (type == TargetType.SERVER) {
            target = new ServerTarget(name, domainContext);
        } else if (type == TargetType.DOMAIN) {
            target = new DomainTarget(name, domainContext);
        } else if (type == TargetType.CONFIG) {
            target = new ConfigTarget(name, domainContext);
        } else if (type == TargetType.CLUSTER) {
            target = new ClusterTarget(name, domainContext);
        } else if (type == TargetType.NODE_AGENT) {
            target = new NodeAgentTarget(name, domainContext);
        } else {
            //Should throw more specific exception. This really should never happen
            throw new ConfigException(
                strMgr.getString("target.cant_determine_type", name));
        }
        return target;
    
public TargetcreateTargetForXPath(java.lang.String xpath, com.sun.enterprise.config.ConfigContext domainContext)

        Target target = null;
        if( (target=testForPrefix(xpath, XPATH_CONFIG_PREFIX,  TargetType.CONFIG,  domainContext))!=null ||
            (target=testForPrefix(xpath, XPATH_SERVER_PREFIX,  TargetType.SERVER,  domainContext))!=null ||
           // (target=testForPrefix(xpath, XPATH_CLUSTER_PREFIX, TargetType.CLUSTER, domainContext))!=null ||
            (target=testForPrefix(xpath, XPATH_NODE_AGENT_PREFIX, TargetType.NODE_AGENT, domainContext))!=null ||
            // DOMAIN should be last 
            (target=testForPrefix(xpath, XPATH_DOMAIN_PREFIX, TargetType.DOMAIN, domainContext))!=null )
            return target;
        return null;
    
private static java.lang.String[]extractElementNameAndTypeForPrefix(java.lang.String xpath, java.lang.String prefix)

    
    /* 
     *  private helper method - returns name for named prefixed xpath
     *  if prefix matches xpath
     */
           
                         
    
        xpath = xpath.trim();
        if(xpath!=null && xpath.startsWith(prefix) && xpath.endsWith("']"))
        {
            xpath = xpath.substring(prefix.length(), xpath.length()-2);
            int idx   = xpath.lastIndexOf('[");
            if(idx<0)
                return null;
            String type = xpath.substring(0, idx);
            if(type.indexOf('\'")>=0)
                return null;
            idx = xpath.indexOf('\'", idx);
            if(idx<0)
                return null;
            String key = xpath.substring(idx+1);
            if(key.indexOf('\'")>=0)
                return null;
            return (new String[]{key, type});
        }
        return null;
    
private static java.lang.StringextractElementNameForPrefix(java.lang.String xpath, java.lang.String prefix)

        String name = null;
        if(xpath!=null && xpath.startsWith(prefix))
        {
            if(prefix.endsWith("'"))
            {
                int    beg = prefix.length();
                int    end = xpath.indexOf("'", beg); 
                if(end>=0)
                    name = xpath.substring(beg, end);
            }
        }
        return name;
    
protected java.lang.StringgetDefaultTarget(com.sun.enterprise.config.ConfigContext cc)

        //We assume that the 0th server is the DAS
        Server[] servers = ServerHelper.getServersInDomain(cc);
        if (servers.length > 0) {
            return servers[0].getName();
        } else {
            throw new ConfigException(
                strMgr.getString("target.no_servers"));
        }
    
protected TargetType[]getDefaultValidTargets()

        return new TargetType[] {TargetType.DAS};
    
public java.lang.StringgetTargetNameForXPath(java.lang.String xpath, com.sun.enterprise.config.ConfigContext ctx, boolean bIncludingAppAndRes)

        
        if(bIncludingAppAndRes)
        {
            //now time for applications
            String[] pair = null;
            if((pair=extractElementNameAndTypeForPrefix(xpath, XPATH_APPLICATIONS_PREFIX))!=null)
               return ServerTags.APPLICATIONS+"|"+pair[0]+"|"+pair[1];
            //now time for applications
            if((pair=extractElementNameAndTypeForPrefix(xpath, XPATH_RESOURCES_PREFIX))!=null)
               return ServerTags.RESOURCES+"|"+pair[0]+"|"+pair[1];
                
        }
        Target target = createTargetForXPath(xpath, ctx);
        return target.getName();        
    
private TargetTypegetTargetType(java.lang.String targetName, com.sun.enterprise.config.ConfigContext domainContext)

        TargetType targetType;
        if (targetName.equals(IAdminConstants.DOMAIN_TARGET)) {
            targetType = TargetType.DOMAIN;
        } else if (ConfigAPIHelper.isAConfig(domainContext, targetName)) {
            targetType = TargetType.CONFIG;                             
        } else if (ServerHelper.isAServer(domainContext, targetName)) {
            if (ServerHelper.isDAS(domainContext, targetName)) {
                targetType = TargetType.DAS;
            } else {
                targetType = TargetType.SERVER;                       
            }
        } else if (ClusterHelper.isACluster(domainContext, targetName)) {
            targetType = TargetType.CLUSTER;
        } else if (NodeAgentHelper.isANodeAgent(domainContext, targetName)) {
            targetType = TargetType.NODE_AGENT;
        } else {
            //Should throw more specific exception
            throw new ConfigException(
                strMgr.getString("target.cant_determine_type", targetName));
        }
        return targetType;
    
private TargettestForPrefix(java.lang.String xpath, java.lang.String prefix, TargetType type, com.sun.enterprise.config.ConfigContext domainContext)

        if(xpath!=null && xpath.startsWith(prefix))
        {
            String name = extractElementNameForPrefix(xpath, prefix);
            return createTarget(type.getName(), null,  name, domainContext);
        }
        return null;
    
private voidvalidateTargetType(TargetType[] validTypes, TargetType targetType, java.lang.String targetName, com.sun.enterprise.config.ConfigContext domainContext)
Validate that the given target is one of the specified types.

        if (validTypes != null) {
            boolean isValid = false;
            String validTargets = new String();
            for (int i = 0; i < validTypes.length; i++) {
                if (targetType == validTypes[i]) {                    
                    isValid = true;
                } else if (targetType == TargetType.SERVER) {
                    if (validTypes[i] == TargetType.STANDALONE_SERVER) {
                        if (ServerHelper.isServerStandAlone(domainContext, targetName)) {
                            isValid = true;
                        }
                    } else if (validTypes[i] == TargetType.UNCLUSTERED_SERVER) {
                        if (!ServerHelper.isServerClustered(domainContext, targetName)) {
                            isValid = true;
                        }
                    } 
                } else if (targetType == TargetType.CLUSTER && validTypes[i] == TargetType.STANDALONE_CLUSTER) {
                    if (ClusterHelper.isClusterStandAlone(domainContext, targetName)) {
                        isValid = true;
                    }
                }
                validTargets += validTypes[i].getName();
                if (i < validTypes.length - 1) {
                    validTargets += ", ";
                }
            }
            if (!isValid) {
                throw new ConfigException(strMgr.getString("target.invalid_type",
                    targetName, targetType.getName(), validTargets));
            }
        }