KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > container > PicoContainerFactory


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21
22 package org.jacorb.notification.container;
23
24 import java.lang.reflect.Constructor JavaDoc;
25
26 import org.apache.avalon.framework.logger.Logger;
27 import org.jacorb.notification.MessageFactory;
28 import org.jacorb.notification.engine.ConfigurablePushTaskExecutorFactory;
29 import org.jacorb.notification.engine.DefaultTaskProcessor;
30 import org.jacorb.notification.engine.PushTaskExecutorFactory;
31 import org.jacorb.notification.engine.TaskProcessor;
32 import org.jacorb.notification.filter.ETCLEvaluator;
33 import org.jacorb.notification.filter.impl.DefaultETCLEvaluator;
34 import org.jacorb.notification.impl.DefaultMessageFactory;
35 import org.jacorb.notification.impl.PoolingEvaluationContextFactory;
36 import org.jacorb.notification.interfaces.EvaluationContextFactory;
37 import org.jacorb.orb.ORB;
38 import org.picocontainer.ComponentAdapter;
39 import org.picocontainer.MutablePicoContainer;
40 import org.picocontainer.PicoContainer;
41 import org.picocontainer.defaults.CachingComponentAdapter;
42 import org.picocontainer.defaults.ComponentAdapterFactory;
43 import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
44 import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
45 import org.picocontainer.defaults.DefaultPicoContainer;
46
47 /**
48  * @author Alphonse Bendt
49  * @version $Id: PicoContainerFactory.java,v 1.4 2005/05/01 21:09:47 alphonse.bendt Exp $
50  */

51 public class PicoContainerFactory
52 {
53     public static MutablePicoContainer createRootContainer(ORB orb)
54     {
55         return createRootContainer(null, orb);
56     }
57
58     public static MutablePicoContainer createRootContainer(PicoContainer parent, ORB orb)
59     {
60         final org.jacorb.config.Configuration config = orb.getConfiguration();
61         final Logger _logger = config.getNamedLogger(PicoContainerFactory.class.getName());
62
63         final MutablePicoContainer _container;
64
65         if (parent == null)
66         {
67             ComponentAdapterFactory _componentAdapterFactory = new ConstructorInjectionComponentAdapterFactory();
68
69             _container = new DefaultPicoContainer(_componentAdapterFactory);
70
71             _container.registerComponentInstance(ComponentAdapterFactory.class,
72                     _componentAdapterFactory);
73
74             _logger.debug("Created Top Level Container");
75         }
76         else
77         {
78             ComponentAdapterFactory _componentAdapterFactory = (ComponentAdapterFactory) parent
79                     .getComponentInstance(ComponentAdapterFactory.class);
80
81             _container = new DefaultPicoContainer(_componentAdapterFactory, parent);
82
83             _logger.debug("Created Container with Parent");
84         }
85
86         // register existing orb
87
_container.registerComponentInstance(org.omg.CORBA.ORB JavaDoc.class, orb);
88
89         // register services that need to be looked up via orb using custom componentadapter
90
_container.registerComponent(new CachingComponentAdapter(new BiDirGiopPOAComponentAdapter(new POAComponentAdapter())));
91
92         _container.registerComponent(new CachingComponentAdapter(
93                 new DynAnyFactoryComponentAdapter()));
94
95         _container.registerComponent(new CachingComponentAdapter(
96                 new ConfigurationComponentAdapter()));
97
98         _container.registerComponent(new CachingComponentAdapter(
99                 new FilterFactoryComponentAdapter()));
100
101         _container.registerComponent(new CachingComponentAdapter(new RepositoryComponentAdapter()));
102
103         _container.registerComponent(new CachingComponentAdapter(
104                 new PushTaskExecutorFactoryComponentAdapter(
105                         new ConstructorInjectionComponentAdapter(PushTaskExecutorFactory.class,
106                                 ConfigurablePushTaskExecutorFactory.class))));
107
108         // register core services
109

110         // etcl evaluator
111

112         ComponentAdapter _evaluatorAdapter = newComponentAdapter(_container, ETCLEvaluator.class,
113                 DefaultETCLEvaluator.class);
114
115         _container.registerComponent(_evaluatorAdapter);
116
117         // message factory
118

119         ComponentAdapter _messageFactoryAdapter = newComponentAdapter(_container,
120                 MessageFactory.class, DefaultMessageFactory.class);
121
122         _container.registerComponent(_messageFactoryAdapter);
123
124         // taskprocessor
125

126         ComponentAdapter _taskProcessorAdapter = newComponentAdapter(_container,
127                 TaskProcessor.class, DefaultTaskProcessor.class);
128
129         _container.registerComponent(_taskProcessorAdapter);
130
131         // evaluation context factory
132

133         ComponentAdapter _evaluationContextAdapter = newComponentAdapter(_container,
134                 EvaluationContextFactory.class, PoolingEvaluationContextFactory.class);
135
136         _container.registerComponent(_evaluationContextAdapter);
137
138         return _container;
139     }
140
141     public static MutablePicoContainer createChildContainer(MutablePicoContainer parent)
142     {
143         MutablePicoContainer child = parent.makeChildContainer();
144
145         return child;
146     }
147
148     /**
149      * create a ComponentAdapter using the ComponentAdapterFactory registered in the provided
150      * container. the resulting ComponentAdapter is wrapped in a CachingComponentAdapter.
151      */

152     private static ComponentAdapter newComponentAdapter(PicoContainer container, Class JavaDoc service,
153             Class JavaDoc implementation)
154     {
155         ComponentAdapterFactory _adapterFactory = (ComponentAdapterFactory) container
156                 .getComponentInstance(ComponentAdapterFactory.class);
157
158         return new CachingComponentAdapter(_adapterFactory.createComponentAdapter(service,
159                 implementation, null));
160     }
161
162     /**
163      * helper method for easier debugging of unresolved dependencies.
164      *
165      * DO NOT DELETE even if method is not referenced.
166      */

167     public static void dumpDependencies(PicoContainer container, Class JavaDoc clazzToBeCreated)
168     {
169         try
170         {
171             Constructor JavaDoc[] ctors = clazzToBeCreated.getConstructors();
172
173             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
174             for (int i = 0; i < ctors.length; i++)
175             {
176                 Constructor JavaDoc constructor = ctors[i];
177
178                 b.append(constructor);
179                 b.append("\n");
180                 Class JavaDoc[] params = constructor.getParameterTypes();
181
182                 for (int j = 0; j < params.length; j++)
183                 {
184                     Class JavaDoc param = params[j];
185
186                     boolean resolvable = container.getComponentInstanceOfType(param) != null;
187                     b.append(j);
188                     b.append(": ");
189                     b.append(param);
190                     b.append(" -> ");
191                     b.append(resolvable);
192                     b.append("\n");
193                 }
194             }
195
196             System.err.println(b.toString());
197         } catch (Exception JavaDoc e)
198         {
199             e.printStackTrace();
200         }
201     }
202 }
Popular Tags