1 17 package org.apache.servicemix.jbi.framework; 18 19 import java.io.File ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.component.Component; 28 import javax.jbi.component.ComponentContext; 29 import javax.jbi.management.DeploymentException; 30 import javax.jbi.servicedesc.ServiceEndpoint; 31 import javax.management.JMException ; 32 import javax.management.ObjectName ; 33 import javax.resource.spi.work.Work ; 34 import javax.resource.spi.work.WorkException ; 35 import javax.xml.namespace.QName ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.apache.servicemix.jbi.container.ActivationSpec; 40 import org.apache.servicemix.jbi.container.EnvironmentContext; 41 import org.apache.servicemix.jbi.container.ServiceAssemblyEnvironment; 42 import org.apache.servicemix.jbi.container.SubscriptionSpec; 43 import org.apache.servicemix.jbi.deployment.ServiceAssembly; 44 import org.apache.servicemix.jbi.deployment.ServiceUnit; 45 import org.apache.servicemix.jbi.management.BaseSystemService; 46 import org.apache.servicemix.jbi.messaging.MessageExchangeImpl; 47 import org.apache.servicemix.jbi.servicedesc.AbstractServiceEndpoint; 48 import org.apache.servicemix.jbi.servicedesc.DynamicEndpoint; 49 import org.apache.servicemix.jbi.servicedesc.InternalEndpoint; 50 import org.apache.servicemix.jbi.util.DOMUtil; 51 import org.w3c.dom.Document ; 52 import org.w3c.dom.DocumentFragment ; 53 import org.w3c.dom.Element ; 54 import org.w3c.dom.Node ; 55 import org.w3c.dom.NodeList ; 56 57 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 58 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; 59 60 65 public class Registry extends BaseSystemService implements RegistryMBean { 66 67 private static final Log log = LogFactory.getLog(Registry.class); 68 private ComponentRegistry componentRegistry; 69 private EndpointRegistry endpointRegistry; 70 private SubscriptionRegistry subscriptionRegistry; 71 private ServiceAssemblyRegistry serviceAssemblyRegistry; 72 private Map sharedLibraries; 73 private Map serviceUnits; 74 private List pendingAssemblies; 75 private List pendingComponents; 76 77 80 public Registry() { 81 this.componentRegistry = new ComponentRegistry(this); 82 this.endpointRegistry = new EndpointRegistry(this); 83 this.subscriptionRegistry = new SubscriptionRegistry(this); 84 this.serviceAssemblyRegistry = new ServiceAssemblyRegistry(this); 85 this.serviceUnits = new ConcurrentHashMap(); 86 this.pendingAssemblies = new CopyOnWriteArrayList(); 87 this.sharedLibraries = new ConcurrentHashMap(); 88 this.pendingComponents = new CopyOnWriteArrayList(); 89 } 90 91 95 public String getDescription(){ 96 return "Registry of Components/SU's and Endpoints"; 97 } 98 99 protected Class getServiceMBean() { 100 return RegistryMBean.class; 101 } 102 103 104 public ComponentRegistry getComponentRegistry() { 105 return componentRegistry; 106 } 107 108 public EndpointRegistry getEndpointRegistry() { 109 return endpointRegistry; 110 } 111 112 117 public void start() throws JBIException { 118 119 componentRegistry.start(); 120 serviceAssemblyRegistry.start(); 121 super.start(); 122 } 123 124 129 public void stop() throws JBIException { 130 serviceAssemblyRegistry.stop(); 131 componentRegistry.stop(); 132 super.stop(); 133 } 134 135 140 public void shutDown() throws JBIException { 141 serviceAssemblyRegistry.shutDown(); 142 componentRegistry.shutDown(); 143 super.shutDown(); 144 container.getManagementContext().unregisterMBean(this); 145 } 146 147 150 protected EnvironmentContext getEnvironmentContext(){ 151 return container.getEnvironmentContext(); 152 } 153 154 157 protected boolean isContainerEmbedded(){ 158 return container.isEmbedded(); 159 } 160 161 protected InternalEndpoint matchEndpointByName(ServiceEndpoint[] endpoints, String endpointName) { 162 InternalEndpoint result = null; 163 if (endpoints != null && endpointName != null && endpointName.length() > 0) { 164 for (int i = 0;i < endpoints.length;i++) { 165 if (endpoints[i].getEndpointName().equals(endpointName)) { 166 result = (InternalEndpoint) endpoints[i]; 167 break; 168 } 169 } 170 } 171 return result; 172 } 173 174 181 public ServiceEndpoint activateEndpoint(ComponentContextImpl context, 182 QName serviceName, 183 String endpointName) throws JBIException { 184 InternalEndpoint result = endpointRegistry.registerInternalEndpoint(context, serviceName, endpointName); 185 return result; 186 } 187 188 public ServiceEndpoint[] getEndpointsForComponent(ComponentNameSpace cns) { 189 return endpointRegistry.getEndpointsForComponent(cns); 190 } 191 192 196 public ServiceEndpoint[] getEndpointsForInterface(QName interfaceName) { 197 return endpointRegistry.getEndpointsForInterface(interfaceName); 198 } 199 200 204 public void deactivateEndpoint(ComponentContext provider, InternalEndpoint serviceEndpoint) { 205 endpointRegistry.unregisterInternalEndpoint(provider, serviceEndpoint); 206 } 207 208 217 public Document getEndpointDescriptor(ServiceEndpoint endpoint) throws JBIException { 218 if (endpoint instanceof AbstractServiceEndpoint == false) { 219 throw new JBIException("Descriptors can not be queried for external endpoints"); 220 } 221 AbstractServiceEndpoint se = (AbstractServiceEndpoint) endpoint; 222 ComponentMBeanImpl component = getComponent(se.getComponentNameSpace()); 224 return component.getComponent().getServiceDescription(endpoint); 225 } 226 227 239 public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) { 240 Collection connectors = getComponents(); 241 for (Iterator iter = connectors.iterator(); iter.hasNext();) { 242 ComponentMBeanImpl connector = (ComponentMBeanImpl) iter.next(); 243 ServiceEndpoint se = connector.getComponent().resolveEndpointReference(epr); 244 if (se != null) { 245 return new DynamicEndpoint(connector.getComponentNameSpace(), se, epr); 246 } 247 } 248 ServiceEndpoint se = resolveInternalEPR(epr); 249 if (se != null) { 250 return se; 251 } 252 return resolveStandardEPR(epr); 253 } 254 255 274 public ServiceEndpoint resolveInternalEPR(DocumentFragment epr) { 275 if (epr == null) { 276 throw new NullPointerException ("resolveInternalEPR(epr) called with null epr."); 277 } 278 NodeList nl = epr.getChildNodes(); 279 for (int i = 0 ; i < nl.getLength(); ++i) { 280 Node n = nl.item(i); 281 if (n.getNodeType() != Node.ELEMENT_NODE) { 282 continue; 283 } 284 Element el = (Element ) n; 285 if (el.getNamespaceURI() == null || 287 !el.getNamespaceURI().equals("http://java.sun.com/jbi/end-point-reference")) 288 { 289 continue; 290 } 291 if (el.getLocalName() == null || !el.getLocalName().equals("end-point-reference")) { 292 continue; 293 } 294 String serviceName = el.getAttributeNS(el.getNamespaceURI(), "service-name"); 295 QName serviceQName = DOMUtil.createQName(el, serviceName); 299 String endpointName = el.getAttributeNS(el.getNamespaceURI(), "end-point-name"); 300 return getInternalEndpoint(serviceQName, endpointName); 301 } 302 return null; 303 } 304 305 340 public ServiceEndpoint resolveStandardEPR(DocumentFragment epr) { 341 try { 342 if (epr.getChildNodes().getLength() == 1) { 343 Node child = epr.getFirstChild(); 344 if (child instanceof Element ) { 345 Element elem = (Element ) child; 346 NodeList nl = elem.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address"); 347 if (nl.getLength() == 1) { 348 Element address = (Element ) nl.item(0); 349 String uri = DOMUtil.getElementText(address); 350 if (uri != null) { 351 uri = uri.trim(); 352 } 353 if (uri.startsWith("endpoint:")) { 354 uri = uri.substring("endpoint:".length()); 355 String [] parts = split(uri); 356 return getInternalEndpoint(new QName (parts[0], parts[1]), parts[2]); 357 } 358 else if (uri.startsWith("service:")) { 359 uri = uri.substring("service:".length()); 360 String [] parts = splitService(uri); 361 return getEndpoint(new QName (parts[0], parts[1]), parts[1]); 362 } 363 } 365 } 366 } 367 } catch (Exception e) { 368 } 370 return null; 371 } 372 373 protected String [] splitService(String uri) { 374 char sep; 375 uri = uri.trim(); 376 if (uri.indexOf('/') > 0) { 377 sep = '/'; 378 } else { 379 sep = ':'; 380 } 381 int idx1 = uri.lastIndexOf(sep); 382 String svcName = uri.substring(idx1 + 1); 383 String nsUri = uri.substring(0, idx1); 384 return new String [] { nsUri, svcName }; 385 } 386 387 protected String [] split(String uri) { 388 char sep; 389 uri = uri.trim(); 390 if (uri.indexOf('/') > 0) { 391 sep = '/'; 392 } else { 393 sep = ':'; 394 } 395 int idx1 = uri.lastIndexOf(sep); 396 int idx2 = uri.lastIndexOf(sep, idx1 - 1); 397 String epName = uri.substring(idx1 + 1); 398 String svcName = uri.substring(idx2 + 1, idx1); 399 String nsUri = uri.substring(0, idx2); 400 return new String [] { nsUri, svcName, epName }; 401 } 402 403 408 public void registerExternalEndpoint(ComponentNameSpace cns, ServiceEndpoint externalEndpoint) throws JBIException { 409 if (externalEndpoint != null) { 410 endpointRegistry.registerExternalEndpoint(cns, externalEndpoint); 411 } 412 } 413 414 418 public void deregisterExternalEndpoint(ComponentNameSpace cns, ServiceEndpoint externalEndpoint) { 419 endpointRegistry.unregisterExternalEndpoint(cns, externalEndpoint); 420 } 421 422 427 public ServiceEndpoint getEndpoint(QName service, String name) { 428 return endpointRegistry.getEndpoint(service, name); 429 } 430 431 public ServiceEndpoint getInternalEndpoint(QName service, String name) { 432 return endpointRegistry.getInternalEndpoint(service, name); 433 } 434 435 439 public ServiceEndpoint[] getEndpointsForService(QName serviceName) { 440 return endpointRegistry.getEndpointsForService(serviceName); 441 } 442 443 447 public ServiceEndpoint[] getExternalEndpoints(QName interfaceName) { 448 return endpointRegistry.getExternalEndpointsForInterface(interfaceName); 449 } 450 451 455 public ServiceEndpoint[] getExternalEndpointsForService(QName serviceName) { 456 return endpointRegistry.getExternalEndpointsForService(serviceName); 457 } 458 459 471 public ComponentMBeanImpl registerComponent(ComponentNameSpace name, 472 String description, 473 Component component, 474 boolean binding, 475 boolean service, 476 String [] sharedLibraries) throws JBIException { 477 return componentRegistry.registerComponent(name,description, component, binding, service, sharedLibraries); 478 } 479 480 484 public void deregisterComponent(ComponentMBeanImpl component) { 485 componentRegistry.deregisterComponent(component); 486 } 487 488 491 public Collection getComponents() { 492 return componentRegistry.getComponents(); 493 } 494 495 500 public ComponentMBeanImpl getComponent(ComponentNameSpace cns) { 501 return componentRegistry.getComponent(cns); 502 } 503 504 509 public ComponentMBeanImpl getComponent(String name) { 510 ComponentNameSpace cns = new ComponentNameSpace(container.getName(), name); 511 return getComponent(cns); 512 } 513 514 518 public ObjectName [] getEngineComponents() { 519 ObjectName [] result = null; 520 List tmpList = new ArrayList (); 521 for (Iterator i = getComponents().iterator(); i.hasNext();){ 522 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next(); 523 if (!lcc.isPojo() && lcc.isService() && lcc.getMBeanName() != null){ 524 tmpList.add(lcc.getMBeanName()); 525 } 526 } 527 result = new ObjectName [tmpList.size()]; 528 tmpList.toArray(result); 529 return result; 530 531 } 532 533 537 public ObjectName [] getBindingComponents() { 538 ObjectName [] result = null; 539 List tmpList = new ArrayList (); 540 for (Iterator i = getComponents().iterator(); i.hasNext();){ 541 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next(); 542 if (!lcc.isPojo() && lcc.isBinding() && lcc.getMBeanName() != null){ 543 tmpList.add(lcc.getMBeanName()); 544 } 545 } 546 result = new ObjectName [tmpList.size()]; 547 tmpList.toArray(result); 548 return result; 549 } 550 551 555 public ObjectName [] getPojoComponents() { 556 ObjectName [] result = null; 557 List tmpList = new ArrayList (); 558 for (Iterator i = getComponents().iterator(); i.hasNext();){ 559 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next(); 560 if (lcc.isPojo() && lcc.getMBeanName() != null){ 561 tmpList.add(lcc.getMBeanName()); 562 } 563 } 564 result = new ObjectName [tmpList.size()]; 565 tmpList.toArray(result); 566 return result; 567 568 } 569 570 575 public void registerSubscriptions(ComponentContextImpl context,ActivationSpec as) { 576 QName service = as.getService(); 577 String endpointName = as.getEndpoint(); 578 InternalEndpoint endpoint = new InternalEndpoint(context.getComponentNameSpace(), endpointName, service); 579 SubscriptionSpec[] specs = as.getSubscriptions(); 580 if (specs != null) { 581 for (int i =0; i<specs.length; i++) { 582 registerSubscription(context, specs[i], endpoint); 583 } 584 } 585 } 586 587 592 public void deregisterSubscriptions(ComponentContextImpl context,ActivationSpec as) { 593 SubscriptionSpec[] specs = as.getSubscriptions(); 594 if (specs != null) { 595 for (int i =0; i<specs.length; i++) { 596 deregisterSubscription(context,specs[i]); 597 } 598 } 599 } 600 601 606 public void registerSubscription(ComponentContextImpl context,SubscriptionSpec subscription, ServiceEndpoint endpoint) { 607 InternalEndpoint sei = (InternalEndpoint)endpoint; 608 subscription.setName(context.getComponentNameSpace()); 609 subscriptionRegistry.registerSubscription(subscription,sei); 610 } 611 612 617 public InternalEndpoint deregisterSubscription(ComponentContextImpl context,SubscriptionSpec subscription) { 618 subscription.setName(context.getComponentNameSpace()); 619 InternalEndpoint result = subscriptionRegistry.deregisterSubscription(subscription); 620 return result; 621 } 622 623 624 628 public List getMatchingSubscriptionEndpoints(MessageExchangeImpl exchange) { 629 return subscriptionRegistry.getMatchingSubscriptionEndpoints(exchange); 630 } 631 632 638 public ServiceAssemblyLifeCycle registerServiceAssembly(ServiceAssembly sa, 639 ServiceAssemblyEnvironment env) throws DeploymentException{ 640 return serviceAssemblyRegistry.register(sa, env); 641 } 642 643 649 public ServiceAssemblyLifeCycle registerServiceAssembly(ServiceAssembly sa, 650 String [] suKeys, 651 ServiceAssemblyEnvironment env) throws DeploymentException{ 652 return serviceAssemblyRegistry.register(sa, suKeys, env); 653 } 654 655 660 public boolean unregisterServiceAssembly(String saName) { 661 return serviceAssemblyRegistry.unregister(saName); 662 } 663 664 669 public ServiceAssemblyLifeCycle getServiceAssembly(String saName){ 670 return serviceAssemblyRegistry.getServiceAssembly(saName); 671 } 672 673 679 public ServiceUnitLifeCycle[] getDeployedServiceUnits(String componentName) { 680 Collection sus = serviceUnits.values(); 681 List tmpList = new ArrayList (); 682 for (Iterator iter = sus.iterator(); iter.hasNext();) { 683 ServiceUnitLifeCycle su = (ServiceUnitLifeCycle) iter.next(); 684 if (su.getComponentName().equals(componentName)) { 685 tmpList.add(su); 686 } 687 } 688 ServiceUnitLifeCycle[] result = new ServiceUnitLifeCycle[tmpList.size()]; 689 tmpList.toArray(result); 690 return result; 691 } 692 693 698 public String [] getDeployedServiceAssemblies() { 699 return serviceAssemblyRegistry.getDeployedServiceAssemblies(); 700 } 701 702 708 public String [] getDeployedServiceAssembliesForComponent(String componentName) { 709 return serviceAssemblyRegistry.getDeployedServiceAssembliesForComponent(componentName); 710 } 711 712 718 public String [] getComponentsForDeployedServiceAssembly(String saName) { 719 return serviceAssemblyRegistry.getComponentsForDeployedServiceAssembly(saName); 720 } 721 722 729 public boolean isSADeployedServiceUnit(String componentName, String suName) { 730 return serviceAssemblyRegistry.isDeployedServiceUnit(componentName, suName); 731 } 732 733 739 public ServiceUnitLifeCycle getServiceUnit(String suKey) { 740 return (ServiceUnitLifeCycle) serviceUnits.get(suKey); 741 } 742 743 750 public String registerServiceUnit(ServiceUnit su, String saName, File suDir) { 751 ServiceUnitLifeCycle sulc = new ServiceUnitLifeCycle( 752 su, 753 saName, 754 this, 755 suDir); 756 this.serviceUnits.put(sulc.getKey(), sulc); 757 try { 758 ObjectName objectName = getContainer().getManagementContext().createObjectName(sulc); 759 getContainer().getManagementContext().registerMBean(objectName, sulc, ServiceUnitMBean.class); 760 } catch (JMException e) { 761 log.error("Could not register MBean for service unit", e); 762 } 763 return sulc.getKey(); 764 } 765 766 771 public void unregisterServiceUnit(String suKey) { 772 ServiceUnitLifeCycle sulc = (ServiceUnitLifeCycle) this.serviceUnits.remove(suKey); 773 if (sulc != null) { 774 try { 775 getContainer().getManagementContext().unregisterMBean(sulc); 776 } catch (JBIException e) { 777 log.error("Could not unregister MBean for service unit", e); 778 } 779 } 780 } 781 782 public void registerSharedLibrary(org.apache.servicemix.jbi.deployment.SharedLibrary sl, 783 File installationDir) { 784 SharedLibrary library = new SharedLibrary(sl, installationDir); 785 this.sharedLibraries.put(library.getName(), library); 786 try { 787 ObjectName objectName = getContainer().getManagementContext().createObjectName(library); 788 getContainer().getManagementContext().registerMBean(objectName, library, SharedLibraryMBean.class); 789 } catch (JMException e) { 790 log.error("Could not register MBean for service unit", e); 791 } 792 checkPendingComponents(); 793 } 794 795 public void unregisterSharedLibrary(String name) { 796 SharedLibrary sl = (SharedLibrary) this.sharedLibraries.remove(name); 799 if (sl != null) { 800 try { 801 getContainer().getManagementContext().unregisterMBean(sl); 802 } catch (JBIException e) { 803 log.error("Could not unregister MBean for shared library", e); 804 } 805 } 806 } 807 808 public SharedLibrary getSharedLibrary(String name) { 809 return (SharedLibrary) sharedLibraries.get(name); 810 } 811 812 public Collection getSharedLibraries() { 813 return sharedLibraries.values(); 814 } 815 816 public void registerEndpointConnection(QName fromSvc, String fromEp, QName toSvc, String toEp, String link) throws JBIException { 817 endpointRegistry.registerEndpointConnection(fromSvc, fromEp, toSvc, toEp, link); 818 } 819 820 public void unregisterEndpointConnection(QName fromSvc, String fromEp) { 821 endpointRegistry.unregisterEndpointConnection(fromSvc, fromEp); 822 } 823 824 public void registerInterfaceConnection(QName fromItf, QName toSvc, String toEp) throws JBIException { 825 endpointRegistry.registerInterfaceConnection(fromItf, toSvc, toEp); 826 } 827 828 public void unregisterInterfaceConnection(QName fromItf) { 829 endpointRegistry.unregisterInterfaceConnection(fromItf); 830 } 831 832 public void registerRemoteEndpoint(ServiceEndpoint endpoint) { 833 endpointRegistry.registerRemoteEndpoint((InternalEndpoint) endpoint); 834 } 835 836 public void unregisterRemoteEndpoint(ServiceEndpoint endpoint) { 837 endpointRegistry.unregisterRemoteEndpoint((InternalEndpoint) endpoint); 838 } 839 840 public void checkPendingAssemblies() { 841 try { 842 getContainer().getWorkManager().scheduleWork(new Work () { 843 public void release() { 844 } 845 public void run() { 846 startPendingAssemblies(); 847 } 848 }); 849 } catch (WorkException e) { 850 log.error("Could not schedule work", e); 851 } 852 } 853 854 public void addPendingAssembly(ServiceAssemblyLifeCycle sa) { 855 if (!pendingAssemblies.contains(sa)) { 856 pendingAssemblies.add(sa); 857 } 858 } 859 860 protected synchronized void startPendingAssemblies() { 861 for (Iterator iter = pendingAssemblies.iterator(); iter.hasNext();) { 862 ServiceAssemblyLifeCycle sa = (ServiceAssemblyLifeCycle) iter.next(); 863 ServiceUnitLifeCycle[] sus = sa.getDeployedSUs(); 864 boolean ok = true; 865 for (int i = 0; i < sus.length; i++) { 866 ComponentMBeanImpl c = getComponent(sus[i].getComponentName()); 867 if (c == null || !c.isStarted()) { 868 ok = false; 869 break; 870 } 871 } 872 if (ok) { 873 try { 874 sa.restore(); 875 pendingAssemblies.remove(sa); 876 } catch (Exception e) { 877 log.error("Error trying to restore service assembly state", e); 878 } 879 } 880 } 881 } 882 883 public void checkPendingComponents() { 884 try { 885 getContainer().getWorkManager().scheduleWork(new Work () { 886 public void release() { 887 } 888 public void run() { 889 startPendingComponents(); 890 } 891 }); 892 } catch (WorkException e) { 893 log.error("Could not schedule work", e); 894 } 895 } 896 897 public void addPendingComponent(ComponentMBeanImpl comp) { 898 if (!pendingComponents.contains(comp)) { 899 pendingComponents.add(comp); 900 } 901 } 902 903 protected synchronized void startPendingComponents() { 904 for (Iterator iter = pendingComponents.iterator(); iter.hasNext();) { 905 ComponentMBeanImpl comp = (ComponentMBeanImpl) iter.next(); 906 } 908 } 909 910 } 911 | Popular Tags |