KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > basic > AbstractServiceUnitManager


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: AbstractServiceUnitManager.java 601 2006-06-13 12:53:50Z wjoseph $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.basic;
23
24 import java.io.File JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 import javax.jbi.JBIException;
33 import javax.jbi.component.ComponentContext;
34 import javax.jbi.component.ServiceUnitManager;
35 import javax.jbi.management.DeploymentException;
36 import javax.jbi.servicedesc.ServiceEndpoint;
37 import javax.xml.namespace.QName JavaDoc;
38
39 import org.objectweb.petals.component.common.util.ManagementMessageUtil;
40 import org.objectweb.petals.component.common.util.WSDLHelper;
41 import org.objectweb.petals.component.common.util.XMLHelper;
42
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46
47 /**
48  * Generic service unit manager. Only handles one type of service unit.
49  * Endpoints are activated by this manager. An abstract deploy method allows you
50  * to add extra information in the service unit descriptor.
51  *
52  * @version $Rev: 250 $ $Date: 2006-04-21 14:20:57 +0200 (ven, 21 avr 2006) $
53  * @since Petals 1.0
54  * @author wjoseph - eBMWebsourcing
55  */

56 public abstract class AbstractServiceUnitManager implements ServiceUnitManager {
57
58     /**
59      * Namespace of the extension elements in the jbi descriptor file
60      */

61     public static final String JavaDoc EXTENSIONS_XMLNS = "http://petals.objectweb.org/extensions";
62
63     /**
64      * Error messages
65      */

66     protected static final String JavaDoc ERROR_DUPLICATE_SERVICE_UNIT = "A service unit with the given name is already registered ";
67
68     protected static final String JavaDoc FAILED_ACTIVATE_ENDPOINT = "Activate new endpoint failed ";
69
70     protected static final String JavaDoc FAILED_DEACTIVATE_ENDPOINT = "Deactivate endpoint failed ";
71
72     protected static final String JavaDoc INCOMPLETE_SERVICE_UNIT_PACKAGE = "The deployed service package is incomplete ";
73
74     /**
75      * The component's context
76      */

77     protected ComponentContext context;
78
79     /**
80      * The component's Logger
81      */

82     protected Logger JavaDoc logger;
83
84     protected Map JavaDoc<ServiceEndpoint, Document JavaDoc> endpointServiceDescriptionMap;
85
86     protected Map JavaDoc<QName JavaDoc, Set JavaDoc<ServiceEndpoint>> servicesByInterfaceMap;
87
88     protected Map JavaDoc<String JavaDoc, String JavaDoc> serviceUnitInstallationRootPath;
89
90     protected Map JavaDoc<String JavaDoc, ServiceEndpoint> serviceUnitMap;
91
92     /**
93      * Default constructor
94      */

95     public AbstractServiceUnitManager() {
96         endpointServiceDescriptionMap = new HashMap JavaDoc<ServiceEndpoint, Document JavaDoc>();
97         serviceUnitMap = new HashMap JavaDoc<String JavaDoc, ServiceEndpoint>();
98         servicesByInterfaceMap = new HashMap JavaDoc<QName JavaDoc, Set JavaDoc<ServiceEndpoint>>();
99         serviceUnitInstallationRootPath = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
100     }
101
102     /**
103      * Creates a new service unit manager
104      *
105      * @param context
106      * Component context of the callind component
107      * @param logger
108      * Logger of the calling component
109      */

110     public AbstractServiceUnitManager(ComponentContext context, Logger JavaDoc logger)
111         throws DeploymentException {
112         this.context = context;
113         this.logger = logger;
114         endpointServiceDescriptionMap = new HashMap JavaDoc<ServiceEndpoint, Document JavaDoc>();
115         serviceUnitMap = new HashMap JavaDoc<String JavaDoc, ServiceEndpoint>();
116         servicesByInterfaceMap = new HashMap JavaDoc<QName JavaDoc, Set JavaDoc<ServiceEndpoint>>();
117         serviceUnitInstallationRootPath = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
118     }
119
120     /**
121      * Add a service endpoint for an interface QName
122      *
123      * @param interfaceName
124      * the interface QName
125      * @param serviceEndpoint
126      * the endpoint to add
127      */

128     public void addServiceForInterface(QName JavaDoc interfaceName,
129             ServiceEndpoint serviceEndpoint) {
130         Set JavaDoc<ServiceEndpoint> endpoints = null;
131         endpoints = servicesByInterfaceMap.get(interfaceName);
132         if (endpoints == null) {
133             endpoints = new HashSet JavaDoc<ServiceEndpoint>();
134         }
135         endpoints.add(serviceEndpoint);
136         servicesByInterfaceMap.put(interfaceName, endpoints);
137     }
138
139     /**
140      * Deploy a service unit. Before deploying, the service unit type if
141      * verified
142      *
143      * @param serviceUnitName
144      * name of the service unit to deploy
145      * @param serviceUnitRootPath
146      * root path of the service unit to deploy
147      * @return a valid management message
148      * @throws DeploymentException
149      * If an error occurs during deployment
150      */

151     public String JavaDoc deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
152         throws DeploymentException {
153         String JavaDoc result = null;
154         if (serviceUnitMap.get(serviceUnitName) == null) {
155             File JavaDoc jbiXmlFile = new File JavaDoc(serviceUnitRootPath + File.separator
156                     + "META-INF" + File.separator + "jbi.xml");
157             Node JavaDoc servicesNode = getNode(jbiXmlFile, "services");
158             Map JavaDoc<String JavaDoc, String JavaDoc> extensions = getExtensions(servicesNode);
159             logger.log(Level.FINE, "deploy serviceUnitName "
160                     + serviceUnitName + "serviceUnitRootPath"
161                     + serviceUnitRootPath);
162             result = ManagementMessageUtil.getComponentTaskResult(context
163                     .getComponentName(), "deploy",
164                     ManagementMessageUtil.TASK_RESULT_SUCCESS);
165             deploy(extensions);
166         } else {
167             throw new DeploymentException(ERROR_DUPLICATE_SERVICE_UNIT);
168         }
169         return result;
170     }
171
172     /**
173      * Get the service description for one of the component endpoint
174      *
175      * @param se
176      * Service endpoint to get description for
177      * @return The service description or null
178      */

179     public Document JavaDoc getServiceDescription(ServiceEndpoint se) {
180         Document JavaDoc result = null;
181         for (ServiceEndpoint ep : endpointServiceDescriptionMap.keySet()) {
182             if (se.getServiceName().equals(ep.getServiceName())) {
183                 result = endpointServiceDescriptionMap.get(ep);
184                 break;
185             }
186         }
187         return result;
188     }
189
190     /**
191      * Return the endpoint for a given service unit name
192      *
193      * @param serviceUnitName
194      * The service unit name
195      * @return The matching endpoint or null if no matching endpoint found
196      */

197     public ServiceEndpoint getServiceUnitEndpoint(String JavaDoc serviceUnitName) {
198         return serviceUnitMap.get(serviceUnitName);
199     }
200
201     /**
202      * (non-Javadoc)
203      *
204      * @see javax.jbi.component.ServiceUnitManager#init(java.lang.String,
205      * java.lang.String)
206      *
207      * The service unit root path is stored in a Map to support multithreading
208      */

209     public void init(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
210         throws DeploymentException {
211         logger.log(Level.FINE, "init serviceUnitName " + serviceUnitName
212                 + " serviceUnitRootPath " + serviceUnitRootPath);
213         if (serviceUnitInstallationRootPath.put(serviceUnitName,
214                 serviceUnitRootPath) != null) {
215             logger.log(Level.SEVERE, ERROR_DUPLICATE_SERVICE_UNIT);
216             throw new DeploymentException(ERROR_DUPLICATE_SERVICE_UNIT);
217         }
218     }
219
220     /**
221      * Remove the service description for an endpoint
222      *
223      * @param ep
224      * the service endpoint for which the description will be removed
225      */

226     public void removeServiceDescription(ServiceEndpoint ep) {
227         endpointServiceDescriptionMap.remove(ep);
228     }
229
230     /**
231      * Remove the service description for an endpoint
232      *
233      * @param ep
234      * the service endpoint for which the description will be removed
235      */

236     public void removeServiceUnitEndpoint(String JavaDoc suName) {
237         serviceUnitMap.remove(suName);
238     }
239
240     /**
241      * Set the service description for a given endpoint
242      *
243      * @param se
244      * The service endpoint
245      * @param serviceDesc
246      * The dom document representation of the service description
247      */

248     public void setServiceDescription(ServiceEndpoint se, Document JavaDoc serviceDesc) {
249         endpointServiceDescriptionMap.put(se, serviceDesc);
250     }
251
252     /**
253      * Set the endpoint for a given service unit name
254      *
255      * @param serviceUnitName
256      * The name of the service unit
257      * @param serviceEP
258      * The service endpoint
259      */

260     public void setServiceUnitEndpoint(String JavaDoc serviceUnitName,
261             ServiceEndpoint serviceEP) {
262         serviceUnitMap.put(serviceUnitName, serviceEP);
263     }
264
265     /**
266      * (non-Javadoc)
267      *
268      * @see javax.jbi.component.ServiceUnitManager#shutDown(java.lang.String)
269      */

270     public void shutDown(String JavaDoc serviceUnitName) throws DeploymentException {
271         logger.log(Level.FINE, "shutdown serviceUnitName " + serviceUnitName);
272     }
273
274     /**
275      * (non-Javadoc)
276      *
277      * @see javax.jbi.component.ServiceUnitManager#start(java.lang.String)
278      *
279      * Endpoints are activated here
280      */

281     public void start(String JavaDoc serviceUnitName) throws DeploymentException {
282         logger.log(Level.FINE, "start serviceUnitName " + serviceUnitName);
283         String JavaDoc suRootPath = serviceUnitInstallationRootPath
284                 .get(serviceUnitName);
285         Document JavaDoc serviceDesc = getServiceUnitWSDL(suRootPath);
286         if (serviceDesc != null) {
287             try {
288                 activateEndpointsFromJBIDescription(serviceDesc,
289                         serviceUnitName, suRootPath);
290             } catch (Exception JavaDoc ex) {
291                 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT + ex);
292                 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, ex);
293             }
294         } else {
295             throw new DeploymentException(INCOMPLETE_SERVICE_UNIT_PACKAGE);
296         }
297     }
298
299     /**
300      * (non-Javadoc)
301      *
302      * @see javax.jbi.component.ServiceUnitManager#stop(java.lang.String)
303      *
304      * Endpoints are deactivated here
305      */

306     public void stop(String JavaDoc serviceUnitName) throws DeploymentException {
307         try {
308             serviceUnitInstallationRootPath.remove(serviceUnitName);
309             deactivateEndpointsFromJBIDescription(serviceUnitName);
310         } catch (Exception JavaDoc e) {
311             throw new DeploymentException(FAILED_DEACTIVATE_ENDPOINT, e);
312         }
313         logger.log(Level.FINE, "stop serviceUnitName " + serviceUnitName);
314     }
315
316     /**
317      * (non-Javadoc)
318      *
319      * @see javax.jbi.component.ServiceUnitManager#undeploy(java.lang.String,
320      * java.lang.String)
321      */

322     public String JavaDoc undeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
323         throws DeploymentException {
324         return ManagementMessageUtil.getComponentTaskResult(context
325                 .getComponentName(), "undeploy",
326                 ManagementMessageUtil.TASK_RESULT_SUCCESS);
327     }
328
329     /**
330      * Activate an endpoint in Petals platform, put the activate endpoint add
331      * the service description of this endpoint in HashMap, add the service unit
332      * name and the endpoint in a HashMap and add the interface QName and the
333      * endpoint in a HashMap. Only the first provides node is activated
334      *
335      * @param serviceDesc
336      * the service unit service description
337      * @param serviceUnitName
338      * the service unit name
339      * @param suRootPath
340      * the service unit root path
341      * @throws Exception
342      * If errors occur during processing
343      * @return the activated endpoint
344      */

345     protected ServiceEndpoint activateEndpointsFromJBIDescription(
346             Document JavaDoc serviceDesc, String JavaDoc serviceUnitName, String JavaDoc suRootPath)
347         throws Exception JavaDoc {
348         File JavaDoc jbiXmlFile = new File JavaDoc(suRootPath + File.separator + "META-INF"
349                 + File.separator + "jbi.xml");
350         Node JavaDoc providesNode = getNode(jbiXmlFile, "provides");
351         ServiceEndpoint ep = null;
352         if (providesNode != null) {
353             QName JavaDoc interfaceName = getInterfaceNameFromJbiXml(providesNode);
354             String JavaDoc endpointName = getEndpointNameFromJbiXml(providesNode);
355             QName JavaDoc serviceName = getServiceNameFromJbiXml(providesNode);
356             ep = context.activateEndpoint(serviceName, endpointName);
357             setServiceDescription(ep, serviceDesc);
358             setServiceUnitEndpoint(serviceUnitName, ep);
359             addServiceForInterface(interfaceName, ep);
360         }
361         return ep;
362     }
363
364     /**
365      * Deactivate an endpoint in Petals platform, remove the activate endpoint
366      * from various HashMap in which it was registered
367      *
368      * @param ep
369      * the service endpoint to be deactivated
370      * @throws Exception
371      * If errors occur during processing
372      */

373     protected void deactivateEndpointsFromJBIDescription(String JavaDoc serviceUnitName)
374         throws JBIException {
375         ServiceEndpoint ep = getServiceUnitEndpoint(serviceUnitName);
376         if (ep != null) {
377             context.deactivateEndpoint(ep);
378             removeServiceDescription(ep);
379             removeServiceUnitEndpoint(serviceUnitName);
380         }
381     }
382
383     /**
384      * Do extra computation with the extension fragment of the jbi descriptor.
385      *
386      * @param jbiExtensionFragment
387      * the extension frangment of the jbi descriptor
388      */

389     protected abstract void deploy(Map JavaDoc<String JavaDoc, String JavaDoc> extensions);
390
391     /**
392      * Return the endpoint name parse from the jbi descriptor file
393      *
394      * @param jbiXml
395      * jbi descriptor file
396      * @return the endpoint name if found, else null
397      */

398     protected String JavaDoc getEndpointNameFromJbiXml(Node JavaDoc providesNode) {
399         String JavaDoc result = new String JavaDoc();
400         result = XMLHelper.getAttributeValue(providesNode, "endpoint-name");
401         return result;
402     }
403
404     /**
405      * Returns a Map with all extension elements. These elements are xml node
406      * placed in the jbi.xml service unit descriptor file. They use the
407      * EXTENSIONS_XMLNS namespace.
408      *
409      * @param servicesNode
410      * @return Map containing all extensions element with their value
411      */

412     protected Map JavaDoc<String JavaDoc, String JavaDoc> getExtensions(Node JavaDoc servicesNode) {
413         Map JavaDoc<String JavaDoc, String JavaDoc> extensions = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
414         NodeList JavaDoc nl = servicesNode.getChildNodes();
415         String JavaDoc prefix = XMLHelper.getPrefixForNamespaceURI(servicesNode,
416                 EXTENSIONS_XMLNS, false);
417         for (int i = 0; i < nl.getLength(); i++) {
418             Node JavaDoc node = nl.item(i);
419             if (node.getNodeType() == Node.ELEMENT_NODE
420                     && node.getNodeName().startsWith(prefix)) {
421                 extensions.put(node.getNodeName().replaceFirst(prefix, ""),
422                         node.getTextContent());
423             }
424         }
425         return extensions;
426     }
427
428     /**
429      * Return the interface QName parse from the jbi descriptor file
430      *
431      * @param jbiXml
432      * jbi descriptor file
433      * @return the interface QName if found, else null
434      */

435     protected QName JavaDoc getInterfaceNameFromJbiXml(Node JavaDoc providesNode) {
436         QName JavaDoc result = null;
437         result = QName.valueOf(XMLHelper.getAttributeValue(providesNode,
438                 "interface-name"));
439         return result;
440     }
441
442     /**
443      * Parse the JBI descriptor into a Dom Document and search for the first
444      * node whom name matches the NodeName
445      *
446      * @param jbiXmlFile
447      * the service unit description file
448      * @param nodeName
449      * Name of the node to search
450      * @return node with the right name if found, else null
451      */

452     protected Node JavaDoc getNode(File JavaDoc jbiXmlFile, String JavaDoc nodeName) {
453         Document JavaDoc jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXmlFile);
454         Node JavaDoc providesNode = XMLHelper.findChild(jbiDoc, nodeName, true);
455         return providesNode;
456     }
457
458     /**
459      * Return the service QName parse from the jbi descriptor file
460      *
461      * @param jbiXml
462      * jbi descriptor file
463      * @return the service QName if found, else null
464      */

465     protected QName JavaDoc getServiceNameFromJbiXml(Node JavaDoc providesNode) {
466         QName JavaDoc result = null;
467         result = QName.valueOf(XMLHelper.getAttributeValue(providesNode,
468                 "service-name"));
469         return result;
470     }
471
472     /**
473      * Returns A Dom Document represenation of the service description file
474      *
475      * @param suRootPath
476      * @return
477      */

478     protected Document JavaDoc getServiceUnitWSDL(String JavaDoc suRootPath) {
479         Document JavaDoc result = null;
480         File JavaDoc[] files = new File JavaDoc(suRootPath).listFiles();
481         for (File JavaDoc file : files) {
482             if (file.getName().endsWith(".wsdl")) {
483                 result = WSDLHelper.createDocumentFromWSDL(file);
484                 break;
485             }
486         }
487         return result;
488     }
489 }
490
Popular Tags