1 25 26 package org.objectweb.easybeans.client.xml; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.util.jar.JarFile ; 33 import java.util.zip.ZipEntry ; 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 ; 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.NodeList ; 47 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2; 48 49 53 public final class ApplicationClientLoader { 54 55 58 private static final String JAVAEE_NS = "http://java.sun.com/xml/ns/javaee"; 59 60 63 private static final String EJB_REF = "ejb-ref"; 64 65 68 private static final String DIRECTORY_APPLICATION_CLIENT_XML_FILE = "META-INF"; 69 70 73 private static final String APPLICATION_CLIENT_XML_FILE = "application-client.xml"; 74 75 78 private static JLog logger = JLogFactory.getLog(ApplicationClientLoader.class); 79 80 83 private static boolean validating = true; 84 85 88 private ApplicationClientLoader() { 89 90 } 91 92 98 private static ApplicationClient loadXML(final URL url) throws ParsingException { 99 100 logger.debug("Analyzing url {0}", url); 101 102 ApplicationClient applicationClient = new ApplicationClient(); 104 105 Document 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 Element applicationClientRootElement = document.getDocumentElement(); 115 116 NodeList ejbRefList = applicationClientRootElement.getElementsByTagNameNS(JAVAEE_NS, EJB_REF); 117 118 for (int i = 0; i < ejbRefList.getLength(); i++) { 120 Element ejbRefElement = (Element ) ejbRefList.item(i); 121 EJBRef ejbRef = new EJBRef(); 122 123 String ejbRefName = XMLUtils.getStringValueElement(JAVAEE_NS, ejbRefElement, "ejb-ref-name"); 125 ejbRef.setEjbRefName(ejbRefName); 126 127 String ejbRefType = XMLUtils.getStringValueElement(JAVAEE_NS, ejbRefElement, "ejb-ref-type"); 129 ejbRef.setEjbRefType(ejbRefType); 130 131 String remote = XMLUtils.getStringValueElement(JAVAEE_NS, ejbRefElement, "remote"); 133 ejbRef.setRemote(remote); 134 135 String ejbLink = XMLUtils.getStringValueElement(JAVAEE_NS, ejbRefElement, "ejb-link"); 137 ejbRef.setEjbLink(ejbLink); 138 139 applicationClient.addEJBRef(ejbRef); 141 } 142 return applicationClient; 143 } 144 145 152 public static ApplicationClient loadApplicationClient(final File archive) throws ParsingException { 153 154 URL applicationClientXmlURL = null; 155 if (archive.isFile()) { 157 158 JarFile jarFile = null; 160 try { 161 jarFile = new JarFile (archive); 162 } catch (IOException e) { 163 throw new ParsingException("File '" + jarFile + "' is not a valid jar file.", e); 164 } 165 166 String applicationClientEntry = DIRECTORY_APPLICATION_CLIENT_XML_FILE + '/' + APPLICATION_CLIENT_XML_FILE; 168 ZipEntry zipEntry = jarFile.getEntry(applicationClientEntry); 169 if (zipEntry != null) { 170 String jarUrl = "jar:file:" + archive.getPath() + "!/" + applicationClientEntry; 171 try { 172 applicationClientXmlURL = new URL (jarUrl); 173 } catch (MalformedURLException 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 e) { 180 logger.error("Problem while closing jar file '" + jarFile + "'."); 181 } 182 183 } else { 184 File applicationClientXmlFile = new File (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 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 |