1 23 24 27 28 package com.sun.enterprise.admin.target; 29 30 import com.sun.enterprise.config.ConfigContext; 32 import com.sun.enterprise.config.ConfigException; 33 34 import com.sun.enterprise.util.i18n.StringManager; 35 import com.sun.enterprise.admin.util.IAdminConstants; 36 37 import com.sun.enterprise.config.serverbeans.Server; 38 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper; 39 import com.sun.enterprise.config.serverbeans.ServerHelper; 40 import com.sun.enterprise.config.serverbeans.NodeAgentHelper; 41 import com.sun.enterprise.config.serverbeans.ClusterHelper; 42 import com.sun.enterprise.config.serverbeans.ServerTags; 43 44 import com.sun.enterprise.server.pluggable.PluggableFeatureFactory; 45 import com.sun.enterprise.server.ApplicationServer; 46 47 public class TargetBuilder 48 { 49 public static TargetBuilder INSTANCE; 50 51 static { 52 PluggableFeatureFactory featureFactory = 53 ApplicationServer.getServerContext().getPluggableFeatureFactory(); 54 TargetFactory tFactory = featureFactory.getTargetFactory(); 55 INSTANCE = tFactory.getTargetBuilder(); 56 } 57 58 61 private static final StringManager strMgr = 62 StringManager.getManager(TargetBuilder.class); 63 64 public TargetBuilder() 65 { 66 } 67 68 public Target createTarget(String name, ConfigContext domainContext) 69 throws ConfigException 70 { 71 return createTarget(null, getDefaultValidTargets(), name, domainContext); 72 } 73 74 protected TargetType[] getDefaultValidTargets() 75 { 76 return new TargetType[] {TargetType.DAS}; 77 } 78 79 public Target createTarget(TargetType validTypes[], String name, ConfigContext domainContext) 80 throws ConfigException 81 { 82 return createTarget(null, validTypes, name, domainContext); 83 } 84 85 public Target createTarget(String defaultTargetName, String name, ConfigContext domainContext) 86 throws ConfigException 87 { 88 return createTarget(defaultTargetName, getDefaultValidTargets(), name, domainContext); 89 } 90 91 public Target createTarget(String defaultTargetName, TargetType validTypes[], 92 String name, ConfigContext domainContext) throws ConfigException 93 { 94 checkArg(domainContext, strMgr.getString("target.config_context")); 95 if (name == null || "".equals(name) || "null".equals(name)) { 96 if (defaultTargetName == null || "".equals(defaultTargetName)) { 97 name = getDefaultTarget(domainContext); 98 } else { 99 name = defaultTargetName; 100 } 101 } 102 TargetType type = getTargetType(name, domainContext); 103 validateTargetType(validTypes, type, name, domainContext); 104 105 Target target = null; 106 if (type == TargetType.DAS) { 107 target = new DASTarget(name, domainContext); 108 } else if (type == TargetType.SERVER) { 109 target = new ServerTarget(name, domainContext); 110 } else if (type == TargetType.DOMAIN) { 111 target = new DomainTarget(name, domainContext); 112 } else if (type == TargetType.CONFIG) { 113 target = new ConfigTarget(name, domainContext); 114 } else if (type == TargetType.CLUSTER) { 115 target = new ClusterTarget(name, domainContext); 116 } else if (type == TargetType.NODE_AGENT) { 117 target = new NodeAgentTarget(name, domainContext); 118 } else { 119 throw new ConfigException( 121 strMgr.getString("target.cant_determine_type", name)); 122 } 123 return target; 124 } 125 126 129 private void validateTargetType(TargetType[] validTypes, TargetType targetType, String targetName, 130 ConfigContext domainContext) throws ConfigException 131 { 132 if (validTypes != null) { 133 boolean isValid = false; 134 String validTargets = new String (); 135 for (int i = 0; i < validTypes.length; i++) { 136 if (targetType == validTypes[i]) { 137 isValid = true; 138 } else if (targetType == TargetType.SERVER) { 139 if (validTypes[i] == TargetType.STANDALONE_SERVER) { 140 if (ServerHelper.isServerStandAlone(domainContext, targetName)) { 141 isValid = true; 142 } 143 } else if (validTypes[i] == TargetType.UNCLUSTERED_SERVER) { 144 if (!ServerHelper.isServerClustered(domainContext, targetName)) { 145 isValid = true; 146 } 147 } 148 } else if (targetType == TargetType.CLUSTER && validTypes[i] == TargetType.STANDALONE_CLUSTER) { 149 if (ClusterHelper.isClusterStandAlone(domainContext, targetName)) { 150 isValid = true; 151 } 152 } 153 validTargets += validTypes[i].getName(); 154 if (i < validTypes.length - 1) { 155 validTargets += ", "; 156 } 157 } 158 if (!isValid) { 159 throw new ConfigException(strMgr.getString("target.invalid_type", 160 targetName, targetType.getName(), validTargets)); 161 } 162 } 163 } 164 165 private TargetType getTargetType(String targetName, ConfigContext domainContext) 166 throws ConfigException 167 { 168 TargetType targetType; 169 if (targetName.equals(IAdminConstants.DOMAIN_TARGET)) { 170 targetType = TargetType.DOMAIN; 171 } else if (ConfigAPIHelper.isAConfig(domainContext, targetName)) { 172 targetType = TargetType.CONFIG; 173 } else if (ServerHelper.isAServer(domainContext, targetName)) { 174 if (ServerHelper.isDAS(domainContext, targetName)) { 175 targetType = TargetType.DAS; 176 } else { 177 targetType = TargetType.SERVER; 178 } 179 } else if (ClusterHelper.isACluster(domainContext, targetName)) { 180 targetType = TargetType.CLUSTER; 181 } else if (NodeAgentHelper.isANodeAgent(domainContext, targetName)) { 182 targetType = TargetType.NODE_AGENT; 183 } else { 184 throw new ConfigException( 186 strMgr.getString("target.cant_determine_type", targetName)); 187 } 188 return targetType; 189 } 190 191 protected String getDefaultTarget(ConfigContext cc) throws ConfigException 192 { 193 Server[] servers = ServerHelper.getServersInDomain(cc); 195 if (servers.length > 0) { 196 return servers[0].getName(); 197 } else { 198 throw new ConfigException( 199 strMgr.getString("target.no_servers")); 200 } 201 } 202 203 private void checkArg(Object o, Object name) 204 { 205 if (null == o) 206 { 207 throw new IllegalArgumentException ( 208 strMgr.getString("target.cant_be_null", name.toString())); 209 } 210 } 211 212 216 private static final String XPATH_CONFIG_PREFIX = "/" + ServerTags.DOMAIN + "/" + 218 ServerTags.CONFIGS + "/" + 219 ServerTags.CONFIG + "[@" + ServerTags.NAME + "='"; 220 private static final String XPATH_SERVER_PREFIX = "/" + ServerTags.DOMAIN + "/" + 221 ServerTags.SERVERS + "/" + 222 ServerTags.SERVER + "[@" + ServerTags.NAME + "='"; 223 private static final String XPATH_CLUSTER_PREFIX = "/" + ServerTags.DOMAIN + "/" + 224 ServerTags.CLUSTERS + "/" + 225 ServerTags.CLUSTER + "[@" + ServerTags.NAME + "='"; 226 private static final String XPATH_NODE_AGENT_PREFIX = "/" + ServerTags.DOMAIN + "/" + 227 ServerTags.NODE_AGENTS + "/" + 228 ServerTags.NODE_AGENT + "[@" + ServerTags.NAME + "='"; 229 private static final String XPATH_DOMAIN_PREFIX = "/" + ServerTags.DOMAIN + "/"; 230 231 private static final String XPATH_APPLICATIONS_PREFIX = "/" + ServerTags.DOMAIN + "/" + ServerTags.APPLICATIONS + "/"; 232 private static final String XPATH_RESOURCES_PREFIX = "/" + ServerTags.DOMAIN + "/" + ServerTags.RESOURCES + "/"; 233 234 238 private static String [] extractElementNameAndTypeForPrefix(String xpath, String prefix) 239 throws ConfigException 240 { 241 xpath = xpath.trim(); 242 if(xpath!=null && xpath.startsWith(prefix) && xpath.endsWith("']")) 243 { 244 xpath = xpath.substring(prefix.length(), xpath.length()-2); 245 int idx = xpath.lastIndexOf('['); 246 if(idx<0) 247 return null; 248 String type = xpath.substring(0, idx); 249 if(type.indexOf('\'')>=0) 250 return null; 251 idx = xpath.indexOf('\'', idx); 252 if(idx<0) 253 return null; 254 String key = xpath.substring(idx+1); 255 if(key.indexOf('\'')>=0) 256 return null; 257 return (new String []{key, type}); 258 } 259 return null; 260 } 261 262 266 private static String extractElementNameForPrefix(String xpath, String prefix) 267 throws ConfigException 268 { 269 String name = null; 270 if(xpath!=null && xpath.startsWith(prefix)) 271 { 272 if(prefix.endsWith("'")) 273 { 274 int beg = prefix.length(); 275 int end = xpath.indexOf("'", beg); 276 if(end>=0) 277 name = xpath.substring(beg, end); 278 } 279 } 280 return name; 281 } 282 283 288 private Target testForPrefix(String xpath, String prefix, TargetType type, ConfigContext domainContext) 289 throws ConfigException 290 { 291 if(xpath!=null && xpath.startsWith(prefix)) 292 { 293 String name = extractElementNameForPrefix(xpath, prefix); 294 return createTarget(type.getName(), null, name, domainContext); 295 } 296 return null; 297 } 298 299 304 public Target createTargetForXPath(String xpath, ConfigContext domainContext) 305 throws ConfigException 306 { 307 Target target = null; 308 if( (target=testForPrefix(xpath, XPATH_CONFIG_PREFIX, TargetType.CONFIG, domainContext))!=null || 309 (target=testForPrefix(xpath, XPATH_SERVER_PREFIX, TargetType.SERVER, domainContext))!=null || 310 (target=testForPrefix(xpath, XPATH_NODE_AGENT_PREFIX, TargetType.NODE_AGENT, domainContext))!=null || 312 (target=testForPrefix(xpath, XPATH_DOMAIN_PREFIX, TargetType.DOMAIN, domainContext))!=null ) 314 return target; 315 return null; 316 } 317 318 325 public String getTargetNameForXPath(String xpath, ConfigContext ctx, boolean bIncludingAppAndRes ) 326 throws ConfigException 327 { 328 329 if(bIncludingAppAndRes) 330 { 331 String [] pair = null; 333 if((pair=extractElementNameAndTypeForPrefix(xpath, XPATH_APPLICATIONS_PREFIX))!=null) 334 return ServerTags.APPLICATIONS+"|"+pair[0]+"|"+pair[1]; 335 if((pair=extractElementNameAndTypeForPrefix(xpath, XPATH_RESOURCES_PREFIX))!=null) 337 return ServerTags.RESOURCES+"|"+pair[0]+"|"+pair[1]; 338 339 } 340 Target target = createTargetForXPath(xpath, ctx); 341 return target.getName(); 342 } 343 } 344 | Popular Tags |