001: /*
002: * Copyright 1999-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.startup;
018:
019: import java.io.File;
020: import java.lang.reflect.Method;
021: import java.net.URL;
022:
023: import org.apache.catalina.Context;
024: import org.apache.catalina.Deployer;
025: import org.apache.catalina.Host;
026: import org.apache.catalina.core.StandardHost;
027: import org.apache.commons.digester.Rule;
028: import org.xml.sax.Attributes;
029:
030: /**
031: * <p>Rule that modifies the docBase of the host, setting it appropriately,
032: * before adding the Context to the parent Host.</p>
033: *
034: * @author Remy Maucherat
035: */
036: public class SetDocBaseRule extends Rule {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: /**
041: * Construct a new instance of this Rule.
042: */
043: public SetDocBaseRule() {
044: }
045:
046: // ----------------------------------------------------- Instance Variables
047:
048: // --------------------------------------------------------- Public Methods
049:
050: /**
051: * Handle the beginning of an XML element.
052: *
053: * @param attributes The attributes of this element
054: *
055: * @exception Exception if a processing error occurs
056: */
057: public void begin(String namespace, String name,
058: Attributes attributes) throws Exception {
059:
060: Context child = (Context) digester.peek(0);
061: Deployer parent = (Deployer) digester.peek(1);
062: Host host = null;
063: if (!(parent instanceof StandardHost)) {
064: Method method = parent.getClass()
065: .getMethod("getHost", null);
066: host = (Host) method.invoke(parent, null);
067: } else {
068: host = (Host) parent;
069: }
070: String appBase = host.getAppBase();
071:
072: boolean unpackWARs = true;
073: if (host instanceof StandardHost) {
074: unpackWARs = ((StandardHost) host).isUnpackWARs();
075: }
076: if (!unpackWARs
077: && !("true".equals(attributes.getValue("unpackWAR")))) {
078: return;
079: }
080: if ("false".equals(attributes.getValue("unpackWAR"))) {
081: return;
082: }
083:
084: File canonicalAppBase = new File(appBase);
085: if (canonicalAppBase.isAbsolute()) {
086: canonicalAppBase = canonicalAppBase.getCanonicalFile();
087: } else {
088: canonicalAppBase = new File(System
089: .getProperty("catalina.base"), appBase)
090: .getCanonicalFile();
091: }
092:
093: String docBase = child.getDocBase();
094: if (docBase == null) {
095: // Trying to guess the docBase according to the path
096: String path = child.getPath();
097: if (path == null) {
098: return;
099: }
100: if (path.equals("")) {
101: docBase = "ROOT";
102: } else {
103: if (path.startsWith("/")) {
104: docBase = path.substring(1);
105: } else {
106: docBase = path;
107: }
108: }
109: }
110:
111: File file = new File(docBase);
112: if (!file.isAbsolute()) {
113: docBase = (new File(canonicalAppBase, docBase)).getPath();
114: } else {
115: docBase = file.getCanonicalPath();
116: }
117:
118: if (docBase.toLowerCase().endsWith(".war")) {
119: URL war = new URL("jar:" + (new File(docBase)).toURL()
120: + "!/");
121: String contextPath = child.getPath();
122: if (contextPath.equals("")) {
123: contextPath = "ROOT";
124: }
125: docBase = ExpandWar.expand(host, war, contextPath);
126: file = new File(docBase);
127: docBase = file.getCanonicalPath();
128: } else {
129: File docDir = new File(docBase);
130: if (!docDir.exists()) {
131: File warFile = new File(docBase + ".war");
132: if (warFile.exists()) {
133: URL war = new URL("jar:" + warFile.toURL() + "!/");
134: docBase = ExpandWar.expand(host, war, child
135: .getPath());
136: file = new File(docBase);
137: docBase = file.getCanonicalPath();
138: }
139: }
140: }
141:
142: if (docBase.startsWith(canonicalAppBase.getPath())) {
143: docBase = docBase.substring(canonicalAppBase.getPath()
144: .length());
145: docBase = docBase.replace(File.separatorChar, '/');
146: if (docBase.startsWith("/")) {
147: docBase = docBase.substring(1);
148: }
149: } else {
150: docBase = docBase.replace(File.separatorChar, '/');
151: }
152:
153: child.setDocBase(docBase);
154:
155: }
156:
157: }
|