KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > config > ModulesXMLHelper


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 package com.sun.enterprise.admin.server.core.mbean.config;
25
26 //Logging imports
27
import java.util.logging.Logger JavaDoc;
28 import com.sun.enterprise.admin.server.core.AdminService;
29
30 //Config imports
31
import com.sun.enterprise.config.serverbeans.ServerTags;
32
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import java.util.ArrayList JavaDoc;
38
39 import javax.xml.parsers.DocumentBuilder JavaDoc;
40 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
41 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
42 import javax.xml.parsers.ParserConfigurationException JavaDoc;
43
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.DOMException JavaDoc;
46 import org.w3c.dom.Node JavaDoc;
47 import org.w3c.dom.NamedNodeMap JavaDoc;
48
49 import org.xml.sax.SAXException JavaDoc;
50 import org.xml.sax.SAXParseException JavaDoc;
51 import org.xml.sax.EntityResolver JavaDoc;
52 import org.xml.sax.InputSource JavaDoc;
53
54 //Keep the following registry at one place - used for deployment
55
import com.sun.enterprise.deployment.xml.DTDRegistry;
56 //i18n import
57
import com.sun.enterprise.admin.common.exception.ControlException;
58 import com.sun.enterprise.util.i18n.StringManager;
59
60 public class ModulesXMLHelper
61 {
62     public static final int MODULE_TYPE_EJB = 0x01;
63     public static final int MODULE_TYPE_WEB = 0x02;
64     public static final int MODULE_TYPE_JAVA = 0x04;
65     public static final int MODULE_TYPE_ALL = 0xff;
66     
67     public static final int EJB_TYPE_SESSION = 0x01;
68     public static final int EJB_TYPE_ENTITY = 0x02;
69     public static final int EJB_TYPE_MSGDRIVEN = 0x04;
70     public static final int EJB_TYPE_ALL = 0xff;
71     
72     //application tags
73
static final String JavaDoc APPLICATION_TAG = "application";
74     static final String JavaDoc MODULE_TAG = "module";
75     static final String JavaDoc EJB_MODULE_TAG = "ejb";
76     static final String JavaDoc WEB_MODULE_TAG = "web";
77     static final String JavaDoc WEB_URI_TAG = "web-uri";
78     static final String JavaDoc CONTEXT_ROOT_TAG= "context-root";
79     static final String JavaDoc JAVA_MODULE_TAG = "java";
80     
81     //EJB - tags
82
static final String JavaDoc ENTERPRISE_BEANS_TAG = "enterprise-beans";
83     static final String JavaDoc SESSION_TAG = "session";
84     static final String JavaDoc ENTITY_TAG = "entity";
85     static final String JavaDoc MESSAGE_DRIVEN_TAG = "message-driven";
86     static final String JavaDoc EJB_NAME_TAG = "ejb-name";
87     
88     //WEB - tags
89
static final String JavaDoc SERVLET_TAG = "servlet";
90     static final String JavaDoc SERVLET_NAME_TAG = "servlet-name";
91     
92     static final Logger JavaDoc sLogger = AdminService.sLogger;
93
94     Document JavaDoc document;
95     
96     public ModulesXMLHelper(String JavaDoc fileName) throws Exception JavaDoc
97     {
98         document = createDocument(fileName);
99     }
100     
101     public static String JavaDoc[] getModulesFromApplicationLocation(String JavaDoc appLocation, int moduleType) throws Exception JavaDoc
102     {
103         ModulesXMLHelper myObj = new ModulesXMLHelper(appLocation + "/META-INF/application.xml");
104         return myObj.getModules(moduleType);
105     }
106     
107     public static boolean isModuleExists(String JavaDoc appLocation, String JavaDoc moduleName, int moduleType) throws Exception JavaDoc
108     {
109         String JavaDoc[] strs = getModulesFromApplicationLocation(appLocation, moduleType);
110         if(strs!=null)
111             for(int i=0; i<strs.length; i++)
112                 if(moduleName.equals(strs[i]))
113                     return true;
114         return false;
115     }
116     
117     public static String JavaDoc[] getEnterpriseBeansForEjbModule(String JavaDoc location, String JavaDoc ejbModuleName, int ejbTypes) throws Exception JavaDoc
118     {
119         if(ejbModuleName!=null)
120         { //not standalone - add super directory
121
if(ejbModuleName.endsWith(".jar"))
122                 ejbModuleName = ejbModuleName.substring(0, ejbModuleName.length()-4);
123             location = location + "/" + ejbModuleName + "_jar";
124         }
125         ModulesXMLHelper myObj = new ModulesXMLHelper(location + "/META-INF/ejb-jar.xml");
126         return myObj.getEnterpriseBeans(ejbTypes);
127     }
128     
129     public static String JavaDoc[] getServletsForWebModule(String JavaDoc location, String JavaDoc webModuleName) throws Exception JavaDoc
130     {
131         if(webModuleName!=null)
132         { //not standalone - add super directory to appLocation
133
if(webModuleName.endsWith(".war"))
134                 webModuleName = webModuleName.substring(0, webModuleName.length()-4);
135             location = location + "/" + webModuleName + "_war";
136         }
137         ModulesXMLHelper myObj = new ModulesXMLHelper(location + "/WEB-INF/web.xml");
138         return myObj.getServlets();
139     }
140     
141     public String JavaDoc[] getModules(int moduleType) throws Exception JavaDoc
142     {
143         if (document != null)
144         {
145             ArrayList JavaDoc arr = findChildNodesByName(document.getDocumentElement(), MODULE_TAG);
146             String JavaDoc [] strs = new String JavaDoc[arr.size()];
147             int noNullCount = 0;
148             for (int i=0; i<arr.size(); i++)
149             {
150                 String JavaDoc str = getModuleNameFromNode((Node JavaDoc)arr.get(i), moduleType);
151                 if(str!=null)
152                     strs[noNullCount++] = str;
153             }
154             String JavaDoc [] res = new String JavaDoc[noNullCount];
155             for (int i=0; i<noNullCount; i++)
156             {
157                 res[i] = strs[i];
158             }
159             return res;
160         }
161         return new String JavaDoc[0];
162         
163     }
164     
165     public String JavaDoc[] getEnterpriseBeans(int ejbType) throws Exception JavaDoc
166     {
167         if (document != null)
168         {
169             Node JavaDoc beansListNode = findChildNodeByName(document.getDocumentElement(), ENTERPRISE_BEANS_TAG);
170             if(beansListNode!=null)
171             {
172                 ArrayList JavaDoc resList = new ArrayList JavaDoc();
173                 if((ejbType&EJB_TYPE_SESSION)!=0)
174                     addToListEjbNames(resList, beansListNode, SESSION_TAG);
175                 if((ejbType&EJB_TYPE_SESSION)!=0)
176                     addToListEjbNames(resList, beansListNode, ENTITY_TAG);
177                 if((ejbType&EJB_TYPE_SESSION)!=0)
178                     addToListEjbNames(resList, beansListNode, MESSAGE_DRIVEN_TAG);
179                 String JavaDoc [] res = new String JavaDoc[resList.size()];
180                 for (int i=0; i<res.length; i++)
181                 {
182                     res[i] = (String JavaDoc)(resList.get(i));
183                 }
184                 return res;
185                 // return ((String[]) resList.toArray());
186
}
187         }
188         return new String JavaDoc[0];
189     }
190     
191     public String JavaDoc[] getServlets() throws Exception JavaDoc
192     {
193         if (document != null)
194         {
195             ArrayList JavaDoc arr = findChildNodesByName(document.getDocumentElement(), SERVLET_TAG);
196             String JavaDoc [] res = new String JavaDoc[arr.size()];
197             for (int i=0; i<arr.size(); i++)
198             {
199                 Node JavaDoc nameNode = findChildNodeByName((Node JavaDoc)arr.get(i), SERVLET_NAME_TAG);
200                 res[i] = getTextForNode(nameNode);
201             }
202             return res;
203         }
204         return new String JavaDoc[0];
205     }
206     
207     private void addToListEjbNames(ArrayList JavaDoc listToAdd, Node JavaDoc listNode, String JavaDoc tag)
208     {
209         ArrayList JavaDoc arr = findChildNodesByName(listNode, tag);
210         for (int i=0; i<arr.size(); i++)
211         {
212             Node JavaDoc nameNode = findChildNodeByName((Node JavaDoc)arr.get(i), EJB_NAME_TAG);
213             listToAdd.add(getTextForNode(nameNode));
214         }
215     }
216     
217     private Document JavaDoc createDocument(String JavaDoc fileName) throws Exception JavaDoc
218     {
219         DocumentBuilderFactory JavaDoc factory =
220         DocumentBuilderFactory.newInstance();
221         Document JavaDoc document;
222         try
223         {
224             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
225             EntityResolver JavaDoc resolver = new AdminEntityResolver();
226             builder.setEntityResolver(resolver);
227             document = builder.parse(new File JavaDoc(fileName));
228         }
229         catch (SAXException JavaDoc sxe)
230         {
231             // Error generated during parsing)
232
Exception JavaDoc x = sxe;
233             if (sxe.getException() != null)
234                 x = sxe.getException();
235             sLogger.throwing(getClass().getName(), "createDocument", x);
236             throw new ControlException(x.getLocalizedMessage());
237             
238         }
239         catch (ParserConfigurationException JavaDoc pce)
240         {
241             // Parser with specified options can't be built
242
sLogger.throwing(getClass().getName(), "createDocument", pce);
243             throw new ControlException(pce.getLocalizedMessage());
244         }
245         catch (IOException JavaDoc ioe)
246         {
247             // I/O error
248
sLogger.throwing(getClass().getName(), "createDocument", ioe);
249             throw new ControlException(ioe.getLocalizedMessage());
250         }
251         return document;
252     }
253     
254     private ArrayList JavaDoc findChildNodesByName(Node JavaDoc node, String JavaDoc name)
255     {
256         ArrayList JavaDoc resNodes = new ArrayList JavaDoc();
257         for (node = node.getFirstChild(); node != null; node = node.getNextSibling())
258         {
259             if(node.getNodeName().equalsIgnoreCase(name))
260                 resNodes.add(node);
261         }
262         return resNodes;
263     }
264     
265     private Node JavaDoc findChildNodeByName(Node JavaDoc node, String JavaDoc name)
266     {
267         ArrayList JavaDoc resNodes = new ArrayList JavaDoc();
268         for (node = node.getFirstChild(); node != null; node = node.getNextSibling())
269         {
270             if(node.getNodeName().equalsIgnoreCase(name))
271                 return node;
272         }
273         return null;
274     }
275     
276     private String JavaDoc getModuleNameFromNode(Node JavaDoc node, int moduleType)
277     {
278         ArrayList JavaDoc arr;
279         if((moduleType&MODULE_TYPE_EJB)!=0 &&
280         (arr=findChildNodesByName(node, EJB_MODULE_TAG)).size()>0)
281             return getTextForNode((Node JavaDoc)arr.get(0));
282         if((moduleType&MODULE_TYPE_JAVA)!=0 &&
283         (arr=findChildNodesByName(node, JAVA_MODULE_TAG)).size()>0)
284             return getTextForNode((Node JavaDoc)arr.get(0));
285         if((moduleType&MODULE_TYPE_WEB)!=0 &&
286         (arr=findChildNodesByName(node, WEB_MODULE_TAG)).size()>0)
287         {
288             node = (Node JavaDoc)arr.get(0);
289             if((arr=findChildNodesByName(node, WEB_URI_TAG))!=null &&
290                     arr.size()>0)
291             {
292                 String JavaDoc desc = getTextForNode((Node JavaDoc)arr.get(0));
293                 /*
294                 // desc = web-uri till now
295                 String contextRoot = "";
296                 // now get the context-root - this is helpful
297                 arr = findChildNodesByName(node, CONTEXT_ROOT_TAG);
298                 if (arr != null && arr.size() > 0)
299                 {
300                     contextRoot = getTextForNode((Node) arr.get(0));
301                 }
302                 if (contextRoot != null)
303                 {
304                     desc = contextRoot + ":" + desc;
305                 }
306                 */

307                 return desc;
308             }
309         }
310         return null;
311     }
312     
313     private String JavaDoc getTextForNode(Node JavaDoc node)
314     {
315         for (node = node.getFirstChild(); node != null; node = node.getNextSibling())
316         {
317             if(node.getNodeType()==Node.TEXT_NODE)
318                 return node.getNodeValue();
319         }
320         return null;
321     }
322 }
323
324 class AdminEntityResolver implements EntityResolver JavaDoc
325 {
326     static final Logger JavaDoc sLogger = AdminService.sLogger;
327     private static StringManager localStrings = StringManager.getManager
328       (com.sun.enterprise.admin.server.core.mbean.config.ModulesXMLHelper.class);
329
330     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws
331             SAXException JavaDoc, IOException JavaDoc
332     {
333         InputSource JavaDoc is = null;
334         String JavaDoc completeDTDPath ="";
335         if (completeDTDPath != null)
336         {
337             /* DTDRegistry returns the paths with a leading '/' which we
338             * should get rid of here */

339             is = new InputSource JavaDoc
340                     (ClassLoader.getSystemResourceAsStream(completeDTDPath.substring(1)));
341             sLogger.finest("publicId = " + publicId);
342             sLogger.finest("dtd path = " + completeDTDPath);
343         }
344         else
345         {
346             String JavaDoc msg = localStrings.getString
347                 ("admin.server.core.mbean.config.invalid_public_id", publicId);
348             SAXException JavaDoc se = new SAXException JavaDoc(msg);
349             sLogger.throwing(getClass().getName(), "resolveEntity", se);
350             throw se;
351         }
352         return is;
353     }
354 }
355
Popular Tags