1 23 24 30 31 package com.sun.enterprise.deployment.autodeploy; 32 33 34 import com.sun.enterprise.deployment.backend.DeploymentLogger; 35 import java.util.Vector ; 36 import java.util.Timer ; 37 import java.util.TimerTask ; 38 import java.util.Enumeration ; 39 import java.io.File ; 40 import java.util.logging.Logger ; 41 import java.util.logging.Level ; 42 import com.sun.enterprise.config.ConfigContextEventListener; 43 import com.sun.enterprise.config.ConfigContextEvent; 44 import com.sun.enterprise.admin.common.constant.AdminConstants; 45 import com.sun.enterprise.util.i18n.StringManager; 46 47 48 60 public class AutoDeployControllerImpl implements AutoDeployController,ConfigContextEventListener { 61 62 63 64 67 private Vector autodeployDirs=new Vector (); 68 private long pollingInterval = AutoDeployConstants.MIN_POOLING_INTERVAL; 69 private boolean verify = false; 70 private boolean preJspCompilation = false; 71 72 private Timer timer =null; 73 private AutoDeployTask deployTask=null; 74 75 public static final Logger sLogger=DeploymentLogger.get(); 76 private static StringManager localStrings = 77 StringManager.getManager( AutoDeployControllerImpl.class ); 78 79 80 public AutoDeployControllerImpl(String autodeployDir, long pollingInterval) throws AutoDeploymentException { 81 try { 82 addAutoDeployDir(autodeployDir); 83 setPollingInterval(pollingInterval); 84 } catch(AutoDeploymentException ae){ 85 sLogger.log(Level.SEVERE, "enterprise.deployment.backend.autoDeploymentStartFailure"); 86 throw ae; 87 } 88 } 89 90 public AutoDeployControllerImpl(String [] autodeployDirs, long pollingInterval) throws AutoDeploymentException { 91 try { 92 for (int i=0;i<autodeployDirs.length;i++) { 93 addAutoDeployDir(autodeployDirs[i]); 94 } 95 setPollingInterval(pollingInterval); 96 } catch(AutoDeploymentException ae){ 97 sLogger.log(Level.SEVERE, "enterprise.deployment.backend.autoDeploymentStartFailure"); 98 throw ae; 99 } 100 101 } 102 103 106 public boolean enableAutoDeploy() { 107 timer = new Timer (); 108 deployTask=new AutoDeployTask(); 109 timer.schedule(deployTask,AutoDeployConstants.STARTING_DELAY*1000,pollingInterval*1000); 110 String msg = localStrings.getString("enterprise.deployment.autodeploy.autoDeployment_service_enabled"); 111 sLogger.log(Level.FINE, msg+System.currentTimeMillis()); 112 return true; 113 114 } 115 116 119 public boolean disableAutoDeploy() { 120 if(deployTask!=null) { 121 deployTask.cancel(); 122 } 123 if(timer!=null) { 124 timer.cancel(); 125 } 126 String msg = localStrings.getString("enterprise.deployment.autodeploy.autoDeployment_service_disabled"); 127 sLogger.log(Level.INFO, msg); 128 return true; 129 130 131 } 132 133 136 public String [] getAllAutoDeployDirs() { 137 if(autodeployDirs != null && ! autodeployDirs.isEmpty()) { 138 String [] dirs=new String [autodeployDirs.size()]; 139 int i=0; 140 for (Enumeration list = autodeployDirs.elements() ; list.hasMoreElements() ;i++) { 141 142 dirs[i]=((File )list.nextElement()).getAbsolutePath(); 143 } 144 145 return dirs; 146 }else { 147 return null; 148 } 149 150 } 151 152 155 public void addAutoDeployDir(String autodeployDir) throws AutoDeploymentException { 156 if(!validateDir(autodeployDir)) { 157 String msg = localStrings.getString("enterprise.deployment.autodeploy.invalid_source_dir",autodeployDir); 158 sLogger.log(Level.INFO, msg); 159 } 160 if((locateAlreadyExistingDir(autodeployDir)== -1)){ 161 autodeployDirs.add(new File (autodeployDir)); 162 } else { 163 String msg = localStrings.getString("enterprise.deployment.autodeploy.duplicate_source_dir",autodeployDir); 164 sLogger.log(Level.WARNING, msg); 165 } 167 168 } 169 170 171 174 175 public void removeAutoDeployDir(String autodeployDir) { 176 int location=locateAlreadyExistingDir(autodeployDir); 177 if(location>=0) { 178 this.autodeployDirs.remove(location); 179 } 180 } 181 182 185 public long getPollingInterval() { 186 return pollingInterval; 187 188 } 189 190 193 public void setPollingInterval(long pollInterval) throws AutoDeploymentException { 194 195 if(validatePollingInterval(pollInterval)) { 196 this.pollingInterval=pollInterval; 197 } else{ 198 String sInterval= new String (""+pollInterval); 199 String sMinInterval= new String (""+AutoDeployConstants.MIN_POOLING_INTERVAL); 200 String msg = localStrings.getString("enterprise.deployment.autodeploy.invalid_pooling_interval", sInterval, sMinInterval); 201 sLogger.log(Level.INFO, msg+pollInterval); 202 throw new AutoDeploymentException(msg+pollInterval); 203 } 204 } 205 206 210 public boolean isVerifyEnabled() { 211 return verify; 212 } 213 214 217 public void setVerify(boolean verify) { 218 this.verify=verify; 219 } 220 221 225 public boolean isPreJspCompilationEnabled() { 226 return preJspCompilation; 227 } 228 229 232 public void setPreJspCompilation(boolean preJspCompilation) { 233 this.preJspCompilation=preJspCompilation; 234 } 235 236 237 private boolean validateDir(String dir) { 238 boolean valid=false; 239 File autodeployDir=new File (dir); 240 if(autodeployDir != null && autodeployDir.exists() 241 && autodeployDir.isDirectory()&& autodeployDir.canWrite() && 242 autodeployDir.canRead() ) { 243 valid=true; 244 } 245 return valid; 246 247 } 248 249 private int locateAlreadyExistingDir(String dir) { 250 int location=-1; 252 String extingDirs[]=getAllAutoDeployDirs(); 253 if(extingDirs!=null) { 254 for (int i=0; i<extingDirs.length ; i++) { 255 if(dir.equalsIgnoreCase(extingDirs[i])) { 256 location=i; break; 258 } 259 } 260 261 } 262 return location; 263 } 264 265 private boolean validatePollingInterval(long time) { 266 boolean valid=false; 267 268 if(time >= AutoDeployConstants.MIN_POOLING_INTERVAL) { 269 valid= true; 270 } 271 return valid; 272 273 } 274 275 276 280 public void preChangeNotification(ConfigContextEvent ccce) { 281 } 283 284 285 289 public void postChangeNotification(ConfigContextEvent ccce) { 290 } 292 293 297 public void preAccessNotification(ConfigContextEvent ccce) { 298 } 300 301 305 public void postAccessNotification(ConfigContextEvent ccce) { 306 } 308 309 310 311 312 private class AutoDeployTask extends TimerTask { 313 314 324 private AutoDeployer currentDeployer= null; 325 326 private DirectoryScanner directoryScanner = null; 327 328 public void run() { 329 330 try{ 331 if (sLogger.isLoggable(Level.FINEST)) { 332 String msg = localStrings.getString("enterprise.deployment.autodeploy.thread_started"); 333 sLogger.log(Level.FINEST, msg + System.currentTimeMillis()); 334 } 335 336 if(autodeployDirs != null && ! autodeployDirs.isEmpty()) { 337 338 for (Enumeration list = autodeployDirs.elements() ; list.hasMoreElements() ;) { 339 340 if(currentDeployer !=null && currentDeployer.isCancelled()) { break; 342 } 343 344 File sourceDir = (File )list.nextElement(); 345 if (sLogger.isLoggable(Level.FINEST)) { 346 sLogger.log(Level.FINEST, 347 localStrings.getString("enterprise.deployment.autodeploy.processing_source_directory",sourceDir)); 348 } 349 File [] sourceDirFiles = sourceDir.listFiles(); 351 if (sourceDirFiles.length==1 352 && AutoDeployedFilesManager.STATUS_DIR_NAME.equals(sourceDirFiles[0].getName())) { 353 continue; 354 } 355 AutoDeployer deployer = getAutoDeployer(); 357 try { 358 deployer.deployAll(sourceDir, true); 359 deployer.undeployAll(sourceDir); 360 }catch(Exception ae) { 361 sLogger.log(Level.SEVERE, "enterprise.deployment.backend.autoDeploymentFailure", 362 new Object [] {ae.getMessage()}); 363 } 365 366 if(deployer.isCancelled()) { break; 368 } 369 } 370 371 } 372 373 }catch(Exception e){ 374 currentDeployer=null; 375 sLogger.log(Level.SEVERE, "enterprise.deployment.backend.autoDeploymentFailure", new Object [] {e.getMessage()}); 376 } 377 } 378 379 384 public boolean cancel(){ 385 boolean flag=false; 386 if(currentDeployer !=null) { 388 currentDeployer.setCancel(true); 389 } 390 super.cancel(); 391 flag=true; 392 return flag; 393 394 } 395 396 private DirectoryScanner getDirectoryScanner() { 397 if (directoryScanner == null) { 398 directoryScanner = new AutoDeployDirectoryScanner(); 399 } 400 return directoryScanner; 401 } 402 403 private AutoDeployer getAutoDeployer() { 404 if (currentDeployer == null) { 405 currentDeployer = new AutoDeployer(verify, preJspCompilation); 406 } else { 407 411 currentDeployer.setVerify(verify); 412 currentDeployer.setJspPreCompilation(preJspCompilation); 413 } 414 currentDeployer.setDirectoryScanner(getDirectoryScanner()); 415 return currentDeployer; 416 } 417 } 418 } 419 | Popular Tags |