1 29 30 package com.caucho.server.webapp; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.loader.Environment; 34 import com.caucho.loader.EnvironmentListener; 35 import com.caucho.log.Log; 36 import com.caucho.server.deploy.DeployContainer; 37 import com.caucho.server.deploy.ExpandDeployGenerator; 38 import com.caucho.vfs.CaseInsensitive; 39 import com.caucho.vfs.Path; 40 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 import java.util.Set ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 50 public class WebAppExpandDeployGenerator extends ExpandDeployGenerator<WebAppController> 51 implements EnvironmentListener { 52 private static final Logger log = Log.open(WebAppExpandDeployGenerator.class); 53 54 private final WebAppExpandDeployGeneratorAdmin _admin; 55 56 private WebAppContainer _container; 57 58 private WebAppController _parent; 59 60 private String _urlPrefix = ""; 61 62 private ArrayList <WebAppConfig> _webAppDefaults 63 = new ArrayList <WebAppConfig>(); 64 65 private HashMap <Path,WebAppConfig> _webAppConfigMap 66 = new HashMap <Path,WebAppConfig>(); 67 68 private HashMap <String ,Path> _contextPathMap 70 = new HashMap <String ,Path>(); 71 72 private ClassLoader _parentLoader; 73 74 77 public WebAppExpandDeployGenerator(DeployContainer<WebAppController> container, 78 WebAppContainer webAppContainer) 79 { 80 super(container, webAppContainer.getRootDirectory()); 81 82 _container = webAppContainer; 83 84 _parentLoader = webAppContainer.getClassLoader(); 85 86 try { 87 setExtension(".war"); 88 } catch (Exception e) { 89 log.log(Level.WARNING, e.toString(), e); 90 } 91 92 _admin = new WebAppExpandDeployGeneratorAdmin(this); 93 } 94 95 98 public WebAppContainer getContainer() 99 { 100 return _container; 101 } 102 103 106 public void setParent(WebAppController parent) 107 { 108 _parent = parent; 109 } 110 111 114 public void setParentClassLoader(ClassLoader loader) 115 { 116 _parentLoader = loader; 117 } 118 119 122 public void setURLPrefix(String prefix) 123 { 124 if (prefix.equals("")) { 125 } 126 127 while (prefix.endsWith("/")) 128 prefix = prefix.substring(0, prefix.length() - 1); 129 130 _urlPrefix = prefix; 131 132 } 133 134 137 public String getURLPrefix() 138 { 139 return _urlPrefix; 140 } 141 142 145 public void setLazyInit(boolean lazyInit) 146 throws ConfigException 147 { 148 log.config("lazy-init is deprecated. Use <startup>lazy</startup> instead."); 149 if (lazyInit) 150 setStartupMode("lazy"); 151 else 152 setStartupMode("automatic"); 153 } 154 155 158 public void addWebApp(WebAppConfig config) 159 { 160 String docDir = config.getDocumentDirectory(); 161 162 Path appDir = getExpandDirectory().lookup(docDir); 163 164 _webAppConfigMap.put(appDir, config); 165 166 if (config.getContextPath() != null) 167 _contextPathMap.put(config.getContextPath(), appDir); 168 } 169 170 173 public void addWebAppDefault(WebAppConfig config) 174 { 175 _webAppDefaults.add(config); 176 } 177 178 @Override 179 protected void initImpl() 180 { 181 super.initImpl(); 182 } 183 184 187 @Override 188 protected Logger getLog() 189 { 190 return log; 191 } 192 193 196 @Override 197 protected void fillDeployedKeys(Set <String > keys) 198 { 199 super.fillDeployedKeys(keys); 200 201 for (WebAppConfig cfg : _webAppConfigMap.values()) { 202 if (cfg.getContextPath() != null) 203 keys.add(cfg.getContextPath()); 204 } 205 } 206 207 210 @Override 211 protected void startImpl() 212 { 213 super.startImpl(); 214 215 Environment.addEnvironmentListener(this, _parentLoader); 216 217 _admin.register(); 218 } 219 220 223 @Override 224 protected WebAppController createController(String name) 225 { 226 if (! name.startsWith(_urlPrefix)) 227 return null; 228 229 String segmentName = name.substring(_urlPrefix.length()); 230 231 Path webAppRoot = _contextPathMap.get(segmentName); 232 233 if (webAppRoot != null) 234 segmentName = "/" + webAppRoot.getTail(); 235 else if (segmentName.indexOf('/', 1) > 0) 236 return null; 237 238 if (segmentName.equals("")) { 239 if (CaseInsensitive.isCaseInsensitive()) 240 segmentName = "/root"; 241 else 242 segmentName = "/ROOT"; 243 } 244 245 String expandName = getExpandName(segmentName.substring(1)); 246 247 String archiveName = segmentName + ".war"; 248 Path jarPath = getArchiveDirectory().lookup("." + archiveName); 249 250 Path rootDirectory; 251 252 if (jarPath.isDirectory()) { 253 rootDirectory = getExpandDirectory().lookup("." + archiveName); 254 jarPath = null; 255 } 256 else { 257 rootDirectory = getExpandDirectory().lookup("./" + expandName); 259 } 260 261 if (! rootDirectory.isDirectory() && 262 (jarPath == null || ! jarPath.isFile())) 263 return null; 264 else if (rootDirectory.isDirectory() && 265 ! isValidDirectory(rootDirectory, segmentName.substring(1))) 266 return null; 267 268 WebAppConfig cfg = _webAppConfigMap.get(rootDirectory); 269 270 if (cfg != null && cfg.getContextPath() != null) 271 name = cfg.getContextPath(); 272 273 WebAppController controller 274 = new WebAppController(name, rootDirectory, _container); 275 276 controller.setWarName(segmentName.substring(1)); 277 278 controller.setParentWebApp(_parent); 279 280 controller.setDynamicDeploy(true); 281 controller.setSourceType("expand"); 282 283 return controller; 284 } 285 286 287 290 @Override 291 protected WebAppController mergeController(WebAppController controller, 292 String key) 293 { 294 try { 295 Path expandDirectory = getExpandDirectory(); 296 Path rootDirectory = controller.getRootDirectory(); 297 298 if (! expandDirectory.equals(rootDirectory.getParent())) 299 return controller; 300 301 controller = super.mergeController(controller, key); 302 303 if (controller.getArchivePath() == null) { 304 String archiveName = rootDirectory.getTail() + ".war"; 305 306 Path jarPath = getArchiveDirectory().lookup(archiveName); 307 308 if (! jarPath.isDirectory()) { 309 controller.setArchivePath(jarPath); 310 controller.addDepend(jarPath); 311 } 312 } 313 314 controller.setStartupMode(getStartupMode()); 315 317 for (int i = 0; i < _webAppDefaults.size(); i++) 318 controller.addConfigDefault(_webAppDefaults.get(i)); 319 320 WebAppConfig cfg = _webAppConfigMap.get(rootDirectory); 321 322 if (cfg != null) 323 controller.addConfigDefault(cfg); 324 } catch (ConfigException e) { 325 controller.setConfigException(e); 326 327 log.log(Level.FINER, e.toString(), e); 328 log.warning(e.toString()); 329 } catch (Throwable e) { 330 controller.setConfigException(e); 331 332 log.log(Level.WARNING, e.toString(), e); 333 } 334 335 return controller; 336 } 337 338 341 @Override 342 protected String pathNameToEntryName(String name) 343 { 344 String entryName = super.pathNameToEntryName(name); 345 346 if (entryName == null) 347 return null; 348 349 if (CaseInsensitive.isCaseInsensitive()) { 350 try { 351 String []list = getExpandDirectory().list(); 352 353 String matchName = null; 354 355 for (int i = 0; i < list.length; i++) { 356 if (list[i].equalsIgnoreCase(entryName)) 357 matchName = list[i]; 358 } 359 360 if (matchName == null) 361 matchName = entryName.toLowerCase(); 362 } catch (Exception e) { 363 entryName = entryName.toLowerCase(); 364 } 365 } 366 367 if (entryName.equalsIgnoreCase("root")) 368 return _urlPrefix; 369 else 370 return _urlPrefix + "/" + entryName; 371 } 372 373 @Override 374 protected String entryNameToArchiveName(String entryName) 375 { 376 String prefix = _urlPrefix + "/"; 377 378 if (entryName.equals(_urlPrefix)) 379 return "ROOT" + getExtension(); 380 else if (entryName.startsWith(prefix)) 381 return entryName.substring(prefix.length()) + getExtension(); 382 else 383 return null; 384 } 385 386 389 @Override 390 protected void destroyImpl() 391 { 392 _admin.unregister(); 393 394 _container.removeWebAppDeploy(this); 395 396 Environment.removeEnvironmentListener(this, _parentLoader); 397 398 super.destroyImpl(); 399 } 400 } 401 | Popular Tags |