KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > target > TargetBuilder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: TargetBuilder.java,v 1.4 2005/12/25 04:14:39 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.target;
29
30 //config imports
31
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     /**
59      * i18n strings manager object
60      */

61     private static final StringManager strMgr =
62         StringManager.getManager(TargetBuilder.class);
63
64     public TargetBuilder()
65     {
66     }
67
68     public Target createTarget(String JavaDoc 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 JavaDoc name, ConfigContext domainContext)
80         throws ConfigException
81     {
82         return createTarget(null, validTypes, name, domainContext);
83     }
84         
85     public Target createTarget(String JavaDoc defaultTargetName, String JavaDoc name, ConfigContext domainContext)
86         throws ConfigException
87     {
88         return createTarget(defaultTargetName, getDefaultValidTargets(), name, domainContext);
89     }
90     
91     public Target createTarget(String JavaDoc defaultTargetName, TargetType validTypes[],
92         String JavaDoc 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             //Should throw more specific exception. This really should never happen
120
throw new ConfigException(
121                 strMgr.getString("target.cant_determine_type", name));
122         }
123         return target;
124     }
125     
126     /**
127      * Validate that the given target is one of the specified types.
128      */

129     private void validateTargetType(TargetType[] validTypes, TargetType targetType, String JavaDoc targetName,
130         ConfigContext domainContext) throws ConfigException
131     {
132         if (validTypes != null) {
133             boolean isValid = false;
134             String JavaDoc validTargets = new String JavaDoc();
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 JavaDoc 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             //Should throw more specific exception
185
throw new ConfigException(
186                 strMgr.getString("target.cant_determine_type", targetName));
187         }
188         return targetType;
189     }
190
191     protected String JavaDoc getDefaultTarget(ConfigContext cc) throws ConfigException
192     {
193         //We assume that the 0th server is the DAS
194
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 JavaDoc o, Object JavaDoc name)
204     {
205         if (null == o)
206         {
207             throw new IllegalArgumentException JavaDoc(
208                 strMgr.getString("target.cant_be_null", name.toString()));
209         }
210     }
211     
212     //*************************************************
213
// getting Target correspondent to config node xpath
214
//*************************************************
215

216     // predefined xpath prefixes for different Target types
217
private static final String JavaDoc XPATH_CONFIG_PREFIX = "/" + ServerTags.DOMAIN + "/" +
218                                 ServerTags.CONFIGS + "/" +
219                                 ServerTags.CONFIG + "[@" + ServerTags.NAME + "='";
220     private static final String JavaDoc XPATH_SERVER_PREFIX = "/" + ServerTags.DOMAIN + "/" +
221                                 ServerTags.SERVERS + "/" +
222                                 ServerTags.SERVER + "[@" + ServerTags.NAME + "='";
223     private static final String JavaDoc XPATH_CLUSTER_PREFIX = "/" + ServerTags.DOMAIN + "/" +
224                                 ServerTags.CLUSTERS + "/" +
225                                 ServerTags.CLUSTER + "[@" + ServerTags.NAME + "='";
226     private static final String JavaDoc XPATH_NODE_AGENT_PREFIX = "/" + ServerTags.DOMAIN + "/" +
227                                 ServerTags.NODE_AGENTS + "/" +
228                                 ServerTags.NODE_AGENT + "[@" + ServerTags.NAME + "='";
229     private static final String JavaDoc XPATH_DOMAIN_PREFIX = "/" + ServerTags.DOMAIN + "/";
230
231     private static final String JavaDoc XPATH_APPLICATIONS_PREFIX = "/" + ServerTags.DOMAIN + "/" + ServerTags.APPLICATIONS + "/";
232     private static final String JavaDoc XPATH_RESOURCES_PREFIX = "/" + ServerTags.DOMAIN + "/" + ServerTags.RESOURCES + "/";
233     
234     /*
235      * private helper method - returns name for named prefixed xpath
236      * if prefix matches xpath
237      */

238     private static String JavaDoc[] extractElementNameAndTypeForPrefix(String JavaDoc xpath, String JavaDoc 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 JavaDoc 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 JavaDoc key = xpath.substring(idx+1);
255             if(key.indexOf('\'')>=0)
256                 return null;
257             return (new String JavaDoc[]{key, type});
258         }
259         return null;
260     }
261
262     /*
263      * private helper method - returns name for named prefixed xpath
264      * if prefix matches xpath
265      */

266     private static String JavaDoc extractElementNameForPrefix(String JavaDoc xpath, String JavaDoc prefix)
267                         throws ConfigException
268     {
269         String JavaDoc 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     /*
284      * private helper method - testing by Target type predefined xpath prefixes
285      * returns Target object if xpath element belongs to type
286      *
287      */

288     private Target testForPrefix(String JavaDoc xpath, String JavaDoc prefix, TargetType type, ConfigContext domainContext)
289                         throws ConfigException
290     {
291         if(xpath!=null && xpath.startsWith(prefix))
292         {
293             String JavaDoc name = extractElementNameForPrefix(xpath, prefix);
294             return createTarget(type.getName(), null, name, domainContext);
295         }
296         return null;
297     }
298     
299     /* creates Target object for specified config element's xpath
300      * @param xpath config element's xpath
301      * @param domainContext
302      * @throws ConfigException
303      */

304     public Target createTargetForXPath(String JavaDoc 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_CLUSTER_PREFIX, TargetType.CLUSTER, domainContext))!=null ||
311
(target=testForPrefix(xpath, XPATH_NODE_AGENT_PREFIX, TargetType.NODE_AGENT, domainContext))!=null ||
312             // DOMAIN should be last
313
(target=testForPrefix(xpath, XPATH_DOMAIN_PREFIX, TargetType.DOMAIN, domainContext))!=null )
314             return target;
315         return null;
316     }
317
318     /*
319      * returns target name
320      * @param xpath config element's xpath
321      * @param domainContext
322      * @bIncludingAppAndRes - including virtual "target" names for applications and resources
323      * @throws ConfigException
324      */

325     public String JavaDoc getTargetNameForXPath(String JavaDoc xpath, ConfigContext ctx, boolean bIncludingAppAndRes )
326         throws ConfigException
327     {
328         
329         if(bIncludingAppAndRes)
330         {
331             //now time for applications
332
String JavaDoc[] pair = null;
333             if((pair=extractElementNameAndTypeForPrefix(xpath, XPATH_APPLICATIONS_PREFIX))!=null)
334                return ServerTags.APPLICATIONS+"|"+pair[0]+"|"+pair[1];
335             //now time for applications
336
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