KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileFilter JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Arrays JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.jfox.ioc.ComponentContext;
20 import org.jfox.ioc.ext.ServicableComponent;
21 import org.jfox.ioc.logger.Logger;
22 import org.jfox.ioc.util.FileFilters;
23
24 /**
25  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
26  */

27
28 public class DeployMonitor implements ServicableComponent, Runnable JavaDoc {
29
30     private final static String JavaDoc DEFAULT_MODULE_DIR = "./modules";
31     /**
32      * 模块目录下含有该文件,该模块将被忽略,不被部署
33      */

34     private final static String JavaDoc IGNORE_FLAG_FILENAME = ".ignore";
35     /**
36      * 忽略的模块目录,这些目录不被当作模块目录部署
37      */

38     private final static String JavaDoc[] IGNORE_MODULE_DIR = new String JavaDoc[]{".","CVS"};
39
40     private boolean started = false;
41
42     // 默认使用 modules 目录作为发布目录
43
private String JavaDoc moduleDir = DEFAULT_MODULE_DIR;
44     private ComponentDeployer deployer = null;
45     // Modue dir => Module
46
private Map JavaDoc<File JavaDoc,ComponentModule> deployments = new HashMap JavaDoc<File JavaDoc,ComponentModule>();
47
48     // module dirs that faild to deployed
49
private List JavaDoc failedModules = new ArrayList JavaDoc();
50
51     private static Logger logger = Logger.getLogger(DeployMonitor.class.getName());
52
53     public DeployMonitor(ComponentDeployer deployer) {
54         this.deployer = deployer;
55     }
56     
57     public DeployMonitor(ComponentDeployer deployer, String JavaDoc componentsDir) {
58         this.deployer = deployer;
59         if(componentsDir != null && !componentsDir.equals("")) {
60             this.moduleDir = componentsDir;
61         }
62     }
63
64     /**
65      * start to monitor the Component directory
66      *
67      * @throws Exception
68      */

69     public void start() throws Exception JavaDoc {
70         started = true;
71         Thread JavaDoc t = new Thread JavaDoc(this, "DeployMonitor");
72         t.setPriority(Thread.MIN_PRIORITY);
73         t.start();
74     }
75
76     /**
77      * stop monitor
78      *
79      * @throws Exception
80      */

81     public void stop() throws Exception JavaDoc {
82         started = false;
83     }
84
85
86     public void run() {
87         File JavaDoc dir = new File JavaDoc(moduleDir);
88         if(!dir.exists()) {
89             logger.warn("the modules directory \"" + dir.toString() + "\" not exist, deploy monitor stop.");
90             return;
91         }
92
93         while(isStarted()) {
94 // List moduleDirs = new ArrayList(Arrays.asList(dir.listFiles(Files.directoryFileFilter(new String[]{"lib"}))));
95
List JavaDoc<File JavaDoc> moduleDirs = new ArrayList JavaDoc<File JavaDoc>(Arrays.asList(dir.listFiles(FileFilters.and(new FileFilter JavaDoc[]{
96                 FileFilters.directoryFileFilter(),
97                 FileFilters.not(FileFilters.prefixFileFilter(IGNORE_MODULE_DIR))}))));
98
99             Map JavaDoc<File JavaDoc,ComponentModule> modules = new HashMap JavaDoc<File JavaDoc,ComponentModule>(moduleDirs.size());
100             for(Iterator JavaDoc it = moduleDirs.iterator(); it.hasNext();) {
101                 File JavaDoc moduleDir = (File JavaDoc) it.next();
102                 if(deployments.containsKey(moduleDir)) { // deployed
103
it.remove();
104                     modules.put(moduleDir, deployments.remove(moduleDir));
105                 }
106             }
107
108             // the rest of the deployments must be undeployed
109
for(Iterator JavaDoc it = deployments.values().iterator(); it.hasNext();) {
110                 ComponentModule componentModule = (ComponentModule) it.next();
111                 logger.info("undeploying module " + componentModule + " ...");
112                 try {
113                     undeploy(componentModule);
114                     logger.info("undeploy module " + componentModule + ", OK!");
115                 }
116                 catch(DeployException e) {
117                     logger.info("undeploy module " + componentModule + " failed.", e);
118                 }
119             }
120             deployments.clear();
121
122             // load newModule Dirs
123
List JavaDoc<ComponentModule> newModules = new ArrayList JavaDoc<ComponentModule>(moduleDirs.size());
124             for(int i = 0; i < moduleDirs.size(); i++) {
125                 File JavaDoc moduleDir = moduleDirs.get(i);
126
127                 File JavaDoc ignoreFlag = new File JavaDoc(moduleDir,IGNORE_FLAG_FILENAME);
128                 if(ignoreFlag.exists()){ // 存在忽略标志文件,忽略该Module
129
logger.info("ignore module " + moduleDir.toString());
130                 }
131                 else if(failedModules.contains(moduleDir)) {
132                     logger.warn("module " + moduleDir.toString() + " has been deployed failed, skip!!!");
133                 }
134                 else {
135                     logger.info("parsing module " + moduleDir.toString() + " ...");
136
137                     try {
138                         ComponentModule componentModule = new ComponentModule(moduleDir, deployer.getRegistry().getClassLoaderRepository());
139                         logger.debug("parse module " + moduleDir.toString() + ", OK!");
140                         newModules.add(componentModule);
141                         failedModules.remove(componentModule.getPackage());
142                     }
143                     catch(ComponentDescriptionException e) {
144                         failedModules.add(moduleDir);
145                         logger.warn("parse module " + moduleDir.toString() + " failed!", e);
146                     }
147                 }
148             }
149             moduleDirs.clear();
150             // sort by priority
151
Collections.sort(newModules);
152             for(int i = 0; i < newModules.size(); i++) {
153                 ComponentModule componentModule = (ComponentModule) newModules.get(i);
154                 if(failedModules.contains(componentModule.getPackage())) {
155                     logger.warn("module " + componentModule.getPackage().toString() + " has been deployed failed, skip!!!");
156                 }
157                 else {
158                     try {
159                         logger.info("deploying module " + componentModule + " ...");
160                         deployer.deploy(componentModule);
161                         logger.debug("deploy module " + componentModule + ", OK!");
162                         modules.put(componentModule.getPackage(), componentModule);
163                         failedModules.remove(componentModule.getPackage());
164                     }
165                     catch(DeployException e) {
166                         failedModules.add(componentModule.getPackage());
167                         logger.warn("deploy module " + componentModule + " failed.", e);
168                     }
169                 }
170                 try {
171                     //部署完一个模块之后,休息250ms
172
Thread.sleep(250);
173                 }
174                 catch(Exception JavaDoc e){
175                     e.printStackTrace();
176                 }
177             }
178             newModules.clear();
179             // set the new deployments
180
deployments = modules;
181             try {
182                 Thread.sleep(5000L);
183             }
184             catch(InterruptedException JavaDoc e) {
185                 logger.warn(e);
186             }
187         }
188     }
189
190     private void undeploy(ComponentModule componentModule) throws DeployException {
191         deployments.remove(componentModule);
192         deployer.undeploy(componentModule);
193     }
194
195     public boolean isStarted() {
196         return started;
197     }
198
199     public File JavaDoc[] getDeployedModules() {
200         return (File JavaDoc[]) deployments.keySet().toArray(new File JavaDoc[deployments.size()]);
201     }
202
203     public void setComponentContext(ComponentContext ctx) {
204     }
205
206     public static void main(String JavaDoc[] args) {
207
208     }
209 }
Popular Tags