KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > container > pico > PicoContainerWrapper


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7  http://www.apache.org/licenses/LICENSE-2.0
8
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15
16 package com.jdon.container.pico;
17
18 import java.util.List JavaDoc;
19
20 import org.picocontainer.ComponentAdapter;
21 import org.picocontainer.MutablePicoContainer;
22 import org.picocontainer.Parameter;
23 import org.picocontainer.defaults.ConstantParameter;
24 import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
25 import org.picocontainer.defaults.DefaultPicoContainer;
26
27 import com.jdon.container.ContainerWrapper;
28 import com.jdon.container.finder.ContainerCallback;
29 import com.jdon.util.Debug;
30
31 /**
32  * Picocontainer is the implemention of containerWrapper.
33  *
34  *
35  * @author banq
36  */

37 public class PicoContainerWrapper implements ContainerWrapper {
38     public final static String JavaDoc module = PicoContainerWrapper.class.getName();
39
40     private MutablePicoContainer container;
41   
42
43     /**
44      * construct a picocontainer without cache.
45      *
46      */

47     public PicoContainerWrapper() {
48         this.container = new JdonPicoContainer(new ConstructorInjectionComponentAdapterFactory());
49         registerContainerCallback();
50     }
51     
52     public PicoContainerWrapper(MutablePicoContainer container) {
53         this.container = container;
54         registerContainerCallback();
55     }
56     
57     public synchronized void registerContainerCallback(){
58         register(ContainerCallback.NAME, new ContainerCallback(this));
59     }
60     
61   
62     
63
64     /**
65      * registe a child container in parent container
66      */

67     public synchronized void registerChild(String JavaDoc name) {
68         try {
69             MutablePicoContainer child = new DefaultPicoContainer(container);
70             register(name, child);
71             //todo: bidirector register, so components can find each other
72
//child.registerComponentInstance(name, container);
73
} catch (Exception JavaDoc ex) {
74             Debug.logWarning(" registe error: " + name, module);
75         }
76     }
77
78     /**
79      * get the child container from the parent container
80      */

81     public ContainerWrapper getChild(String JavaDoc name) {
82         MutablePicoContainer child = (MutablePicoContainer) lookup(name);
83         return new PicoContainerWrapper(child);
84     }
85     
86
87     public synchronized void register(String JavaDoc name, Class JavaDoc className) {
88         try {
89             Debug.logVerbose("[JdonFramework]registe: name=" + name + " class=" + className.getName(), module);
90             container.registerComponentImplementation(name, className);
91
92         } catch (Exception JavaDoc ex) {
93             Debug.logWarning(" registe error: " + name, module);
94         }
95     }
96
97     public synchronized void register(String JavaDoc name) {
98         try {
99             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
100             Class JavaDoc oClass = classLoader.loadClass(name);
101             
102             Debug.logVerbose("[JdonFramework]registe: name=" + name + " class=" + oClass.getName(), module);
103             register(name, oClass);
104
105         } catch (Exception JavaDoc ex) {
106             Debug.logWarning(" registe error: " + name, module);
107         }
108     }
109
110     public synchronized void register(String JavaDoc name, Class JavaDoc className, String JavaDoc[] constructors){
111         if (constructors == null){
112             register(name, className);
113             return;
114         }
115         try {
116             Debug.logVerbose("[JdonFramework]registe: name=" + name + " class=" + className.getName(), module);
117             Debug.logVerbose("[JdonFramework] constructor params size =" + constructors.length, module);
118             
119             Parameter[] params = new Parameter[constructors.length];
120             for(int i=0;i<constructors.length;i++){
121                ConstantParameter param = new ConstantParameter(new String JavaDoc(constructors[i]));
122                Debug.logVerbose("[JdonFramework] register its constructor value is " + constructors[i], module);
123                params[i] = param;
124             }
125             container.registerComponentImplementation(name, className, params);
126         } catch (Exception JavaDoc ex) {
127             Debug.logWarning(" registe error: " + name, module);
128         }
129
130     }
131
132     public synchronized void register(String JavaDoc name, Object JavaDoc instance) {
133         try {
134             Debug.logVerbose("[JdonFramework]registe: name=" + name + " class=" + instance.getClass().getName(), module);
135             container.registerComponentInstance(name, instance);
136         } catch (Exception JavaDoc ex) {
137             Debug.logWarning(" registe error: " + name, module);
138         }
139
140     }
141
142     public synchronized void start() {
143         try {
144             container.start();
145         } catch (RuntimeException JavaDoc e) {
146             Debug.logError("[JdonFramework] container start error: " + e, module);
147         }
148     }
149
150     public void stop() {
151         try {
152             container.stop();
153             container.dispose();
154         } catch (Exception JavaDoc e) {
155         }
156     }
157
158     public List JavaDoc getAllInstances() {
159         return container.getComponentInstances();
160     }
161
162     public Object JavaDoc lookup(String JavaDoc name) {
163         //Debug.logImportant("[JdonFramework]lookup: name=" + name, module);
164
Object JavaDoc object = container.getComponentInstance(name);
165         if (object == null)
166             Debug.logVerbose("[JdonFramework]Not find the component in container :" + name, module);
167         return object;
168     }
169
170     /**
171      * This method will usually create a new instance each time it is called
172      * @param name component name
173      * @return object new instance
174      */

175     public Object JavaDoc getComponentNewInstance(String JavaDoc name) {
176         Debug.logVerbose("[JdonFramework]getComponentNewInstance: name=" + name, module);
177         ComponentAdapter componentAdapter = container.getComponentAdapter(name);
178         if (componentAdapter == null)
179             Debug.logVerbose("[JdonFramework]Not find the component in container :" + name, module);
180         return componentAdapter.getComponentInstance(container);
181     }
182
183     
184     public Class JavaDoc getComponentClass(String JavaDoc name) {
185         Debug.logVerbose("[JdonFramework]getComponentClass: name=" + name, module);
186         ComponentAdapter componentAdapter = container.getComponentAdapter(name);
187         if (componentAdapter == null)
188             Debug.logVerbose("[JdonFramework]Not find the component in container :" + name, module);
189         return componentAdapter.getComponentImplementation();
190     }
191
192     
193 }
194
Popular Tags