1 6 7 package org.jfox.ioc.factory; 8 9 import java.lang.reflect.Constructor ; 10 import java.lang.reflect.Modifier ; 11 12 import org.jfox.ioc.Component; 13 import org.jfox.ioc.ComponentName; 14 import org.jfox.ioc.Registry; 15 import org.jfox.ioc.depend.Dependency; 16 import org.jfox.ioc.exception.ComponentException; 17 import org.jfox.ioc.ext.InstantiatedComponent; 18 import org.jfox.ioc.util.Classes; 19 20 29 30 public class ConstrComponentFactory extends ComponentFactory { 31 34 private Dependency[] constrParams; 35 38 private Class implementation; 39 42 private ComponentName compName; 43 46 private Registry registry; 47 48 51 private boolean instantiating = false; 52 55 private Constructor constructor; 56 57 65 public ConstrComponentFactory(ComponentName name, Class implementation, Dependency[] constrParams) throws ComponentException { 66 this.constrParams = constrParams; 67 this.implementation = implementation; 68 checkImplementation(implementation); 69 setComponentName(name); 70 constructor = getContructor(); 71 } 72 73 protected ConstrComponentFactory(Component comp) { 74 this.constrParams = Dependency.EMPTY_PARAMETERS; 75 this.implementation = comp.getClass(); 76 setComponentName(ComponentName.newInstance(comp.getClass())); 77 constructor = null; 78 } 79 80 105 public Component makeComponent() throws ComponentException { 106 if(instantiating == true) { 107 throw new ComponentException("cycle depencies in component " + getImplementation().getName()); 108 } 109 instantiating = true; 110 111 try { 112 Dependency[] params = getParameters(); 113 Object [] paramValues = new Object []{}; 114 if(params != null) { 115 paramValues = resolveParameters(params); 116 } 117 Component comp = instantiateComponent(constructor, paramValues); 118 119 if(comp instanceof InstantiatedComponent) { 120 comp = ((InstantiatedComponent) comp).afterInstantiate(comp); 121 } 122 123 return comp; 124 } 125 finally { 126 instantiating = false; 127 } 128 129 } 130 131 134 public ComponentName getComponentName() { 135 return compName; 136 } 137 138 141 protected void setComponentName(ComponentName compName) { 142 this.compName = compName; 143 } 144 145 148 public Class getImplementation() { 149 return implementation; 150 } 151 152 156 public Registry getRegistry() { 157 return registry; 158 } 159 160 164 public void setRegistry(Registry registry) { 165 this.registry = registry; 166 } 167 168 172 public Dependency[] getParameters() { 173 return constrParams; 174 } 175 176 private Constructor getContructor() throws ComponentException { 177 Class [] types = getParameterTypes(constrParams); 180 181 Constructor constructor = null; 182 try { 183 constructor = Classes.getConstructor(implementation, types); 184 } 185 catch(NoSuchMethodException e) { 186 throw new ComponentException(e); 187 } 188 return constructor; 189 } 190 191 198 protected Component instantiateComponent(Constructor constructor, Object [] params) throws ComponentException { 199 try { 200 Object comp = constructor.newInstance(params); 201 return (Component) comp; 202 } 203 catch(Exception e) { 204 throw new ComponentException("instantiate component error " + getImplementation().getName(), e); 205 } 206 } 207 208 214 private static void checkImplementation(Class implementation) throws ComponentException { 215 if(!Component.class.isAssignableFrom(implementation)) { 216 throw new ComponentException("component implementation " + implementation.getName() + " is not a valid component, because it not implements " + Component.class.getName()); 217 } 218 219 boolean isAbstract = (implementation.getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT; 221 boolean isPrivate = (implementation.getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE; 222 boolean isProtected = (implementation.getModifiers() & Modifier.PROTECTED) == Modifier.PROTECTED; 223 if(implementation.isInterface() || isAbstract) { 224 throw new ComponentException("component implementation is not a concrete class " + implementation.getName()); 225 } 226 if(isPrivate || isProtected) { 227 throw new ComponentException("component implementation is a private or protected class " + implementation.getName()); 228 } 229 } 230 231 } 232 233 | Popular Tags |