001: /**
002: * EasyBeans
003: * Copyright (C) 2007 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: WARDeploymentDesc.java 2059 2007-11-22 17:22:33Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.xml;
025:
026: import java.net.URL;
027: import java.util.Iterator;
028:
029: import org.ow2.easybeans.deployment.xml.parsing.ParsingException;
030: import org.ow2.easybeans.deployment.xml.parsing.WARDeploymentDescLoader;
031: import org.ow2.easybeans.deployment.xml.parsing.WARTLDLoader;
032: import org.ow2.easybeans.deployment.xml.struct.WAR;
033: import org.ow2.util.ee.deploy.api.archive.ArchiveException;
034: import org.ow2.util.ee.deploy.api.archive.IArchive;
035: import org.ow2.util.log.Log;
036: import org.ow2.util.log.LogFactory;
037:
038: /**
039: * Utility class to get a WAR deployment descriptor.
040: * @author Florent Benoit
041: */
042: public final class WARDeploymentDesc {
043:
044: /**
045: * Logger.
046: */
047: private static Log logger = LogFactory
048: .getLog(WARDeploymentDesc.class);
049:
050: /**
051: * Utility class, no public constructor.
052: */
053: private WARDeploymentDesc() {
054:
055: }
056:
057: /**
058: * Gets WAR for a given archive.
059: * @param archive file/directory.
060: * @throws WARDeploymentDescException if no xml is present.
061: * @return WAR instance.
062: */
063: public static WAR getWAR(final IArchive archive)
064: throws WARDeploymentDescException {
065:
066: // Get XML entry
067: URL webXmlURL = null;
068: try {
069: webXmlURL = archive.getResource("WEB-INF/web.xml");
070: } catch (ArchiveException e) {
071: throw new WARDeploymentDescException(
072: "Cannot get resource 'WEB-INF/web.xml' on the archive '"
073: + archive.getName() + "'.");
074: }
075:
076: WAR war = null;
077: // Get WAR object (if url valid)
078: if (webXmlURL != null) {
079: try {
080: war = WARDeploymentDescLoader
081: .loadDeploymentDescriptor(webXmlURL);
082: } catch (ParsingException e) {
083: throw new WARDeploymentDescException(
084: "Cannot parse URL '" + webXmlURL + "'.", e);
085: }
086: } else {
087: war = new WAR();
088: }
089:
090: // Check if there are .tld files
091: Iterator<URL> itUrlResources = null;
092: try {
093: itUrlResources = archive.getResources();
094: } catch (ArchiveException e) {
095: throw new WARDeploymentDescException(
096: "Cannot get resources on the archive '"
097: + archive.getName() + "'.");
098: }
099:
100: // Scan all URLs to search .tld
101: while (itUrlResources.hasNext()) {
102: URL url = itUrlResources.next();
103: if (url.getPath().endsWith(".tld")) {
104: // Complete the structure of the war object.
105: try {
106: WARTLDLoader.scanTLD(url, war);
107: } catch (ParsingException e) {
108: logger.error("Cannot analyze the TLD file ''{0}''",
109: url, e);
110: }
111: }
112: }
113:
114: return war;
115: }
116: }
|