1 /*
2 * ========================================================================
3 *
4 * Copyright 2003 The Apache Software Foundation.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * ========================================================================
19 */
20 package org.apache.cactus.integration.ant.deployment.webapp;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.xml.parsers.ParserConfigurationException;
29
30 import org.apache.cactus.integration.ant.deployment.DefaultJarArchive;
31 import org.apache.cactus.integration.ant.deployment.JarArchive;
32 import org.xml.sax.SAXException;
33
34 /**
35 * Class that encapsulates access to a WAR.
36 *
37 * @since Cactus 1.5
38 * @version $Id: DefaultWarArchive.java,v 1.1 2004/05/31 20:05:23 vmassol Exp $
39 */
40 public class DefaultWarArchive extends DefaultJarArchive implements WarArchive
41 {
42 // Instance Variables ------------------------------------------------------
43
44 /**
45 * The parsed deployment descriptor.
46 */
47 private WebXml webXml;
48
49 // Constructors ------------------------------------------------------------
50
51 /**
52 * Constructor.
53 *
54 * @param theFile The web application archive
55 * @throws IOException If there was a problem reading the WAR
56 */
57 public DefaultWarArchive(File theFile)
58 throws IOException
59 {
60 super(theFile);
61 }
62
63 /**
64 * Constructor.
65 *
66 * @param theInputStream The input stream for the web application archive
67 * @throws IOException If there was a problem reading the WAR
68 */
69 public DefaultWarArchive(InputStream theInputStream)
70 throws IOException
71 {
72 super(theInputStream);
73 }
74
75 // Public Methods ----------------------------------------------------------
76
77 /**
78 * @see WarArchive#getWebXml()
79 */
80 public final WebXml getWebXml()
81 throws IOException, SAXException, ParserConfigurationException
82 {
83 if (this.webXml == null)
84 {
85 InputStream in = null;
86 try
87 {
88 in = getResource("WEB-INF/web.xml");
89 if (in != null)
90 {
91 this.webXml = WebXmlIo.parseWebXml(in, null);
92 }
93 }
94 finally
95 {
96 if (in != null)
97 {
98 in.close();
99 }
100 }
101 }
102 return this.webXml;
103 }
104
105 /**
106 * Returns whether a class of the specified name is contained in the web-app
107 * archive, either directly in WEB-INF/classes, or in one of the JARs in
108 * WEB-INF/lib.
109 *
110 * @param theClassName The name of the class to search for
111 * @return Whether the class was found in the archive
112 * @throws IOException If an I/O error occurred reading the archive
113 */
114 public final boolean containsClass(String theClassName)
115 throws IOException
116 {
117 // Look in WEB-INF/classes first
118 String resourceName =
119 "WEB-INF/classes/" + theClassName.replace('.', '/') + ".class";
120 if (getResource(resourceName) != null)
121 {
122 return true;
123 }
124
125 // Next scan the JARs in WEB-INF/lib
126 List jars = getResources("WEB-INF/lib/");
127 for (Iterator i = jars.iterator(); i.hasNext();)
128 {
129 JarArchive jar = new DefaultJarArchive(
130 getResource((String) i.next()));
131 if (jar.containsClass(theClassName))
132 {
133 return true;
134 }
135 }
136
137 return false;
138 }
139
140 }