1 6 7 package org.jfox.ioc.deploy; 8 9 import java.io.File ; 10 import java.io.FileFilter ; 11 import java.util.ArrayList ; 12 import java.util.Arrays ; 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 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 27 28 public class DeployMonitor implements ServicableComponent, Runnable { 29 30 private final static String DEFAULT_MODULE_DIR = "./modules"; 31 34 private final static String IGNORE_FLAG_FILENAME = ".ignore"; 35 38 private final static String [] IGNORE_MODULE_DIR = new String []{".","CVS"}; 39 40 private boolean started = false; 41 42 private String moduleDir = DEFAULT_MODULE_DIR; 44 private ComponentDeployer deployer = null; 45 private Map <File ,ComponentModule> deployments = new HashMap <File ,ComponentModule>(); 47 48 private List failedModules = new ArrayList (); 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 componentsDir) { 58 this.deployer = deployer; 59 if(componentsDir != null && !componentsDir.equals("")) { 60 this.moduleDir = componentsDir; 61 } 62 } 63 64 69 public void start() throws Exception { 70 started = true; 71 Thread t = new Thread (this, "DeployMonitor"); 72 t.setPriority(Thread.MIN_PRIORITY); 73 t.start(); 74 } 75 76 81 public void stop() throws Exception { 82 started = false; 83 } 84 85 86 public void run() { 87 File dir = new File (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 <File > moduleDirs = new ArrayList <File >(Arrays.asList(dir.listFiles(FileFilters.and(new FileFilter []{ 96 FileFilters.directoryFileFilter(), 97 FileFilters.not(FileFilters.prefixFileFilter(IGNORE_MODULE_DIR))})))); 98 99 Map <File ,ComponentModule> modules = new HashMap <File ,ComponentModule>(moduleDirs.size()); 100 for(Iterator it = moduleDirs.iterator(); it.hasNext();) { 101 File moduleDir = (File ) it.next(); 102 if(deployments.containsKey(moduleDir)) { it.remove(); 104 modules.put(moduleDir, deployments.remove(moduleDir)); 105 } 106 } 107 108 for(Iterator 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 List <ComponentModule> newModules = new ArrayList <ComponentModule>(moduleDirs.size()); 124 for(int i = 0; i < moduleDirs.size(); i++) { 125 File moduleDir = moduleDirs.get(i); 126 127 File ignoreFlag = new File (moduleDir,IGNORE_FLAG_FILENAME); 128 if(ignoreFlag.exists()){ 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 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 Thread.sleep(250); 173 } 174 catch(Exception e){ 175 e.printStackTrace(); 176 } 177 } 178 newModules.clear(); 179 deployments = modules; 181 try { 182 Thread.sleep(5000L); 183 } 184 catch(InterruptedException 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 [] getDeployedModules() { 200 return (File []) deployments.keySet().toArray(new File [deployments.size()]); 201 } 202 203 public void setComponentContext(ComponentContext ctx) { 204 } 205 206 public static void main(String [] args) { 207 208 } 209 } | Popular Tags |