KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > SetDocBaseRule


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.catalina.startup;
30
31
32 import java.io.File JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34 import java.net.URL JavaDoc;
35
36 import org.apache.catalina.Context;
37 import org.apache.catalina.Deployer;
38 import org.apache.catalina.Host;
39 import org.apache.catalina.core.StandardHost;
40 import com.sun.org.apache.commons.digester.Digester;
41 import com.sun.org.apache.commons.digester.Rule;
42 import org.xml.sax.Attributes JavaDoc;
43
44
45 /**
46  * <p>Rule that modifies the docBase of the host, setting it appropriately,
47  * before adding the Context to the parent Host.</p>
48  *
49  * @author Remy Maucherat
50  */

51 public class SetDocBaseRule extends Rule {
52
53
54     // ----------------------------------------------------------- Constructors
55

56
57     /**
58      * Construct a new instance of this Rule.
59      *
60      * @param digester Digester we are associated with
61      */

62     public SetDocBaseRule(Digester digester) {
63
64         super(digester);
65
66     }
67
68
69     // ----------------------------------------------------- Instance Variables
70

71
72     // --------------------------------------------------------- Public Methods
73

74
75     /**
76      * Handle the beginning of an XML element.
77      *
78      * @param attributes The attributes of this element
79      *
80      * @exception Exception if a processing error occurs
81      */

82     public void begin(Attributes JavaDoc attributes) throws Exception JavaDoc {
83
84         Context child = (Context) digester.peek(0);
85         Deployer parent = (Deployer) digester.peek(1);
86         Host host = null;
87         if (!(parent instanceof StandardHost)) {
88             Method JavaDoc method = parent.getClass().getMethod("getHost",(Class JavaDoc[])null);
89             host = (Host) method.invoke(parent,(Object JavaDoc[])null);
90         } else {
91             host = (Host) parent;
92         }
93         String JavaDoc appBase = host.getAppBase();
94
95         boolean unpackWARs = true;
96         if (host instanceof StandardHost) {
97             unpackWARs = ((StandardHost) host).isUnpackWARs();
98         }
99         if (!unpackWARs
100             && !("true".equals(attributes.getValue("unpackWAR")))) {
101             return;
102         }
103         if ("false".equals(attributes.getValue("unpackWAR"))) {
104             return;
105         }
106
107         File JavaDoc canonicalAppBase = new File JavaDoc(appBase);
108         if (canonicalAppBase.isAbsolute()) {
109             canonicalAppBase = canonicalAppBase.getCanonicalFile();
110         } else {
111             canonicalAppBase =
112                 new File JavaDoc(System.getProperty("catalina.base"), appBase)
113                 .getCanonicalFile();
114         }
115
116         String JavaDoc docBase = child.getDocBase();
117         if (docBase == null) {
118             // Trying to guess the docBase according to the path
119
String JavaDoc path = child.getPath();
120             if (path == null) {
121                 return;
122             }
123             if (path.equals("")) {
124                 docBase = "ROOT";
125             } else {
126                 if (path.startsWith("/")) {
127                     docBase = path.substring(1);
128                 } else {
129                     docBase = path;
130                 }
131             }
132         }
133
134         File JavaDoc file = new File JavaDoc(docBase);
135         if (!file.isAbsolute()) {
136             docBase = (new File JavaDoc(canonicalAppBase, docBase)).getPath();
137         } else {
138             docBase = file.getCanonicalPath();
139         }
140
141         if (docBase.toLowerCase().endsWith(".war")) {
142             URL JavaDoc war = new URL JavaDoc("jar:" + (new File JavaDoc(docBase)).toURL() + "!/");
143             String JavaDoc contextPath = child.getPath();
144             if (contextPath.equals("")) {
145                 contextPath = "ROOT";
146             }
147             docBase = ExpandWar.expand(host, war, contextPath);
148             file = new File JavaDoc(docBase);
149             docBase = file.getCanonicalPath();
150         } else {
151             File JavaDoc docDir = new File JavaDoc(docBase);
152             if (!docDir.exists()) {
153                 File JavaDoc warFile = new File JavaDoc(docBase + ".war");
154                 if (warFile.exists()) {
155                     URL JavaDoc war = new URL JavaDoc("jar:" + warFile.toURL() + "!/");
156                     docBase = ExpandWar.expand(host, war, child.getPath());
157                     file = new File JavaDoc(docBase);
158                     docBase = file.getCanonicalPath();
159                 }
160             }
161         }
162
163         if (docBase.startsWith(canonicalAppBase.getPath())) {
164             docBase = docBase.substring
165                 (canonicalAppBase.getPath().length() + 1);
166         }
167         docBase = docBase.replace(File.separatorChar, '/');
168
169         child.setDocBase(docBase);
170
171
172     }
173
174
175 }
176
Popular Tags