001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.wsn.component;
018
019 import java.util.Properties;
020
021 import javax.jms.ConnectionFactory;
022 import javax.naming.Context;
023 import javax.naming.InitialContext;
024 import javax.naming.NamingException;
025
026 import org.apache.servicemix.common.BaseComponent;
027 import org.apache.servicemix.common.BaseLifeCycle;
028 import org.apache.servicemix.common.ServiceUnit;
029 import org.apache.servicemix.wsn.EndpointManager;
030 import org.apache.servicemix.wsn.EndpointRegistrationException;
031 import org.apache.servicemix.wsn.jbi.JbiNotificationBroker;
032 import org.apache.servicemix.wsn.jms.JmsCreatePullPoint;
033
034 public class WSNLifeCycle extends BaseLifeCycle {
035
036 private JbiNotificationBroker notificationBroker;
037
038 private JmsCreatePullPoint createPullPoint;
039
040 private WSNConfiguration configuration;
041
042 private ConnectionFactory connectionFactory;
043
044 private ServiceUnit serviceUnit;
045
046 public WSNLifeCycle(BaseComponent component) {
047 super(component);
048 configuration = new WSNConfiguration();
049 serviceUnit = new ServiceUnit();
050 serviceUnit.setComponent(component);
051 }
052
053 protected Object getExtensionMBean() throws Exception {
054 return configuration;
055 }
056
057 @Override
058 protected void doInit() throws Exception {
059 super.doInit();
060 configuration.setRootDir(context.getWorkspaceRoot());
061 configuration.load();
062 // Notification Broker
063 notificationBroker = new JbiNotificationBroker(configuration.getBrokerName());
064 notificationBroker.setLifeCycle(this);
065 notificationBroker.setManager(new WSNEndpointManager());
066 if (connectionFactory == null) {
067 connectionFactory = lookupConnectionFactory();
068 }
069 notificationBroker.setConnectionFactory(connectionFactory);
070 notificationBroker.init();
071 // Create PullPoint
072 createPullPoint = new JmsCreatePullPoint(configuration.getBrokerName());
073 createPullPoint.setManager(new WSNEndpointManager());
074 if (connectionFactory == null) {
075 connectionFactory = lookupConnectionFactory();
076 }
077 createPullPoint.setConnectionFactory(connectionFactory);
078 createPullPoint.init();
079 }
080
081 @Override
082 protected void doShutDown() throws Exception {
083 notificationBroker.destroy();
084 createPullPoint.destroy();
085 super.doShutDown();
086 }
087
088 @Override
089 protected void doStart() throws Exception {
090 super.doStart();
091 }
092
093 @Override
094 protected void doStop() throws Exception {
095 // TODO Auto-generated method stub
096 super.doStop();
097 }
098
099 public WSNConfiguration getConfiguration() {
100 return configuration;
101 }
102
103 public void setConfiguration(WSNConfiguration configuration) {
104 this.configuration = configuration;
105 }
106
107 public ConnectionFactory getConnectionFactory() {
108 return connectionFactory;
109 }
110
111 public void setConnectionFactory(ConnectionFactory connectionFactory) {
112 this.connectionFactory = connectionFactory;
113 }
114
115 protected ConnectionFactory lookupConnectionFactory() throws NamingException {
116 Properties props = new Properties();
117 if (configuration.getInitialContextFactory() != null && configuration.getJndiProviderURL() != null) {
118 props.put(Context.INITIAL_CONTEXT_FACTORY, configuration.getInitialContextFactory());
119 props.put(Context.PROVIDER_URL, configuration.getJndiProviderURL());
120 }
121 InitialContext ctx = new InitialContext(props);
122 return (ConnectionFactory) ctx.lookup(configuration.getJndiConnectionFactoryName());
123 }
124
125 public class WSNEndpointManager implements EndpointManager {
126
127 public Object register(String address, Object service) throws EndpointRegistrationException {
128 try {
129 WSNEndpoint endpoint = new WSNEndpoint(address, service);
130 endpoint.setServiceUnit(serviceUnit);
131 serviceUnit.addEndpoint(endpoint);
132 component.getRegistry().registerEndpoint(endpoint);
133 endpoint.activate();
134 return endpoint;
135 } catch (Exception e) {
136 throw new EndpointRegistrationException("Unable to activate endpoint", e);
137 }
138 }
139
140 public void unregister(Object endpoint) throws EndpointRegistrationException {
141 try {
142 serviceUnit.getEndpoints().remove(endpoint);
143 component.getRegistry().unregisterEndpoint((WSNEndpoint) endpoint);
144 ((WSNEndpoint) endpoint).deactivate();
145 } catch (Exception e) {
146 throw new EndpointRegistrationException("Unable to activate endpoint", e);
147 }
148 }
149
150 }
151
152 public JbiNotificationBroker getNotificationBroker() {
153 return notificationBroker;
154 }
155
156 public JmsCreatePullPoint getCreatePullPoint() {
157 return createPullPoint;
158 }
159
160 }