KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > serviceunitmanager > handler > PetalsServiceUnitHandler


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

22 package org.objectweb.petals.component.common.serviceunitmanager.handler;
23
24 import java.io.File JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.jbi.JBIException;
30 import javax.jbi.component.ComponentContext;
31 import javax.jbi.management.DeploymentException;
32 import javax.jbi.servicedesc.ServiceEndpoint;
33 import javax.xml.namespace.QName JavaDoc;
34
35 import org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager;
36 import org.objectweb.petals.component.common.util.WSDLHelper;
37 import org.objectweb.petals.component.common.util.XMLHelper;
38 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41
42 /**
43  * This is the abstract class of all handlers managed by the
44  * PetalsServiceUnitManager
45  *
46  *
47  * @version $Rev: 250 $ $Date: 2006-04-21 14:20:57 +0200 (ven, 21 avr 2006) $
48  * @since Petals 1.0
49  * @author wjoseph - EBM WebSourcing
50  *
51  */

52 public abstract class PetalsServiceUnitHandler {
53
54     protected final static String JavaDoc SERVICE_UNIT_PROPERTY_FILE = "serviceUnit.xml";
55
56     protected static final String JavaDoc FAILED_ACTIVATE_ENDPOINT = "Activate new endpoint failed ";
57
58     protected static final String JavaDoc FAILED_DEACTIVATE_ENDPOINT = "Deactivate endpoint failed ";
59
60     protected static final String JavaDoc INCOMPLETE_SERVICE_UNIT_PACKAGE = "The deployed service package is incomplete ";
61     
62     protected static final String JavaDoc INVALID_SERVICE_DESCRIPTION = "The provided service description is not valid ";
63     
64     protected static final String JavaDoc ERROR_DUPLICATE_SERVICE_UNIT = "A service unit with the given name is already registered ";
65
66     protected ComponentContext context;
67
68     protected Logger JavaDoc logger;
69
70     protected String JavaDoc SERVICE_UNIT_TYPE;
71
72     protected JBIDescriptor descriptor;
73     
74     protected Map JavaDoc<String JavaDoc,String JavaDoc> serviceUnitInstallationRootPath = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
75
76     public PetalsServiceUnitHandler(){
77     }
78             
79     
80     public PetalsServiceUnitHandler(ComponentContext context,
81             Logger JavaDoc logger) {
82         this.context = context;
83         this.logger = logger;
84     }
85     
86    
87     /**
88      * Deploy a service unit. Before deploying, the service unit type if verified
89      * @param serviceUnitName name of the service unit to deploy
90      * @param serviceUnitType type of the service unit to deploy
91      * @param serviceUnitRootPath root path of the service unit to deploy
92      * @return a valid management message
93      * @throws DeploymentException If an error occurs during deployment
94      */

95     abstract public String JavaDoc deploy(String JavaDoc serviceUnitName,
96             String JavaDoc serviceUnitType, String JavaDoc serviceUnitRootPath) throws DeploymentException;
97
98     public ComponentContext getContext() {
99         return context;
100     }
101
102     public Logger JavaDoc getLogger() {
103         return logger;
104     }
105
106     public String JavaDoc getSERVICE_UNIT_TYPE() {
107         return SERVICE_UNIT_TYPE;
108     }
109
110     /* (non-Javadoc)
111      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#init(java.lang.String, java.lang.String)
112      */

113     abstract public void init(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
114         throws DeploymentException;
115
116     /* (non-Javadoc)
117      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#setContext(javax.jbi.component.ComponentContext)
118      */

119     public void setContext(ComponentContext context) {
120         this.context = context;
121     }
122
123     /* (non-Javadoc)
124      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#setLogger(java.util.logging.Logger)
125      */

126     public void setLogger(Logger JavaDoc logger) {
127         this.logger = logger;
128     }
129
130     public void setSERVICE_UNIT_TYPE(String JavaDoc service_unit_type) {
131         SERVICE_UNIT_TYPE = service_unit_type;
132     }
133
134     /* (non-Javadoc)
135      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#shutDown(java.lang.String, org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager)
136      */

137     abstract public void shutDown(String JavaDoc serviceUnitName,
138             PetalsServiceUnitManager epHandler) throws DeploymentException;
139     
140     /* (non-Javadoc)
141      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#start(java.lang.String, org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager)
142      */

143     abstract public void start(String JavaDoc serviceUnitName,
144             PetalsServiceUnitManager epHandler) throws DeploymentException;
145
146     /* (non-Javadoc)
147      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#stop(java.lang.String, org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager)
148      */

149     abstract public void stop(String JavaDoc serviceUnitName,
150             PetalsServiceUnitManager epHandler) throws DeploymentException;
151
152     /* (non-Javadoc)
153      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#undeploy(java.lang.String, java.lang.String)
154      */

155     abstract public String JavaDoc undeploy(String JavaDoc serviceUnitName,
156             String JavaDoc serviceUnitRootPath) throws DeploymentException;
157
158     /**
159      * Activate an endpoint in Petals platform, put the activate endpoint add the service description of this endpoint in HashMap,
160      * add the service unit name and the endpoint in a HashMap and add the interface QName and the endpoint in a HashMap
161      * @param epHandler the component service unit manager
162      * @param serviceDesc the service unit service description
163      * @param serviceUnitName the service unit name
164      * @param suRootPath the service unit root path
165      * @throws Exception If errors occur during processing
166      */

167     protected void activateEndpointsFromJBIDescription(
168             PetalsServiceUnitManager epHandler, Document JavaDoc serviceDesc,
169             String JavaDoc serviceUnitName, String JavaDoc suRootPath) throws Exception JavaDoc {
170         File JavaDoc jbiXmlFile = new File JavaDoc(suRootPath + File.separator
171                 + "META-INF" + File.separator + "jbi.xml");
172         /*JBIDescriptorBuilder descriptorBuilder = new JBIDescriptorBuilder(
173                 jbiXmlFile.toURI(), null);
174         JBIDescriptor descriptor = descriptorBuilder.build();
175         Services services = descriptor.getServices();
176         for (Provides provides : services.getProvides()) {*/

177         /*
178          * TODO here only the first provides node is activated, if there is more problem
179          */

180             QName JavaDoc interfaceName = getInterfaceNameFromJbiXml(jbiXmlFile);
181             String JavaDoc endpointName = getEndpointNameFromJbiXml(jbiXmlFile);
182             QName JavaDoc serviceName = getServiceNameFromJbiXml(jbiXmlFile);
183             ServiceEndpoint ep = context.activateEndpoint(serviceName,
184                     endpointName);
185             epHandler.setServiceDescription(ep, serviceDesc);
186             epHandler.setServiceUnitEndpoint(serviceUnitName, ep);
187             epHandler.addServiceForInterface(interfaceName, ep);
188             epHandler.addSUHandlerForEndpoint(ep, this);
189         //}
190
}
191     
192     /**
193      * Deactivate an endpoint in Petals platform, remove the activate endpoint from various HashMap in which it was registered
194      * @param epHandler the component service unit manager
195      * @param ep the service endpoint to be deactivated
196      * @throws Exception If errors occur during processing
197      */

198     protected void deactivateEndpointsFromJBIDescription( String JavaDoc serviceUnitName,
199             PetalsServiceUnitManager epHandler) throws JBIException {
200             ServiceEndpoint ep = epHandler.getServiceUnitEndpoint(serviceUnitName);
201             if(ep != null) {
202                 context.deactivateEndpoint(ep);
203                 epHandler.removeServiceDescription(ep);
204                 epHandler.removeServiceUnitEndpoint(serviceUnitName);
205                 epHandler.removeServiceForInterface(ep);
206                 epHandler.removeSUHandlerForEndpoint(ep);
207             }
208     }
209     
210     /**
211      * Return the endpoint name parse from the jbi descriptor file
212      * @param jbiXml jbi descriptor file
213      * @return the endpoint name if found, else null
214      */

215     protected String JavaDoc getEndpointNameFromJbiXml(File JavaDoc jbiXml){
216         String JavaDoc result = new String JavaDoc();
217         Document JavaDoc jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXml);
218         Node JavaDoc providesNode = XMLHelper.findChild(jbiDoc, "provides", true);
219         result = XMLHelper.getAttributeValue(providesNode, "endpoint-name");
220         return result;
221     }
222     
223     /**
224      * Return the interface QName parse from the jbi descriptor file
225      * @param jbiXml jbi descriptor file
226      * @return the interface QName if found, else null
227      */

228     protected QName JavaDoc getInterfaceNameFromJbiXml(File JavaDoc jbiXml){
229         QName JavaDoc result = null;
230         Document JavaDoc jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXml);
231         Node JavaDoc providesNode = XMLHelper.findChild(jbiDoc, "provides", true);
232         result = QName.valueOf(XMLHelper.getAttributeValue(providesNode, "interface-name"));
233         return result;
234     }
235
236     /**
237      * Return the service QName parse from the jbi descriptor file
238      * @param jbiXml jbi descriptor file
239      * @return the service QName if found, else null
240      */

241     protected QName JavaDoc getServiceNameFromJbiXml(File JavaDoc jbiXml){
242         QName JavaDoc result = null;
243         Document JavaDoc jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXml);
244         Node JavaDoc providesNode = XMLHelper.findChild(jbiDoc, "provides", true);
245         result = QName.valueOf(XMLHelper.getAttributeValue(providesNode, "service-name"));
246         return result;
247     }
248
249 }
250
Popular Tags