KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > deployment > web > WebModule


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.deployment.web;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URLClassLoader JavaDoc;
12
13 import org.jfox.deployment.J2EEClassLoader;
14 import org.jfox.deployment.J2EEDeployMonitor;
15 import org.jfox.deployment.J2EEModule;
16 import org.jfox.ioc.deploy.DeployException;
17 import org.jfox.ioc.deployment.Deployer;
18 import org.jfox.ioc.util.JarUtils;
19
20 /**
21  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
22  */

23
24 public class WebModule extends J2EEModule {
25     private File JavaDoc warFile;
26     private String JavaDoc baseDeployDirectory = J2EEDeployMonitor.getDeploymentDir();
27
28     private J2EEClassLoader loader;
29     private String JavaDoc webContext;
30
31     public WebModule(File JavaDoc warFile) throws DeployException {
32         this.warFile = warFile;
33         // trim the ".war"
34
this.webContext = warFile.getName().substring(0, warFile.getName().length() - 4);
35         init();
36     }
37
38     protected WebModule(File JavaDoc warFile, String JavaDoc webContext) {
39         this.warFile = warFile;
40         this.webContext = webContext;
41     }
42
43     protected void init() throws DeployException {
44         try {
45             JarUtils.extract(warFile, getTempDeployDirectory());
46         }
47         catch(IOException JavaDoc e) {
48             throw new DeployException("extract web module " + warFile + " error", e);
49         }
50     }
51
52     public URL JavaDoc getDescriptorURL() {
53         try {
54             return new File JavaDoc(getTempDeployDirectory(), "WEB-INF/web.xml").toURL();
55         }
56         catch(Exception JavaDoc e) {
57             return null;
58         }
59     }
60
61     public File JavaDoc getPackage() {
62         return warFile;
63     }
64
65     /**
66      * just return the Thread ContextClassLoader
67      *
68      * @return
69      */

70     public URLClassLoader JavaDoc getClassLoader() {
71         if(loader == null) {
72             loader = new J2EEClassLoader(new URL JavaDoc[]{}, this.getClass().getClassLoader());
73         }
74         return loader;
75     }
76
77     public File JavaDoc getTempDeployDirectory() {
78         File JavaDoc dir = new File JavaDoc(baseDeployDirectory, warFile.getName() + File.separator + "webapp" + File.separator + webContext);
79         if(!dir.exists()) {
80             dir.mkdirs();
81         }
82         return dir;
83     }
84
85     public void deploy(Deployer deployer) throws DeployException {
86         deployer.deploy(this);
87     }
88
89     public String JavaDoc getWebContext() {
90         return webContext;
91     }
92
93     public static void main(String JavaDoc[] args) {
94
95     }
96 }
97
98
Popular Tags