1 17 package org.apache.servicemix.jbi.container; 18 19 import java.io.File ; 20 import java.util.Calendar ; 21 import java.util.Collection ; 22 import java.util.EventListener ; 23 import java.util.MissingResourceException ; 24 import java.util.logging.Logger ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.component.Component; 28 import javax.jbi.component.ComponentLifeCycle; 29 import javax.jbi.component.ServiceUnitManager; 30 import javax.jbi.management.DeploymentException; 31 import javax.jbi.management.LifeCycleMBean; 32 import javax.jbi.messaging.ExchangeStatus; 33 import javax.jbi.messaging.MessageExchange; 34 import javax.jbi.messaging.MessagingException; 35 import javax.jbi.servicedesc.ServiceEndpoint; 36 import javax.management.JMException ; 37 import javax.management.MBeanServer ; 38 import javax.management.ObjectName ; 39 import javax.naming.InitialContext ; 40 import javax.naming.NamingException ; 41 import javax.resource.spi.work.WorkManager ; 42 import javax.swing.event.EventListenerList ; 43 import javax.transaction.TransactionManager ; 44 import javax.xml.namespace.QName ; 45 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 import org.apache.geronimo.connector.work.GeronimoWorkManager; 49 import org.apache.servicemix.JbiConstants; 50 import org.apache.servicemix.MessageExchangeListener; 51 import org.apache.servicemix.components.util.ComponentAdaptor; 52 import org.apache.servicemix.components.util.ComponentAdaptorMEListener; 53 import org.apache.servicemix.components.util.ComponentSupport; 54 import org.apache.servicemix.components.util.PojoLifecycleAdaptor; 55 import org.apache.servicemix.components.util.PojoSupport; 56 import org.apache.servicemix.id.IdGenerator; 57 import org.apache.servicemix.jbi.event.ComponentListener; 58 import org.apache.servicemix.jbi.event.EndpointListener; 59 import org.apache.servicemix.jbi.event.ExchangeEvent; 60 import org.apache.servicemix.jbi.event.ExchangeListener; 61 import org.apache.servicemix.jbi.event.ServiceAssemblyListener; 62 import org.apache.servicemix.jbi.event.ServiceUnitListener; 63 import org.apache.servicemix.jbi.framework.AdminCommandsService; 64 import org.apache.servicemix.jbi.framework.AutoDeploymentService; 65 import org.apache.servicemix.jbi.framework.ClientFactory; 66 import org.apache.servicemix.jbi.framework.ComponentContextImpl; 67 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; 68 import org.apache.servicemix.jbi.framework.ComponentNameSpace; 69 import org.apache.servicemix.jbi.framework.DeploymentService; 70 import org.apache.servicemix.jbi.framework.InstallationService; 71 import org.apache.servicemix.jbi.framework.Registry; 72 import org.apache.servicemix.jbi.management.BaseLifeCycle; 73 import org.apache.servicemix.jbi.management.ManagementContext; 74 import org.apache.servicemix.jbi.messaging.MessageExchangeImpl; 75 import org.apache.servicemix.jbi.nmr.Broker; 76 import org.apache.servicemix.jbi.nmr.DefaultBroker; 77 import org.apache.servicemix.jbi.nmr.flow.Flow; 78 import org.jencks.factory.WorkManagerFactoryBean; 79 import org.w3c.dom.DocumentFragment ; 80 81 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; 82 83 88 public class JBIContainer extends BaseLifeCycle { 89 92 public static final String DEFAULT_NAME = "ServiceMix"; 93 private static final Log log = LogFactory.getLog(JBIContainer.class); 94 private String name = DEFAULT_NAME; 95 private InitialContext namingContext; 96 private MBeanServer mbeanServer; 97 private TransactionManager transactionManager; 98 private String rootDir = "." + File.separator + "rootDir"; 99 private AtomicBoolean started = new AtomicBoolean(false); 100 private AtomicBoolean containerInitialized = new AtomicBoolean(false); 101 private IdGenerator idGenerator = new IdGenerator(); 102 protected Broker broker = new DefaultBroker(); 103 protected ServiceUnitManager serviceManager; 104 protected ManagementContext managementContext = new ManagementContext(); 105 protected EnvironmentContext environmentContext = new EnvironmentContext(); 106 protected InstallationService installationService = new InstallationService(); 107 protected DeploymentService deploymentService = new DeploymentService(); 108 protected AutoDeploymentService autoDeployService = new AutoDeploymentService(); 109 protected AdminCommandsService adminCommandsService = new AdminCommandsService(); 110 protected ClientFactory clientFactory = new ClientFactory(); 111 protected Registry registry = new Registry(); 112 protected WorkManager workManager; 113 protected boolean isWorkManagerCreated; 114 protected boolean autoEnlistInTransaction = false; 115 protected boolean persistent = false; 116 protected boolean embedded = false; 117 protected boolean notifyStatistics = false; 118 protected EventListenerList listeners = new EventListenerList (); 119 protected EventListener [] configuredListeners; 120 protected boolean useShutdownHook = true; 121 protected transient Thread shutdownHook; 122 123 126 public JBIContainer() { 127 } 128 129 132 public String getName() { 133 return name; 134 } 135 136 139 public void setName(String name) { 140 this.name = name; 141 } 142 143 148 public String getDescription() { 149 return "ServiceMix JBI Container"; 150 } 151 152 155 public String getFlowName() { 156 String flowNames = getDefaultBroker().getFlowNames(); 157 if (flowNames == null) { 158 return null; 159 } 160 String [] flows = flowNames.split(","); 161 if (flows.length > 1) { 162 throw new IllegalStateException ("Multiple flows have been defined"); 163 } 164 return flows[0]; 165 } 166 167 170 public void setFlowName(String flowName) { 171 getDefaultBroker().setFlowNames(flowName); 172 } 173 174 177 public String getFlowNames() { 178 return getDefaultBroker().getFlowNames(); 179 } 180 181 184 public void setFlowNames(String flowNames) { 185 getDefaultBroker().setFlowNames(flowNames); 186 } 187 188 191 public String getSubscriptionFlowName() { 192 return getDefaultBroker().getSubscriptionFlowName(); 193 } 194 195 199 public void setSubscriptionFlowName(String subscriptionFlowName) { 200 getDefaultBroker().setSubscriptionFlowName(subscriptionFlowName); 201 } 202 203 208 public void setFlow(Flow flow) { 209 getDefaultBroker().setFlows(new Flow[] { flow }); 210 } 211 212 215 public Flow getFlow() { 216 Flow[] flows = getDefaultBroker().getFlows(); 217 if (flows == null || flows.length == 0) { 218 return null; 219 } else if (flows.length > 1) { 220 throw new IllegalStateException ("Multiple flows have been defined"); 221 } else { 222 return flows[0]; 223 } 224 } 225 226 231 public void setFlows(Flow[] flows) { 232 getDefaultBroker().setFlows(flows); 233 } 234 235 238 public Flow[] getFlows() { 239 return getDefaultBroker().getFlows(); 240 } 241 242 public boolean isUseShutdownHook() { 243 return useShutdownHook; 244 } 245 246 251 public void setUseShutdownHook(boolean useShutdownHook) { 252 this.useShutdownHook = useShutdownHook; 253 } 254 255 260 public ManagementContext getManagementContext() { 261 return managementContext; 262 } 263 264 267 public EnvironmentContext getEnvironmentContext() { 268 return environmentContext; 269 } 270 271 274 public Registry getRegistry() { 275 return registry; 276 } 277 278 281 public DefaultBroker getDefaultBroker() { 282 if (broker == null || 283 broker instanceof DefaultBroker == false) { 284 throw new IllegalStateException ("Broker is not a DefaultBroker"); 285 } 286 return (DefaultBroker) broker; 287 } 288 289 292 public Broker getBroker(){ 293 return broker; 294 } 295 296 299 public void setBroker(Broker broker) { 300 this.broker = broker; 301 } 302 303 306 public boolean isCreateMBeanServer() { 307 return managementContext.isCreateMBeanServer(); 308 } 309 310 315 public void setCreateMBeanServer(boolean enableJMX) { 316 managementContext.setCreateMBeanServer(enableJMX); 317 } 318 319 322 public boolean isUseMBeanServer() { 323 return managementContext.isUseMBeanServer(); 324 } 325 326 329 public void setUseMBeanServer(boolean useMBeanServer) { 330 managementContext.setUseMBeanServer(useMBeanServer); 331 } 332 333 336 public boolean isCreateJmxConnector() { 337 return managementContext.isCreateJmxConnector(); 338 } 339 340 343 public void setCreateJmxConnector(boolean createJmxConnector) { 344 managementContext.setCreateJmxConnector(createJmxConnector); 345 } 346 347 350 public boolean isMonitorInstallationDirectory() { 351 return autoDeployService.isMonitorInstallationDirectory(); 352 } 353 354 357 public void setMonitorInstallationDirectory(boolean monitorInstallationDirectory) { 358 autoDeployService.setMonitorInstallationDirectory(monitorInstallationDirectory); 359 } 360 361 364 public boolean isMonitorDeploymentDirectory(){ 365 return autoDeployService.isMonitorDeploymentDirectory(); 366 } 367 368 371 public void setMonitorDeploymentDirectory(boolean monitorDeploymentDirectory){ 372 autoDeployService.setMonitorDeploymentDirectory(monitorDeploymentDirectory); 373 } 374 375 378 public String getInstallationDirPath() { 379 File dir = environmentContext.getInstallationDir(); 380 return dir != null ? dir.getAbsolutePath() : ""; 381 } 382 383 388 public void setInstallationDirPath(String installationDir) { 389 if (installationDir != null && installationDir.length() > 0) { 390 environmentContext.setInstallationDir(new File (installationDir)); 391 } 392 } 393 394 397 public String getDeploymentDirPath() { 398 File dir = environmentContext.getDeploymentDir(); 399 return dir != null ? dir.getAbsolutePath() : ""; 400 } 401 402 405 public void setDeploymentDirPath(String deploymentDir) { 406 if (deploymentDir != null && deploymentDir.length() > 0) { 407 environmentContext.setDeploymentDir(new File (deploymentDir)); 408 } 409 } 410 411 414 public int getStatsInterval() { 415 return environmentContext.getStatsInterval(); 416 } 417 418 421 public void setStatsInterval(int statsInterval) { 422 environmentContext.setStatsInterval(statsInterval); 423 } 424 425 428 public int getMonitorInterval() { 429 return autoDeployService.getMonitorInterval(); 430 } 431 432 435 public void setMonitorInterval(int monitorInterval) { 436 autoDeployService.setMonitorInterval(monitorInterval); 437 } 438 439 442 public boolean isDumpStats() { 443 return environmentContext.isDumpStats(); 444 } 445 446 449 public void setDumpStats(boolean value) { 450 environmentContext.setDumpStats(value); 451 } 452 453 459 public void installArchive(String url) throws DeploymentException { 460 installationService.install(url, null, true); 461 } 462 463 470 public void updateExternalArchive(String location,boolean autoStart) throws DeploymentException { 471 autoDeployService.updateExternalArchive(location, autoStart); 472 } 473 474 480 public void updateExternalArchive(String location) throws DeploymentException { 481 updateExternalArchive(location, true); 482 } 483 484 485 486 487 488 491 public DeploymentService getDeploymentService() { 492 return deploymentService; 493 } 494 495 498 public InstallationService getInstallationService() { 499 return installationService; 500 } 501 502 505 public AutoDeploymentService getAutoDeploymentService() { 506 return autoDeployService; 507 } 508 509 513 public AdminCommandsService getAdminCommandsService() { 514 return adminCommandsService; 515 } 516 517 public ClientFactory getClientFactory() { 518 return clientFactory; 519 } 520 521 522 527 public void init() throws JBIException { 528 if (containerInitialized.compareAndSet(false, true)) { 529 log.info("ServiceMix " + 530 EnvironmentContext.getVersion() + 531 " JBI Container (" + getName() + ") is starting"); 532 log.info("For help or more informations please see: http://incubator.apache.org/servicemix/"); 533 addShutdownHook(); 534 if (this.workManager == null) { 535 this.workManager = createWorkManager(); 536 this.isWorkManagerCreated = true; 537 } 538 if (this.namingContext == null) { 539 try { 540 this.namingContext = new InitialContext (); 541 } 542 catch (NamingException e) { 543 if (log.isDebugEnabled()) { 545 log.warn("Failed to set InitialContext", e); 546 } else { 547 log.warn("Failed to set InitialContext"); 548 } 549 } 550 } 551 managementContext.init(this, getMBeanServer()); 552 mbeanServer = this.managementContext.getMBeanServer(); environmentContext.init(this, rootDir); 554 clientFactory.init(this); 555 registry.init(this); 556 broker.init(this); 557 installationService.init(this); 558 deploymentService.init(this); 559 autoDeployService.init(this); 560 adminCommandsService.init(this); 561 562 try { 564 managementContext.registerMBean(ManagementContext.getContainerObjectName(managementContext.getJmxDomainName(), getName()), 565 this, LifeCycleMBean.class); 566 } catch (JMException e) { 567 throw new JBIException(e); 568 } 569 570 if (configuredListeners != null) { 573 for (int i = 0; i < configuredListeners.length; i++) { 574 EventListener listener = configuredListeners[i]; 575 addListener(listener); 576 } 577 } 578 } 579 } 580 581 586 public void start() throws JBIException { 587 checkInitialized(); 588 if (started.compareAndSet(false, true)) { 589 managementContext.start(); 590 environmentContext.start(); 591 clientFactory.start(); 592 registry.start(); 593 broker.start(); 594 installationService.start(); 595 deploymentService.start(); 596 autoDeployService.start(); 597 adminCommandsService.start(); 598 super.start(); 599 log.info("ServiceMix JBI Container (" + getName() + ") started"); 600 } 601 } 602 603 608 public void stop() throws JBIException { 609 checkInitialized(); 610 if (started.compareAndSet(true, false)) { 611 log.info("ServiceMix JBI Container (" + getName() + ") stopping"); 612 adminCommandsService.stop(); 613 autoDeployService.stop(); 614 deploymentService.stop(); 615 installationService.stop(); 616 registry.stop(); 617 broker.stop(); 618 clientFactory.stop(); 619 environmentContext.stop(); 620 managementContext.stop(); 621 super.stop(); 622 } 623 } 624 625 630 public void shutDown() throws JBIException { 631 if (containerInitialized.compareAndSet(true, false)) { 632 removeShutdownHook(); 633 adminCommandsService.shutDown(); 634 autoDeployService.shutDown(); 635 deploymentService.shutDown(); 636 installationService.shutDown(); 637 broker.shutDown(); 640 registry.shutDown(); 641 clientFactory.shutDown(); 642 environmentContext.shutDown(); 643 super.shutDown(); 645 managementContext.unregisterMBean(this); 646 managementContext.shutDown(); 647 if (isWorkManagerCreated && workManager instanceof GeronimoWorkManager) { 648 try { 649 ((GeronimoWorkManager) workManager).doStop(); 650 } catch (Exception e) { 651 throw new JBIException("Could not stop workManager", e); 652 } 653 } 654 log.info("ServiceMix JBI Container (" + getName() + ") stopped"); 655 } 656 } 657 658 659 protected void addShutdownHook() { 660 if (useShutdownHook) { 661 shutdownHook = new Thread ("ServiceMix ShutdownHook") { 662 public void run() { 663 containerShutdown(); 664 } 665 }; 666 Runtime.getRuntime().addShutdownHook(shutdownHook); 667 } 668 } 669 670 protected void removeShutdownHook() { 671 if (shutdownHook != null) { 672 try { 673 Runtime.getRuntime().removeShutdownHook(shutdownHook); 674 } 675 catch (Exception e) { 676 log.debug("Caught exception, must be shutting down: " + e); 677 } 678 } 679 } 680 681 684 protected void containerShutdown() { 685 try { 686 shutDown(); 687 } 688 catch (Exception e) { 689 System.err.println("Failed to shut down: " + e); 690 } 691 } 692 693 696 public synchronized MBeanServer getMBeanServer() { 697 return mbeanServer; 698 } 699 700 705 public synchronized void setMBeanServer(MBeanServer mbs) { 706 this.mbeanServer = mbs; 707 } 708 709 712 public synchronized InitialContext getNamingContext() { 713 return namingContext; 714 } 715 716 721 public synchronized void setNamingContext(InitialContext ic) { 722 this.namingContext = ic; 723 } 724 725 728 public synchronized Object getTransactionManager() { 729 if (transactionManager == null && namingContext != null) { 730 try { 731 transactionManager = (TransactionManager ) namingContext.lookup("java:appserver/TransactionManager"); 732 } 733 catch (NamingException e) { 734 log.debug("No transaction manager found from naming context: " + e.getMessage()); 735 try { 736 transactionManager = (TransactionManager ) namingContext.lookup("javax.transaction.TransactionManager"); 737 } 738 catch (NamingException e1) { 739 log.debug("No transaction manager found from naming context: " + e.getMessage()); 740 } 741 } 742 } 743 return transactionManager; 744 } 745 746 751 public synchronized void setTransactionManager(Object tm) { 752 this.transactionManager = (TransactionManager ) tm; 753 } 754 755 758 public synchronized String getRootDir() { 759 return this.rootDir; 760 } 761 762 767 public synchronized void setRootDir(String root) { 768 this.rootDir = root; 769 } 770 771 774 public WorkManager getWorkManager() { 775 return workManager; 776 } 777 778 781 public void setWorkManager(WorkManager workManager) { 782 this.workManager = workManager; 783 } 784 785 791 public void sendExchange(MessageExchangeImpl exchange) throws MessagingException { 792 try { 793 broker.sendExchangePacket(exchange); 794 } 795 catch (MessagingException e) { 796 throw e; 797 } 798 catch (JBIException e) { 799 throw new MessagingException(e); 800 } 801 } 802 803 804 809 public void registerExternalEndpoint(ComponentNameSpace cns, ServiceEndpoint externalEndpoint) 810 throws JBIException { 811 registry.registerExternalEndpoint(cns, externalEndpoint); 812 } 813 814 819 public void deregisterExternalEndpoint(ComponentNameSpace cns, ServiceEndpoint externalEndpoint) 820 throws JBIException { 821 registry.deregisterExternalEndpoint(cns, externalEndpoint); 822 } 823 824 829 public ServiceEndpoint resolveEndpointReference(ComponentContextImpl context, DocumentFragment epr) { 830 return registry.resolveEndpointReference(epr); 831 } 832 833 839 public ServiceEndpoint getEndpoint(ComponentContextImpl context, QName service, String name) { 840 return registry.getEndpoint(service, name); 841 } 842 843 848 public ServiceEndpoint[] getEndpoints(ComponentContextImpl context, QName interfaceName) { 849 return registry.getEndpointsForInterface(interfaceName); 850 } 851 852 857 public ServiceEndpoint[] getEndpointsForService(ComponentContextImpl context, QName serviceName) { 858 return registry.getEndpointsForService(serviceName); 859 } 860 861 866 public ServiceEndpoint[] getExternalEndpoints(ComponentContextImpl context, QName interfaceName) { 867 return registry.getExternalEndpoints(interfaceName); 868 } 869 870 875 public ServiceEndpoint[] getExternalEndpointsForService(ComponentContextImpl context, QName serviceName) { 876 return registry.getExternalEndpointsForService(serviceName); 877 } 878 879 886 public Logger getLogger(String suffix, String resourceBundleName) throws MissingResourceException , JBIException { 887 try { 888 return Logger.getLogger(suffix, resourceBundleName); 889 } catch (IllegalArgumentException e) { 890 throw new JBIException("A logger can not be created using resource bundle " + resourceBundleName); 891 } 892 } 893 894 900 public void deactivateComponent(String name) throws JBIException { 901 ComponentMBeanImpl component = registry.getComponent(name); 902 if (component != null) { 903 component.doShutDown(); 904 component.unregisterMbeans(managementContext); 905 registry.deregisterComponent(component); 906 environmentContext.unreregister(component); 907 log.info("Deactivating component " + name); 908 } 909 else { 910 throw new JBIException("Could not find component " + name); 911 } 912 } 913 914 920 public void deleteComponent(String id) throws JBIException { 921 deactivateComponent(id); 922 environmentContext.removeComponentRootDirectory(id); 923 } 924 925 931 public ComponentMBeanImpl getComponent(String componentName) { 932 return registry.getComponent(componentName); 933 } 934 935 938 public Collection getLocalComponentConnectors() { 939 return registry.getComponents(); 940 } 941 942 949 public Component activateComponent(ActivationSpec activationSpec) throws JBIException { 950 if (activationSpec.getId() == null) { 951 if (activationSpec.getComponentName() == null) { 952 activationSpec.setId(createComponentID()); 954 } else { 955 activationSpec.setId(activationSpec.getComponentName()); 956 } 957 } 958 String id = activationSpec.getId(); 959 if (id == null) { 960 throw new IllegalArgumentException ("A Registration must have an ID"); 961 } 962 if (activationSpec.getEndpoint() == null && activationSpec.getService() != null) { 963 activationSpec.setEndpoint(id); 965 } 966 if (activationSpec.getComponentName() == null) { 967 activationSpec.setComponentName(id); 968 } 969 Object bean = activationSpec.getComponent(); 970 if (bean == null) { 971 throw new IllegalArgumentException ("A Registration must have a component associated with it"); 972 } 973 if (bean instanceof Component) { 974 Component component = (Component) bean; 975 if (component instanceof ComponentSupport) { 976 defaultComponentServiceAndEndpoint((ComponentSupport) component, activationSpec); 977 } 978 activateComponent(component, activationSpec); 979 return component; 980 } 981 else if (bean instanceof ComponentLifeCycle) { 982 ComponentLifeCycle lifeCycle = (ComponentLifeCycle) bean; 984 if (bean instanceof PojoSupport) { 985 defaultComponentServiceAndEndpoint((PojoSupport) bean, activationSpec); 986 } 987 Component adaptor = createComponentAdaptor(lifeCycle, activationSpec); 988 activateComponent(adaptor, activationSpec); 989 return adaptor; 990 } 991 else if (bean instanceof MessageExchangeListener) { 992 MessageExchangeListener listener = (MessageExchangeListener) bean; 994 Component adaptor = createComponentAdaptor(listener, activationSpec); 995 activateComponent(adaptor, activationSpec); 996 return adaptor; 997 } 998 else { 999 throw new IllegalArgumentException ("Component name: " + id 1000 + " is bound to an object which is not a JBI component, it is of type: " 1001 + bean.getClass().getName()); 1002 } 1003 } 1004 1005 1013 public ObjectName activateComponent(Component component, String name) throws JBIException { 1014 ActivationSpec activationSpec = new ActivationSpec(); 1015 ComponentNameSpace cns = new ComponentNameSpace(getName(), name); 1016 activationSpec.setComponent(component); 1017 activationSpec.setComponentName(cns.getName()); 1018 return activateComponent(component, activationSpec); 1019 } 1020 1021 1029 public ObjectName activateComponent(Component component, ActivationSpec activationSpec) throws JBIException { 1030 return activateComponent(component, "POJO Component", activationSpec, true, false, false, null); 1031 } 1032 1033 1045 public ObjectName activateComponent(File installDir, Component component, String description, 1046 ComponentContextImpl context, boolean binding, boolean service, 1047 String [] sharedLibraries) throws JBIException { 1048 ComponentNameSpace cns = context.getComponentNameSpace(); 1049 ActivationSpec activationSpec = new ActivationSpec(); 1050 activationSpec.setComponent(component); 1051 activationSpec.setComponentName(cns.getName()); 1052 return activateComponent(installDir, component, description, context, activationSpec, false, binding, service, sharedLibraries); 1053 } 1054 1055 1065 public ObjectName activateComponent(Component component, String description, ActivationSpec activationSpec, 1066 boolean pojo, boolean binding, boolean service, 1067 String [] sharedLibraries) throws JBIException { 1068 ComponentNameSpace cns = new ComponentNameSpace(getName(), activationSpec.getComponentName()); 1069 if (registry.getComponent(cns) != null) { 1070 throw new JBIException("A component is already registered for " + cns); 1071 } 1072 ComponentContextImpl context = new ComponentContextImpl(this, cns); 1073 return activateComponent(new File ("."), component, description, context, activationSpec, pojo, binding, service, sharedLibraries); 1074 } 1075 1076 1088 public ObjectName activateComponent(File installationDir, Component component, String description, 1089 ComponentContextImpl context, ActivationSpec activationSpec, 1090 boolean pojo, boolean binding, boolean service, 1091 String [] sharedLibraries) 1092 throws JBIException { 1093 ObjectName result = null; 1094 ComponentNameSpace cns = new ComponentNameSpace(getName(), activationSpec.getComponentName()); 1095 if (log.isDebugEnabled()) { 1096 log.info("Activating component for: " + cns + " with service: " + activationSpec.getService() + " component: " 1097 + component); 1098 } 1099 ComponentMBeanImpl lcc = registry.registerComponent(cns, description, component, binding, service, sharedLibraries); 1100 if (lcc != null) { 1101 lcc.setPojo(pojo); 1102 ComponentEnvironment env = environmentContext.registerComponent(context.getEnvironment(),lcc); 1103 if (env.getInstallRoot() == null) { 1104 env.setInstallRoot(installationDir); 1105 } 1106 context.activate(component, env, activationSpec); 1107 lcc.setContext(context); 1108 lcc.setActivationSpec(activationSpec); 1109 1110 if (lcc.isPojo()) { 1111 lcc.init(); 1114 } else { 1115 lcc.doShutDown(); 1116 } 1117 result = lcc.registerMBeans(managementContext); 1118 if (lcc.isPojo() && started.get()) { 1121 lcc.start(); 1122 } 1123 } 1124 return result; 1125 } 1126 1127 1134 protected void defaultComponentServiceAndEndpoint(PojoSupport component, ActivationSpec activationSpec) { 1135 if (activationSpec.getService() != null) { 1136 component.setService(activationSpec.getService()); 1137 } 1138 if (activationSpec.getEndpoint() != null) { 1139 component.setEndpoint(activationSpec.getEndpoint()); 1140 } 1141 } 1142 1143 1148 protected WorkManager createWorkManager() throws JBIException { 1149 WorkManagerFactoryBean factory = new WorkManagerFactoryBean(); 1150 try { 1151 return factory.getWorkManager(); 1152 } 1153 catch (Exception e) { 1154 throw new JBIException("Failed to start WorkManager: " + e, e); 1155 } 1156 } 1157 1158 1159 1166 protected Component createComponentAdaptor(ComponentLifeCycle lifeCycle, ActivationSpec activationSpec) { 1167 ComponentAdaptor answer = null; 1168 if (lifeCycle instanceof MessageExchangeListener) { 1169 answer = new ComponentAdaptorMEListener(lifeCycle, activationSpec.getService(), activationSpec 1170 .getEndpoint(), (MessageExchangeListener) lifeCycle); 1171 } 1172 else { 1173 answer = new ComponentAdaptor(lifeCycle, activationSpec.getService(), activationSpec.getEndpoint()); 1174 } 1175 answer.setServiceManager(serviceManager); 1176 return answer; 1177 } 1178 1179 protected Component createComponentAdaptor(MessageExchangeListener listener, ActivationSpec activationSpec) { 1180 ComponentLifeCycle lifecCycle = new PojoLifecycleAdaptor(listener, activationSpec.getService(), activationSpec 1181 .getEndpoint()); 1182 return new ComponentAdaptorMEListener(lifecCycle, listener); 1183 } 1184 1185 1190 protected String createComponentID() { 1191 return idGenerator.generateId(); 1192 } 1193 1194 protected void checkInitialized() throws JBIException { 1195 if (containerInitialized.get() == false) { 1196 throw new JBIException("The Container is not initialized - please call init(...)"); 1197 } 1198 } 1199 1200 1204 public boolean isAutoEnlistInTransaction() { 1205 return autoEnlistInTransaction; 1206 } 1207 1208 1216 public void setAutoEnlistInTransaction(boolean autoEnlistInTransaction) { 1217 this.autoEnlistInTransaction = autoEnlistInTransaction; 1218 } 1219 1220 public boolean isPersistent() { 1221 return persistent; 1222 } 1223 1224 1231 public void setPersistent(boolean persistent) { 1232 this.persistent = persistent; 1233 } 1234 1235 public void addListener(EventListener listener) { 1236 if (listener instanceof ContainerAware) { 1237 ContainerAware containerAware = (ContainerAware) listener; 1238 containerAware.setContainer(this); 1239 } 1240 if (listener instanceof ExchangeListener) { 1241 listeners.add(ExchangeListener.class, (ExchangeListener) listener); 1242 } 1243 if (listener instanceof ComponentListener) { 1244 listeners.add(ComponentListener.class, (ComponentListener) listener); 1245 } 1246 if (listener instanceof ServiceAssemblyListener) { 1247 listeners.add(ServiceAssemblyListener.class, (ServiceAssemblyListener) listener); 1248 } 1249 if (listener instanceof ServiceUnitListener) { 1250 listeners.add(ServiceUnitListener.class, (ServiceUnitListener) listener); 1251 } 1252 if (listener instanceof EndpointListener) { 1253 listeners.add(EndpointListener.class, (EndpointListener) listener); 1254 } 1255 } 1256 1257 public void removeListener(EventListener listener) { 1258 if (listener instanceof ExchangeListener) { 1259 listeners.remove(ExchangeListener.class, (ExchangeListener) listener); 1260 } 1261 if (listener instanceof ComponentListener) { 1262 listeners.remove(ComponentListener.class, (ComponentListener) listener); 1263 } 1264 if (listener instanceof ServiceAssemblyListener) { 1265 listeners.remove(ServiceAssemblyListener.class, (ServiceAssemblyListener) listener); 1266 } 1267 if (listener instanceof ServiceUnitListener) { 1268 listeners.remove(ServiceUnitListener.class, (ServiceUnitListener) listener); 1269 } 1270 if (listener instanceof EndpointListener) { 1271 listeners.remove(EndpointListener.class, (EndpointListener) listener); 1272 } 1273 } 1274 1275 public Object [] getListeners(Class lc) { 1276 return listeners.getListeners(lc); 1277 } 1278 1279 public void setListeners(EventListener [] listeners) { 1280 configuredListeners = listeners; 1281 } 1282 1283 public void callListeners(MessageExchange exchange) { 1284 ExchangeListener[] l = (ExchangeListener[]) listeners.getListeners(ExchangeListener.class); 1285 ExchangeEvent event = new ExchangeEvent(exchange); 1286 for (int i = 0; i < l.length; i++) { 1287 try { 1288 l[i].exchangeSent(event); 1289 } catch (Exception e) { 1290 log.warn("Error calling listener: " + e.getMessage(), e); 1291 } 1292 } 1293 } 1294 1295 public void resendExchange(MessageExchange exchange) throws JBIException { 1296 if (exchange instanceof MessageExchangeImpl == false) { 1297 throw new IllegalArgumentException ("exchange should be a MessageExchangeImpl"); 1298 } 1299 MessageExchangeImpl me = (MessageExchangeImpl) exchange; 1300 me.getPacket().setExchangeId(new IdGenerator().generateId()); 1301 me.getPacket().setOut(null); 1302 me.getPacket().setFault(null); 1303 me.getPacket().setError(null); 1304 me.getPacket().setStatus(ExchangeStatus.ACTIVE); 1305 me.getPacket().setProperty(JbiConstants.DATESTAMP_PROPERTY_NAME, Calendar.getInstance()); 1306 callListeners(me); 1307 me.handleSend(false); 1308 sendExchange(me.getMirror()); 1309 } 1310 1311 public boolean isEmbedded() { 1312 return embedded; 1313 } 1314 1315 public void setEmbedded(boolean embedded) { 1316 this.embedded = embedded; 1317 } 1318 1319 public void setRmiPort( int portNum ) { 1320 getManagementContext().setNamingPort( portNum ); 1321 } 1322 1323 public int getRmiPort() { 1324 return getManagementContext().getNamingPort(); 1325 } 1326 1327 1330 public boolean isNotifyStatistics() { 1331 return notifyStatistics; 1332 } 1333 1334 1337 public void setNotifyStatistics(boolean notifyStatistics) { 1338 this.notifyStatistics = notifyStatistics; 1339 } 1340} 1341 | Popular Tags |