KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
20 import java.io.FilenameFilter JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jbi.management.DeploymentException;
28 import javax.jbi.management.LifeCycleMBean;
29 import javax.jbi.messaging.MessageExchange.Role;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.namespace.QName JavaDoc;
33
34 import org.apache.activemq.util.IdGenerator;
35 import org.apache.servicemix.common.AbstractDeployer;
36 import org.apache.servicemix.common.BaseComponent;
37 import org.apache.servicemix.common.Deployer;
38 import org.apache.servicemix.common.Endpoint;
39 import org.apache.servicemix.common.ExchangeProcessor;
40 import org.apache.servicemix.common.ServiceUnit;
41 import org.apache.servicemix.wsn.EndpointManager;
42 import org.apache.servicemix.wsn.EndpointRegistrationException;
43 import org.apache.servicemix.wsn.jaxws.NotificationBroker;
44 import org.apache.servicemix.wsn.jbi.JbiNotificationBroker;
45 import org.apache.servicemix.wsn.jms.JmsCreatePullPoint;
46 import org.oasis_open.docs.wsn.b_2.CreatePullPoint;
47 import org.oasis_open.docs.wsn.b_2.CreatePullPointResponse;
48 import org.oasis_open.docs.wsn.b_2.Subscribe;
49 import org.oasis_open.docs.wsn.b_2.SubscribeResponse;
50 import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
51 import org.oasis_open.docs.wsn.br_2.RegisterPublisherResponse;
52
53 public class WSNDeployer extends AbstractDeployer implements Deployer {
54
55     protected FilenameFilter JavaDoc filter;
56     protected JAXBContext context;
57
58     public WSNDeployer(BaseComponent component) {
59         super(component);
60         filter = new XmlFilter();
61         try {
62             context = WSNEndpoint.createJAXBContext(NotificationBroker.class);
63         } catch (JAXBException e) {
64             throw new RuntimeException JavaDoc("Could not create jaxb context", e);
65         }
66     }
67
68     public boolean canDeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) {
69         File JavaDoc[] xmls = new File JavaDoc(serviceUnitRootPath).listFiles(filter);
70         return xmls != null && xmls.length > 0;
71     }
72
73     public ServiceUnit deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) throws DeploymentException {
74         File JavaDoc[] xmls = new File JavaDoc(serviceUnitRootPath).listFiles(filter);
75         if (xmls == null || xmls.length == 0) {
76             throw failure("deploy", "No wsdl found", null);
77         }
78         WSNServiceUnit su = new WSNServiceUnit();
79         su.setComponent(component);
80         su.setName(serviceUnitName);
81         su.setRootPath(serviceUnitRootPath);
82         for (int i = 0; i < xmls.length; i++) {
83             Endpoint ep;
84             URL JavaDoc url;
85             try {
86                 url = xmls[i].toURL();
87             } catch (MalformedURLException JavaDoc e) {
88                 // TODO Auto-generated catch block
89
throw new DeploymentException("Error deploying xml file", e);
90             }
91             ep = createEndpoint(url);
92             ep.setServiceUnit(su);
93             su.addEndpoint(ep);
94         }
95         if (su.getEndpoints().size() == 0) {
96             throw failure("deploy", "Invalid wsdl: no endpoints found", null);
97         }
98         return su;
99     }
100
101     public Endpoint createEndpoint(URL JavaDoc url) throws DeploymentException {
102         Object JavaDoc request = null;
103         try {
104             request = context.createUnmarshaller().unmarshal(url);
105         } catch (JAXBException e) {
106             throw failure("deploy", "Invalid xml", e);
107         }
108         return createEndpoint(request);
109     }
110
111     public Endpoint createEndpoint(Object JavaDoc request) throws DeploymentException {
112         if (request instanceof Subscribe) {
113             return new WSNSubscriptionEndpoint((Subscribe) request);
114         } else if (request instanceof CreatePullPoint) {
115             return new WSNPullPointEndpoint((CreatePullPoint) request);
116         } else if (request instanceof RegisterPublisher) {
117             return new WSNPublisherEndpoint((RegisterPublisher) request);
118         } else {
119             throw failure("deploy", "Unsupported request " + request.getClass().getName(), null);
120         }
121     }
122
123     public class WSNSubscriptionEndpoint extends Endpoint implements EndpointManager {
124
125         private Subscribe request;
126         private SubscribeResponse response;
127
128         public WSNSubscriptionEndpoint(Subscribe request) throws DeploymentException {
129             this.service = new QName JavaDoc("http://servicemix.org/wsnotification", "Subscription");
130             this.endpoint = new IdGenerator().generateSanitizedId();
131             this.request = request;
132         }
133
134         @Override JavaDoc
135         public Role getRole() {
136             return Role.CONSUMER;
137         }
138
139         @Override JavaDoc
140         public void activate() throws Exception JavaDoc {
141             JbiNotificationBroker broker = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getNotificationBroker();
142             response = broker.handleSubscribe(request, this);
143         }
144
145         @Override JavaDoc
146         public void deactivate() throws Exception JavaDoc {
147             JbiNotificationBroker broker = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getNotificationBroker();
148             broker.unsubscribe(response.getSubscriptionReference().getAddress().getValue());
149         }
150
151         @Override JavaDoc
152         public ExchangeProcessor getProcessor() {
153             return null;
154         }
155
156         public Object JavaDoc register(String JavaDoc address, Object JavaDoc service) throws EndpointRegistrationException {
157             return null;
158         }
159
160         public void unregister(Object JavaDoc endpoint) throws EndpointRegistrationException {
161         }
162
163     }
164
165     public class WSNPullPointEndpoint extends Endpoint implements EndpointManager {
166
167         private CreatePullPoint request;
168         private CreatePullPointResponse response;
169
170         public WSNPullPointEndpoint(CreatePullPoint request) throws DeploymentException {
171             this.service = new QName JavaDoc("http://servicemix.org/wsnotification", "Subscription");
172             this.endpoint = new IdGenerator().generateSanitizedId();
173             this.request = request;
174         }
175
176         @Override JavaDoc
177         public Role getRole() {
178             return Role.PROVIDER;
179         }
180
181         @Override JavaDoc
182         public void activate() throws Exception JavaDoc {
183             JmsCreatePullPoint createPullPoint = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getCreatePullPoint();
184             response = createPullPoint.createPullPoint(request);
185         }
186
187         @Override JavaDoc
188         public void deactivate() throws Exception JavaDoc {
189             JmsCreatePullPoint createPullPoint = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getCreatePullPoint();
190             createPullPoint.destroyPullPoint(response.getPullPoint().getAddress().getValue());
191         }
192
193         @Override JavaDoc
194         public ExchangeProcessor getProcessor() {
195             return null;
196         }
197
198         public Object JavaDoc register(String JavaDoc address, Object JavaDoc service) throws EndpointRegistrationException {
199             return null;
200         }
201
202         public void unregister(Object JavaDoc endpoint) throws EndpointRegistrationException {
203         }
204
205     }
206
207     public static class WSNPublisherEndpoint extends Endpoint implements EndpointManager {
208
209         private RegisterPublisher request;
210         private RegisterPublisherResponse response;
211         
212         public WSNPublisherEndpoint(RegisterPublisher request) {
213             this.service = new QName JavaDoc("http://servicemix.org/wsnotification", "Publisher");
214             this.endpoint = new IdGenerator().generateSanitizedId();
215             this.request = request;
216         }
217
218         @Override JavaDoc
219         public Role getRole() {
220             return Role.CONSUMER;
221         }
222
223         @Override JavaDoc
224         public void activate() throws Exception JavaDoc {
225             JbiNotificationBroker broker = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getNotificationBroker();
226             response = broker.handleRegisterPublisher(request, this);
227         }
228
229         @Override JavaDoc
230         public void deactivate() throws Exception JavaDoc {
231             JbiNotificationBroker broker = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getNotificationBroker();
232             broker.unsubscribe(response.getPublisherRegistrationReference().getAddress().getValue());
233         }
234
235         @Override JavaDoc
236         public ExchangeProcessor getProcessor() {
237             return null;
238         }
239
240         public Object JavaDoc register(String JavaDoc address, Object JavaDoc service) throws EndpointRegistrationException {
241             return null;
242         }
243
244         public void unregister(Object JavaDoc endpoint) throws EndpointRegistrationException {
245         }
246
247     }
248
249     public static class WSNServiceUnit extends ServiceUnit {
250         public void start() throws Exception JavaDoc {
251             List JavaDoc<Endpoint> activated = new ArrayList JavaDoc<Endpoint>();
252             try {
253                 for (Iterator JavaDoc iter = getEndpoints().iterator(); iter.hasNext();) {
254                     Endpoint endpoint = (Endpoint) iter.next();
255                     if (endpoint instanceof WSNPullPointEndpoint) {
256                         endpoint.activate();
257                         activated.add(endpoint);
258                     }
259                 }
260                 for (Iterator JavaDoc iter = getEndpoints().iterator(); iter.hasNext();) {
261                     Endpoint endpoint = (Endpoint) iter.next();
262                     if (endpoint instanceof WSNSubscriptionEndpoint) {
263                         endpoint.activate();
264                         activated.add(endpoint);
265                     }
266                 }
267                 this.status = LifeCycleMBean.STARTED;
268             } catch (Exception JavaDoc e) {
269                 // Deactivate activated endpoints
270
for (Endpoint endpoint : activated) {
271                     try {
272                         endpoint.deactivate();
273                     } catch (Exception JavaDoc e2) {
274                         // do nothing
275
}
276                 }
277                 throw e;
278             }
279         }
280     }
281     
282     public static class XmlFilter implements FilenameFilter JavaDoc {
283
284         public boolean accept(File JavaDoc dir, String JavaDoc name) {
285             return name.endsWith(".xml");
286         }
287         
288     }
289     
290 }
291
Popular Tags