KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > serviceunitmanager > manager > PetalsServiceUnitManager


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: PetalsServiceUnitManager.java 154 2006-03-27 15:30:10Z wjoseph $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.serviceunitmanager.manager;
23
24 import java.io.File JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
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.serviceunitmanager.handler.ExternalServiceManager;
40 import org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler;
41 import org.objectweb.petals.component.common.util.ManagementMessageUtil;
42 import org.objectweb.petals.component.common.util.WSDLHelper;
43 import org.objectweb.petals.component.common.util.XMLHelper;
44
45 import org.w3c.dom.Document JavaDoc;
46 import org.w3c.dom.Node JavaDoc;
47
48 /**
49  * General service unit manager in charge of delegating the acutal deployment,
50  * installation, etc to dedicated ServiceUnitHandler
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 class PetalsServiceUnitManager implements ServiceUnitManager {
57
58     private static final String JavaDoc NO_HANDLER_FOR_THIS_TYPE = "No registered handlers for this type of service unit ";
59
60     private static final String JavaDoc DUPLICATE_SERVICE_UNIT = "Another service unit with the same name is already deployed ";
61
62     private static final String JavaDoc HANDLER_FOR_THIS_SERVICE_NOT_FOUND = "Handler for this service name unit not found ";
63     
64     public static final String JavaDoc EXTENSIONS_XMLNS = "http://petals.objectweb.org/extensions";
65
66     private Map JavaDoc<ServiceEndpoint, Document JavaDoc> serviceDescriptionMap;
67
68     private Map JavaDoc<String JavaDoc, ServiceEndpoint> serviceUnitMap;
69
70     private Map JavaDoc<String JavaDoc, PetalsServiceUnitHandler> handlersMap;
71
72     private Map JavaDoc<QName JavaDoc, Set JavaDoc<ServiceEndpoint>> servicesByInterfaceMap;
73
74     private Set JavaDoc<PetalsServiceUnitHandler> handlersSet;
75     
76     private Map JavaDoc<ServiceEndpoint, PetalsServiceUnitHandler> endpointSUHandler;
77
78     protected ComponentContext context;
79
80     protected Logger JavaDoc logger;
81
82     /**
83      * Creates a new service unit manager
84      *
85      * @param context
86      * Component context of the callind component
87      * @param logger
88      * Logger of the calling component
89      */

90     public PetalsServiceUnitManager(ComponentContext context, Logger JavaDoc logger)
91         throws DeploymentException {
92         this.context = context;
93         this.logger = logger;
94         serviceDescriptionMap = new HashMap JavaDoc<ServiceEndpoint, Document JavaDoc>();
95         serviceUnitMap = new HashMap JavaDoc<String JavaDoc, ServiceEndpoint>();
96         handlersMap = new HashMap JavaDoc<String JavaDoc, PetalsServiceUnitHandler>();
97         handlersSet = new HashSet JavaDoc<PetalsServiceUnitHandler>();
98         handlersSet.add(new ExternalServiceManager(context, logger));
99         servicesByInterfaceMap = new HashMap JavaDoc<QName JavaDoc, Set JavaDoc<ServiceEndpoint>>();
100         endpointSUHandler = new HashMap JavaDoc<ServiceEndpoint, PetalsServiceUnitHandler>();
101     }
102     
103     /**
104      * Default constructor
105      */

106     public PetalsServiceUnitManager() {
107     serviceDescriptionMap = new HashMap JavaDoc<ServiceEndpoint, Document JavaDoc>();
108     serviceUnitMap = new HashMap JavaDoc<String JavaDoc, ServiceEndpoint>();
109     handlersMap = new HashMap JavaDoc<String JavaDoc, PetalsServiceUnitHandler>();
110     handlersSet = new HashSet JavaDoc<PetalsServiceUnitHandler>();
111     handlersSet.add(new ExternalServiceManager(context, logger));
112     servicesByInterfaceMap = new HashMap JavaDoc<QName JavaDoc, Set JavaDoc<ServiceEndpoint>>();
113 }
114
115
116     /**
117      * Add a new handler in the handlersSet
118      *
119      * @param suh
120      * The new handler to be added
121      */

122     public void addNewHandler(PetalsServiceUnitHandler suh) {
123         if (!isHandlerPresent(suh.getClass().getName())) {
124             handlersSet.add(suh);
125         }
126     }
127
128     /**
129      * Add a service endpoint for an interface QName
130      * @param interfaceName the interface QName
131      * @param serviceEndpoint the endpoint to add
132      */

133     public void addServiceForInterface(QName JavaDoc interfaceName,
134             ServiceEndpoint serviceEndpoint) {
135         Set JavaDoc<ServiceEndpoint> endpoints = null;
136         endpoints = servicesByInterfaceMap.get(interfaceName);
137         if (endpoints == null) {
138             endpoints = new HashSet JavaDoc<ServiceEndpoint>();
139         }
140         endpoints.add(serviceEndpoint);
141         servicesByInterfaceMap.put(interfaceName, endpoints);
142     }
143     
144     
145     /**
146      * Add a PetalsServiceUnitHandler for a ServiceEnpoint
147      * @param ep the endpoint
148      * @param handler the PetalsServiceUnitHandler
149      */

150     public void addSUHandlerForEndpoint(ServiceEndpoint ep, PetalsServiceUnitHandler handler) {
151         endpointSUHandler.put(ep, handler);
152     }
153     
154     public String JavaDoc deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
155         throws DeploymentException {
156         String JavaDoc result = null;
157         if (serviceUnitMap.get(serviceUnitName) == null) {
158             String JavaDoc serviceUnitType = getServiceUnitType(serviceUnitRootPath);
159             PetalsServiceUnitHandler serviceUnitManager = null;
160             Iterator JavaDoc<PetalsServiceUnitHandler> it = handlersSet.iterator();
161             while (result == null && it.hasNext()) {
162                 serviceUnitManager = it.next();
163                 result = serviceUnitManager.deploy(serviceUnitName,
164                          serviceUnitType, serviceUnitRootPath);
165             }
166             if (result != null) {
167                 handlersMap.put(serviceUnitName, serviceUnitManager);
168             } else {
169                 logger.log(Level.SEVERE, NO_HANDLER_FOR_THIS_TYPE);
170                 throw new DeploymentException(NO_HANDLER_FOR_THIS_TYPE);
171             }
172         } else {
173             throw new DeploymentException(DUPLICATE_SERVICE_UNIT);
174         }
175         return result;
176     }
177
178     public ServiceEndpoint findEndpointForService(QName JavaDoc serviceName) {
179         ServiceEndpoint result = null;
180         for(ServiceEndpoint endpoint : serviceUnitMap.values()) {
181             if (endpoint.getServiceName().equals(serviceName)){
182                 result = endpoint;
183                 break;
184             }
185         }
186         return result;
187     }
188     
189     /**
190      * Get the service description for one of the component endpoint
191      *
192      * @param se
193      * Service endpoint to get description for
194      * @return The service description or null if this endpoint is not found
195      */

196     public Document JavaDoc getServiceDescription(ServiceEndpoint se) {
197         Document JavaDoc result = null;
198         for (ServiceEndpoint ep : serviceDescriptionMap.keySet()) {
199             if (se.getServiceName().equals(ep.getServiceName())) {
200                 result = serviceDescriptionMap.get(ep);
201                 break;
202             }
203         }
204         return result;
205     }
206
207     public Set JavaDoc<ServiceEndpoint> getServicesForInterface(String JavaDoc interfaceName) {
208         return servicesByInterfaceMap.get(interfaceName);
209     }
210
211     /**
212      * Return the endpoint for a given service unit name
213      *
214      * @param serviceUnitName
215      * The service unit name
216      * @return The matching endpoint or null if no matching endpoint found
217      */

218     public ServiceEndpoint getServiceUnitEndpoint(String JavaDoc serviceUnitName) {
219         return serviceUnitMap.get(serviceUnitName);
220     }
221     
222     
223     /**
224      * Returns the PetalsServiceUnitHandler used to register this endpoint
225      * @param ep the endpoint
226      * @return a PetalsServiceUnitHandler, or null if the endpoint was not registered by this component
227      */

228     public PetalsServiceUnitHandler getServiceUnitHandlerForEndpoint(ServiceEndpoint ep) {
229         return endpointSUHandler.get(ep);
230     }
231
232     public void init(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
233         throws DeploymentException {
234
235         PetalsServiceUnitHandler serviceUnitManager = handlersMap
236                 .get(serviceUnitName);
237         if (serviceUnitManager != null) {
238             serviceUnitManager.init(serviceUnitName, serviceUnitRootPath);
239         } else {
240             logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
241             throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
242         }
243     }
244
245     /**
246      * Remove the service description for an endpoint
247      * @param ep the service endpoint for which the description will be removed
248      */

249     public void removeServiceDescription (ServiceEndpoint ep) {
250         serviceDescriptionMap.remove(ep);
251     }
252
253     public void removeServiceForInterface(ServiceEndpoint serviceEndpoint) {
254         for (Set JavaDoc<ServiceEndpoint> endpoints : servicesByInterfaceMap.values()){
255             endpoints.remove(serviceEndpoint);
256         }
257     }
258     
259     /**
260      * Remove the service description for an endpoint
261      * @param ep the service endpoint for which the description will be removed
262      */

263     public void removeServiceUnitEndpoint (String JavaDoc suName) {
264         serviceUnitMap.remove(suName);
265     }
266     
267     /**
268      * Remove the PetalsServiceUnitHandler for a ServiceEnpoint
269      * @param ep the endpoint
270      */

271     public void removeSUHandlerForEndpoint(ServiceEndpoint ep) {
272         endpointSUHandler.remove(ep);
273     }
274
275     /**
276      * Set the service description for a given endpoint
277      *
278      * @param se
279      * The service endpoint
280      * @param serviceDesc
281      * The dom document representation of the service description
282      */

283     public void setServiceDescription(ServiceEndpoint se, Document JavaDoc serviceDesc) {
284         serviceDescriptionMap.put(se, serviceDesc);
285     }
286     
287     /**
288      * Set the endpoint for a given service unit name
289      *
290      * @param serviceUnitName
291      * The name of the service unit
292      * @param serviceEP
293      * The service endpoint
294      */

295     public void setServiceUnitEndpoint(String JavaDoc serviceUnitName,
296             ServiceEndpoint serviceEP) {
297         serviceUnitMap.put(serviceUnitName, serviceEP);
298     }
299
300     public void shutDown(String JavaDoc serviceUnitName) throws DeploymentException {
301         PetalsServiceUnitHandler serviceUnitManager = handlersMap
302                 .get(serviceUnitName);
303         if (serviceUnitManager != null) {
304             serviceUnitManager.shutDown(serviceUnitName, this);
305         } else {
306             logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
307             throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
308         }
309     }
310
311     public void start(String JavaDoc serviceUnitName) throws DeploymentException {
312         PetalsServiceUnitHandler serviceUnitManager = handlersMap
313                 .get(serviceUnitName);
314         if (serviceUnitManager != null) {
315             serviceUnitManager.start(serviceUnitName, this);
316         } else {
317             logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
318             throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
319         }
320     }
321
322     public void stop(String JavaDoc serviceUnitName) throws DeploymentException {
323         PetalsServiceUnitHandler serviceUnitManager = handlersMap
324                 .get(serviceUnitName);
325         if (serviceUnitManager != null) {
326             serviceUnitManager.stop(serviceUnitName, this);
327         } else {
328             logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
329             throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
330         }
331     }
332     
333     public String JavaDoc undeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
334         throws DeploymentException {
335         PetalsServiceUnitHandler serviceUnitManager = handlersMap
336                 .get(serviceUnitName);
337         if (serviceUnitManager != null) {
338             String JavaDoc result = serviceUnitManager.undeploy(serviceUnitName,
339                     serviceUnitRootPath);
340             if (ManagementMessageUtil.getComponentTaskResult(
341                     context.getComponentName(), "undeploy",
342                     ManagementMessageUtil.TASK_RESULT_SUCCESS).equals(result)) {
343                 handlersMap.remove(serviceUnitName);
344             }
345             return result;
346         } else {
347             logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
348             throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND);
349         }
350     }
351
352     /**
353      * Checks if a handler with the given class name is already present in the
354      * handlerSet
355      *
356      * @param className
357      * Fully qualified class name
358      * @return true if class name found; false otherwise
359      */

360     protected boolean isHandlerPresent(String JavaDoc className) {
361         for (PetalsServiceUnitHandler suh : handlersSet) {
362             if (suh.getClass().getName().equals(className)) {
363                 return true;
364             }
365         }
366         return false;
367     }
368
369
370     /**
371      * Parse the jbi descriptor file of the service unit looking for a type node containing the service unit type.
372      * If no type node is found, null is returned
373      *
374      * @param suRootPath the service unit root path
375      * @return the value of the type node if found, else null
376      */

377     protected String JavaDoc getServiceUnitType(String JavaDoc suRootPath) {
378         String JavaDoc result = null;
379         File JavaDoc jbiXmlFile = new File JavaDoc(suRootPath + File.separator + "META-INF"
380                 + File.separator + "jbi.xml");
381         if (jbiXmlFile.exists()) {
382             Document JavaDoc configDoc = WSDLHelper.createDocumentFromWSDL(jbiXmlFile);
383             Node JavaDoc node = XMLHelper.findChild(configDoc, "services", true);
384             if (node != null) {
385                 if (node != null) {
386                     Node JavaDoc typeNode = XMLHelper.findChild(node, EXTENSIONS_XMLNS,
387                             "type", true);
388                     if (typeNode != null
389                             && typeNode.getNodeType() == Node.ELEMENT_NODE) {
390                         result = typeNode.getTextContent();
391                     }
392                 }
393             }
394         }
395         return result;
396     }
397     
398 }
399
Popular Tags