KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > panther > deploy > EJBJarUtils


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.panther.deploy;
56
57 import java.io.InputStreamReader JavaDoc;
58 import java.lang.reflect.Constructor JavaDoc;
59 import java.util.jar.JarEntry JavaDoc;
60 import java.util.jar.JarFile JavaDoc;
61 import java.util.ArrayList JavaDoc;
62 import java.util.HashMap JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Map JavaDoc;
65
66 import org.w3c.dom.Document JavaDoc;
67 import org.w3c.dom.Element JavaDoc;
68 import org.w3c.dom.Node JavaDoc;
69 import org.w3c.dom.NodeList JavaDoc;
70
71 import org.lateralnz.common.util.Constants;
72 import org.lateralnz.common.util.IOUtils;
73 import org.lateralnz.common.util.StringUtils;
74 import org.lateralnz.common.util.XMLUtils;
75 import org.lateralnz.panther.util.EJBConstants;
76
77 /**
78  * general utility functions for accessing an ejb jar
79  *
80  * @author J R Briggs
81  */

82 public final class EJBJarUtils implements Constants, EJBConstants {
83   private static final String JavaDoc JAVA_LANG_STRING = "java.lang.String";
84   private static final Class JavaDoc[] CONS_PARAMS = { String JavaDoc.class };
85   
86   private EJBJarUtils() {
87   }
88   
89  /**
90   * get the XML document "ejb-jar.xml" from a specified jar file, returning null
91   * if no matching file is found
92   */

93   public static final Document JavaDoc getEJBJarXML(JarFile JavaDoc jar) throws Exception JavaDoc {
94     JarEntry JavaDoc ejbxml = jar.getJarEntry("META-INF/ejb-jar.xml");
95     if (ejbxml == null) {
96       return null;
97     }
98
99     InputStreamReader JavaDoc isr = null;
100     String JavaDoc s;
101     try {
102       isr = new InputStreamReader JavaDoc(jar.getInputStream(ejbxml));
103       s = StringUtils.readFrom(isr);
104     }
105     finally {
106       IOUtils.close(isr);
107     }
108     
109
110     Document JavaDoc doc = XMLUtils.parse(s);
111     return doc;
112   }
113   
114  /**
115   * get a list of session bean descriptors
116   * @param doc the ejb-jar xml document object
117   * @returns a list of SessionDescriptor objects
118   * @see org.lateralnz.panther.model.SessionDescriptor
119   */

120   public static final List JavaDoc getSessionDescriptors(Document JavaDoc doc) throws Exception JavaDoc {
121     ArrayList JavaDoc al = new ArrayList JavaDoc();
122     
123     NodeList JavaDoc sessionBeans = doc.getElementsByTagName(SESSION);
124     for (int i = 0; i < sessionBeans.getLength(); i++) {
125       Node JavaDoc n = sessionBeans.item(i);
126
127       String JavaDoc name = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, EJB_NAME));
128       String JavaDoc remote = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, REMOTE));
129       String JavaDoc home = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, HOME));
130       String JavaDoc ejb = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, EJB_CLASS));
131       String JavaDoc session = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, SESSION_TYPE));
132       String JavaDoc trans = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, TRANSACTION_TYPE));
133       Map JavaDoc transEntries;
134       if (trans.equalsIgnoreCase(CONTAINER)) {
135         transEntries = getTransactionAttr(doc, name);
136       }
137       else {
138         transEntries = new HashMap JavaDoc();
139       }
140       
141       SessionDescriptor sd = new SessionDescriptor(name, remote, home, ejb, session);
142       sd.setEnvEntries(getEnvEntries(n));
143       sd.setTransEntries(transEntries);
144             
145       al.add(sd);
146     }
147     
148     return al;
149   }
150   
151  /**
152   * get the container transaction attribute for an ejb
153   */

154   private static Map JavaDoc getTransactionAttr(Document JavaDoc doc, String JavaDoc ejbName) {
155     boolean debug = false;
156     
157     HashMap JavaDoc rtn = new HashMap JavaDoc();
158     NodeList JavaDoc transList = doc.getElementsByTagName(CONTAINER_TRANSACTION);
159     for (int i = 0; i < transList.getLength(); i++) {
160       Element JavaDoc e1 = (Element JavaDoc)transList.item(i);
161       NodeList JavaDoc methodList = e1.getElementsByTagName(METHOD);
162       for (int j = 0; j < methodList.getLength(); j++) {
163         Element JavaDoc e2 = (Element JavaDoc)methodList.item(j);
164         String JavaDoc method = e2.getElementsByTagName("method-name").item(0).getFirstChild().getNodeValue();
165         if (!StringUtils.isEmpty(method)) {
166           NodeList JavaDoc nameList = e2.getElementsByTagName(EJB_NAME);
167           if (nameList.getLength() > 0) {
168             Element JavaDoc e3 = (Element JavaDoc)nameList.item(0);
169             String JavaDoc name = e3.getFirstChild().getNodeValue();
170             if (name.equals(ejbName)) {
171               NodeList JavaDoc transAttList = e1.getElementsByTagName(TRANS_ATTRIBUTE);
172               if (transAttList.getLength() > 0) {
173                 rtn.put(method, transAttList.item(0).getFirstChild().getNodeValue());
174               }
175
176               break;
177             }
178           }
179         }
180       }
181     }
182     
183     if (!rtn.containsKey("*")) {
184       rtn.put("*", "NotSupported");
185     }
186     
187     return rtn;
188   }
189   
190  /**
191   * get a map of environment entries for a node (typically the root 'session' node)
192   */

193   public static Map JavaDoc getEnvEntries(Node JavaDoc n) throws Exception JavaDoc {
194     HashMap JavaDoc hm = new HashMap JavaDoc();
195     NodeList JavaDoc nl = n.getChildNodes();
196     for (int i = 0; i < nl.getLength(); i++) {
197       Node JavaDoc tst = nl.item(i);
198       if (tst.getNodeName().equalsIgnoreCase(ENV_ENTRY)) {
199         NodeList JavaDoc envitems = tst.getChildNodes();
200         String JavaDoc name = null;
201         String JavaDoc type = null;
202         String JavaDoc value = null;
203         for (int j = 0; j < envitems.getLength(); j++) {
204           Node JavaDoc env = envitems.item(j);
205           String JavaDoc nodename = env.getNodeName();
206           if (nodename.equals(ENV_ENTRY_NAME)) {
207             name = env.getFirstChild().getNodeValue();
208           }
209           else if (nodename.equals(ENV_ENTRY_TYPE)) {
210             type = env.getFirstChild().getNodeValue();
211           }
212           else if (nodename.equals(ENV_ENTRY_VALUE)) {
213             value = env.getFirstChild().getNodeValue();
214           }
215         }
216         
217         if (StringUtils.isEmpty(name) || StringUtils.isEmpty(type) || StringUtils.isEmpty(value)) {
218           System.out.println("note: missing one of name/type/value in env-entry");
219           continue;
220         }
221         else if (type.equalsIgnoreCase(JAVA_LANG_STRING)) {
222           hm.put(name, value);
223         }
224         else {
225           try {
226             Class JavaDoc c = Class.forName(type);
227             Constructor JavaDoc cons = c.getConstructor(CONS_PARAMS);
228             Object JavaDoc obj = cons.newInstance(new Object JavaDoc[]{ value });
229             hm.put(name, obj);
230           }
231           catch (Exception JavaDoc e) {
232             System.out.println("note (error): " + e.getMessage());
233           }
234         }
235       }
236     }
237     return hm;
238   }
239   
240   // for testing
241
public static final void main(String JavaDoc[] args) {
242     try {
243       java.io.File JavaDoc f = new java.io.File JavaDoc(args[0]);
244       JarFile JavaDoc jar = new JarFile JavaDoc(f, true, JarFile.OPEN_READ);
245       Document JavaDoc doc = EJBJarUtils.getEJBJarXML(jar);
246       EJBJarUtils.getSessionDescriptors(doc);
247     }
248     catch (Exception JavaDoc e) {
249       e.printStackTrace();
250     }
251   }
252 }
Popular Tags