KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > deploy > ComponentDeployer


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.deploy;
8
9 import java.io.File JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
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 /**
32  * deploy Component to Registry
33  *
34  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
35  */

36
37 public class ComponentDeployer implements Deployer, Component {
38     private Registry registry;
39
40     // module => ComponentNames
41
private Map JavaDoc modules = new HashMap JavaDoc();
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         // load public classes
55
String JavaDoc[] publicClasses = md.getPublicClasses();
56         for(int i=0; i<publicClasses.length; i++ ){
57             String JavaDoc pubClass = publicClasses[i];
58             try {
59                 componentModule.getClassLoader().loadClass(pubClass);
60             }
61             catch(Exception JavaDoc e){
62                 throw new DeployException("failed to load module public class " + pubClass,e);
63             }
64         }
65
66         ComponentDescriptor[] cds = md.getComponentDescriptors();
67         List JavaDoc metas = new ArrayList JavaDoc(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 JavaDoc names = new ArrayList JavaDoc(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 JavaDoc names = (List JavaDoc) 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 /*
104     private ComponentName deploy(LocalClassLoader cl, ComponentDescriptor cd, File moduleDeployDir) throws DeployException {
105         String description = cd.getDescription();
106         String compImpl = cd.getImplementation();
107         String compName = cd.getName();
108         if(compName == null || compName.trim().equals("")){
109             throw new DeployException("component name can not be null. it's implementation is " + compImpl);
110         }
111         boolean singleton = cd.isSingleton();
112         boolean typeSingleton = cd.isTypeSingleton();
113         int priority = cd.getPriority();
114         ConstructorDescriptor constructorDesc = cd.getConstructorDescriptor();
115         PropertyDescriptor[] propertyDesces = cd.getPropertyDescriptors();
116
117         try {
118             ComponentMeta meta = ComponentMetaFactory.getComponentMeta(ComponentName.parseString(compName, cl),
119                     cl.loadClass(compImpl),
120                     buildConstrParameter(cl, constructorDesc),
121                     buildPropertyParameterPacks(cl,propertyDesces),
122                     singleton,
123                     typeSingleton
124                     );
125             meta.setPriority(priority);
126             meta.setDescription(description);
127             meta.setModuleDescription(cd.getModuleDescription());
128             meta.setModule(moduleDeployDir.getPath());
129             registry.registerComponent(meta);
130             return meta.getComponentName();
131         }
132         catch(Exception e) {
133             throw new DeployException(e);
134         }
135
136     }
137 */

138
139     private Dependency[] buildConstrParameter(ClassLoader JavaDoc cl, ConstructorDescriptor cd) throws ComponentException {
140         if(cd == null) {
141             // null will case the InstantiatingComponentFactory auto detect constructor
142
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 JavaDoc 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 JavaDoc cl, PropertyDescriptor pd) throws ComponentException {
161         String JavaDoc 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 JavaDoc cl, ParameterDescriptor pd) throws ComponentException {
168         String JavaDoc 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 JavaDoc e) {
178                     throw new ComponentException("build parameter error.", e);
179                 }
180             }
181         }
182         String JavaDoc type = pd.getType();
183         String JavaDoc value = pd.getValue();
184         try {
185             Class JavaDoc typeClass = null;
186             if(Classes.isPrimitiveClass(type)) {
187                 typeClass = Classes.loadPrimitiveClass(type);
188             }
189             else {
190                 typeClass = cl.loadClass(type);
191             }
192             Object JavaDoc param = Classes.newObject(typeClass, value);
193             return new ObjectDependency(param, typeClass);
194         }
195         catch(Exception JavaDoc 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 JavaDoc moduleDeployDir) throws DeployException {
206         String JavaDoc description = cd.getDescription();
207         String JavaDoc compImpl = cd.getImplementation();
208         String JavaDoc 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 JavaDoc e) {
232             throw new DeployException(e);
233         }
234
235     }
236
237     public static void main(String JavaDoc[] args) {
238
239     }
240 }
241
242
Popular Tags