KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > xml > parsing > EJB3DeploymentDescLoader


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EJB3DeploymentDescLoader.java 519 2006-05-28 19:49:27Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.xml.parsing;
27
28 import static org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback.POST_CONSTRUCT;
29 import static org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback.PRE_DESTROY;
30
31 import java.net.URL JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.objectweb.easybeans.deployment.xml.struct.AbsBean;
35 import org.objectweb.easybeans.deployment.xml.struct.EJB3;
36 import org.objectweb.easybeans.deployment.xml.struct.EnterpriseBeans;
37 import org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback;
38 import org.objectweb.easybeans.deployment.xml.struct.Session;
39 import org.objectweb.easybeans.deployment.xml.struct.common.AbsEnvironment;
40 import org.objectweb.easybeans.deployment.xml.struct.common.EnvEntry;
41 import org.objectweb.easybeans.log.JLog;
42 import org.objectweb.easybeans.log.JLogFactory;
43 import org.objectweb.easybeans.util.xml.DocumentParser;
44 import org.objectweb.easybeans.util.xml.DocumentParserException;
45 import org.objectweb.easybeans.util.xml.XMLUtils;
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.Element JavaDoc;
48 import org.w3c.dom.NodeList JavaDoc;
49
50 /**
51  * This class analyzes the EJB3 deployment descriptor and fill a struct with
52  * getters.
53  * @author Florent Benoit
54  */

55 public final class EJB3DeploymentDescLoader {
56
57     /**
58      * Persistence namespace.
59      */

60     private static final String JavaDoc JAVAEE_NS = "http://java.sun.com/xml/ns/javaee";
61
62     /**
63      * Logger.
64      */

65     private static JLog logger = JLogFactory.getLog(EJB3DeploymentDescLoader.class);
66
67     /**
68      * Validating with schema ?
69      */

70     private static boolean validating = true;
71
72     /**
73      * Utility class, no constructor.
74      */

75     private EJB3DeploymentDescLoader() {
76
77     }
78
79     /**
80      * Load the ejb-jar.xml file.
81      * @param url the URL of the XML file.
82      * @throws ParsingException if parsing of XML file fails.
83      * @return an application object.
84      */

85     public static EJB3 loadDeploymentDescriptor(final URL JavaDoc url) throws ParsingException {
86         logger.debug("Analyzing url {0}", url);
87
88         // Get document
89
Document JavaDoc document = null;
90         try {
91             document = DocumentParser.getDocument(url, validating, new EJB3EntityResolver());
92         } catch (DocumentParserException e) {
93             throw new ParsingException("Cannot parse the url", e);
94         }
95
96         EJB3 ejb3 = new EJB3();
97
98         // Root element = <ejb-jar>
99
Element JavaDoc ejbJarRootElement = document.getDocumentElement();
100
101         NodeList JavaDoc enterprisebeansList = ejbJarRootElement.getElementsByTagNameNS(JAVAEE_NS, EnterpriseBeans.NAME);
102         if (enterprisebeansList.getLength() == 1) {
103             Element JavaDoc enterpriseBeansElement = (Element JavaDoc) enterprisebeansList.item(0);
104
105             // Create struct
106
EnterpriseBeans enterpriseBeans = new EnterpriseBeans();
107             ejb3.setEnterpriseBeans(enterpriseBeans);
108
109             // Analyze beans
110
analyzeBeans(enterpriseBeansElement, enterpriseBeans);
111
112         }
113
114         return ejb3;
115
116     }
117
118     /**
119      * Analyze common stuff of beans.
120      * @param enterpriseBeansElement the dom element.
121      * @param enterpriseBeans the structure representing object.
122      */

123     private static void analyzeBeans(final Element JavaDoc enterpriseBeansElement, final EnterpriseBeans enterpriseBeans) {
124         // Get session beans
125
NodeList JavaDoc sessionBeansList = enterpriseBeansElement.getElementsByTagNameNS(JAVAEE_NS, Session.NAME);
126
127         // Loop on session beans
128
for (int i = 0; i < sessionBeansList.getLength(); i++) {
129             Element JavaDoc sessionElement = (Element JavaDoc) sessionBeansList.item(i);
130
131             // Build instance that is created.
132
Session session = new Session();
133
134             enterpriseBeans.addSession(session);
135
136             // First, analyze common things
137
analyzeSessionBean(sessionElement, session);
138         }
139
140     }
141
142     /**
143      * Analyze common stuff of a bean.
144      * @param beanElement the dom element.
145      * @param bean the structure representing object.
146      */

147     private static void analyzeBean(final Element JavaDoc beanElement, final AbsBean bean) {
148         // Get ejb-name
149
String JavaDoc ejbName = XMLUtils.getStringValueElement(JAVAEE_NS, beanElement, "ejb-name");
150         bean.setEjbName(ejbName);
151
152         // Get ejb-class
153
String JavaDoc ejbClass = XMLUtils.getStringValueElement(JAVAEE_NS, beanElement, "ejb-class");
154         bean.setEjbClass(ejbClass);
155
156         // Analyze common things
157
analyzeEnvironement(beanElement, bean);
158     }
159
160     /**
161      * Analyze XML of a session bean.
162      * @param beanElement the dom element.
163      * @param bean the structure representing object.
164      */

165     private static void analyzeSessionBean(final Element JavaDoc beanElement, final Session bean) {
166         analyzeBean(beanElement, bean);
167
168         // Get business-remote
169
List JavaDoc<String JavaDoc> businessRemoteList = XMLUtils.getStringListValueElement(JAVAEE_NS, beanElement, "business-remote");
170         for (String JavaDoc businessRemote : businessRemoteList) {
171             bean.addBusinessRemote(businessRemote);
172         }
173
174         // Get business-local
175
List JavaDoc<String JavaDoc> businessLocalList = XMLUtils.getStringListValueElement(JAVAEE_NS, beanElement, "business-local");
176         for (String JavaDoc businessLocal : businessLocalList) {
177             bean.addBusinessLocal(businessLocal);
178         }
179
180         // Get session-type
181
String JavaDoc sessionType = XMLUtils.getStringValueElement(JAVAEE_NS, beanElement, "session-type");
182         bean.setSessionType(sessionType);
183
184         // Get transaction-type
185
String JavaDoc transactionType = XMLUtils.getStringValueElement(JAVAEE_NS, beanElement, "transaction-type");
186         bean.setTransactionType(transactionType);
187
188     }
189
190     /**
191      * Analyze the environment (could be a bean, servlet, etc).
192      * @param envElement the dom element.
193      * @param bean the structure representing object.
194      */

195     private static void analyzeEnvironement(final Element JavaDoc envElement, final AbsEnvironment bean) {
196         analyzeLifeCycle(envElement, bean, POST_CONSTRUCT);
197         analyzeLifeCycle(envElement, bean, PRE_DESTROY);
198         analyzeEnvEnvtry(envElement, bean);
199     }
200
201     /**
202      * Analyze the post-construct and pre-destroy element.
203      * @param envElement the dom element.
204      * @param bean the structure representing object.
205      * @param lifecycleName the name of lifecycle (post-construct, etc)
206      */

207     private static void analyzeLifeCycle(final Element JavaDoc envElement, final AbsEnvironment bean, final String JavaDoc lifecycleName) {
208         // Get the lifecycle element
209
NodeList JavaDoc lifecycleList = envElement.getElementsByTagNameNS(JAVAEE_NS, lifecycleName);
210
211         // Loop on the lifecycle
212
for (int i = 0; i < lifecycleList.getLength(); i++) {
213             Element JavaDoc lifeCycleElement = (Element JavaDoc) lifecycleList.item(i);
214
215             // Build instance that is created.
216
LifeCycleCallback lifecycleCallback = new LifeCycleCallback();
217             if (POST_CONSTRUCT.equals(lifecycleName)) {
218                 bean.addPostConstruct(lifecycleCallback);
219             } else if (PRE_DESTROY.equals(lifecycleName)) {
220                 bean.addPreDestroy(lifecycleCallback);
221             } else {
222                 throw new IllegalArgumentException JavaDoc("Unknown lifecycle named '" + lifecycleName + "'.");
223             }
224
225             // lifecycle-callback-class
226
String JavaDoc className = XMLUtils.getStringValueElement(JAVAEE_NS, lifeCycleElement, "lifecycle-callback-class");
227             lifecycleCallback.setClassName(className);
228
229             // lifecycle-callback-method
230
String JavaDoc method = XMLUtils.getStringValueElement(JAVAEE_NS, lifeCycleElement, "lifecycle-callback-method");
231             lifecycleCallback.setMethod(method);
232
233         }
234
235     }
236
237     /**
238      * Analyze the env-entry element.
239      * @param envElement the dom element.
240      * @param bean the structure representing object.
241      */

242     private static void analyzeEnvEnvtry(final Element JavaDoc envElement, final AbsEnvironment bean) {
243         // Get env-entry
244
NodeList JavaDoc envEntryList = envElement.getElementsByTagNameNS(JAVAEE_NS, EnvEntry.NAME);
245
246         // Loop on env entry
247
for (int i = 0; i < envEntryList.getLength(); i++) {
248             Element JavaDoc envEntryElement = (Element JavaDoc) envEntryList.item(i);
249
250             // Build instance that is created.
251
EnvEntry envEntry = new EnvEntry();
252             bean.addEnvEntry(envEntry);
253
254             // Env-entry name
255
String JavaDoc envEntryName = XMLUtils.getStringValueElement(JAVAEE_NS, envEntryElement, "env-entry-name");
256             envEntry.setEnvEntryName(envEntryName);
257
258             // Env-entry type
259
String JavaDoc envEntryType = XMLUtils.getStringValueElement(JAVAEE_NS, envEntryElement, "env-entry-type");
260             envEntry.setEnvEntryType(envEntryType);
261
262             // Env-entry value
263
String JavaDoc envEntryValue = XMLUtils.getStringValueElement(JAVAEE_NS, envEntryElement, "env-entry-value");
264             envEntry.setEnvEntryValue(envEntryValue);
265         }
266     }
267
268 }
269
Popular Tags