1 17 package org.apache.servicemix.wsn.component; 18 19 import java.util.Hashtable ; 20 21 import javax.jms.ConnectionFactory ; 22 import javax.naming.Context ; 23 import javax.naming.InitialContext ; 24 import javax.naming.NamingException ; 25 26 import org.apache.servicemix.common.BaseComponent; 27 import org.apache.servicemix.common.BaseLifeCycle; 28 import org.apache.servicemix.common.ServiceUnit; 29 import org.apache.servicemix.wsn.EndpointManager; 30 import org.apache.servicemix.wsn.EndpointRegistrationException; 31 import org.apache.servicemix.wsn.jbi.JbiNotificationBroker; 32 import org.apache.servicemix.wsn.jms.JmsCreatePullPoint; 33 34 public class WSNLifeCycle extends BaseLifeCycle { 35 36 private JbiNotificationBroker notificationBroker; 37 private JmsCreatePullPoint createPullPoint; 38 private WSNConfiguration configuration; 39 private ConnectionFactory connectionFactory; 40 private ServiceUnit serviceUnit; 41 42 public WSNLifeCycle(BaseComponent component) { 43 super(component); 44 configuration = new WSNConfiguration(); 45 serviceUnit = new ServiceUnit(); 46 serviceUnit.setComponent(component); 47 } 48 49 protected Object getExtensionMBean() throws Exception { 50 return configuration; 51 } 52 53 @Override 54 protected void doInit() throws Exception { 55 super.doInit(); 56 configuration.setRootDir(context.getWorkspaceRoot()); 57 configuration.load(); 58 notificationBroker = new JbiNotificationBroker(configuration.getBrokerName()); 60 notificationBroker.setLifeCycle(this); 61 notificationBroker.setManager(new WSNEndpointManager()); 62 if (connectionFactory == null) { 63 connectionFactory = lookupConnectionFactory(); 64 } 65 notificationBroker.setConnectionFactory(connectionFactory); 66 notificationBroker.init(); 67 createPullPoint = new JmsCreatePullPoint(configuration.getBrokerName()); 69 createPullPoint.setManager(new WSNEndpointManager()); 70 if (connectionFactory == null) { 71 connectionFactory = lookupConnectionFactory(); 72 } 73 createPullPoint.setConnectionFactory(connectionFactory); 74 createPullPoint.init(); 75 } 76 77 @Override 78 protected void doShutDown() throws Exception { 79 super.doShutDown(); 81 } 82 83 @Override 84 protected void doStart() throws Exception { 85 super.doStart(); 86 } 87 88 @Override 89 protected void doStop() throws Exception { 90 super.doStop(); 92 } 93 94 public WSNConfiguration getConfiguration() { 95 return configuration; 96 } 97 98 public void setConfiguration(WSNConfiguration configuration) { 99 this.configuration = configuration; 100 } 101 102 public ConnectionFactory getConnectionFactory() { 103 return connectionFactory; 104 } 105 106 public void setConnectionFactory(ConnectionFactory connectionFactory) { 107 this.connectionFactory = connectionFactory; 108 } 109 110 protected ConnectionFactory lookupConnectionFactory() throws NamingException { 111 Hashtable <String ,String > props = new Hashtable <String ,String >(); 112 if (configuration.getInitialContextFactory() != null && configuration.getJndiProviderURL() != null) { 113 props.put(Context.INITIAL_CONTEXT_FACTORY, configuration.getInitialContextFactory()); 114 props.put(Context.PROVIDER_URL, configuration.getJndiProviderURL()); 115 } 116 InitialContext ctx = new InitialContext (props); 117 ConnectionFactory connectionFactory = (ConnectionFactory ) ctx.lookup(configuration.getJndiConnectionFactoryName()); 118 return connectionFactory; 119 } 120 121 public class WSNEndpointManager implements EndpointManager { 122 123 public Object register(String address, Object service) throws EndpointRegistrationException { 124 try { 125 WSNEndpoint endpoint = new WSNEndpoint(address, service); 126 endpoint.setServiceUnit(serviceUnit); 127 serviceUnit.addEndpoint(endpoint); 128 component.getRegistry().registerEndpoint(endpoint); 129 endpoint.activate(); 130 return endpoint; 131 } catch (Exception e) { 132 throw new EndpointRegistrationException("Unable to activate endpoint", e); 133 } 134 } 135 136 public void unregister(Object endpoint) throws EndpointRegistrationException { 137 try { 138 serviceUnit.getEndpoints().remove(endpoint); 139 component.getRegistry().unregisterEndpoint((WSNEndpoint) endpoint); 140 ((WSNEndpoint) endpoint).deactivate(); 141 } catch (Exception e) { 142 throw new EndpointRegistrationException("Unable to activate endpoint", e); 143 } 144 } 145 146 } 147 148 public JbiNotificationBroker getNotificationBroker() { 149 return notificationBroker; 150 } 151 152 public JmsCreatePullPoint getCreatePullPoint() { 153 return createPullPoint; 154 } 155 156 } 157 | Popular Tags |