1 15 16 package com.jdon.container.pico; 17 18 import java.util.List ; 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 37 public class PicoContainerWrapper implements ContainerWrapper { 38 public final static String module = PicoContainerWrapper.class.getName(); 39 40 private MutablePicoContainer container; 41 42 43 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 67 public synchronized void registerChild(String name) { 68 try { 69 MutablePicoContainer child = new DefaultPicoContainer(container); 70 register(name, child); 71 } catch (Exception ex) { 74 Debug.logWarning(" registe error: " + name, module); 75 } 76 } 77 78 81 public ContainerWrapper getChild(String name) { 82 MutablePicoContainer child = (MutablePicoContainer) lookup(name); 83 return new PicoContainerWrapper(child); 84 } 85 86 87 public synchronized void register(String name, Class className) { 88 try { 89 Debug.logVerbose("[JdonFramework]registe: name=" + name + " class=" + className.getName(), module); 90 container.registerComponentImplementation(name, className); 91 92 } catch (Exception ex) { 93 Debug.logWarning(" registe error: " + name, module); 94 } 95 } 96 97 public synchronized void register(String name) { 98 try { 99 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 100 Class oClass = classLoader.loadClass(name); 101 102 Debug.logVerbose("[JdonFramework]registe: name=" + name + " class=" + oClass.getName(), module); 103 register(name, oClass); 104 105 } catch (Exception ex) { 106 Debug.logWarning(" registe error: " + name, module); 107 } 108 } 109 110 public synchronized void register(String name, Class className, String [] 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 (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 ex) { 127 Debug.logWarning(" registe error: " + name, module); 128 } 129 130 } 131 132 public synchronized void register(String name, Object instance) { 133 try { 134 Debug.logVerbose("[JdonFramework]registe: name=" + name + " class=" + instance.getClass().getName(), module); 135 container.registerComponentInstance(name, instance); 136 } catch (Exception 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 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 e) { 155 } 156 } 157 158 public List getAllInstances() { 159 return container.getComponentInstances(); 160 } 161 162 public Object lookup(String name) { 163 Object 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 175 public Object getComponentNewInstance(String 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 getComponentClass(String 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 |