1 26 27 package com.rift.coad.lib.deployment; 29 30 import java.io.File ; 32 import java.util.Arrays ; 33 import java.util.Map ; 34 import java.util.HashMap ; 35 import java.util.HashSet ; 36 import java.util.concurrent.ConcurrentHashMap ; 37 import java.util.Iterator ; 38 import java.util.Set ; 39 import java.util.Vector ; 40 41 import org.apache.log4j.Logger; 43 44 import com.rift.coad.lib.common.FileUtil; 46 import com.rift.coad.lib.cache.CacheRegistry; 47 import com.rift.coad.lib.configuration.Configuration; 48 import com.rift.coad.lib.configuration.ConfigurationFactory; 49 import com.rift.coad.lib.deployment.bean.BeanManager; 50 import com.rift.coad.lib.deployment.jmxbean.JMXBeanManager; 51 import com.rift.coad.lib.deployment.webservice.WebServiceManager; 52 import com.rift.coad.lib.naming.NamingDirector; 53 import com.rift.coad.lib.thread.CoadunationThreadGroup; 54 import com.rift.coad.lib.thread.BasicThread; 55 import com.rift.coad.lib.thread.ThreadGroupManager; 56 import com.rift.coad.util.lock.ObjectLockFactory; 57 import com.rift.coad.util.transaction.TransactionManager; 58 59 65 public class DeploymentManager { 66 67 71 public class DeploymentThread extends BasicThread { 72 private DeploymentManager deploymentManager = null; 74 private boolean terminated = false; 75 private long delayPeriod = 0; 76 77 82 public DeploymentThread(DeploymentManager deploymentManager) throws 83 DeploymentException, Exception { 84 try { 85 this.deploymentManager = deploymentManager; 86 87 Configuration config = ConfigurationFactory.getInstance(). 89 getConfig(this.getClass()); 90 91 delayPeriod = config.getLong("delay"); 92 93 } catch (Exception ex) { 94 throw new DeploymentException( 95 "Failed to init the deployment thread",ex); 96 } 97 } 98 99 100 103 public void process() { 104 while(isTerminated() == false) { 106 deploymentManager.manage(); 107 delay(); 108 } 109 } 110 111 112 116 public synchronized void terminate() { 117 terminated = true; 118 DeploymentMonitor.getInstance().terminate(); 119 } 120 121 122 127 public String getInfo() { 128 return "Managing the Coadunation deployment files."; 129 } 130 131 132 135 private synchronized boolean isTerminated() { 136 return terminated; 137 } 138 139 140 144 private synchronized void delay() { 145 try { 146 wait(delayPeriod); 147 } catch (Exception ex) { 148 } 150 } 151 } 152 153 154 158 public class LoaderList { 159 private Map loaders = null; 161 162 163 166 public LoaderList() { 167 loaders = new ConcurrentHashMap (); 168 } 169 170 171 178 public synchronized boolean contains(File file) { 179 return loaders.containsKey(file); 180 } 181 182 183 189 public synchronized void add(File file, Object obj) { 190 loaders.put(file,obj); 191 } 192 193 194 201 public synchronized DeploymentLoader get(File file) { 202 return (DeploymentLoader)loaders.get(file); 203 } 204 205 206 212 public synchronized void remove(File file) { 213 loaders.remove(file); 214 } 215 216 217 222 public synchronized Set getKeys() { 223 return loaders.keySet(); 224 } 225 } 226 227 protected Logger log = 229 Logger.getLogger(DeploymentManager.class.getName()); 230 231 private LoaderList loaderList = null; 233 private CoadunationThreadGroup threadGroup = null; 234 private BeanManager beanManager = null; 235 private JMXBeanManager jmxBeanManager = null; 236 private WebServiceManager webServiceManager = null; 237 private DeploymentThread deploymentThread = null; 238 private File deploymentDir = null; 239 240 241 250 public DeploymentManager(CoadunationThreadGroup threadGroup,BeanManager beanManager, 251 JMXBeanManager jmxBeanManager, WebServiceManager webServiceManager) 252 throws DeploymentException { 253 try { 254 loaderList = new LoaderList(); 256 this.threadGroup = threadGroup.createThreadGroup(); 257 this.beanManager = beanManager; 258 this.jmxBeanManager = jmxBeanManager; 259 this.webServiceManager = webServiceManager; 260 261 maintainTmpDirectory(); 263 264 Configuration config = ConfigurationFactory.getInstance().getConfig( 266 this.getClass()); 267 268 String user = config.getString("username"); 270 271 deploymentDir = new File (config.getString("directory")); 272 if (deploymentDir.isDirectory() == false) { 273 throw new DeploymentException( 274 "The deployment directory [" + config.getString("directory") 275 + "] is invalid"); 276 } 277 278 deploymentThread = new DeploymentThread(this); 280 this.threadGroup.addThread(deploymentThread,user); 281 deploymentThread.start(); 282 283 } catch (Exception ex) { 284 throw new DeploymentException( 285 "Failed to start the deployment loader : " + ex.getMessage() 286 ,ex); 287 } 288 } 289 290 291 294 public void shutdown() { 295 threadGroup.terminate(); 296 297 try { 299 deploymentThread.join(); 300 Set keys = loaderList.getKeys(); 301 File [] loadedFiles = new File [keys.size()]; 302 loadedFiles = (File [])keys.toArray(loadedFiles); 303 Arrays.sort(loadedFiles); 304 for (int count = 0; count < loadedFiles.length; count++) { 305 removeEntry(loadedFiles[loadedFiles.length - (count + 1)]); 306 } 307 } catch (Exception ex) { 308 log.error("Failed to shut down an entry because : " + ex. 309 getMessage(),ex); 310 } 311 } 312 313 314 318 private void manage() { 319 try { 320 File [] files = FileUtil.filter(deploymentDir.listFiles(),".jar"); 322 Arrays.sort(files); 323 324 for (int count = 0; count < files.length; count++) { 326 File file = files[count]; 327 if (loaderList.contains(file) == false) { 328 continue; 329 } else if (loaderList.get(file).getLastModified() != 330 file.lastModified()) { 331 removeEntry(file); 332 } 333 } 334 335 336 for (int count = 0; count < files.length; count++) { 338 File file = files[count]; 339 if (loaderList.contains(file) == false) { 340 loadEntry(file); 341 } 342 } 343 344 Set keySet = new HashSet (loaderList.getKeys()); 346 for (Iterator iter = keySet.iterator(); iter.hasNext();) 347 { 348 File key = (File )iter.next(); 350 boolean found = false; 351 for (int count = 0; count < files.length; count++) { 352 File file = files[count]; 353 if (file.equals(key)) { 354 found = true; 355 break; 356 } 357 } 358 if (found == false) { 359 removeEntry(key); 360 } 361 } 362 363 364 } catch (Exception ex) { 365 log.error("Failed to load the files : " + ex.getMessage(),ex); 366 } 367 if (false == DeploymentMonitor.getInstance(). 369 isInitDeployComplete()) { 370 DeploymentMonitor.getInstance().initDeployCompleted(); 371 } 372 } 373 374 375 380 private void removeEntry(File file) { 381 ClassLoader originalLoader = null; 382 try { 383 DeploymentLoader loader = loaderList.get(file); 385 if (loader.getSuccessful() == false) { 386 log.info("This object was not loaded succesfully [" 387 + file.getAbsolutePath() + "] removing internal " + 388 "reference"); 389 loaderList.remove(file); 390 return; 391 } 392 log.info("Un-load the deployment file [" + file.getAbsolutePath() + 393 "]"); 394 originalLoader = Thread.currentThread().getContextClassLoader(); 395 Thread.currentThread().setContextClassLoader(loader.getClassLoader()); 396 CacheRegistry.getInstance().terminateCache(); 397 beanManager.unLoad(loader); 398 jmxBeanManager.unLoad(loader); 399 webServiceManager.unLoad(loader); 400 ObjectLockFactory.fin(); 401 TransactionManager.fin(); 402 loaderList.remove(file); 403 404 DeploymentLoader.ClassLoaderLookup.getInstance().removeClassLoader( 406 loader); 407 408 NamingDirector.getInstance().releaseContext(); 409 ThreadGroupManager.getInstance().terminateThreadGroup(); 410 } catch (Exception ex) { 411 log.error("Failed to unload the file [" + file.getAbsolutePath() + 412 "]",ex); 413 } finally { 414 try { 415 if (originalLoader != null) { 416 Thread.currentThread().setContextClassLoader( 418 originalLoader); 419 } 420 } catch (Exception exc) { 421 } 423 } 424 } 425 426 427 432 private void loadEntry(File file) { 433 DeploymentLoader loader = null; 434 ClassLoader originalLoader = null; 435 try { 436 log.info("Load the deployment file [" + file.getAbsolutePath() + 438 "]"); 439 try { 440 loader = new DeploymentLoader(file); 441 } catch (Exception ex) { 442 log.warn("Failed to load the file [" + file.getAbsolutePath() + 443 "] will try this it again later.",ex); 444 return; 445 } 446 447 loaderList.add(file,loader); 448 if (loader.getSuccessful() == false) { 449 log.info("The failed to load the [" 450 + file.getAbsolutePath() + "]."); 451 return; 452 } 453 454 originalLoader = Thread.currentThread().getContextClassLoader(); 456 Thread.currentThread().setContextClassLoader(loader.getClassLoader()); 457 ThreadGroupManager.getInstance().initThreadGroup(threadGroup); 458 NamingDirector.getInstance().initContext(); 459 CacheRegistry.getInstance().initCache(); 460 ObjectLockFactory.init(); 461 TransactionManager.init(); 462 beanManager.load(loader); 463 jmxBeanManager.load(loader); 464 webServiceManager.load(loader); 465 466 log.info("The file [" 467 + file.getAbsolutePath() + "] was loaded successfully"); 468 } catch (Exception ex) { 469 try { 471 if (loader != null) { 472 beanManager.unLoad(loader); 473 jmxBeanManager.unLoad(loader); 474 webServiceManager.unLoad(loader); 475 } 476 477 } catch (Exception exc) { 478 } 480 try { 481 CacheRegistry.getInstance().terminateCache(); 482 } catch (Exception exc) { 483 log.error("Failed to terminate the cache [" + exc.getMessage() + 485 "]",ex); 486 } 487 try { 488 ThreadGroupManager.getInstance().terminateThreadGroup(); 489 } catch (Exception exc) { 490 log.error("Failed to terminate the thread group [" 492 + exc.getMessage() + "]",ex); 493 } 494 log.error("Failed to load the file [" + file.getAbsolutePath() + 495 "]",ex); 496 } finally { 497 try { 498 if (originalLoader != null) { 499 Thread.currentThread().setContextClassLoader( 501 originalLoader); 502 } 503 } catch (Exception exc) { 504 } 506 } 507 } 508 509 510 517 private void maintainTmpDirectory() throws DeploymentException { 518 try { 519 Configuration loaderConfig = ConfigurationFactory.getInstance(). 521 getConfig(DeploymentLoader.class); 522 523 String tmpDirPath = loaderConfig.getString( 525 DeploymentLoader.TEMP_DIRECTORY); 526 527 File tmpDir = new File (tmpDirPath); 529 tmpDir.mkdirs(); 530 if (tmpDir.isDirectory() == false) { 531 throw new DeploymentException( 532 "Failed to create the directory [" + tmpDirPath + "]"); 533 } 534 deleteDirectoryRecursive(tmpDir); 535 } catch (Exception ex) { 536 throw new DeploymentException("Failed to clean the tmp directory : " 537 + ex.getMessage(),ex); 538 } 539 } 540 541 542 547 private void deleteDirectoryRecursive(File dir) throws Exception { 548 File [] tmpFiles = dir.listFiles(); 550 for (int index = 0; index < tmpFiles.length; index++) { 551 if (tmpFiles[index].isDirectory()) { 552 deleteDirectoryRecursive(tmpFiles[index]); 553 } 554 tmpFiles[index].delete(); 555 } 556 } 557 } 558 | Popular Tags |