1 64 package com.jcorporate.expresso.kernel; 65 66 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 67 import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock; 68 import EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock; 69 import com.jcorporate.expresso.kernel.exception.InstallationException; 70 import com.jcorporate.expresso.kernel.internal.ContainerImplBase; 71 import org.apache.log4j.Logger; 72 73 import java.util.ArrayList ; 74 import java.util.Collections ; 75 import java.util.HashMap ; 76 import java.util.Iterator ; 77 import java.util.List ; 78 import java.util.Map ; 79 80 88 public class DefaultContainerImpl extends ContainerImplBase { 89 90 93 private ReadWriteLock containerLock = new WriterPreferenceReadWriteLock(); 94 95 98 private ComponentContainer parent = null; 99 100 103 private Map childComponents = new ConcurrentReaderHashMap(); 104 105 108 private List containerListeners = new ArrayList (); 109 110 113 private static final Logger log = Logger.getLogger(DefaultContainerImpl.class); 114 115 118 public DefaultContainerImpl() { 119 super(); 120 } 121 122 127 public void removeComponent(String componentName) { 128 try { 129 containerLock.writeLock().acquire(); 130 } catch (InterruptedException ex) { 131 log.error("Interrupted waiting for write lock. Aborting method", ex); 132 return; 133 } 134 135 try { 136 childComponents.remove(componentName); 137 } finally { 138 containerLock.writeLock().release(); 139 } 140 } 141 142 150 public Map getChildComponents() { 151 try { 152 containerLock.readLock().acquire(); 153 } catch (InterruptedException ex) { 154 log.error("Interrupted waiting for read lock. Aborting method", ex); 155 return new HashMap(); 156 } 157 try { 158 return Collections.unmodifiableMap(childComponents); 159 } finally { 160 containerLock.readLock().release(); 161 } 162 } 163 164 174 public ExpressoComponent locateComponent(String componentName) { 175 try { 176 containerLock.readLock().acquire(); 177 } catch (InterruptedException ex) { 178 log.error("Interrupted waiting for read lock. Aborting method", ex); 179 return null; 180 } 181 182 try { 183 ExpressoComponent returnValue = (ExpressoComponent) childComponents.get(componentName); 184 if (returnValue == null && _getParentContainer() != null) { 185 returnValue = _getParentContainer().locateComponent(componentName); 186 } 187 188 return returnValue; 189 } finally { 190 containerLock.readLock().release(); 191 } 192 } 193 194 204 public void installComponent(ExpressoComponent newComponent, 205 InstallationOptions installOptions, 206 InstallLog installLog) { 207 if (log.isInfoEnabled()) { 208 log.info("Installing component: " + newComponent.getMetaData().getName() + 209 " class: " + newComponent.getClass().getName()); 210 } 211 try { 212 containerLock.writeLock().acquire(); 213 } catch (InterruptedException ex) { 214 log.error("Interrupted waiting for write lock. Aborting method", ex); 215 installLog.error("Error getting container lock, Aborting Installation", ex); 216 return; 217 } 218 219 try { 220 this.addComponent(newComponent); 221 222 try { 223 if (newComponent instanceof Installable) { 224 Installable i = (Installable) newComponent; 225 i.install(installOptions, installLog); 226 } 227 228 if (log.isInfoEnabled()) { 229 log.info("Successfully installed component: " + newComponent.getMetaData().getName()); 230 } 231 } catch (InstallationException ex) { 232 log.error("Error installing component: " + newComponent.getClass().getName()); 233 this.removeComponent(newComponent.getMetaData().getName()); 234 } 235 } finally { 236 containerLock.writeLock().release(); 237 } 238 } 239 240 250 public void uninstallComponent(String componentName, 251 InstallationOptions installOptions, 252 InstallLog installLog) { 253 if (log.isInfoEnabled()) { 254 log.info("Installing component: " + componentName); 255 } 256 257 try { 258 containerLock.writeLock().acquire(); 259 } catch (InterruptedException ex) { 260 log.error("Interrupted waiting for write lock. Aborting method", ex); 261 installLog.error("Error getting container lock, Aborting Installation", ex); 262 return; 263 } 264 265 ExpressoComponent removeComponent = null; 266 try { 267 268 removeComponent = (ExpressoComponent) childComponents.get(componentName); 269 if (removeComponent == null) { 270 throw new IllegalArgumentException ("Component: '" + componentName + 271 "' does not exist in this container"); 272 } 273 274 if (removeComponent instanceof Installable) { 275 Installable i = (Installable) removeComponent; 276 i.uninstall(installOptions, installLog); 277 } 278 279 if (log.isInfoEnabled()) { 280 log.info("Successfully installed component: " + removeComponent.getMetaData().getName()); 281 } 282 } catch (InstallationException ex) { 283 284 log.error("Error installing component: " + componentName); 285 this.removeComponent(removeComponent.getMetaData().getName()); 286 } finally { 287 containerLock.writeLock().release(); 288 } 289 290 } 291 292 299 public boolean isComponentExists(String componentName) { 300 try { 301 containerLock.readLock().acquire(); 302 } catch (InterruptedException ex) { 303 log.error("Interrupted waiting for read lock. Aborting method", ex); 304 return false; 305 } 306 try { 307 return _isComponentExists(componentName); 308 } finally { 309 containerLock.readLock().release(); 310 } 311 312 } 313 314 315 322 protected boolean _isComponentExists(String componentName) { 323 boolean returnValue = childComponents.containsKey(componentName); 324 if (returnValue == false && _getParentContainer() != null) { 325 returnValue = _getParentContainer().isComponentExists(componentName); 326 } 327 return returnValue; 328 } 329 330 337 public void addComponent(ExpressoComponent newComponent) { 338 if (newComponent == null) { 339 throw new IllegalArgumentException ("newComponent must not be null"); 340 } 341 342 try { 343 containerLock.writeLock().acquire(); 344 } catch (InterruptedException ex) { 345 log.error("Interrupted waiting for read lock. Aborting method", ex); 346 return; 347 } 348 349 try { 350 351 if (this._isComponentExists(newComponent.getMetaData().getName())) { 352 throw new IllegalArgumentException ("Error adding component: " + 353 newComponent.getClass().getName() + " of name " + 354 newComponent.getMetaData().getName() 355 + " already exists in the container."); 356 } 357 358 359 childComponents.put(newComponent.getMetaData().getName(), newComponent); 360 } finally { 361 containerLock.writeLock().release(); 362 } 363 } 364 365 370 public ComponentContainer getParentContainer() { 371 try { 372 containerLock.readLock().acquire(); 373 } catch (InterruptedException ex) { 374 log.error("Interrupted waiting for read lock. Aborting method", ex); 375 return null; 376 } 377 378 try { 379 return parent; 380 } finally { 381 containerLock.readLock().release(); 382 } 383 } 384 385 390 protected ComponentContainer _getParentContainer() { 391 return parent; 392 } 393 394 399 public void setParentContainer(ComponentContainer newParent) { 400 try { 401 containerLock.writeLock().acquire(); 402 } catch (InterruptedException ex) { 403 log.error("Interrupted waiting for read lock. Aborting method", ex); 404 return; 405 } 406 407 try { 408 parent = newParent; 409 } finally { 410 containerLock.writeLock().release(); 411 } 412 } 413 414 418 public void destroyContainer() { 419 try { 420 containerLock.writeLock().acquire(); 421 } catch (InterruptedException ex) { 422 log.error("Interrupted waiting for read lock. Aborting method", ex); 423 return; 424 } 425 426 try { 427 if (log.isInfoEnabled()) { 428 log.info("Destroying Container " + this.getClass().getName()); 429 } 430 431 for (Iterator i = childComponents.values().iterator(); i.hasNext();) { 432 Object nextObject = i.next(); 433 String componentName = nextObject.getClass().getName(); 434 if (nextObject instanceof Startable) { 435 if (log.isInfoEnabled()) { 436 log.info("Stopping : " + componentName); 437 } 438 439 try { 440 ((Startable) nextObject).stop(); 441 } catch (Throwable ex) { 442 log.error("Error stopping component: " + 443 nextObject.getClass().getName(), ex); 444 } 445 } 446 447 if (nextObject instanceof Containable) { 448 try { 449 ((Containable) nextObject).getContainerImplementation().destroyContainer(); 450 } catch (Throwable ex) { 451 log.error("Error destroying container: " + 452 nextObject.getClass().getName(), ex); 453 } 454 } 455 456 if (nextObject instanceof ComponentLifecycle) { 457 if (log.isInfoEnabled()) { 458 log.info("Destroying : " + componentName); 459 try { 460 ((ComponentLifecycle) nextObject).destroy(); 461 } catch (Throwable ex) { 462 log.error("Error destroying component: " + 463 nextObject.getClass().getName(), ex); 464 } 465 } 466 467 } 468 } 469 470 childComponents.clear(); 471 } catch (Throwable t) { 472 log.error("Error destroying component", t); 473 } finally { 474 containerLock.writeLock().release(); 475 } 476 } 477 478 485 protected void finalize() throws java.lang.Throwable { 486 if (childComponents != null && !childComponents.isEmpty()) { 487 if (log != null) { 488 log.warn("Container getting GC'ed and subcomponents " + 489 "have not been destroyed."); 490 } 491 destroyContainer(); 492 } 493 494 super.finalize(); 495 } 496 497 498 } | Popular Tags |