KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > wsn > component > WSNLifeCycle


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.wsn.component;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.jms.ConnectionFactory JavaDoc;
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingException JavaDoc;
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 JavaDoc 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 JavaDoc getExtensionMBean() throws Exception JavaDoc {
50         return configuration;
51     }
52     
53     @Override JavaDoc
54     protected void doInit() throws Exception JavaDoc {
55         super.doInit();
56         configuration.setRootDir(context.getWorkspaceRoot());
57         configuration.load();
58         // Notification Broker
59
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         // Create PullPoint
68
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 JavaDoc
78     protected void doShutDown() throws Exception JavaDoc {
79         // TODO Auto-generated method stub
80
super.doShutDown();
81     }
82
83     @Override JavaDoc
84     protected void doStart() throws Exception JavaDoc {
85         super.doStart();
86     }
87
88     @Override JavaDoc
89     protected void doStop() throws Exception JavaDoc {
90         // TODO Auto-generated method stub
91
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 JavaDoc getConnectionFactory() {
103         return connectionFactory;
104     }
105
106     public void setConnectionFactory(ConnectionFactory JavaDoc connectionFactory) {
107         this.connectionFactory = connectionFactory;
108     }
109     
110     protected ConnectionFactory JavaDoc lookupConnectionFactory() throws NamingException JavaDoc {
111         Hashtable JavaDoc<String JavaDoc,String JavaDoc> props = new Hashtable JavaDoc<String JavaDoc,String JavaDoc>();
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 JavaDoc ctx = new InitialContext JavaDoc(props);
117         ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) ctx.lookup(configuration.getJndiConnectionFactoryName());
118         return connectionFactory;
119     }
120     
121     public class WSNEndpointManager implements EndpointManager {
122
123         public Object JavaDoc register(String JavaDoc address, Object JavaDoc 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 JavaDoc e) {
132                 throw new EndpointRegistrationException("Unable to activate endpoint", e);
133             }
134         }
135
136         public void unregister(Object JavaDoc endpoint) throws EndpointRegistrationException {
137             try {
138                 serviceUnit.getEndpoints().remove(endpoint);
139                 component.getRegistry().unregisterEndpoint((WSNEndpoint) endpoint);
140                 ((WSNEndpoint) endpoint).deactivate();
141             } catch (Exception JavaDoc 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