KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > integrationkit > LifecycleContainerBuilder


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

9 package org.nanocontainer.integrationkit;
10
11 import org.picocontainer.MutablePicoContainer;
12 import org.picocontainer.PicoContainer;
13 import org.picocontainer.defaults.ObjectReference;
14
15 /**
16  * @author <a HREF="mailto:joe@thoughtworks.net">Joe Walnes</a>
17  * @author Aslak Helles&oslash;y
18  * @author Paul Hammant
19  * @author Mauro Talevi
20  * @version $Revision: 2816 $
21  */

22 public abstract class LifecycleContainerBuilder implements ContainerBuilder {
23
24     public final void buildContainer(ObjectReference containerRef, ObjectReference parentContainerRef, Object JavaDoc 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                 // this synchronization is necessary, because several servlet requests may
33
// occur at the same time for given session, and this produce race condition
34
// especially in framed environments
35
synchronized (mutableParentContainer) {
36                     // register the child in the parent so that lifecycle can be propagated down the hierarchy
37
mutableParentContainer.addChildContainer(container);
38                 }
39             }
40         }
41
42         if (container instanceof MutablePicoContainer) {
43             composeContainer((MutablePicoContainer) container, assemblyScope);
44         }
45         autoStart(container);
46
47         // hold on to it
48
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                 // see comment in buildContainer
63
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 JavaDoc assemblyScope);
73
74     protected abstract PicoContainer createContainer(PicoContainer parentContainer, Object JavaDoc assemblyScope);
75 }
76
Popular Tags