KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jfox.ioc.deploy;
7
8 import org.jfox.ioc.Constants;
9 import org.jfox.ioc.classloader.ClassLoaderRepository;
10 import org.jfox.ioc.classloader.LocalClassLoader;
11 import org.jfox.ioc.deployment.Deployable;
12 import org.jfox.ioc.deployment.Deployer;
13 import org.jfox.ioc.logger.Logger;
14 import org.jfox.ioc.util.FileFilters;
15 import org.jfox.ioc.util.Files;
16 import org.jfox.ioc.util.XMLUtils;
17
18 import java.io.File JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.net.URLClassLoader JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * a module directory
26  *
27  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
28  */

29
30 public class ComponentModule implements Deployable, Comparable JavaDoc<ComponentModule> {
31     private File JavaDoc moduleDir;
32
33     private LocalClassLoader moduleClassLoader;
34
35     private ClassLoaderRepository loaderRepo;
36
37     private ComponentModuleDescriptor componentModuleDescriptor;
38
39     private Logger logger;
40
41     private static final String JavaDoc singleComponentDescriptorSuffix = ".component.xml";
42
43     public ComponentModule(File JavaDoc moduleDir, ClassLoaderRepository loaderRespo)
44             throws ComponentDescriptionException {
45         this.moduleDir = moduleDir;
46         this.loaderRepo = loaderRespo;
47         logger = Logger.getLogger(this.getClass().getName() + "["
48                                   + moduleDir.getName() + "]");
49         init();
50     }
51
52     public File JavaDoc getPackage() {
53         return moduleDir;
54     }
55
56     public void deploy(Deployer deployer) throws DeployException {
57         deployer.deploy(this);
58     }
59
60     public File JavaDoc getTempDeployDirectory() {
61         return moduleDir;
62     }
63
64     public void init() throws ComponentDescriptionException {
65         try {
66             componentModuleDescriptor = new ComponentModuleDescriptor(XMLUtils
67                     .getDocument(getDescriptorURL()));
68         }
69         catch(Exception JavaDoc e) {
70             throw new ComponentDescriptionException("build module descriptor failed.", e);
71         }
72
73         // Ҷ component
74
try {
75             List JavaDoc components = Files.listFiles(new File JavaDoc(moduleDir, "META-INF"), FileFilters.suffixFileFilter(singleComponentDescriptorSuffix));
76             for(int i = 0; i < components.size(); i++) {
77                 File JavaDoc componentXml = (File JavaDoc) components.get(i);
78                 ComponentDescriptor cd = new ComponentDescriptor(componentModuleDescriptor);
79                 cd.processXML(XMLUtils.getDocument(componentXml.toURL()).getDocumentElement());
80                 componentModuleDescriptor.addComponent(cd);
81             }
82
83         }
84         catch(Exception JavaDoc e) {
85             throw new ComponentDescriptionException("build module descriptor failed.", e);
86         }
87
88         // add component jars to Common loader
89
File JavaDoc[] componentJars = getComponentJars();
90         List JavaDoc urls = new ArrayList JavaDoc(componentJars.length + 1);
91         try {
92             // add the classes dir to pub class path
93
File JavaDoc classesDir = new File JavaDoc(moduleDir, Constants.JFOXCO_MODULE_ClASSPATH);
94             if(classesDir.exists() && classesDir.isDirectory()) {
95                 urls.add(classesDir.toURL());
96                 logger
97                         .debug("add component classes directory to module's public ClassLoader "
98                                + classesDir.toString());
99             }
100
101             for(int i = 0; i < componentJars.length; i++) {
102                 urls.add(componentJars[i].toURL());
103                 logger
104                         .debug("add component jar to module's public ClassLoader "
105                                + componentJars[i].toString());
106             }
107
108             // add resource dir
109
File JavaDoc resourceDir = new File JavaDoc(moduleDir, Constants.JFOXCO_MODULE_CONFIG_PATH);
110             if(resourceDir.exists() && resourceDir.isDirectory()) {
111                 urls.add(resourceDir.toURL());
112                 logger
113                         .debug("add module resource directory to module's public ClassLoader "
114                                + resourceDir.toString());
115             }
116
117         }
118         catch(Exception JavaDoc e) {
119             e.printStackTrace();
120         }
121         // constructor's urls is the module's pub jars
122
moduleClassLoader = new LocalClassLoader((URL JavaDoc[]) urls
123                 .toArray(new URL JavaDoc[urls.size()]), loaderRepo);
124
125         // add component lib jars to module private loader
126
File JavaDoc[] libJars = getComponentLibJars();
127         try {
128             for(int i = 0; i < libJars.length; i++) {
129                 moduleClassLoader.addURL(libJars[i].toURL());
130                 logger
131                         .debug("add component lib to module's private ClassLoader "
132                                + libJars[i].toString());
133             }
134         }
135         catch(Exception JavaDoc e) {
136             e.printStackTrace();
137         }
138     }
139
140     public URL JavaDoc getDescriptorURL() {
141         try {
142             return new File JavaDoc(moduleDir, "META-INF/" + Constants.JFOXCO_MODULE_DEPLOY_FILE).toURL();
143         }
144         catch(Exception JavaDoc e) {
145             e.printStackTrace();
146             return null;
147         }
148     }
149
150     public URLClassLoader JavaDoc getClassLoader() {
151         return moduleClassLoader;
152     }
153
154     public ComponentModuleDescriptor getModuleDescriptor() {
155         return componentModuleDescriptor;
156     }
157
158     public File JavaDoc[] getComponentJars() {
159         return moduleDir.listFiles(FileFilters.suffixFileFilter(new String JavaDoc[]{
160                 "jar", "zip"}));
161     }
162
163     private File JavaDoc[] getComponentLibJars() {
164         File JavaDoc lib = new File JavaDoc(moduleDir, Constants.JFOXCO_MODULE_PRIVATE_LIBS);
165         if(!lib.exists())
166             return new File JavaDoc[0];
167         List JavaDoc files = Files.listFiles(lib, FileFilters
168                 .suffixFileFilter(new String JavaDoc[]{"jar", "zip"}));
169         return (File JavaDoc[]) files.toArray(new File JavaDoc[files.size()]);
170     }
171
172     public int compareTo(ComponentModule o) {
173         ComponentModule _module = (ComponentModule) o;
174         return new Integer JavaDoc(componentModuleDescriptor.getPriority())
175                 .compareTo(new Integer JavaDoc(_module.getModuleDescriptor()
176                         .getPriority()));
177     }
178
179     public boolean equals(Object JavaDoc obj) {
180         return moduleDir.getName().equals(((ComponentModule) obj).moduleDir.getName());
181     }
182
183     public int hashCode() {
184         return moduleDir.getName().toString().hashCode();
185     }
186
187     public String JavaDoc toString() {
188         return "Module{" + "moduleDir=" + moduleDir + ", description="
189                + componentModuleDescriptor.getDescription() + "}";
190     }
191
192     public static void main(String JavaDoc[] args) {
193
194     }
195 }
196
197
Popular Tags