1 2 3 27 28 29 package org.apache.catalina.startup; 30 31 32 import java.io.File ; 33 import java.lang.reflect.Method ; 34 import java.net.URL ; 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 ; 43 44 45 51 public class SetDocBaseRule extends Rule { 52 53 54 56 57 62 public SetDocBaseRule(Digester digester) { 63 64 super(digester); 65 66 } 67 68 69 71 72 74 75 82 public void begin(Attributes attributes) throws Exception { 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 method = parent.getClass().getMethod("getHost",(Class [])null); 89 host = (Host) method.invoke(parent,(Object [])null); 90 } else { 91 host = (Host) parent; 92 } 93 String 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 canonicalAppBase = new File (appBase); 108 if (canonicalAppBase.isAbsolute()) { 109 canonicalAppBase = canonicalAppBase.getCanonicalFile(); 110 } else { 111 canonicalAppBase = 112 new File (System.getProperty("catalina.base"), appBase) 113 .getCanonicalFile(); 114 } 115 116 String docBase = child.getDocBase(); 117 if (docBase == null) { 118 String 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 file = new File (docBase); 135 if (!file.isAbsolute()) { 136 docBase = (new File (canonicalAppBase, docBase)).getPath(); 137 } else { 138 docBase = file.getCanonicalPath(); 139 } 140 141 if (docBase.toLowerCase().endsWith(".war")) { 142 URL war = new URL ("jar:" + (new File (docBase)).toURL() + "!/"); 143 String contextPath = child.getPath(); 144 if (contextPath.equals("")) { 145 contextPath = "ROOT"; 146 } 147 docBase = ExpandWar.expand(host, war, contextPath); 148 file = new File (docBase); 149 docBase = file.getCanonicalPath(); 150 } else { 151 File docDir = new File (docBase); 152 if (!docDir.exists()) { 153 File warFile = new File (docBase + ".war"); 154 if (warFile.exists()) { 155 URL war = new URL ("jar:" + warFile.toURL() + "!/"); 156 docBase = ExpandWar.expand(host, war, child.getPath()); 157 file = new File (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 |