1 6 7 package org.jfox.ioc.deploy; 8 9 import java.io.File ; 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 import org.jfox.ioc.Component; 16 import org.jfox.ioc.ComponentContext; 17 import org.jfox.ioc.ComponentMeta; 18 import org.jfox.ioc.ComponentMetaFactory; 19 import org.jfox.ioc.ComponentName; 20 import org.jfox.ioc.Registry; 21 import org.jfox.ioc.classloader.LocalClassLoader; 22 import org.jfox.ioc.depend.ComponentRefDependency; 23 import org.jfox.ioc.depend.Dependency; 24 import org.jfox.ioc.depend.DependencyPack; 25 import org.jfox.ioc.depend.ObjectDependency; 26 import org.jfox.ioc.deployment.Deployable; 27 import org.jfox.ioc.deployment.Deployer; 28 import org.jfox.ioc.exception.ComponentException; 29 import org.jfox.ioc.util.Classes; 30 31 36 37 public class ComponentDeployer implements Deployer, Component { 38 private Registry registry; 39 40 private Map modules = new HashMap (); 42 43 public Registry getRegistry() { 44 return registry; 45 } 46 47 public void deploy(Deployable comp) throws DeployException { 48 if(!(comp instanceof ComponentModule)) { 49 throw new DeployException("can not deploy " + comp + ", it's not a instance of" + ComponentModule.class.getName()); 50 } 51 ComponentModule componentModule = (ComponentModule) comp; 52 ComponentModuleDescriptor md = componentModule.getModuleDescriptor(); 53 54 String [] publicClasses = md.getPublicClasses(); 56 for(int i=0; i<publicClasses.length; i++ ){ 57 String pubClass = publicClasses[i]; 58 try { 59 componentModule.getClassLoader().loadClass(pubClass); 60 } 61 catch(Exception e){ 62 throw new DeployException("failed to load module public class " + pubClass,e); 63 } 64 } 65 66 ComponentDescriptor[] cds = md.getComponentDescriptors(); 67 List metas = new ArrayList (cds.length); 68 for(int i = 0; i < cds.length; i++) { 69 ComponentMeta meta = createComponentMeta(((LocalClassLoader) componentModule.getClassLoader()), cds[i], componentModule.getTempDeployDirectory()); 70 metas.add(meta); 71 } 72 73 ComponentMeta[] successMetas = registry.registerComponentBatch((ComponentMeta[])metas.toArray(new ComponentMeta[metas.size()])); 74 75 List names = new ArrayList (cds.length); 76 for(int i = 0; i < successMetas.length; i++) { 77 ComponentName name = ((ComponentMeta)successMetas[i]).getComponentName(); 78 names.add(name); 79 } 80 modules.put(componentModule, names); 81 } 82 83 public void undeploy(Deployable comp) throws DeployException { 84 if(!(comp instanceof ComponentModule)) { 85 throw new DeployException("can not undeploy " + comp + ", it's not a instance of" + ComponentModule.class.getName()); 86 } 87 ComponentModule componentModule = (ComponentModule) comp; 88 if(!modules.containsKey(componentModule)) return; 89 List names = (List ) modules.get(componentModule); 90 91 for(int i = names.size() - 1; i >= 0; i--) { 92 ComponentName name = (ComponentName) names.get(i); 93 try { 94 registry.unregisterComponent(name); 95 } 96 catch(ComponentException e) { 97 throw new DeployException("undeploy " + name + " faild.", e); 98 } 99 } 100 modules.remove(componentModule); 101 } 102 103 138 139 private Dependency[] buildConstrParameter(ClassLoader cl, ConstructorDescriptor cd) throws ComponentException { 140 if(cd == null) { 141 return null; 143 } 144 ParameterDescriptor[] pds = cd.getParamDescriptors(); 145 Dependency[] params = new Dependency[pds.length]; 146 for(int i = 0; i < pds.length; i++) { 147 params[i] = buildParameter(cl, pds[i]); 148 } 149 return params; 150 } 151 152 private DependencyPack[] buildPropertyParameterPacks(ClassLoader cl, PropertyDescriptor[] pds) throws ComponentException { 153 DependencyPack[] ppps = new DependencyPack[pds.length]; 154 for(int i = 0; i < pds.length; i++) { 155 ppps[i] = buildPropertyParameterPack(cl, pds[i]); 156 } 157 return ppps; 158 } 159 160 private DependencyPack buildPropertyParameterPack(ClassLoader cl, PropertyDescriptor pd) throws ComponentException { 161 String name = pd.getName(); 162 ParameterDescriptor[] pds = pd.getParamDescriptors(); 163 Dependency param = buildParameter(cl, pds[0]); 164 return new DependencyPack(name, param); 165 } 166 167 private Dependency buildParameter(ClassLoader cl, ParameterDescriptor pd) throws ComponentException { 168 String ref = pd.getRef(); 169 if(ref != null && !ref.equals("")) { 170 if(ref.indexOf("@") > 0) { 171 return new ComponentRefDependency(ComponentName.parseString(ref, cl)); 172 } 173 else { 174 try { 175 return new ComponentRefDependency(ComponentName.newInstance(cl.loadClass(ref))); 176 } 177 catch(Exception e) { 178 throw new ComponentException("build parameter error.", e); 179 } 180 } 181 } 182 String type = pd.getType(); 183 String value = pd.getValue(); 184 try { 185 Class typeClass = null; 186 if(Classes.isPrimitiveClass(type)) { 187 typeClass = Classes.loadPrimitiveClass(type); 188 } 189 else { 190 typeClass = cl.loadClass(type); 191 } 192 Object param = Classes.newObject(typeClass, value); 193 return new ObjectDependency(param, typeClass); 194 } 195 catch(Exception e) { 196 throw new ComponentException("build parameter error.", e); 197 } 198 } 199 200 public void setComponentContext(ComponentContext ctx) { 201 this.registry = ctx.getRegistry(); 202 } 203 204 205 private ComponentMeta createComponentMeta(LocalClassLoader cl, ComponentDescriptor cd, File moduleDeployDir) throws DeployException { 206 String description = cd.getDescription(); 207 String compImpl = cd.getImplementation(); 208 String compName = cd.getName(); 209 if(compName == null || compName.trim().equals("")){ 210 throw new DeployException("component name can not be null. it's implementation is " + compImpl); 211 } 212 boolean singleton = cd.isSingleton(); 213 boolean typeSingleton = cd.isTypeSingleton(); 214 int priority = cd.getPriority(); 215 ConstructorDescriptor constructorDesc = cd.getConstructorDescriptor(); 216 PropertyDescriptor[] propertyDesces = cd.getPropertyDescriptors(); 217 218 try { 219 ComponentMeta meta = ComponentMetaFactory.getComponentMeta( 220 ComponentName.parseString(compName, cl), cl 221 .loadClass(compImpl), buildConstrParameter(cl, 222 constructorDesc), buildPropertyParameterPacks(cl, 223 propertyDesces), singleton, typeSingleton); 224 meta.setPriority(priority); 225 meta.setDescription(description); 226 meta.setModule(moduleDeployDir.getName()); 227 meta.setModuleDescription(cd.getModuleDescription()); 228 meta.setModuleDir(moduleDeployDir.getPath()); 229 return meta; 230 } 231 catch (Exception e) { 232 throw new DeployException(e); 233 } 234 235 } 236 237 public static void main(String [] args) { 238 239 } 240 } 241 242 | Popular Tags |