001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ApplicationClientLoader.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.client.xml;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.net.MalformedURLException;
029: import java.net.URL;
030: import java.util.jar.JarFile;
031: import java.util.zip.ZipEntry;
032:
033: import org.ow2.easybeans.deployment.xml.parsing.EJB3EntityResolver;
034: import org.ow2.easybeans.deployment.xml.parsing.ParsingException;
035: import org.ow2.easybeans.deployment.xml.struct.common.EJBRef;
036: import org.ow2.easybeans.deployment.xml.struct.common.InjectionTarget;
037: import org.ow2.easybeans.util.url.URLUtilsException;
038: import org.ow2.easybeans.util.xml.DocumentParser;
039: import org.ow2.easybeans.util.xml.DocumentParserException;
040: import org.ow2.easybeans.util.xml.XMLUtils;
041: import org.ow2.util.log.Log;
042: import org.ow2.util.log.LogFactory;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Element;
045: import org.w3c.dom.NodeList;
046:
047: import static org.ow2.easybeans.util.url.URLUtils.fileToURL2;
048:
049: /**
050: * Class used to fill ApplicationClient implementation class by loading an XML.
051: * @author Florent Benoit
052: */
053: public final class ApplicationClientLoader {
054:
055: /**
056: * Persistence namespace.
057: */
058: private static final String JAVAEE_NS = "http://java.sun.com/xml/ns/javaee";
059:
060: /**
061: * <ejb-ref> element.
062: */
063: private static final String EJB_REF = "ejb-ref";
064:
065: /**
066: * Directory where application-client.xml file should be.
067: */
068: private static final String DIRECTORY_APPLICATION_CLIENT_XML_FILE = "META-INF";
069:
070: /**
071: * Name of the persistence.xml file.
072: */
073: private static final String APPLICATION_CLIENT_XML_FILE = "application-client.xml";
074:
075: /**
076: * Logger.
077: */
078: private static Log logger = LogFactory
079: .getLog(ApplicationClientLoader.class);
080:
081: /**
082: * Validating with schema ?
083: */
084: private static boolean validating = true;
085:
086: /**
087: * Utility class, no public constructor.
088: */
089: private ApplicationClientLoader() {
090:
091: }
092:
093: /**
094: * Load the application-client.xml file.
095: * @param url the URL of the the Reader of the XML file.
096: * @throws ParsingException if parsing of XML file fails.
097: * @return an application object.
098: */
099: private static ApplicationClient loadXML(final URL url)
100: throws ParsingException {
101:
102: logger.debug("Analyzing url {0}", url);
103:
104: // Object that will be returned
105: ApplicationClient applicationClient = new ApplicationClient();
106:
107: // Get document
108: Document document = null;
109: try {
110: document = DocumentParser.getDocument(url, validating,
111: new EJB3EntityResolver());
112: } catch (DocumentParserException e) {
113: throw new ParsingException("Cannot parse the url", e);
114: }
115:
116: // Root element = <application-client>
117: Element applicationClientRootElement = document
118: .getDocumentElement();
119:
120: NodeList ejbRefList = applicationClientRootElement
121: .getElementsByTagNameNS(JAVAEE_NS, EJB_REF);
122:
123: // Loop on this list
124: for (int i = 0; i < ejbRefList.getLength(); i++) {
125: Element ejbRefElement = (Element) ejbRefList.item(i);
126: EJBRef ejbRef = new EJBRef();
127:
128: // ejb-ref-name
129: String ejbRefName = XMLUtils.getStringValueElement(
130: JAVAEE_NS, ejbRefElement, "ejb-ref-name");
131: ejbRef.setEjbRefName(ejbRefName);
132:
133: // ejb-ref-type
134: String ejbRefType = XMLUtils.getStringValueElement(
135: JAVAEE_NS, ejbRefElement, "ejb-ref-type");
136: ejbRef.setEjbRefType(ejbRefType);
137:
138: // home
139: String home = XMLUtils.getStringValueElement(JAVAEE_NS,
140: ejbRefElement, "home");
141: ejbRef.setHome(home);
142:
143: // remote
144: String remote = XMLUtils.getStringValueElement(JAVAEE_NS,
145: ejbRefElement, "remote");
146: ejbRef.setRemote(remote);
147:
148: // ejb-link
149: String ejbLink = XMLUtils.getStringValueElement(JAVAEE_NS,
150: ejbRefElement, "ejb-link");
151: ejbRef.setEjbLink(ejbLink);
152:
153: // injection target
154: NodeList injectionTargetList = ejbRefElement
155: .getElementsByTagNameNS(JAVAEE_NS,
156: "injection-target");
157: for (int j = 0; j < injectionTargetList.getLength(); j++) {
158: Element injectionTargetElement = (Element) injectionTargetList
159: .item(j);
160:
161: // Build Object and add it
162: InjectionTarget injectionTarget = new InjectionTarget();
163: ejbRef.addInjectionTarget(injectionTarget);
164:
165: // Get class name
166: String className = XMLUtils.getStringValueElement(
167: JAVAEE_NS, injectionTargetElement,
168: "injection-target-class");
169: injectionTarget.setClassname(className);
170:
171: // Get target
172: String targetName = XMLUtils.getStringValueElement(
173: JAVAEE_NS, injectionTargetElement,
174: "injection-target-name");
175: injectionTarget.setTargetName(targetName);
176:
177: }
178:
179: // add ejb-ref in application-client element
180: applicationClient.addEJBRef(ejbRef);
181: }
182: return applicationClient;
183: }
184:
185: /**
186: * Detects and analyze the META-INF/application-client.xml file.
187: * @param archive the file to analyze (or directory) in order to find a
188: * application-client.xml file.
189: * @throws ParsingException if parsing of XML file fails.
190: * @return an application object.
191: */
192: public static ApplicationClient loadApplicationClient(
193: final File archive) throws ParsingException {
194:
195: URL applicationClientXmlURL = null;
196: // if archive is a file, needs to look into the jar file
197: if (archive.isFile()) {
198:
199: // Try to see if the archive contains the persistence.xml file.
200: JarFile jarFile = null;
201: try {
202: jarFile = new JarFile(archive);
203: } catch (IOException e) {
204: throw new ParsingException("File '" + jarFile
205: + "' is not a valid jar file.", e);
206: }
207:
208: // Gets the entry.
209: String applicationClientEntry = DIRECTORY_APPLICATION_CLIENT_XML_FILE
210: + '/' + APPLICATION_CLIENT_XML_FILE;
211: ZipEntry zipEntry = jarFile
212: .getEntry(applicationClientEntry);
213: if (zipEntry != null) {
214: String jarUrl = "jar:file:" + archive.getPath() + "!/"
215: + applicationClientEntry;
216: try {
217: applicationClientXmlURL = new URL(jarUrl);
218: } catch (MalformedURLException e) {
219: throw new ParsingException(
220: "Error while trying to build an URL with '"
221: + jarUrl + "'", e);
222: }
223: }
224: try {
225: jarFile.close();
226: } catch (IOException e) {
227: logger.error("Problem while closing jar file '"
228: + jarFile + "'.");
229: }
230:
231: } else {
232: // directory mode
233: File applicationClientXmlFile = new File(archive,
234: DIRECTORY_APPLICATION_CLIENT_XML_FILE
235: + File.separator
236: + APPLICATION_CLIENT_XML_FILE);
237: if (applicationClientXmlFile.exists()) {
238: try {
239: applicationClientXmlURL = fileToURL2(applicationClientXmlFile);
240: } catch (URLUtilsException e) {
241: throw new ParsingException(
242: "Cannot get URL from file '"
243: + applicationClientXmlFile + "'.");
244: }
245: }
246: }
247:
248: // Now, do the parsing and fill the structure.
249: ApplicationClient applicationClient = null;
250: if (applicationClientXmlURL != null) {
251: try {
252: applicationClient = loadXML(applicationClientXmlURL);
253: } catch (ParsingException e) {
254: throw new ParsingException("Cannot parse the URL '"
255: + applicationClientXmlURL + "'.", e);
256: }
257: }
258: return applicationClient;
259: }
260:
261: }
|