1 9 package org.nanocontainer.integrationkit; 10 11 import org.picocontainer.MutablePicoContainer; 12 import org.picocontainer.PicoContainer; 13 import org.picocontainer.defaults.ObjectReference; 14 15 22 public abstract class LifecycleContainerBuilder implements ContainerBuilder { 23 24 public final void buildContainer(ObjectReference containerRef, ObjectReference parentContainerRef, Object assemblyScope, boolean addChildToParent) { 25 PicoContainer parentContainer = parentContainerRef == null ? null : (PicoContainer) parentContainerRef.get(); 26 PicoContainer container = createContainer(parentContainer, assemblyScope); 27 28 if (parentContainer != null && parentContainer instanceof MutablePicoContainer) { 29 MutablePicoContainer mutableParentContainer = (MutablePicoContainer) parentContainer; 30 31 if (addChildToParent) { 32 synchronized (mutableParentContainer) { 36 mutableParentContainer.addChildContainer(container); 38 } 39 } 40 } 41 42 if (container instanceof MutablePicoContainer) { 43 composeContainer((MutablePicoContainer) container, assemblyScope); 44 } 45 autoStart(container); 46 47 containerRef.set(container); 49 } 50 51 protected void autoStart(PicoContainer container) { 52 container.start(); 53 } 54 55 public void killContainer(ObjectReference containerRef) { 56 try { 57 PicoContainer pico = (PicoContainer) containerRef.get(); 58 pico.stop(); 59 pico.dispose(); 60 PicoContainer parent = pico.getParent(); 61 if (parent != null && parent instanceof MutablePicoContainer) { 62 synchronized (parent) { 64 ((MutablePicoContainer) parent).unregisterComponentByInstance(pico); 65 } 66 } 67 } finally { 68 containerRef.set(null); 69 } 70 } 71 72 protected abstract void composeContainer(MutablePicoContainer container, Object assemblyScope); 73 74 protected abstract PicoContainer createContainer(PicoContainer parentContainer, Object assemblyScope); 75 } 76 | Popular Tags |