001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.deployment.webapp;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import javax.xml.parsers.ParserConfigurationException;
029:
030: import org.apache.cactus.integration.ant.deployment.DefaultJarArchive;
031: import org.apache.cactus.integration.ant.deployment.JarArchive;
032: import org.xml.sax.SAXException;
033:
034: /**
035: * Class that encapsulates access to a WAR.
036: *
037: * @since Cactus 1.5
038: * @version $Id: DefaultWarArchive.java 239003 2004-05-31 20:05:27Z vmassol $
039: */
040: public class DefaultWarArchive extends DefaultJarArchive implements
041: WarArchive {
042: // Instance Variables ------------------------------------------------------
043:
044: /**
045: * The parsed deployment descriptor.
046: */
047: private WebXml webXml;
048:
049: // Constructors ------------------------------------------------------------
050:
051: /**
052: * Constructor.
053: *
054: * @param theFile The web application archive
055: * @throws IOException If there was a problem reading the WAR
056: */
057: public DefaultWarArchive(File theFile) throws IOException {
058: super (theFile);
059: }
060:
061: /**
062: * Constructor.
063: *
064: * @param theInputStream The input stream for the web application archive
065: * @throws IOException If there was a problem reading the WAR
066: */
067: public DefaultWarArchive(InputStream theInputStream)
068: throws IOException {
069: super (theInputStream);
070: }
071:
072: // Public Methods ----------------------------------------------------------
073:
074: /**
075: * @see WarArchive#getWebXml()
076: */
077: public final WebXml getWebXml() throws IOException, SAXException,
078: ParserConfigurationException {
079: if (this .webXml == null) {
080: InputStream in = null;
081: try {
082: in = getResource("WEB-INF/web.xml");
083: if (in != null) {
084: this .webXml = WebXmlIo.parseWebXml(in, null);
085: }
086: } finally {
087: if (in != null) {
088: in.close();
089: }
090: }
091: }
092: return this .webXml;
093: }
094:
095: /**
096: * Returns whether a class of the specified name is contained in the web-app
097: * archive, either directly in WEB-INF/classes, or in one of the JARs in
098: * WEB-INF/lib.
099: *
100: * @param theClassName The name of the class to search for
101: * @return Whether the class was found in the archive
102: * @throws IOException If an I/O error occurred reading the archive
103: */
104: public final boolean containsClass(String theClassName)
105: throws IOException {
106: // Look in WEB-INF/classes first
107: String resourceName = "WEB-INF/classes/"
108: + theClassName.replace('.', '/') + ".class";
109: if (getResource(resourceName) != null) {
110: return true;
111: }
112:
113: // Next scan the JARs in WEB-INF/lib
114: List jars = getResources("WEB-INF/lib/");
115: for (Iterator i = jars.iterator(); i.hasNext();) {
116: JarArchive jar = new DefaultJarArchive(
117: getResource((String) i.next()));
118: if (jar.containsClass(theClassName)) {
119: return true;
120: }
121: }
122:
123: return false;
124: }
125:
126: }
|