KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > client > xml > ApplicationClientLoader


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: ApplicationClientLoader.java 1026 2006-08-04 14:54:10Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.client.xml;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.jar.JarFile JavaDoc;
33 import java.util.zip.ZipEntry JavaDoc;
34
35 import org.objectweb.easybeans.deployment.xml.parsing.EJB3EntityResolver;
36 import org.objectweb.easybeans.deployment.xml.parsing.ParsingException;
37 import org.objectweb.easybeans.deployment.xml.struct.common.EJBRef;
38 import org.objectweb.easybeans.log.JLog;
39 import org.objectweb.easybeans.log.JLogFactory;
40 import org.objectweb.easybeans.util.url.URLUtilsException;
41 import org.objectweb.easybeans.util.xml.DocumentParser;
42 import org.objectweb.easybeans.util.xml.DocumentParserException;
43 import org.objectweb.easybeans.util.xml.XMLUtils;
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.Element JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2;
48
49 /**
50  * Class used to fill ApplicationClient implementation class by loading an XML.
51  * @author Florent Benoit
52  */

53 public final class ApplicationClientLoader {
54
55     /**
56      * Persistence namespace.
57      */

58     private static final String JavaDoc JAVAEE_NS = "http://java.sun.com/xml/ns/javaee";
59
60     /**
61      * <ejb-ref> element.
62      */

63     private static final String JavaDoc EJB_REF = "ejb-ref";
64
65     /**
66      * Directory where application-client.xml file should be.
67      */

68     private static final String JavaDoc DIRECTORY_APPLICATION_CLIENT_XML_FILE = "META-INF";
69
70     /**
71      * Name of the persistence.xml file.
72      */

73     private static final String JavaDoc APPLICATION_CLIENT_XML_FILE = "application-client.xml";
74
75     /**
76      * Logger.
77      */

78     private static JLog logger = JLogFactory.getLog(ApplicationClientLoader.class);
79
80     /**
81      * Validating with schema ?
82      */

83     private static boolean validating = true;
84
85     /**
86      * Utility class, no public constructor.
87      */

88     private ApplicationClientLoader() {
89
90     }
91
92     /**
93      * Load the application-client.xml file.
94      * @param url the URL of the the Reader of the XML file.
95      * @throws ParsingException if parsing of XML file fails.
96      * @return an application object.
97      */

98     private static ApplicationClient loadXML(final URL JavaDoc url) throws ParsingException {
99
100         logger.debug("Analyzing url {0}", url);
101
102         // Object that will be returned
103
ApplicationClient applicationClient = new ApplicationClient();
104
105         // Get document
106
Document JavaDoc document = null;
107         try {
108             document = DocumentParser.getDocument(url, validating, new EJB3EntityResolver());
109         } catch (DocumentParserException e) {
110             throw new ParsingException("Cannot parse the url", e);
111         }
112
113         // Root element = <application-client>
114
Element JavaDoc applicationClientRootElement = document.getDocumentElement();
115
116         NodeList JavaDoc ejbRefList = applicationClientRootElement.getElementsByTagNameNS(JAVAEE_NS, EJB_REF);
117
118         // Loop on this list
119
for (int i = 0; i < ejbRefList.getLength(); i++) {
120             Element JavaDoc ejbRefElement = (Element JavaDoc) ejbRefList.item(i);
121             EJBRef ejbRef = new EJBRef();
122
123             // ejb-ref-name
124
String JavaDoc ejbRefName = XMLUtils.getStringValueElement(JAVAEE_NS, ejbRefElement, "ejb-ref-name");
125             ejbRef.setEjbRefName(ejbRefName);
126
127             // ejb-ref-type
128
String JavaDoc ejbRefType = XMLUtils.getStringValueElement(JAVAEE_NS, ejbRefElement, "ejb-ref-type");
129             ejbRef.setEjbRefType(ejbRefType);
130
131             // remote
132
String JavaDoc remote = XMLUtils.getStringValueElement(JAVAEE_NS, ejbRefElement, "remote");
133             ejbRef.setRemote(remote);
134
135             // ejb-link
136
String JavaDoc ejbLink = XMLUtils.getStringValueElement(JAVAEE_NS, ejbRefElement, "ejb-link");
137             ejbRef.setEjbLink(ejbLink);
138
139             // add ejb-ref in application-client element
140
applicationClient.addEJBRef(ejbRef);
141         }
142         return applicationClient;
143     }
144
145     /**
146      * Detects and analyze the META-INF/application-client.xml file.
147      * @param archive the file to analyze (or directory) in order to find a
148      * application-client.xml file.
149      * @throws ParsingException if parsing of XML file fails.
150      * @return an application object.
151      */

152     public static ApplicationClient loadApplicationClient(final File JavaDoc archive) throws ParsingException {
153
154         URL JavaDoc applicationClientXmlURL = null;
155         // if archive is a file, needs to look into the jar file
156
if (archive.isFile()) {
157
158             // Try to see if the archive contains the persistence.xml file.
159
JarFile JavaDoc jarFile = null;
160             try {
161                 jarFile = new JarFile JavaDoc(archive);
162             } catch (IOException JavaDoc e) {
163                 throw new ParsingException("File '" + jarFile + "' is not a valid jar file.", e);
164             }
165
166             // Gets the entry.
167
String JavaDoc applicationClientEntry = DIRECTORY_APPLICATION_CLIENT_XML_FILE + '/' + APPLICATION_CLIENT_XML_FILE;
168             ZipEntry JavaDoc zipEntry = jarFile.getEntry(applicationClientEntry);
169             if (zipEntry != null) {
170                 String JavaDoc jarUrl = "jar:file:" + archive.getPath() + "!/" + applicationClientEntry;
171                 try {
172                     applicationClientXmlURL = new URL JavaDoc(jarUrl);
173                 } catch (MalformedURLException JavaDoc e) {
174                     throw new ParsingException("Error while trying to build an URL with '" + jarUrl + "'", e);
175                 }
176             }
177             try {
178                 jarFile.close();
179             } catch (IOException JavaDoc e) {
180                 logger.error("Problem while closing jar file '" + jarFile + "'.");
181             }
182
183         } else {
184             // directory mode
185
File JavaDoc applicationClientXmlFile = new File JavaDoc(archive, DIRECTORY_APPLICATION_CLIENT_XML_FILE + File.separator
186                     + APPLICATION_CLIENT_XML_FILE);
187             if (applicationClientXmlFile.exists()) {
188                 try {
189                     applicationClientXmlURL = fileToURL2(applicationClientXmlFile);
190                 } catch (URLUtilsException e) {
191                     throw new ParsingException("Cannot get URL from file '" + applicationClientXmlFile + "'.");
192                 }
193             }
194         }
195
196         // Now, do the parsing and fill the structure.
197
ApplicationClient applicationClient = null;
198         if (applicationClientXmlURL != null) {
199             try {
200                 applicationClient = loadXML(applicationClientXmlURL);
201             } catch (ParsingException e) {
202                 throw new ParsingException("Cannot parse the URL '" + applicationClientXmlURL + "'.", e);
203             }
204         }
205         return applicationClient;
206     }
207
208 }
209
Popular Tags