1 28 29 package com.caucho.server.webapp; 30 31 import com.caucho.config.types.PathBuilder; 32 import com.caucho.log.Log; 33 import com.caucho.server.deploy.DeployContainer; 34 import com.caucho.server.deploy.DeployGenerator; 35 import com.caucho.vfs.Path; 36 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 import java.util.regex.Matcher ; 42 import java.util.regex.Pattern ; 43 44 47 public class WebAppRegexpDeployGenerator 48 extends DeployGenerator<WebAppController> { 49 private static final Logger log = Log.open(WebAppSingleDeployGenerator.class); 50 51 private WebAppContainer _container; 52 53 private WebAppController _parent; 54 55 private WebAppConfig _config; 56 57 private ArrayList <WebAppConfig> _webAppDefaults = 58 new ArrayList <WebAppConfig>(); 59 60 private ArrayList <WebAppController> _entries = 61 new ArrayList <WebAppController>(); 62 63 66 public WebAppRegexpDeployGenerator(DeployContainer<WebAppController> deployContainer) 67 { 68 super(deployContainer); 69 } 70 71 74 public WebAppRegexpDeployGenerator(DeployContainer<WebAppController> deployContainer, 75 WebAppContainer container, 76 WebAppConfig config) 77 { 78 super(deployContainer); 79 80 setContainer(container); 81 82 _config = config; 83 } 84 85 88 public WebAppContainer getContainer() 89 { 90 return _container; 91 } 92 93 96 public void setContainer(WebAppContainer container) 97 { 98 _container = container; 99 } 100 103 public void setParent(WebAppController parent) 104 { 105 _parent = parent; 106 } 107 108 111 public WebAppController generateController(String name) 112 { 113 Pattern regexp = _config.getURLRegexp(); 114 Matcher matcher = regexp.matcher(name); 115 116 if (! matcher.find() || matcher.start() != 0) 117 return null; 118 119 int length = matcher.end() - matcher.start(); 120 121 String contextPath = matcher.group(); 122 123 ArrayList <String > vars = new ArrayList <String >(); 124 125 HashMap <String ,Object > varMap = new HashMap <String ,Object >(); 127 129 for (int j = 0; j <= matcher.groupCount(); j++) { 130 vars.add(matcher.group(j)); 131 varMap.put("app" + j, matcher.group(j)); 132 } 133 134 varMap.put("regexp", vars); 135 136 Path appDir = null; 137 138 try { 139 String appDirPath = _config.getDocumentDirectory(); 140 141 if (appDirPath == null) 142 appDirPath = "./" + matcher.group(0); 143 144 appDir = PathBuilder.lookupPath(appDirPath, varMap); 145 146 if (! appDir.canRead()) 147 return null; 148 } catch (Exception e) { 149 log.log(Level.FINER, e.toString(), e); 150 151 return null; 152 } 153 154 WebAppController controller = null; 155 156 Thread thread = Thread.currentThread(); 157 ClassLoader oldLoader = thread.getContextClassLoader(); 158 159 try { 160 thread.setContextClassLoader(getParentClassLoader()); 161 162 synchronized (_entries) { 163 for (int i = 0; i < _entries.size(); i++) { 164 controller = _entries.get(i); 165 166 if (appDir.equals(controller.getRootDirectory())) 167 return controller; 168 } 169 170 controller = new WebAppController(name, appDir, _container); 171 172 controller.getVariableMap().putAll(varMap); 175 controller.setRegexpValues(vars); 176 controller.setConfig(_config); 177 179 for (int i = 0; i < _webAppDefaults.size(); i++) 180 controller.addConfigDefault(_webAppDefaults.get(i)); 181 182 _entries.add(controller); 183 } 184 } finally { 185 thread.setContextClassLoader(oldLoader); 186 } 187 188 controller.setSourceType("regexp"); 189 190 192 return controller; 193 } 194 195 public String toString() 196 { 197 if (_config == null) 198 return "WebAppRegexpDeployGenerator[]"; 199 else 200 return "WebAppRegexpDeployGenerator[" + _config.getURLRegexp().pattern() + "]"; 201 } 202 } 203 | Popular Tags |