1 29 30 package com.caucho.server.deploy; 31 32 import com.caucho.config.BuilderProgram; 33 import com.caucho.config.Config; 34 import com.caucho.config.ConfigELContext; 35 import com.caucho.config.ConfigException; 36 import com.caucho.config.types.PathBuilder; 37 import com.caucho.el.EL; 38 import com.caucho.jmx.Jmx; 39 import com.caucho.loader.Environment; 40 import com.caucho.loader.EnvironmentClassLoader; 41 import com.caucho.loader.EnvironmentListener; 42 import com.caucho.log.Log; 43 import com.caucho.util.L10N; 44 import com.caucho.vfs.Path; 45 import com.caucho.vfs.Vfs; 46 47 import javax.el.ELContext; 48 import javax.el.ELException; 49 import javax.el.ELResolver; 50 import javax.management.ObjectName ; 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.LinkedHashMap ; 54 import java.util.Map ; 55 import java.util.logging.Level ; 56 import java.util.logging.Logger ; 57 58 61 abstract public class 62 EnvironmentDeployController<I extends EnvironmentDeployInstance, 63 C extends DeployConfig> 64 extends ExpandDeployController<I> 65 implements EnvironmentListener 66 { 67 private static final L10N L = new L10N(EnvironmentDeployController.class); 68 private static final Logger log 69 = Log.open(EnvironmentDeployController.class); 70 71 private LinkedHashMap <String ,String > _jmxContext; 73 74 private Object _mbean; 75 76 private ObjectName _objectName; 77 78 private ArrayList <C> _configDefaults = new ArrayList <C>(); 80 81 private C _config; 83 private DeployConfig _prologue; 84 85 private ELResolver _parentELResolver; 87 88 private HashMap <String ,Object > _variableMap = new HashMap <String ,Object >(); 90 91 private Throwable _configException; 93 94 public EnvironmentDeployController() 95 { 96 this("", (Path) null); 97 } 98 99 public EnvironmentDeployController(C config) 100 { 101 this(config.getId(), config.calculateRootDirectory()); 102 103 setConfig(config); 104 } 105 106 public EnvironmentDeployController(String id, C config) 107 { 108 this(id, config.calculateRootDirectory()); 109 110 setConfig(config); 111 } 112 113 public EnvironmentDeployController(String id, Path rootDirectory) 114 { 115 super(id, null, rootDirectory); 116 117 ELContext elContext = EL.getEnvironment(getParentClassLoader()); 118 119 if (elContext != null) 120 _parentELResolver = elContext.getELResolver(); 121 122 _jmxContext = Jmx.copyContextProperties(getParentClassLoader()); 123 } 124 125 128 public void setConfig(C config) 129 { 130 if (config == null) 131 return; 132 133 if (_config != null && ! _configDefaults.contains(_config)) 134 addConfigDefault(_config); 135 136 addConfigMode(config); 137 138 _config = config; 139 140 if (_prologue == null) 141 setPrologue(config.getPrologue()); 142 143 148 } 149 150 153 public C getConfig() 154 { 155 return _config; 156 } 157 158 161 public DeployConfig getPrologue() 162 { 163 return _prologue; 164 } 165 166 169 public void setPrologue(DeployConfig prologue) 170 { 171 _prologue = prologue; 172 } 173 174 177 public Throwable getConfigException() 178 { 179 Throwable configException = super.getConfigException(); 180 181 if (configException == null) 182 configException = _configException; 183 184 if (configException == null) { 185 DeployInstance deploy = getDeployInstance(); 186 187 if (deploy != null) 188 configException = deploy.getConfigException(); 189 } 190 191 return configException; 192 } 193 194 197 public void addConfigDefault(C config) 198 { 199 if (! _configDefaults.contains(config)) { 200 _configDefaults.add(config); 201 202 addConfigMode(config); 203 } 204 } 205 206 private void addConfigMode(C config) 207 { 208 if (config.getStartupMode() != null) 209 setStartupMode(config.getStartupMode()); 210 211 if (config.getRedeployCheckInterval() != null) 212 setRedeployCheckInterval(config.getRedeployCheckInterval()); 213 214 if (config.getRedeployMode() != null) 215 setRedeployMode(config.getRedeployMode()); 216 } 217 218 221 public HashMap <String ,Object > getVariableMap() 222 { 223 return _variableMap; 224 } 225 226 229 public Object getMBean() 230 { 231 return _mbean; 232 } 233 234 237 public ObjectName getObjectName() 238 { 239 return _objectName; 240 } 241 242 245 public void setConfigException(Throwable e) 246 { 247 _configException = e; 248 } 249 250 253 protected void initEnd() 254 { 255 super.initEnd(); 256 257 if (getDeployAdmin() != null) 258 getDeployAdmin().register(); 259 } 260 261 264 public boolean isNameMatch(String url) 265 { 266 return url.equals(getId()); 267 } 268 269 272 protected void mergeController(DeployController oldControllerV) 273 { 274 super.mergeController(oldControllerV); 275 276 EnvironmentDeployController<I,C> oldController; 277 oldController = (EnvironmentDeployController) oldControllerV; 278 280 _configDefaults.addAll(oldController._configDefaults); 281 282 if (getConfig() == null) 283 setConfig(oldController.getConfig()); 284 else if (oldController.getConfig() != null) { 285 _configDefaults.add(getConfig()); 286 287 setConfig(oldController.getConfig()); 288 } 289 290 if (getPrologue() == null) 291 setPrologue(oldController.getPrologue()); 292 else if (oldController.getPrologue() != null) { 293 _configDefaults.add(0, (C) getPrologue()); 295 setPrologue(oldController.getPrologue()); 296 } 297 298 if (oldController.getArchivePath() != null) 299 setArchivePath(oldController.getArchivePath()); 300 301 mergeStartupMode(oldController.getStartupMode()); 302 303 mergeRedeployCheckInterval(oldController.getRedeployCheckInterval()); 304 305 mergeRedeployMode(oldController.getRedeployMode()); 306 } 307 308 311 public boolean destroy() 312 { 313 if (! super.destroy()) 314 return false; 315 316 Environment.removeEnvironmentListener(this, getParentClassLoader()); 317 318 Thread thread = Thread.currentThread(); 319 ClassLoader oldLoader = thread.getContextClassLoader(); 320 321 try { 322 thread.setContextClassLoader(getParentClassLoader()); 323 324 if (getDeployAdmin() != null) 325 getDeployAdmin().unregister(); 326 } finally { 327 thread.setContextClassLoader(oldLoader); 328 } 329 330 return true; 331 } 332 333 336 protected void configureInstance(I instance) 337 throws Throwable 338 { 339 Thread thread = Thread.currentThread(); 340 ClassLoader oldLoader = thread.getContextClassLoader(); 341 342 try { 343 ClassLoader classLoader = instance.getClassLoader(); 344 345 thread.setContextClassLoader(classLoader); 346 347 log.fine(instance + " initializing"); 348 349 instance.setConfigException(_configException); 351 352 HashMap <String ,Object > varMap = new HashMap <String ,Object >(); 353 varMap.putAll(_variableMap); 354 355 for (Map.Entry <String ,Object > entry : varMap.entrySet()) { 356 Config.setCurrentVar(entry.getKey(), entry.getValue()); 357 } 358 359 EL.setVariableMap(varMap, classLoader); 360 ConfigELContext elContext = new ConfigELContext(varMap); 361 362 EL.setEnvironment(elContext, classLoader); 363 Config.setELContext(elContext); 364 365 configureInstanceVariables(instance); 366 367 extendJMXContext(_jmxContext); 368 369 Jmx.setContextProperties(_jmxContext, classLoader); 370 371 ArrayList <DeployConfig> initList = new ArrayList <DeployConfig>(); 372 373 if (getPrologue() != null) 374 initList.add(getPrologue()); 375 376 fillInitList(initList); 377 378 thread.setContextClassLoader(instance.getClassLoader()); 379 Vfs.setPwd(getRootDirectory()); 380 381 if (getArchivePath() != null) 382 Environment.addDependency(getArchivePath()); 383 384 for (DeployConfig config : initList) { 385 BuilderProgram program = config.getBuilderProgram(); 386 387 if (program != null) 388 program.configure(instance); 389 } 390 391 instance.init(); 392 } finally { 393 thread.setContextClassLoader(oldLoader); 394 } 395 } 396 397 protected void extendJMXContext(Map <String ,String > context) 398 { 399 } 401 402 protected void fillInitList(ArrayList <DeployConfig> initList) 403 { 404 initList.addAll(_configDefaults); 405 406 if (_config != null && ! initList.contains(_config)) 407 initList.add(_config); 408 } 409 410 protected void configureInstanceVariables(I instance) 411 throws Throwable 412 { 413 Path rootDirectory = getRootDirectory(); 414 415 if (rootDirectory == null) 416 throw new NullPointerException ("Null root directory"); 417 418 if (! rootDirectory.isFile()) { 419 } 420 else if (rootDirectory.getPath().endsWith(".jar") || 421 rootDirectory.getPath().endsWith(".war")) { 422 throw new ConfigException(L.l("root-directory `{0}' must specify a directory. It may not be a .jar or .war.", 423 rootDirectory.getPath())); 424 } 425 else 426 throw new ConfigException(L.l("root-directory `{0}' may not be a file. root-directory must specify a directory.", 427 rootDirectory.getPath())); 428 Vfs.setPwd(rootDirectory); 429 430 if (log.isLoggable(Level.FINE)) 431 log.fine(instance + " root-directory=" + rootDirectory); 432 433 instance.setRootDirectory(rootDirectory); 434 } 435 436 public Path getArchivePath() 437 { 438 Path path = super.getArchivePath(); 439 440 if (path != null) 441 return path; 442 443 if (_config != null) { 444 String pathString = _config.getArchivePath(); 445 446 if (pathString != null) { 447 try { 448 path = PathBuilder.lookupPath(pathString); 449 } catch (ELException e) { 450 throw new RuntimeException (e); 451 } 452 } 453 454 setArchivePath(path); 455 } 456 457 return path; 458 } 459 460 463 public void environmentStart(EnvironmentClassLoader loader) 464 { 465 try { 466 start(); 467 } catch (Throwable e) { 468 log.log(Level.WARNING, e.toString(), e); 469 } 470 } 471 472 475 public void environmentStop(EnvironmentClassLoader loader) 476 { 477 stop(); 478 } 479 480 483 public String toString() 484 { 485 String name = getClass().getName(); 486 487 name = name.substring(name.lastIndexOf('.') + 1); 488 489 return name + "" + System.identityHashCode(this) + "[" + getId() + "]"; 490 } 491 } 492 | Popular Tags |