1 29 30 package com.caucho.server.webapp; 31 32 import com.caucho.config.types.PathBuilder; 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.DeployGenerator; 38 import com.caucho.vfs.Path; 39 40 import java.util.ArrayList ; 41 import java.util.Set ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 45 48 public class WebAppSingleDeployGenerator 49 extends DeployGenerator<WebAppController> 50 implements EnvironmentListener { 51 private static final Logger log = Log.open(WebAppSingleDeployGenerator.class); 52 53 private WebAppContainer _container; 54 55 private WebAppController _parentWebApp; 56 57 private String _urlPrefix = ""; 58 59 private Path _archivePath; 60 private Path _rootDirectory; 61 62 private ArrayList <WebAppConfig> _defaultList = new ArrayList <WebAppConfig>(); 63 private WebAppConfig _config; 64 65 private ClassLoader _parentLoader; 66 67 private WebAppController _controller; 68 69 72 public WebAppSingleDeployGenerator(DeployContainer<WebAppController> deployContainer) 73 { 74 super(deployContainer); 75 } 76 77 80 public WebAppSingleDeployGenerator(DeployContainer<WebAppController> deployContainer, 81 WebAppContainer container, 82 WebAppConfig config) 83 throws Exception 84 { 85 super(deployContainer); 86 87 setContainer(container); 88 89 String contextPath = config.getContextPath(); 90 91 if (contextPath.equals("/")) 92 contextPath = ""; 93 94 setURLPrefix(config.getContextPath()); 95 96 _config = config; 97 } 98 99 102 public WebAppContainer getContainer() 103 { 104 return _container; 105 } 106 107 110 public void setContainer(WebAppContainer container) 111 { 112 _container = container; 113 114 if (_parentLoader == null) 115 _parentLoader = container.getClassLoader(); 116 } 117 120 public void setParentWebApp(WebAppController parent) 121 { 122 _parentWebApp = parent; 123 } 124 125 128 public void setParentClassLoader(ClassLoader loader) 129 { 130 _parentLoader = loader; 131 } 132 133 136 public void setURLPrefix(String prefix) 137 { 138 if (! prefix.startsWith("/")) 139 prefix = "/" + prefix; 140 141 while (prefix.endsWith("/")) { 142 prefix = prefix.substring(0, prefix.length() - 1); 143 } 144 145 _urlPrefix = prefix; 146 } 147 148 151 public String getURLPrefix() 152 { 153 return _urlPrefix; 154 } 155 156 159 public void setRootDirectory(Path rootDirectory) 160 { 161 _rootDirectory = rootDirectory; 162 } 163 164 167 public void addWebAppDefault(WebAppConfig config) 168 { 169 _defaultList.add(config); 170 } 171 172 175 protected Logger getLog() 176 { 177 return log; 178 } 179 180 183 @Override 184 protected void initImpl() 185 { 186 super.initImpl(); 187 188 if (_controller != null) 189 return; 190 191 String appDir = _config.getDocumentDirectory(); 192 193 if (appDir == null) 194 appDir = "./" + _urlPrefix; 195 196 if (_rootDirectory == null) { 197 _rootDirectory = PathBuilder.lookupPath(appDir, null, 198 _container.getDocumentDirectory()); 199 } 200 201 String archivePath = _config.getArchivePath(); 202 203 if (archivePath != null) { 204 _archivePath = PathBuilder.lookupPath(archivePath, null, 205 _container.getRootDirectory()); 206 } 207 208 _controller = new WebAppController(_urlPrefix, _rootDirectory, _container); 209 210 _controller.setArchivePath(_archivePath); 211 212 if (_archivePath != null) 213 _controller.addDepend(_archivePath); 214 215 _controller.setParentWebApp(_parentWebApp); 216 217 for (WebAppConfig config : _defaultList) 218 _controller.addConfigDefault(config); 219 220 _controller.addConfigDefault(_config); 223 224 _controller.setPrologue(_config.getPrologue()); 225 226 _controller.setSourceType("single"); 227 228 Environment.addEnvironmentListener(this, _parentLoader); 229 } 230 231 234 protected void fillDeployedKeys(Set <String > keys) 235 { 236 keys.add(_controller.getContextPath()); 237 } 238 239 242 public WebAppController generateController(String name) 243 { 244 if (name.equals(_controller.getContextPath())) { 245 WebAppController webApp; 246 247 webApp = new WebAppController(_urlPrefix, _rootDirectory, _container); 248 249 webApp.setArchivePath(_controller.getArchivePath()); 250 251 return webApp; 252 } 253 else 254 return null; 255 } 256 257 260 public WebAppController mergeController(WebAppController controller, 261 String name) 262 { 263 if (controller.getRootDirectory().equals(_controller.getRootDirectory())) { 266 controller.setContextPath(_controller.getContextPath()); 268 269 controller.setDynamicDeploy(false); 270 271 return controller.merge(_controller); 272 } 273 else if (! _controller.isNameMatch(name)) 275 return controller; 276 else 278 return _controller; 279 } 280 281 284 public void deploy() 285 { 286 try { 287 init(); 288 } catch (Exception e) { 289 log.log(Level.WARNING, e.toString(), e); 290 } 291 } 292 293 public Throwable getConfigException() 294 { 295 Throwable configException = super.getConfigException(); 296 297 if (configException == null && _controller != null) 298 configException = _controller.getConfigException(); 299 300 return configException; 301 } 302 303 306 @Override 307 protected void destroyImpl() 308 { 309 Environment.removeEnvironmentListener(this, _parentLoader); 310 311 _container.removeWebAppDeploy(this); 312 313 super.destroyImpl(); 314 } 315 316 public String toString() 317 { 318 return "WebAppSingleDeployGenerator[" + _urlPrefix + "]"; 319 } 320 } 321 | Popular Tags |