1 17 package org.alfresco.repo.descriptor; 18 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 27 import org.alfresco.error.AlfrescoRuntimeException; 28 import org.alfresco.model.ContentModel; 29 import org.alfresco.repo.importer.ImporterBootstrap; 30 import org.alfresco.repo.transaction.TransactionUtil; 31 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork; 32 import org.alfresco.service.cmr.repository.NodeRef; 33 import org.alfresco.service.cmr.repository.NodeService; 34 import org.alfresco.service.cmr.repository.StoreRef; 35 import org.alfresco.service.cmr.search.SearchService; 36 import org.alfresco.service.descriptor.Descriptor; 37 import org.alfresco.service.descriptor.DescriptorService; 38 import org.alfresco.service.license.LicenseDescriptor; 39 import org.alfresco.service.license.LicenseException; 40 import org.alfresco.service.license.LicenseService; 41 import org.alfresco.service.namespace.NamespaceService; 42 import org.alfresco.service.namespace.QName; 43 import org.alfresco.service.transaction.TransactionService; 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 import org.springframework.beans.BeansException; 47 import org.springframework.beans.factory.InitializingBean; 48 import org.springframework.context.ApplicationContext; 49 import org.springframework.context.ApplicationContextAware; 50 import org.springframework.context.ApplicationEvent; 51 import org.springframework.context.ApplicationListener; 52 import org.springframework.context.event.ContextRefreshedEvent; 53 import org.springframework.core.io.Resource; 54 55 56 61 public class DescriptorServiceImpl implements DescriptorService, ApplicationListener, InitializingBean, ApplicationContextAware 62 { 63 private static Log logger = LogFactory.getLog(DescriptorServiceImpl.class); 64 65 private ApplicationContext applicationContext; 66 67 private Properties serverProperties; 68 69 private ImporterBootstrap systemBootstrap; 70 private NamespaceService namespaceService; 71 private NodeService nodeService; 72 private SearchService searchService; 73 private TransactionService transactionService; 74 private LicenseService licenseService = null; 75 76 private Descriptor serverDescriptor; 77 private Descriptor installedRepoDescriptor; 78 79 80 83 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException 84 { 85 this.applicationContext = applicationContext; 86 } 87 88 94 public void setDescriptor(Resource descriptorResource) 95 throws IOException 96 { 97 this.serverProperties = new Properties (); 98 this.serverProperties.load(descriptorResource.getInputStream()); 99 } 100 101 104 public void setSystemBootstrap(ImporterBootstrap systemBootstrap) 105 { 106 this.systemBootstrap = systemBootstrap; 107 } 108 109 112 public void setTransactionService(TransactionService transactionService) 113 { 114 this.transactionService = transactionService; 115 } 116 117 120 public void setNamespaceService(NamespaceService namespaceService) 121 { 122 this.namespaceService = namespaceService; 123 } 124 125 128 public void setNodeService(NodeService nodeService) 129 { 130 this.nodeService = nodeService; 131 } 132 133 136 public void setSearchService(SearchService searchService) 137 { 138 this.searchService = searchService; 139 } 140 141 144 public Descriptor getServerDescriptor() 145 { 146 return serverDescriptor; 147 } 148 149 152 public Descriptor getInstalledRepositoryDescriptor() 153 { 154 return installedRepoDescriptor; 155 } 156 157 160 public LicenseDescriptor getLicenseDescriptor() 161 { 162 return (licenseService == null) ? null : licenseService.getLicense(); 163 } 164 165 168 public void onApplicationEvent(ApplicationEvent event) 169 { 170 if (event instanceof ContextRefreshedEvent) 171 { 172 TransactionWork<Descriptor> createDescriptorWork = new TransactionUtil.TransactionWork<Descriptor>() 175 { 176 public Descriptor doWork() 177 { 178 initialiseLicenseService(); 180 181 licenseService.verifyLicense(); 183 184 updateCurrentRepositoryDescriptor(serverDescriptor); 186 187 return createInstalledRepositoryDescriptor(); 189 } 190 }; 191 installedRepoDescriptor = TransactionUtil.executeInUserTransaction(transactionService, createDescriptorWork); 192 } 193 } 194 195 198 public void afterPropertiesSet() throws Exception 199 { 200 serverDescriptor = createServerDescriptor(); 202 } 203 204 209 private Descriptor createServerDescriptor() 210 { 211 return new ServerDescriptor(); 212 } 213 214 219 private Descriptor createInstalledRepositoryDescriptor() 220 { 221 StoreRef storeRef = systemBootstrap.getStoreRef(); 223 Properties systemProperties = systemBootstrap.getConfiguration(); 224 String path = systemProperties.getProperty("system.descriptor.childname"); 225 226 NodeRef descriptorNodeRef = getDescriptorNodeRef(storeRef, path, false); 228 if (descriptorNodeRef != null) 230 { 231 Map <QName, Serializable > properties = nodeService.getProperties(descriptorNodeRef); 232 return new RepositoryDescriptor(properties); 233 } 234 else 235 { 236 return new UnknownDescriptor(); 238 } 239 } 240 241 246 private void updateCurrentRepositoryDescriptor(Descriptor serverDescriptor) 247 { 248 StoreRef storeRef = systemBootstrap.getStoreRef(); 250 Properties systemProperties = systemBootstrap.getConfiguration(); 251 String path = systemProperties.getProperty("system.descriptor.current.childname"); 252 253 NodeRef currentDescriptorNodeRef = getDescriptorNodeRef(storeRef, path, true); 255 if (currentDescriptorNodeRef == null) 257 { 258 return; 259 } 260 nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_MAJOR, serverDescriptor.getVersionMajor()); 262 nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_MINOR, serverDescriptor.getVersionMinor()); 263 nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_REVISION, serverDescriptor.getVersionRevision()); 264 nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_LABEL, serverDescriptor.getVersionLabel()); 265 nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_SCHEMA, serverDescriptor.getSchema()); 266 267 if (logger.isDebugEnabled()) 269 { 270 logger.debug("Updated current repository descriptor properties: \n" + 271 " node: " + currentDescriptorNodeRef + "\n" + 272 " descriptor: " + serverDescriptor); 273 } 274 } 275 276 283 private NodeRef getDescriptorNodeRef(StoreRef storeRef, String path, boolean create) 284 { 285 NodeRef descriptorNodeRef = null; 286 String searchPath = "/" + path; 287 288 if (nodeService.exists(storeRef)) 290 { 291 NodeRef rootNodeRef = nodeService.getRootNode(storeRef); 292 List <NodeRef> nodeRefs = searchService.selectNodes(rootNodeRef, searchPath, null, namespaceService, false); 293 if (nodeRefs.size() == 1) 294 { 295 descriptorNodeRef = nodeRefs.get(0); 296 } 297 else if (nodeRefs.size() == 0) 298 { 299 } 300 else if (nodeRefs.size() > 1) 301 { 302 if (logger.isDebugEnabled()) 303 { 304 logger.debug("Multiple descriptors: \n" + 305 " store: " + storeRef + "\n" + 306 " path: " + searchPath); 307 } 308 descriptorNodeRef = nodeRefs.get(0); 310 } 311 } 312 313 if (descriptorNodeRef == null) 314 { 315 if (logger.isDebugEnabled()) 316 { 317 logger.debug("Descriptor not found: \n" + 318 " store: " + storeRef + "\n" + 319 " path: " + searchPath); 320 } 321 322 if (create) 324 { 325 storeRef = nodeService.createStore(storeRef.getProtocol(), storeRef.getIdentifier()); 326 NodeRef rootNodeRef = nodeService.getRootNode(storeRef); 327 descriptorNodeRef = nodeService.createNode( 328 rootNodeRef, 329 ContentModel.ASSOC_CHILDREN, 330 QName.createQName(path, namespaceService), 331 QName.createQName("sys:descriptor", namespaceService)).getChildRef(); 332 if (logger.isDebugEnabled()) 333 { 334 logger.debug("Created missing descriptor node: " + descriptorNodeRef); 335 } 336 } 337 } 338 return descriptorNodeRef; 339 } 340 341 344 private void initialiseLicenseService() 345 { 346 try 347 { 348 Class licenseComponentClass = Class.forName("org.alfresco.license.LicenseComponent"); 351 Constructor constructor = licenseComponentClass.getConstructor(new Class [] { ApplicationContext.class} ); 352 licenseService = (LicenseService)constructor.newInstance(new Object [] { applicationContext }); 353 } 354 catch (ClassNotFoundException e) 355 { 356 licenseService = new NOOPLicenseService(); 357 } 358 catch (SecurityException e) 359 { 360 throw new AlfrescoRuntimeException("Failed to initialise license service", e); 361 } 362 catch (IllegalArgumentException e) 363 { 364 throw new AlfrescoRuntimeException("Failed to initialise license service", e); 365 } 366 catch (NoSuchMethodException e) 367 { 368 throw new AlfrescoRuntimeException("Failed to initialise license service", e); 369 } 370 catch (InvocationTargetException e) 371 { 372 throw new AlfrescoRuntimeException("Failed to initialise license service", e); 373 } 374 catch (InstantiationException e) 375 { 376 throw new AlfrescoRuntimeException("Failed to initialise license service", e); 377 } 378 catch (IllegalAccessException e) 379 { 380 throw new AlfrescoRuntimeException("Failed to initialise license service", e); 381 } 382 } 383 384 387 private class NOOPLicenseService implements LicenseService 388 { 389 392 public void verifyLicense() throws LicenseException 393 { 394 } 395 396 399 public LicenseDescriptor getLicense() throws LicenseException 400 { 401 return null; 402 } 403 } 404 405 410 private class UnknownDescriptor implements Descriptor 411 { 412 415 public String getVersionMajor() 416 { 417 return "Unknown"; 418 } 419 420 423 public String getVersionMinor() 424 { 425 return "Unknown"; 426 } 427 428 431 public String getVersionRevision() 432 { 433 return "Unknown"; 434 } 435 436 439 public String getVersionLabel() 440 { 441 return "Unknown"; 442 } 443 444 447 public String getVersion() 448 { 449 return "Unknown (pre 1.0.0 RC2)"; 450 } 451 452 455 public String getEdition() 456 { 457 return "Unknown"; 458 } 459 460 464 public int getSchema() 465 { 466 return 0; 467 } 468 469 472 public String [] getDescriptorKeys() 473 { 474 return new String [0]; 475 } 476 477 480 public String getDescriptor(String key) 481 { 482 return null; 483 } 484 } 485 486 489 private class RepositoryDescriptor implements Descriptor 490 { 491 private Map <QName, Serializable > properties; 492 493 494 499 private RepositoryDescriptor(Map <QName, Serializable > properties) 500 { 501 this.properties = properties; 502 } 503 504 507 public String getVersionMajor() 508 { 509 return getDescriptor("sys:versionMajor"); 510 } 511 512 515 public String getVersionMinor() 516 { 517 return getDescriptor("sys:versionMinor"); 518 } 519 520 523 public String getVersionRevision() 524 { 525 return getDescriptor("sys:versionRevision"); 526 } 527 528 531 public String getVersionLabel() 532 { 533 return getDescriptor("sys:versionLabel"); 534 } 535 536 539 public String getVersion() 540 { 541 String version = getVersionMajor() + "." + getVersionMinor() + "." + getVersionRevision(); 542 String label = getVersionLabel(); 543 if (label != null && label.length() > 0) 544 { 545 version += " (" + label + ")"; 546 } 547 return version; 548 } 549 550 553 public String getEdition() 554 { 555 return null; 556 } 557 558 562 public int getSchema() 563 { 564 String schemaStr = getDescriptor("sys:versionSchema"); 565 if (schemaStr == null) 566 { 567 return 0; 568 } 569 try 570 { 571 int schema = Integer.parseInt(schemaStr); 572 if (schema < 0) 573 { 574 throw new NumberFormatException (); 575 } 576 return schema; 577 } 578 catch (NumberFormatException e) 579 { 580 throw new AlfrescoRuntimeException("'version.schema' must be a positive integer"); 581 } 582 } 583 584 587 public String [] getDescriptorKeys() 588 { 589 String [] keys = new String [properties.size()]; 590 properties.keySet().toArray(keys); 591 return keys; 592 } 593 594 597 public String getDescriptor(String key) 598 { 599 String strValue = null; 600 QName qname = QName.createQName(key, namespaceService); 601 Serializable value = properties.get(qname); 602 if (value != null) 603 { 604 strValue = value.toString(); 605 } 606 return strValue; 607 } 608 } 609 610 613 private class ServerDescriptor implements Descriptor 614 { 615 618 public String getVersionMajor() 619 { 620 return serverProperties.getProperty("version.major"); 621 } 622 623 626 public String getVersionMinor() 627 { 628 return serverProperties.getProperty("version.minor"); 629 } 630 631 634 public String getVersionRevision() 635 { 636 return serverProperties.getProperty("version.revision"); 637 } 638 639 642 public String getVersionLabel() 643 { 644 return serverProperties.getProperty("version.label"); 645 } 646 647 650 public String getVersion() 651 { 652 String version = getVersionMajor() + "." + getVersionMinor() + "." + getVersionRevision(); 653 String label = getVersionLabel(); 654 if (label != null && label.length() > 0) 655 { 656 version += " (" + label + ")"; 657 } 658 return version; 659 } 660 661 664 public String getEdition() 665 { 666 return serverProperties.getProperty("version.edition"); 667 } 668 669 673 public int getSchema() 674 { 675 String schemaStr = serverProperties.getProperty("version.schema"); 676 if (schemaStr == null) 677 { 678 return 0; 679 } 680 try 681 { 682 int schema = Integer.parseInt(schemaStr); 683 if (schema < 0) 684 { 685 throw new NumberFormatException (); 686 } 687 return schema; 688 } 689 catch (NumberFormatException e) 690 { 691 throw new AlfrescoRuntimeException("'version.schema' must be a positive integer"); 692 } 693 } 694 695 698 public String [] getDescriptorKeys() 699 { 700 String [] keys = new String [serverProperties.size()]; 701 serverProperties.keySet().toArray(keys); 702 return keys; 703 } 704 705 708 public String getDescriptor(String key) 709 { 710 return serverProperties.getProperty(key, ""); 711 } 712 } 713 714 } 715 | Popular Tags |