1 22 package org.objectweb.petals.jbi.management.systemstate; 23 24 import java.io.File ; 25 import java.net.URI ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 31 import javax.jbi.JBIException; 32 import javax.jbi.management.LifeCycleMBean; 33 import javax.management.InstanceNotFoundException ; 34 import javax.management.MBeanException ; 35 import javax.management.MBeanServer ; 36 import javax.management.ObjectName ; 37 import javax.management.ReflectionException ; 38 39 import org.objectweb.petals.PetalsException; 40 import org.objectweb.petals.jbi.component.lifecycle.Installer; 41 import org.objectweb.petals.jbi.component.lifecycle.ServiceAssemblyLifeCycle; 42 import org.objectweb.petals.jbi.management.deployment.DeploymentContextConstants; 43 import org.objectweb.petals.jbi.management.deployment.DeploymentService; 44 import org.objectweb.petals.jbi.management.deployment.DeploymentTaskFactory; 45 import org.objectweb.petals.jbi.management.service.InstallationService; 46 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService; 47 import org.objectweb.petals.jbi.management.service.ManagementException; 48 import org.objectweb.petals.jbi.management.service.PackageHandler; 49 import org.objectweb.petals.processor.TaskProcessor; 50 import org.objectweb.petals.repository.RepositoryService; 51 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 52 import org.objectweb.petals.util.LoggingUtil; 53 import org.objectweb.util.monolog.api.Logger; 54 55 61 public class SystemRecovery { 62 63 protected DeploymentService deploymentService; 64 65 protected InstallationService installationService; 66 67 protected LoggingUtil log; 68 69 protected LifeCycleManagerService managerService; 70 71 protected PackageHandler packageHandler; 72 73 protected RepositoryService repositorySrv; 74 75 protected StateStorage stateStorage; 76 77 public SystemRecovery(InstallationService installationService, 78 Logger logger, DeploymentService deploymentService, 79 LifeCycleManagerService managerService, StateStorage stateStorage, 80 RepositoryService repositorySrv) { 81 super(); 82 this.deploymentService = deploymentService; 83 this.installationService = installationService; 84 this.managerService = managerService; 85 this.log = new LoggingUtil(logger); 86 this.stateStorage = stateStorage; 87 this.repositorySrv = repositorySrv; 88 this.packageHandler = new PackageHandler(logger, repositorySrv); 89 createLostPlusFoundDir(); 90 createRepositoryDirectories(); 91 } 92 93 protected void createRepositoryDirectories() { 94 File componentsRep = new File (repositorySrv.getComponentsDirectory()); 95 File serviceAssRep = new File (repositorySrv 96 .getServiceAssembliesDirectory()); 97 File sharedLibRep = new File (repositorySrv.getSharedLibsDirectory()); 98 99 if (!componentsRep.exists()) { 101 if (!componentsRep.mkdirs()) { 102 String msg = "Failed to create components dir in Petals repository"; 103 log.error(msg); 104 throw new RuntimeException (msg); 105 } 106 } 107 108 if (!sharedLibRep.exists()) { 110 if (!sharedLibRep.mkdirs()) { 111 String msg = "Failed to create sharedlibs dir in Petals repository"; 112 log.error(msg); 113 throw new RuntimeException (msg); 114 } 115 } 116 117 if (!serviceAssRep.exists()) { 120 if (!serviceAssRep.mkdirs()) { 121 String msg = "Failed to create serviceassemblies dir in Petals repository"; 122 log.error(msg); 123 throw new RuntimeException (msg); 124 } 125 } 126 } 127 128 public boolean recoverAllComponent() throws ManagementException { 129 log.start(); 130 String msg = null; 131 132 139 140 List <File > unknownDirs = new ArrayList <File >(Arrays.asList(new File ( 141 repositorySrv.getComponentsDirectory()).listFiles())); 142 143 146 147 List <ComponentState> cps = stateStorage.retrieveAllComponentStates(); 148 149 152 for (ComponentState state : cps) { 153 154 URI installationRoot = null; 155 try { 156 157 installationRoot = packageHandler.processAndGetPackageURI(state 158 .getInstallURL(), true); 159 160 URI archiveURI = packageHandler.processAndGetPackageURI(state 161 .getArchiveURL(), true); 162 163 JBIDescriptor descriptor = packageHandler 164 .loadDescriptor(archiveURI); 165 166 recoverComponent(installationRoot, descriptor, state 167 .getInstallState(), state.getLifecycleState()); 168 } catch (PetalsException e) { 169 msg = "A component can't be recovered : " + state.getName(); 170 log.error(msg, e); 171 throw new ManagementException(msg, e); 172 } 173 174 if (unknownDirs != null) { 177 File knownComp = new File (installationRoot).getParentFile(); 178 if (knownComp.exists()) { 179 unknownDirs.remove(knownComp); 180 } 181 } 182 } 183 184 moveUnknownDirsToLostPlusFoundDir(unknownDirs); 187 188 log.end(); 189 return false; 190 } 191 192 public boolean recoverAllServiceAssembly() throws ManagementException { 193 log.start(); 194 String msg = null; 195 196 203 204 List <File > unknownDirs = new ArrayList <File >(Arrays.asList(new File ( 205 repositorySrv.getServiceAssembliesDirectory()).listFiles())); 206 207 210 211 List <ServiceAssemblyState> sas = stateStorage 212 .retrieveAllServiceAssemblyStates(); 213 214 217 218 for (ServiceAssemblyState state : sas) { 219 URI installationRoot = null; 220 try { 221 222 installationRoot = packageHandler.processAndGetPackageURI(state 223 .getInstallURL(), true); 224 225 URI archiveURI = packageHandler.processAndGetPackageURI(state 226 .getArchiveURL(), true); 227 228 JBIDescriptor descriptor = packageHandler 229 .loadDescriptor(archiveURI); 230 231 recoverServiceAssembly(archiveURI, descriptor, state 232 .getLifecycleState()); 233 } catch (PetalsException e) { 234 msg = "A service assembly can't be recovered : " 235 + state.getName(); 236 log.error(msg, e); 237 throw new ManagementException(msg, e); 238 } catch (Exception e) { 239 msg = "A service assembly can't be recovered : " 240 + state.getName(); 241 log.error(msg, e); 242 throw new ManagementException(msg, e); 243 } 244 245 if (unknownDirs != null) { 248 File knownComp = new File (installationRoot).getParentFile(); 249 if (knownComp.exists()) { 250 unknownDirs.remove(knownComp); 251 } 252 } 253 } 254 255 moveUnknownDirsToLostPlusFoundDir(unknownDirs); 258 259 log.end(); 260 return false; 261 } 262 263 public boolean recoverAllSharedLibrary() throws ManagementException { 264 log.start(); 265 String msg = null; 266 267 274 275 List <File > unknownDirs = new ArrayList <File >(Arrays.asList(new File ( 276 repositorySrv.getSharedLibsDirectory()).listFiles())); 277 278 281 282 List <SharedLibraryState> sls = stateStorage 283 .retrieveAllSharedLibraryStates(); 284 285 288 for (SharedLibraryState state : sls) { 289 URI installationRoot = null; 290 try { 291 292 installationRoot = packageHandler.processAndGetPackageURI(state 293 .getInstallURL(), true); 294 295 URI archiveURI = packageHandler.processAndGetPackageURI(state 296 .getArchiveURL(), true); 297 298 JBIDescriptor descriptor = packageHandler 299 .loadDescriptor(archiveURI); 300 301 recoverSharedLib(installationRoot, descriptor); 302 } catch (PetalsException e) { 303 msg = "A Shared lib can't be recovered : " + state.getName(); 304 log.error(msg, e); 305 throw new ManagementException(msg, e); 306 } 307 308 if (unknownDirs != null) { 311 File knownComp = new File (installationRoot).getParentFile(); 312 if (knownComp.exists()) { 313 unknownDirs.remove(knownComp); 314 } 315 } 316 } 317 318 moveUnknownDirsToLostPlusFoundDir(unknownDirs); 321 322 log.end(); 323 return false; 324 } 325 326 331 protected void createLostPlusFoundDir() { 332 File lostPlusFoundFile = new File (repositorySrv 333 .getLostPlusFoundDirectory()); 334 if (lostPlusFoundFile.exists()) { 336 if (!lostPlusFoundFile.delete()) { 337 log 338 .warning("\"lost+found\" can't be deleted, " 339 + "following Petals repository cleanup can have some troubles!"); 340 } else { 341 if (!lostPlusFoundFile.mkdirs()) { 342 String msg = "Failed to create lost+found directory, " 343 + "following Petals repository cleanup will fail!"; 344 log.warning(msg); 345 } 346 } 347 } else { 348 if (!lostPlusFoundFile.mkdirs()) { 349 String msg = "Failed to create lost+found directory, " 350 + "following Petals repository cleanup will fail!"; 351 log.warning(msg); 352 } 353 } 354 } 355 356 362 protected void moveUnknownDirsToLostPlusFoundDir(List <File > unknownDirs) { 363 if (unknownDirs != null) { 364 for (File unknownDir : unknownDirs) { 365 366 if (!unknownDir.exists()) { 367 log 368 .warning("The unknown dir (" 369 + unknownDir.getAbsolutePath() 370 + ") doesn't exist."); 371 } 372 boolean moveDir = unknownDir.renameTo(new File (new File ( 373 repositorySrv.getLostPlusFoundDirectory()), unknownDir 374 .getName())); 375 if (!moveDir) { 376 log.warning("Failed to move unknown directory (" 377 + unknownDir.getAbsolutePath() 378 + ") to lost+found directory"); 379 } 380 } 381 } 382 } 383 384 397 protected void recoverComponent(URI installationRoot, 398 JBIDescriptor descriptor, String installState, String lifecycleState) 399 throws ManagementException { 400 log.start(); 401 String msg = null; 402 403 String compName = descriptor.getComponent().getIdentification() 404 .getName(); 405 try { 406 407 if (Installer.INSTALLED.equals(installState)) { 408 411 ObjectName installerName = installationService 412 .loadNewInstaller(installationRoot, descriptor); 413 MBeanServer beanServer = managerService.getMBeanServer(); 414 415 Object [] nullObjects = new Object [0]; 416 String [] nullStrings = new String [0]; 417 ObjectName clcName = (ObjectName ) beanServer.invoke( 418 installerName, "install", nullObjects, nullStrings); 419 420 if (LifeCycleMBean.STOPPED.equals(lifecycleState)) { 421 424 beanServer.invoke(clcName, "start", nullObjects, 425 nullStrings); 426 beanServer 427 .invoke(clcName, "stop", nullObjects, nullStrings); 428 } else if (LifeCycleMBean.STARTED.equals(lifecycleState)) { 429 432 beanServer.invoke(clcName, "start", nullObjects, 433 nullStrings); 434 } 435 } 436 } catch (InstanceNotFoundException e) { 437 msg = "Component can't be recovered : " + compName; 438 log.error(msg, e); 439 throw new ManagementException(msg, e); 440 } catch (MBeanException e) { 441 msg = "Component can't be recovered : " + compName; 442 log.error(msg, e); 443 throw new ManagementException(msg, e); 444 } catch (ReflectionException e) { 445 msg = "Component can't be recovered : " + compName; 446 log.error(msg, e); 447 throw new ManagementException(msg, e); 448 } catch (PetalsException e) { 449 msg = "Component can't be recovered : " + compName; 450 log.error(msg, e); 451 throw new ManagementException(msg, e); 452 } 453 log.end(); 454 } 455 456 468 protected void recoverServiceAssembly(URI archiveURI, 469 JBIDescriptor descriptor, String lifecycleState) throws Exception { 470 log.start(); 471 String msg = null; 472 473 String saName = descriptor.getServiceAssembly().getIdentification() 474 .getName(); 475 try { 476 477 480 481 HashMap <String , Object > deploymentContext = createDeploymentContext( 483 archiveURI, descriptor); 484 485 TaskProcessor processor = createDeploymentProcessor(deploymentContext); 487 488 if (!processor.process()) { 490 msg = "An error occured during the deployment " 491 + "of the following service assembly: " 492 + saName; 493 throw new Exception (msg); 494 } 495 496 if (LifeCycleMBean.STOPPED.equals(lifecycleState)) { 497 500 ServiceAssemblyLifeCycle salc = managerService 501 .getServiceAssemblyByName(saName); 502 salc.start(); 503 salc.stop(); 504 } else if (LifeCycleMBean.STARTED.equals(lifecycleState)) { 505 508 ServiceAssemblyLifeCycle salc = managerService 509 .getServiceAssemblyByName(saName); 510 salc.start(); 511 } 512 513 } catch (PetalsException e) { 514 msg = "ServiceAssembly can't be recovered : " + saName; 515 log.error(msg, e); 516 throw new ManagementException(msg, e); 517 } catch (JBIException e) { 518 msg = "ServiceAssembly can't be recovered : " + saName; 519 log.error(msg, e); 520 throw new ManagementException(msg, e); 521 } 522 log.end(); 523 } 524 525 533 protected TaskProcessor createDeploymentProcessor( 534 HashMap <String , Object > deploymentContext) { 535 TaskProcessor processor = new TaskProcessor(deploymentContext, log); 536 537 DeploymentTaskFactory deploymentTaskFactory = deploymentService 538 .getDeploymentTaskFactory(); 539 540 processor.addTask(deploymentTaskFactory.getPackageCheckingTask()); 541 processor.addTask(deploymentTaskFactory.getResultCreationTask()); 542 processor.addTask(deploymentTaskFactory 543 .getSaAndSUInstallRootRetrievalTask()); 544 processor.addTask(deploymentTaskFactory.getAllSUDeploymentTask()); 545 processor.addTask(deploymentTaskFactory 546 .getSaLifeCycleRegistrationTask()); 547 processor.addTask(deploymentTaskFactory 548 .getAllConnectionRegistrationTask()); 549 processor.addTask(deploymentTaskFactory.getDeploymentSuccessTask()); 550 551 return processor; 552 } 553 554 563 protected HashMap <String , Object > createDeploymentContext(URI archiveURI, 564 JBIDescriptor descriptor) { 565 HashMap <String , Object > deploymentContext = new HashMap <String , Object >(); 566 deploymentContext.put(DeploymentContextConstants.ARCHIVE_URI, 567 archiveURI); 568 deploymentContext.put(DeploymentContextConstants.SA_DESCRIPTOR, 569 descriptor); 570 return deploymentContext; 571 } 572 573 582 protected void recoverSharedLib(URI installationRoot, 583 JBIDescriptor descriptor) throws ManagementException { 584 log.start(); 585 586 589 try { 590 installationService.installSharedLibrary(installationRoot, 591 descriptor); 592 } catch (PetalsException e) { 593 String msg = "ServiceAssembly can't be recovered : " 594 + installationRoot.toString(); 595 log.error(msg, e); 596 throw new ManagementException(msg, e); 597 } 598 599 log.end(); 600 } 601 602 } 603 | Popular Tags |