1 21 22 package org.jacorb.notification.container; 23 24 import java.lang.reflect.Constructor ; 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 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 _container.registerComponentInstance(org.omg.CORBA.ORB .class, orb); 88 89 _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 110 112 ComponentAdapter _evaluatorAdapter = newComponentAdapter(_container, ETCLEvaluator.class, 113 DefaultETCLEvaluator.class); 114 115 _container.registerComponent(_evaluatorAdapter); 116 117 119 ComponentAdapter _messageFactoryAdapter = newComponentAdapter(_container, 120 MessageFactory.class, DefaultMessageFactory.class); 121 122 _container.registerComponent(_messageFactoryAdapter); 123 124 126 ComponentAdapter _taskProcessorAdapter = newComponentAdapter(_container, 127 TaskProcessor.class, DefaultTaskProcessor.class); 128 129 _container.registerComponent(_taskProcessorAdapter); 130 131 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 152 private static ComponentAdapter newComponentAdapter(PicoContainer container, Class service, 153 Class 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 167 public static void dumpDependencies(PicoContainer container, Class clazzToBeCreated) 168 { 169 try 170 { 171 Constructor [] ctors = clazzToBeCreated.getConstructors(); 172 173 StringBuffer b = new StringBuffer (); 174 for (int i = 0; i < ctors.length; i++) 175 { 176 Constructor constructor = ctors[i]; 177 178 b.append(constructor); 179 b.append("\n"); 180 Class [] params = constructor.getParameterTypes(); 181 182 for (int j = 0; j < params.length; j++) 183 { 184 Class 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 e) 198 { 199 e.printStackTrace(); 200 } 201 } 202 } | Popular Tags |