1 29 30 package com.caucho.server.deploy; 31 32 import com.caucho.lifecycle.Lifecycle; 33 import com.caucho.loader.Environment; 34 import com.caucho.make.CachedDependency; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.Dependency; 37 38 import javax.annotation.PostConstruct; 39 import java.util.ArrayList ; 40 import java.util.HashSet ; 41 import java.util.Iterator ; 42 import java.util.LinkedHashSet ; 43 import java.util.Set ; 44 import java.util.TreeSet ; 45 import java.util.logging.Logger ; 46 47 50 public class DeployContainer<C extends DeployController> 51 extends CachedDependency 52 implements Dependency 53 { 54 private static final L10N L = new L10N(DeployContainer.class); 55 private static final Logger log 56 = Logger.getLogger(DeployContainer.class.getName()); 57 58 private final DeployListGenerator<C> _deployListGenerator 59 = new DeployListGenerator<C>(this); 60 61 private final ArrayList <C> _controllerList = new ArrayList <C>(); 62 63 private final Lifecycle _lifecycle = new Lifecycle(); 64 65 68 public DeployContainer() 69 { 70 setCheckInterval(Environment.getDependencyCheckInterval()); 71 } 72 73 76 public void add(DeployGenerator<C> generator) 77 { 78 Set <String > names = new TreeSet <String >(); 79 generator.fillDeployedKeys(names); 80 81 _deployListGenerator.add(generator); 82 83 if (_lifecycle.isActive()) 84 update(names); 85 } 86 87 90 public void remove(DeployGenerator<C> generator) 91 { 92 Set <String > names = new TreeSet <String >(); 93 generator.fillDeployedKeys(names); 94 95 _deployListGenerator.remove(generator); 96 97 if (_lifecycle.isActive()) 98 update(names); 99 } 100 101 104 public boolean isModifiedImpl() 105 { 106 return _deployListGenerator.isModified(); 107 } 108 109 112 public void update() 113 { 114 _deployListGenerator.update(); 115 } 116 117 120 @PostConstruct 121 public void init() 122 { 123 if (! _lifecycle.toInit()) 124 return; 125 } 126 127 130 public void start() 131 { 132 init(); 133 134 if (! _lifecycle.toActive()) 135 return; 136 137 _deployListGenerator.start(); 138 139 HashSet <String > keys = new LinkedHashSet <String >(); 140 141 _deployListGenerator.fillDeployedKeys(keys); 142 143 for (String key : keys) { 144 startImpl(key); 145 } 146 147 ArrayList <C> controllerList; 148 149 synchronized (_controllerList) { 150 controllerList = new ArrayList <C>(_controllerList); 151 } 152 153 for (int i = 0; i < controllerList.size(); i++) { 154 C controller = controllerList.get(i); 155 156 controller.startOnInit(); 157 } 158 } 159 160 163 public C findController(String name) 164 { 165 C controller = findDeployedController(name); 166 167 if (controller != null) 168 return controller; 169 170 controller = generateController(name); 171 172 if (controller == null) 173 return null; 174 else if (controller.isNameMatch(name)) { 176 return controller; 177 } 178 else { 179 return null; 180 } 181 } 182 183 186 public ArrayList <C> getControllers() 187 { 188 ArrayList <C> list = new ArrayList <C>(); 189 190 synchronized (_controllerList) { 191 list.addAll(_controllerList); 192 } 193 194 return list; 195 } 196 197 200 private void update(Set <String > names) 201 { 202 Iterator <String > iter = names.iterator(); 203 while (iter.hasNext()) { 204 String name = iter.next(); 205 206 update(name); 207 } 208 } 209 210 218 public C update(String name) 219 { 220 C newController = updateImpl(name); 221 222 if (_lifecycle.isActive() && newController != null) 223 newController.startOnInit(); 224 225 return newController; 226 } 227 228 236 C updateImpl(String name) 237 { 238 C oldController = null; 239 240 synchronized (_controllerList) { 241 oldController = findDeployedController(name); 242 243 if (oldController != null) 244 _controllerList.remove(oldController); 245 } 246 247 if (oldController != null) 248 oldController.destroy(); 249 250 252 C newController = generateController(name); 253 254 return newController; 255 } 256 257 263 private C startImpl(String name) 264 { 265 C oldController = null; 266 267 synchronized (_controllerList) { 268 oldController = findDeployedController(name); 269 } 270 271 if (oldController != null) 272 return oldController; 273 274 return generateController(name); 275 } 276 277 280 public void remove(String name) 281 { 282 C oldController = null; 283 284 synchronized (_controllerList) { 285 oldController = findDeployedController(name); 286 287 if (oldController != null) 288 _controllerList.remove(oldController); 289 } 290 291 if (oldController != null) 292 oldController.destroy(); 293 } 294 295 298 private C generateController(String name) 299 { 300 if (! _lifecycle.isActive()) 301 return null; 302 303 C newController = _deployListGenerator.generateController(name); 304 305 if (newController == null) 309 return null; 310 311 synchronized (_controllerList) { 313 C controller = findDeployedController(newController.getId()); 314 315 if (controller != null) 316 return controller; 317 318 327 328 _controllerList.add(newController); 329 } 330 331 init(newController); 332 333 return newController; 334 } 335 336 337 private void init(C controller) 338 { 339 Thread thread = Thread.currentThread(); 340 ClassLoader oldLoader = thread.getContextClassLoader(); 341 342 try { 343 thread.setContextClassLoader(controller.getParentClassLoader()); 344 345 controller.init(); 346 } finally { 347 thread.setContextClassLoader(oldLoader); 348 } 349 } 350 351 354 private C findDeployedController(String name) 355 { 356 synchronized (_controllerList) { 357 for (int i = 0; i < _controllerList.size(); i++) { 358 C controller = _controllerList.get(i); 359 360 if (controller.isNameMatch(name)) { 361 return controller; 362 } 363 } 364 } 365 366 return null; 367 } 368 369 372 public void stop() 373 { 374 if (! _lifecycle.toStop()) 375 return; 376 377 ArrayList <C> controllers; 378 379 synchronized (_controllerList) { 380 controllers = new ArrayList <C>(_controllerList); 381 } 382 383 for (int i = 0; i < controllers.size(); i++) 384 controllers.get(i).stop(); 385 } 386 387 390 public void destroy() 391 { 392 stop(); 393 394 if (! _lifecycle.toDestroy()) 395 return; 396 397 _deployListGenerator.destroy(); 398 399 ArrayList <C> controllerList; 400 401 synchronized (_controllerList) { 402 controllerList = new ArrayList <C>(_controllerList); 403 _controllerList.clear(); 404 } 405 406 for (int i = 0; i < controllerList.size(); i++) { 407 C controller = controllerList.get(i); 408 409 controller.destroy(); 410 } 411 } 412 413 public String toString() 414 { 415 return "DeployContainer$" + System.identityHashCode(this) + "[]"; 416 } 417 } 418 | Popular Tags |