KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > su > SimpleServiceUnitManager


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id$
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.component.common.su;
24
25 import java.io.File JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 import javax.jbi.JBIException;
37 import javax.jbi.component.ComponentContext;
38 import javax.jbi.component.ServiceUnitManager;
39 import javax.jbi.management.DeploymentException;
40 import javax.jbi.servicedesc.ServiceEndpoint;
41
42 import org.objectweb.petals.component.common.AbstractComponent;
43 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
44 import org.objectweb.petals.component.common.util.ManagementMessageUtil;
45 import org.objectweb.petals.component.common.util.PetalsExtensionsUtil;
46 import org.objectweb.petals.component.common.util.WSDLHelper;
47 import org.objectweb.petals.tools.jbicommon.descriptor.Consumes;
48 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
49 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorBuilder;
50 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorException;
51 import org.objectweb.petals.tools.jbicommon.descriptor.Provides;
52 import org.w3c.dom.Document JavaDoc;
53
54 /**
55  * A simple ServiceUnitManager. It processes service unit jbi descriptor : <br/>
56  * Provides nodes : new endpoints registered in JBI container<br/> Consumes
57  * nodes : link addresses to existing JBI endpoints
58  *
59  * @author ofabre,alouis
60  *
61  */

62 public class SimpleServiceUnitManager implements ServiceUnitManager {
63
64     private static final String JavaDoc JBI_XML = "jbi.xml";
65
66     private static final String JavaDoc META_INF = "META-INF";
67
68     private Map JavaDoc<ServiceEndpoint, ServiceUnitDataHandler> suHandlerCache;
69
70     /**
71      * The component's context
72      */

73     private final ComponentContext context;
74
75     /**
76      * The component's Logger
77      */

78     private Logger JavaDoc logger; // NOPMD by gblondelle
79

80     private final Map JavaDoc<String JavaDoc, ServiceUnitDataHandler> serviceUnitDatas;
81
82     private final AbstractComponent component;
83
84     public SimpleServiceUnitManager(AbstractComponent engine,
85         ComponentContext context, Logger JavaDoc logger) {
86         super();
87         this.context = context;
88         this.logger = logger;
89         this.component = engine;
90         serviceUnitDatas = new HashMap JavaDoc<String JavaDoc, ServiceUnitDataHandler>();
91         suHandlerCache = new HashMap JavaDoc<ServiceEndpoint, ServiceUnitDataHandler>();
92     }
93
94     public String JavaDoc deploy(final String JavaDoc serviceUnitName, final String JavaDoc suRootPath)
95         throws DeploymentException {
96         logger.log(Level.FINE, "deploy serviceUnitName " + serviceUnitName
97             + "serviceUnitRootPath" + suRootPath);
98         if (serviceUnitDatas.get(serviceUnitName) == null) {
99             ServiceUnitDataHandler handler = new ServiceUnitDataHandler();
100             handler.setInstallRoot(suRootPath);
101             handler.setName(serviceUnitName);
102             File JavaDoc jbiXmlFile = new File JavaDoc(suRootPath + File.separator + META_INF
103                 + File.separator + JBI_XML);
104             JBIDescriptor descriptor = null;
105             try {
106                 descriptor = new JBIDescriptorBuilder(jbiXmlFile.toURI(),
107                     logger).build();
108             } catch (JBIDescriptorException e) {
109                 String JavaDoc msg = "Bad JBI Descriptor";
110                 logger.log(Level.SEVERE, msg);
111                 throw new DeploymentException(msg, e);
112             }
113             if (descriptor != null) {
114                 handler.setDescriptor(descriptor);
115
116                 try {
117                     processConsumesNodes(handler);
118                 } catch (JBIException ex) {
119                     String JavaDoc msg = "Failed to process consumes nodes";
120                     logger.log(Level.SEVERE, msg, ex);
121                     throw new DeploymentException(msg, ex);
122                 }
123
124                 serviceUnitDatas.put(serviceUnitName, handler);
125
126                 if (component.getServiceUnitListener() != null) {
127                     try {
128                         component.getServiceUnitListener().onSUDeployed(
129                             serviceUnitName, suRootPath, descriptor);
130                     } catch (PEtALSComponentSDKException e) {
131                         // clean cache
132
serviceUnitDatas.remove(serviceUnitName);
133
134                         throw new DeploymentException(
135                             "The ServiceUnit listener failed to process.", e);
136                     }
137                 }
138             }
139         } else {
140             throw new DeploymentException(
141                 "A service unit with the given name is already registered");
142         }
143         String JavaDoc result = ManagementMessageUtil.getComponentTaskResult(context
144             .getComponentName(), "deploy",
145             ManagementMessageUtil.TASK_RESULT_SUCCESS);
146         return result;
147     }
148
149     /**
150      * Retrieve the Consumes node linked to the given address
151      *
152      * @param address
153      * address of the search Consumes node
154      * @return a {@link Consumes} element
155      */

156     public Consumes getConsumesFromAddress(final String JavaDoc address) {
157         Consumes result = null;
158         for (Iterator JavaDoc iter = serviceUnitDatas.values().iterator(); iter
159             .hasNext()
160             && result == null;) {
161             ServiceUnitDataHandler handler = (ServiceUnitDataHandler) iter
162                 .next();
163             Map JavaDoc<String JavaDoc, Consumes> addressToConsumes = handler
164                 .getAddressToConsumes();
165             result = addressToConsumes.get(address);
166         }
167         return result;
168     }
169
170     /**
171      * Retrieve the Provides node that has activated the given jbi Endpoint
172      *
173      * @param serviceEndpoint
174      * the previously activated jbi Endpoint
175      * @return the {@link Provides} element
176      */

177     public Provides getProvidesFromEndpoint(
178         final ServiceEndpoint serviceEndpoint) {
179         Provides result = null;
180         for (Iterator JavaDoc iter = serviceUnitDatas.values().iterator(); iter
181             .hasNext()
182             && result == null;) {
183             ServiceUnitDataHandler handler = (ServiceUnitDataHandler) iter
184                 .next();
185             Map JavaDoc<ServiceEndpoint, Provides> epJBIDescs = handler.getEpJBIDesc();
186             result = epJBIDescs.get(serviceEndpoint);
187         }
188         return result;
189     }
190
191     /**
192      * Retrieve the Service description (as Dom Document) of the given jbi
193      * Endpoint
194      *
195      * @param serviceEndpoint
196      * the jbi Endpoint
197      * @return the service description as Dom Document
198      */

199     public Document JavaDoc getServiceDescription(final ServiceEndpoint serviceEndpoint) {
200         Document JavaDoc result = null;
201         for (Iterator JavaDoc iter = serviceUnitDatas.values().iterator(); iter
202             .hasNext()
203             && result == null;) {
204             ServiceUnitDataHandler handler = (ServiceUnitDataHandler) iter
205                 .next();
206             Map JavaDoc<ServiceEndpoint, Document JavaDoc> epServDescs = handler
207                 .getEpServiceDesc();
208             result = epServDescs.get(serviceEndpoint);
209         }
210         return result;
211     }
212
213     public void init(final String JavaDoc serviceUnitName, final String JavaDoc suRootPath)
214         throws DeploymentException {
215         logger.log(Level.FINE, "init serviceUnitName " + serviceUnitName
216             + " serviceUnitRootPath " + suRootPath);
217     }
218
219     public void shutDown(final String JavaDoc serviceUnitName)
220         throws DeploymentException {
221         logger.log(Level.FINE, "shutdown serviceUnitName " + serviceUnitName);
222     }
223
224     public void start(final String JavaDoc serviceUnitName) throws DeploymentException {
225         logger.log(Level.FINE, "start serviceUnitName " + serviceUnitName);
226         ServiceUnitDataHandler handler = serviceUnitDatas.get(serviceUnitName);
227         try {
228             processProvidesNodes(handler);
229         } catch (JBIException ex) {
230             String JavaDoc msg = "Failed to start service unit : " + serviceUnitName;
231             logger.log(Level.SEVERE, msg, ex);
232             throw new DeploymentException(msg, ex);
233         }
234         if (component.getServiceUnitListener() != null) {
235             try {
236                 component.getServiceUnitListener().onSUStarted(serviceUnitName);
237             } catch (PEtALSComponentSDKException e) {
238                 throw new DeploymentException(
239                     "The ServiceUnit listener failed to process.", e);
240             }
241         }
242     }
243
244     public void stop(final String JavaDoc serviceUnitName) throws DeploymentException {
245         if (component.getServiceUnitListener() != null) {
246             try {
247                 component.getServiceUnitListener().onSUStopped(serviceUnitName);
248             } catch (PEtALSComponentSDKException e) {
249                 throw new DeploymentException(
250                     "The ServiceUnit listener failed to process.", e);
251             }
252         }
253         try {
254             ServiceUnitDataHandler handler = serviceUnitDatas
255                 .get(serviceUnitName);
256             List JavaDoc<ServiceEndpoint> endpoints = getServiceUnitEndpoints(serviceUnitName);
257             for (ServiceEndpoint endpoint : endpoints) {
258                 context.deactivateEndpoint(endpoint);
259                 handler.removeEndpoint(endpoint);
260             }
261
262         } catch (JBIException e) {
263             throw new DeploymentException("Deactivate endpoint failed", e);
264         }
265         logger.log(Level.FINE, "stop serviceUnitName " + serviceUnitName);
266     }
267
268     public String JavaDoc undeploy(final String JavaDoc serviceUnitName,
269         final String JavaDoc serviceUnitRootPath) throws DeploymentException {
270         ServiceUnitDataHandler handler = serviceUnitDatas.get(serviceUnitName);
271
272         if (component.getServiceUnitListener() != null) {
273             try {
274                 component.getServiceUnitListener().onSUUndeployed(
275                     serviceUnitName, serviceUnitRootPath,
276                     handler.getDescriptor());
277             } catch (PEtALSComponentSDKException e) {
278                 throw new DeploymentException(
279                     "The ServiceUnit listener failed to process.", e);
280             }
281         }
282
283         // clean the chache
284
for (ServiceEndpoint se : suHandlerCache.keySet()) {
285             if (suHandlerCache.get(se).equals(handler)) {
286                 suHandlerCache.remove(se);
287             }
288         }
289
290         serviceUnitDatas.remove(serviceUnitName);
291         return ManagementMessageUtil.getComponentTaskResult(context
292             .getComponentName(), "undeploy",
293             ManagementMessageUtil.TASK_RESULT_SUCCESS);
294     }
295
296     /**
297      * Search the wsdl description of the activated endpoint in the Provides
298      * extensions. If no wsdl description can be found, it creates a light
299      * weight Wsdl description from Provides Interface, Service and Endpoint
300      * names.
301      *
302      * @param installRoot
303      * the SU install root
304      * @param provides
305      * the Provices node
306      * @return the WSDL description as Dom Document
307      */

308     @SuppressWarnings JavaDoc("unchecked")
309     protected Document JavaDoc getEnpointWSDLDesc(String JavaDoc installRoot, Provides provides) {
310
311         Document JavaDoc wsdlDesc = null;
312
313         String JavaDoc wsdlFileLocation = PetalsExtensionsUtil
314             .extractValueFromKeyValueExtension(provides.getExtensions(),
315                 PetalsExtensionsUtil.WSDL_LOCATION);
316
317         if (wsdlFileLocation != null) {
318
319             if (wsdlFileLocation.toLowerCase().startsWith("http:")
320                 || wsdlFileLocation.toLowerCase().startsWith("https:")
321                 || wsdlFileLocation.toLowerCase().startsWith("file:")) {
322
323                 try {
324                     URL JavaDoc url = new URL JavaDoc(wsdlFileLocation);
325                     wsdlDesc = WSDLHelper.createDocumentFromWSDL(url);
326                 } catch (MalformedURLException JavaDoc e) {
327                 }
328
329             } else {
330                 // get local wsdl (relative or absolute)
331
if (!wsdlFileLocation.startsWith(File.separator)) {
332                     wsdlFileLocation = File.separator + wsdlFileLocation;
333                 }
334
335                 File JavaDoc wsdlFile = new File JavaDoc(installRoot + wsdlFileLocation);
336                 wsdlDesc = WSDLHelper.createDocumentFromWSDL(wsdlFile);
337             }
338         }
339
340         // if the wsdl description is null, generate it
341
if (wsdlDesc == null) {
342             wsdlDesc = WSDLHelper.createLightWSDL20(
343                 provides.getInterfaceName(), provides.getServiceName(),
344                 provides.getEndpointName());
345         }
346
347         return wsdlDesc;
348     }
349
350     protected void processConsumesNode(final ServiceUnitDataHandler handler,
351         final Consumes consumes) throws JBIException {
352         String JavaDoc address = PetalsExtensionsUtil
353             .extractValueFromKeyValueExtension(consumes.getExtensions(),
354                 PetalsExtensionsUtil.ADDRESS);
355
356         if (address != null) {
357             handler.addAddressToConsumes(address, consumes);
358         }
359     }
360
361     protected void processConsumesNodes(final ServiceUnitDataHandler handler)
362         throws JBIException {
363         JBIDescriptor descriptor = handler.getDescriptor();
364         List JavaDoc<Consumes> consumesList = descriptor.getServices().getConsumes();
365         for (Consumes consumes : consumesList) {
366             processConsumesNode(handler, consumes);
367         }
368     }
369
370     protected ServiceEndpoint processProvidesNode(
371         final ServiceUnitDataHandler handler, final Provides provides)
372         throws JBIException {
373         ServiceEndpoint ep = context.activateEndpoint(
374             provides.getServiceName(), provides.getEndpointName());
375         handler.addEndpointDescription(ep, getEnpointWSDLDesc(handler
376             .getInstallRoot(), provides));
377         handler.addJBIDescription(ep, provides);
378         return ep;
379     }
380
381     protected void processProvidesNodes(final ServiceUnitDataHandler handler)
382         throws JBIException {
383         JBIDescriptor descriptor = handler.getDescriptor();
384         List JavaDoc<Provides> providesList = descriptor.getServices().getProvides();
385         for (Provides provides : providesList) {
386             processProvidesNode(handler, provides);
387         }
388     }
389
390     private List JavaDoc<ServiceEndpoint> getServiceUnitEndpoints(
391         final String JavaDoc serviceUnitName) {
392         return new ArrayList JavaDoc<ServiceEndpoint>(serviceUnitDatas.get(
393             serviceUnitName).getEpServiceDesc().keySet());
394     }
395
396     /**
397      * Return all {@link ServiceUnitDataHandler}s as a Map
398      *
399      * @return all {@link ServiceUnitDataHandler}s as a Map
400      */

401     public Map JavaDoc<String JavaDoc, ServiceUnitDataHandler> getServiceUnitDatas() {
402         return serviceUnitDatas;
403     }
404
405     /**
406      * Return the component Logger
407      *
408      * @return the component Logger
409      */

410     public Logger JavaDoc getLogger() {
411         return logger;
412     }
413
414     /**
415      * Retrieve the install root of the Service Unit that activated the given
416      * endpoint
417      *
418      * @param ep
419      * the previously activated endpoint
420      * @return the Service unit install root
421      */

422     public String JavaDoc getSUInstallRootForActivatedEp(ServiceEndpoint ep) {
423         ServiceUnitDataHandler handler = getSuDataHandlerForEp(ep);
424         return handler.getInstallRoot();
425     }
426
427     /**
428      * Retrieve the data handler of the Service Unit that activated the given
429      * endpoint
430      *
431      * @param ep
432      * the previously activated endpoint
433      * @return the {@link ServiceUnitDataHandler}
434      */

435     public ServiceUnitDataHandler getSuDataHandlerForEp(ServiceEndpoint ep) {
436
437         ServiceUnitDataHandler result = suHandlerCache.get(ep);
438
439         if (result == null) {
440             for (Iterator JavaDoc iter = getServiceUnitDatas().values().iterator(); iter
441                 .hasNext()
442                 && result == null;) {
443                 ServiceUnitDataHandler element = (ServiceUnitDataHandler) iter
444                     .next();
445                 for (Iterator JavaDoc iterator2 = element.getEpJBIDesc().keySet()
446                     .iterator(); iterator2.hasNext() && result == null;) {
447                     ServiceEndpoint element2 = (ServiceEndpoint) iterator2
448                         .next();
449                     if (element2.equals(ep)) {
450                         result = element;
451                     }
452                 }
453             }
454         }
455         return result;
456     }
457
458 }
459
Popular Tags