1 25 26 package org.objectweb.petals.jbi.management.autoload; 27 28 import java.io.File ; 29 import java.io.FileNotFoundException ; 30 import java.io.IOException ; 31 import java.io.PrintWriter ; 32 import java.net.URI ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 import java.util.Timer ; 36 37 import javax.management.MBeanServer ; 38 import javax.management.ObjectName ; 39 40 import org.apache.commons.io.FileUtils; 41 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 42 import org.objectweb.fractal.fraclet.annotation.Interface; 43 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 44 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 45 import org.objectweb.fractal.fraclet.annotation.Monolog; 46 import org.objectweb.fractal.fraclet.annotation.Provides; 47 import org.objectweb.fractal.fraclet.annotation.Requires; 48 import org.objectweb.petals.jbi.management.deployment.DeploymentService; 49 import org.objectweb.petals.jbi.management.service.InstallationService; 50 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService; 51 import org.objectweb.petals.jbi.management.service.PackageHandler; 52 import org.objectweb.petals.jbi.management.service.util.XMLResult; 53 import org.objectweb.petals.jbi.management.systemstate.ComponentState; 54 import org.objectweb.petals.jbi.management.systemstate.ServiceAssemblyState; 55 import org.objectweb.petals.jbi.management.systemstate.SharedLibraryState; 56 import org.objectweb.petals.jbi.management.systemstate.SystemState; 57 import org.objectweb.petals.repository.RepositoryService; 58 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 59 import org.objectweb.petals.util.LoggingUtil; 60 import org.objectweb.petals.util.SystemUtil; 61 import org.objectweb.util.monolog.api.Logger; 62 63 75 @FractalComponent 76 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.jbi.management.autoload.AutoLoader.class)) 77 public class AutoLoaderImpl implements AutoLoader { 78 79 private static String install = "install"; 80 81 protected static File installDirectory; 82 83 private static String installed = "installed"; 84 85 protected static File installedDirectory; 86 87 private static String uninstalled = "uninstalled"; 88 89 protected static File uninstalledDirectory; 90 91 private static String work = "work"; 92 93 protected static File workDirectory; 94 95 @Requires(name="deployment",signature=org.objectweb.petals.jbi.management.deployment.DeploymentService.class) 96 protected DeploymentService deploymentService; 97 98 @Requires(name="installation",signature=org.objectweb.petals.jbi.management.service.InstallationService.class) 99 protected InstallationService installationService; 100 101 protected Timer installDirectoryScanTimer; 102 103 protected Timer installedDirectoryScanTimer; 104 105 protected LoggingUtil log; 106 107 @Monolog(name="logger") 108 protected Logger logger; 109 110 @Requires(name="lifecyclemanager",signature=org.objectweb.petals.jbi.management.service.LifeCycleManagerService.class) 111 protected LifeCycleManagerService managerService; 112 113 116 protected PackageHandler packageHandler; 117 118 121 @Requires(name="systemstate",signature=org.objectweb.petals.jbi.management.systemstate.SystemState.class) 122 protected SystemState recoverySrv; 123 124 127 protected RepositoryService repositorySrv; 128 129 private long scanPeriod = 4000L; 130 131 public static File getInstallDirectory() { 132 return installDirectory; 133 } 134 135 public static File getInstalledDirectory() { 136 return installedDirectory; 137 } 138 139 public static File getUninstalledDirectory() { 140 return uninstalledDirectory; 141 } 142 143 public static File getWorkDirectory() { 144 return workDirectory; 145 } 146 147 150 @LifeCycle(on=LifeCycleType.START) 151 public void start() throws IOException { 152 log = new LoggingUtil(logger); 153 log.call(); 154 installDirectory = new File (SystemUtil.getPetalsInstallDirectory() 155 .getAbsolutePath() 156 + File.separator + install); 157 installedDirectory = new File (SystemUtil.getPetalsInstallDirectory() 158 .getAbsolutePath() 159 + File.separator + installed); 160 uninstalledDirectory = new File (SystemUtil.getPetalsInstallDirectory() 161 .getAbsolutePath() 162 + File.separator + uninstalled); 163 workDirectory = new File (SystemUtil.getPetalsInstallDirectory() 164 .getAbsolutePath() 165 + File.separator + work); 166 167 checkValidiyOfdirectory(installDirectory, false); 168 checkValidiyOfdirectory(installedDirectory, false); 169 checkValidiyOfdirectory(uninstalledDirectory, false); 170 checkValidiyOfdirectory(workDirectory, true); 171 172 installDirectoryScanTimer = new Timer (); 174 installedDirectoryScanTimer = new Timer (); 175 InstallDirectoryScanner installScan = new InstallDirectoryScanner(this, 176 installDirectory); 177 InstalledDirectoryScanner installedScan = new InstalledDirectoryScanner( 178 this, installedDirectory); 179 182 packageHandler = new PackageHandler(logger, repositorySrv); 183 184 installDirectoryScanTimer.schedule(installScan, scanPeriod, scanPeriod); 185 installedDirectoryScanTimer.schedule(installedScan, scanPeriod, 186 scanPeriod); 187 } 188 189 193 @LifeCycle(on=LifeCycleType.STOP) 194 public void stop() { 195 log.call(); 196 197 if (installDirectoryScanTimer != null) { 198 installDirectoryScanTimer.cancel(); 199 } 200 if (installedDirectoryScanTimer != null) { 201 installedDirectoryScanTimer.cancel(); 202 } 203 } 204 205 210 protected void install(File [] newInstallers) { 211 log.call(); 212 213 if (newInstallers != null) { 214 for (int i = 0; i < newInstallers.length; i++) { 215 File file = newInstallers[i]; 216 try { 217 220 File workArchive = new File (getWorkDirectory(), file 221 .getName()); 222 FileUtils.copyFile(file, workArchive); 223 FileUtils.forceDelete(file); 224 225 228 229 JBIDescriptor descriptor = packageHandler 230 .loadDescriptor(workArchive.toURI()); 231 232 235 if (descriptor.getComponent() != null) { 236 performInstall(workArchive); 237 } else if (descriptor.getSharedLibrary() != null) { 238 performShare(workArchive); 239 } else if (descriptor.getServiceAssembly() != null) { 240 performDeploy(workArchive); 241 } else { 242 throw new Exception ("auto recover of entity failed : " 243 + workArchive.getCanonicalPath()); 244 } 245 FileUtils.forceDelete(workArchive); 246 247 } catch (Throwable e) { 248 log.error("Error during install", e); 249 } 250 } 251 } 252 } 253 254 259 protected void uninstall(List <File > filesToUninstall) { 260 log.call(); 261 264 List <ComponentState> compStates = recoverySrv 265 .retrieveAllComponentStates(); 266 List <SharedLibraryState> slStates = recoverySrv 267 .retrieveAllSharedLibraryStates(); 268 List <ServiceAssemblyState> saStates = recoverySrv 269 .retrieveAllServiceAssemblyStates(); 270 271 275 List <String > compToUninstall = new ArrayList <String >(); 276 List <String > slToUninstall = new ArrayList <String >(); 277 List <String > saToUninstall = new ArrayList <String >(); 278 for (ComponentState state : compStates) { 279 URI archiveURI = packageHandler.processAndGetPackageURI(state 280 .getArchiveURL(), false); 281 File archiveFile = new File (archiveURI); 282 for (File file : filesToUninstall) { 283 if (file.equals(archiveFile)) { 284 compToUninstall.add(state.getName()); 285 } 286 } 287 } 288 for (SharedLibraryState state : slStates) { 289 URI archiveURI = packageHandler.processAndGetPackageURI(state 290 .getArchiveURL(), false); 291 File archiveFile = new File (archiveURI); 292 for (File file : filesToUninstall) { 293 if (file.equals(archiveFile)) { 294 slToUninstall.add(state.getName()); 295 } 296 } 297 } 298 for (ServiceAssemblyState state : saStates) { 299 URI archiveURI = packageHandler.processAndGetPackageURI(state 300 .getArchiveURL(), false); 301 File archiveFile = new File (archiveURI); 302 for (File file : filesToUninstall) { 303 if (file.equals(archiveFile)) { 304 saToUninstall.add(state.getName()); 305 } 306 } 307 } 308 309 312 for (String name : saToUninstall) { 313 performSAUninstall(name); 314 } 315 for (String name : compToUninstall) { 316 performCompUninstall(name); 317 } 318 for (String name : slToUninstall) { 319 performSLUninstall(name); 320 } 321 322 } 323 324 328 336 private void checkValidiyOfdirectory(File dir, boolean clean) 337 throws IOException { 338 if (dir != null) { 339 if (!dir.exists()) { 341 if (!dir.mkdirs()) { 343 throw new IOException ( 345 "can not create the following directory : " 346 + dir.getCanonicalPath()); 347 } 348 } else { 349 if (!dir.isDirectory()) { 351 throw new IOException (dir.getCanonicalPath() 353 + " already exists and is not a directory."); 354 } else { 355 if (clean) { 357 FileUtils.deleteDirectory(dir); 358 dir.mkdirs(); 359 } 360 } 361 } 362 } else { 363 throw new FileNotFoundException ("the specified directory is null"); 364 } 365 } 366 367 372 protected void performCompUninstall(String name) { 373 installationService.unloadInstaller(name, true); 374 } 375 376 384 private void performDeploy(File f) { 385 log.call(); 386 387 if (f != null && f.exists()) { 388 389 try { 390 391 400 String xml = deploymentService.deploy(f.toURI().toString()); 401 402 if (xml.indexOf(XMLResult.TaskResult.FAILED.toString()) > -1) { 403 log.warning("Problem while deploying a service assembly\n" 404 + xml); 405 } else { 406 String saName = ""; 407 saName = xml.substring(xml.indexOf("<loc-param>") + 11, xml 408 .indexOf("</loc-param>")); 409 deploymentService.start(saName); 410 411 } 412 } catch (Throwable e) { 413 try { 414 File failed = new File (workDirectory, f.getName() 415 + ".failed"); 416 417 failed.createNewFile(); 418 PrintWriter w = null; 419 w = new PrintWriter (failed); 420 e.printStackTrace(w); 421 w.flush(); 422 w.close(); 423 log.error("Error occured during auto deployment", e); 424 } catch (IOException e1) { 425 log 426 .error( 427 "Error occured during failure report file creation.", 428 e1); 429 } 430 } 431 } 432 } 433 434 442 private void performInstall(File f) { 443 log.call(); 444 445 if (f != null && f.exists()) { 446 447 try { 448 449 MBeanServer jmxServer = managerService.getMBeanServer(); 450 451 460 ObjectName newInstallerName = installationService 461 .loadNewInstaller(f.toURI().toString()); 462 if (!newInstallerName.getDomain().equals("error")) { 463 Object [] nullObjects = new Object [0]; 464 String [] nullStrings = new String [0]; 465 466 Object result = jmxServer.invoke(newInstallerName, 467 "install", nullObjects, nullStrings); 468 469 if (result instanceof ObjectName ) { 470 ObjectName componentName = (ObjectName ) result; 471 472 jmxServer.invoke(componentName, "start", nullObjects, 473 nullStrings); 474 475 } else { 476 throw new Exception ("error during installation"); 477 } 478 } else { 479 throw new Exception ("Component can't be autoinstalled"); 480 } 481 } catch (Throwable e) { 482 try { 483 484 File failed = new File (workDirectory, f.getName() 485 + ".failed"); 486 487 failed.createNewFile(); 488 PrintWriter w = null; 489 w = new PrintWriter (failed); 490 e.printStackTrace(w); 491 w.flush(); 492 w.close(); 493 log.error("Error occured during auto installation.", e); 494 } catch (IOException e1) { 495 log 496 .error( 497 "Error occured during failure report file creation.", 498 e1); 499 } 500 } 501 } 502 } 503 504 508 513 protected void performSAUninstall(String name) { 514 try { 515 deploymentService.undeploy(name); 516 } catch (Exception e) { 517 log.error("Error occured during auto sa uninstallation.", e); 518 } 519 } 520 521 529 private void performShare(File f) { 530 log.call(); 531 532 if (f != null && f.exists()) { 533 534 try { 535 536 545 installationService.installSharedLibrary(f.toURI().toString()); 546 547 } catch (Throwable e) { 548 try { 549 550 File failed = new File (workDirectory, f.getName() 551 + ".failed"); 552 553 failed.createNewFile(); 554 PrintWriter w = null; 555 w = new PrintWriter (failed); 556 e.printStackTrace(w); 557 w.flush(); 558 w.close(); 559 log 560 .error( 561 "Error occured during auto share lib installation.", 562 e); 563 } catch (IOException e1) { 564 log 565 .error( 566 "Error occured during failure report file creation.", 567 e1); 568 } 569 } 570 } 571 } 572 573 578 protected void performSLUninstall(String name) { 579 installationService.uninstallSharedLibrary(name); 580 } 581 582 } 583 | Popular Tags |