KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > xquarebc > XQuareSUHandler


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 package org.objectweb.petals.binding.xquarebc;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 import javax.jbi.component.ComponentContext;
33 import javax.jbi.management.DeploymentException;
34 import javax.xml.namespace.QName JavaDoc;
35
36 import org.objectweb.petals.component.common.serviceunitmanager.handler.ExternalServiceManager;
37 import org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager;
38 import org.objectweb.petals.component.common.util.ManagementMessageUtil;
39 import org.objectweb.petals.component.common.util.WSDLHelper;
40 import org.objectweb.petals.component.common.util.XMLHelper;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.xquark.bridge.XQBridge;
44 import org.xquark.jdbc.datasource.JDBCDataSource;
45
46 /**
47  * Service Unit handler for the XQuare component.
48  *
49  * At startup, lookups for a single *.wsdl file and a single
50  * *.properties file. The first provides the service name, and the
51  * second provides properties that are given to the XQuare
52  * component and from there to the XQuareBCJBIProcessor and
53  * XQuareBCListener to use (look at their documentation about
54  * which properties are available).
55  *
56  * @version $Rev: 250 $Date: {date}
57  * @since Petals 1.0
58  * @author Marc Dutoo - Open Wide
59  *
60  */

61 public class XQuareSUHandler extends ExternalServiceManager {
62
63     public static final String JavaDoc JNDI_DATASOURCE = "jndidatasource.name";
64     public static final String JavaDoc DATABASE_URL = "database.url";
65     public static final String JavaDoc DATABASE_USER = "database.user";
66     public static final String JavaDoc DATABASE_PASSWORD = "database.password";
67     public static final String JavaDoc DATABASE_MAPPING = "database.mapping";
68     public static final String JavaDoc XML_CONNECTION_BASE_URI = "xmlconnection.baseuri";
69     public static final String JavaDoc WRAP_QUERY_RESULTS_IN_ROOT_TAG = "queryResults.wrapInRootTag";
70     public static final String JavaDoc XQUARE_BRIDGE = "xquare_bridge_object";
71     public static final String JavaDoc STANDALONE_DATASOURCE = "standalone_datasource_object";
72     
73     public static final String JavaDoc LISTENER_PROP_PREFIX = "newdatalistener.";
74     public static final String JavaDoc LISTENER_SERVICE_PROP = LISTENER_PROP_PREFIX + "service";
75     public static final String JavaDoc LISTENER_OPERATION_PROP = LISTENER_PROP_PREFIX + "operation";
76     
77     protected HashMap JavaDoc<String JavaDoc, Properties JavaDoc> serviceToPropertiesMap;
78     protected HashMap JavaDoc<String JavaDoc, String JavaDoc> serviceUnitToServiceMap;
79
80     public XQuareSUHandler() {
81         super();
82         SERVICE_UNIT_TYPE = "xquare_service";
83     }
84
85     public XQuareSUHandler(ComponentContext context, Logger JavaDoc logger,
86         HashMap JavaDoc<String JavaDoc, Properties JavaDoc> serviceToPropertiesMap) {
87         super(context, logger);
88         this.context = context;
89         this.logger = logger;
90         this.serviceToPropertiesMap = serviceToPropertiesMap;
91         this.serviceUnitToServiceMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
92         SERVICE_UNIT_TYPE = "xquare_service";
93     }
94
95     /* (non-Javadoc)
96      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#deploy(java.lang.String, java.lang.String, java.lang.String)
97      */

98     public String JavaDoc deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitType,
99         String JavaDoc serviceUnitRootPath) {
100         return super.deploy(serviceUnitName, serviceUnitType,
101             serviceUnitRootPath);
102     }
103
104     public void init(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
105     throws DeploymentException {
106         super.init(serviceUnitName, serviceUnitRootPath);
107     }
108
109     public String JavaDoc undeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
110     throws DeploymentException {
111         // closing bridge
112
String JavaDoc serviceName = this.serviceUnitToServiceMap.get(serviceUnitName);
113         Properties JavaDoc serviceProps = serviceToPropertiesMap.get(serviceName);
114         XQBridge bridge = (XQBridge) serviceProps.get(XQUARE_BRIDGE);
115         if (bridge != null) {
116             try {
117                 bridge.close();
118             } catch (Throwable JavaDoc t) {
119                 logger.warning("Error closing bridge when undeploying service " + serviceName);
120             }
121         }
122         // closing datasource (case of standalone datasource)
123
JDBCDataSource xquareDataSource = (JDBCDataSource) serviceProps.get(STANDALONE_DATASOURCE);
124         if (xquareDataSource != null) {
125             try {
126                 xquareDataSource.close();
127             } catch (Throwable JavaDoc t) {
128                 logger.warning("Error closing datasource when undeploying service " + serviceName);
129             }
130         }
131         return ManagementMessageUtil.getComponentTaskResult(context
132             .getComponentName(), "undeploy",
133             ManagementMessageUtil.TASK_RESULT_SUCCESS);
134     }
135
136     public void shutDown(String JavaDoc serviceUnitName,
137         PetalsServiceUnitManager epHandler) throws DeploymentException {
138         super.shutDown(serviceUnitName, epHandler);
139     }
140
141     public void start(String JavaDoc serviceUnitName, PetalsServiceUnitManager epHandler)
142     throws DeploymentException {
143         String JavaDoc serviceUnitRootPath = serviceUnitInstallationRootPath
144         .get(serviceUnitName);
145         File JavaDoc[] files = new File JavaDoc(serviceUnitRootPath).listFiles();
146         Document JavaDoc serviceDesc = null;
147         String JavaDoc propFileName = null;
148         QName JavaDoc service= null;
149
150         for (File JavaDoc file : files) {
151             if (file.getName().endsWith(".wsdl")) {
152                 // found the service definition
153
serviceDesc = WSDLHelper.createDocumentFromWSDL(file);
154             } else if (file.getName().endsWith(".properties")){
155                 // found the service configuration
156
propFileName = file.getAbsolutePath();
157             }
158         }
159
160         // activating found endpoints
161
if (serviceDesc != null) {
162             try {
163                 activateEndpointsFromJBIDescription(epHandler, serviceDesc,
164                     serviceUnitName, serviceUnitRootPath);
165                 service = WSDLHelper.getServiceNameFromWSDLDocument(serviceDesc)[0];
166             } catch (Exception JavaDoc ex) {
167                 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT + ex);
168                 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, ex);
169             }
170         } else {
171             throw new DeploymentException(INCOMPLETE_SERVICE_UNIT_PACKAGE);
172         }
173
174
175         // Now configuring the component for the service & properties found
176
if (service!=null && propFileName != null) {
177             Properties JavaDoc xquareProps = new Properties JavaDoc();
178             try {
179                 xquareProps.load(new FileInputStream JavaDoc(propFileName));
180             } catch (IOException JavaDoc e) {
181                 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT, e);
182                 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, e);
183             }
184
185             // some property polishing :
186
// 1. Correcting database mapping path
187
String JavaDoc databaseMappingFilepath = xquareProps.getProperty(DATABASE_MAPPING, "[none]");
188             File JavaDoc databaseMappingFile = new File JavaDoc(databaseMappingFilepath);
189             if (!databaseMappingFile.exists() && !databaseMappingFile.isAbsolute()) {
190                 // getting the absolute filepath
191
databaseMappingFilepath = serviceUnitRootPath
192                 + File.separatorChar + databaseMappingFilepath;
193                 databaseMappingFile = new File JavaDoc(databaseMappingFilepath);
194                 xquareProps.setProperty(DATABASE_MAPPING, databaseMappingFilepath);
195             }
196             if (!databaseMappingFile.exists() || databaseMappingFile.isDirectory()
197                     || !databaseMappingFile.canRead()) {
198                 throw new DeploymentException("Unable to find database mapping file at "
199                     + databaseMappingFilepath);
200             }
201             // 1. Correcting base URI
202
if (!xquareProps.contains(XML_CONNECTION_BASE_URI)) {
203                 xquareProps.setProperty(XML_CONNECTION_BASE_URI, "file://" + serviceUnitRootPath);
204             }
205
206             // adding the new service unit to ours
207
serviceUnitToServiceMap.put(serviceUnitName, service.getLocalPart());
208             serviceToPropertiesMap.put(service.getLocalPart(), xquareProps);
209         }
210
211         // The following is not used for now, would be for in-jbi.xml configuration
212
/*databaseMappingFilepath = "auction.map";
213         databaseUrl = "jdbc:mysql://localhost/petals-samples-xquare_auction";
214         databaseUser = "petals-samples-xquare";
215         databasePassword = "petals-samples-xquare";
216
217         String jbiFilePath = serviceUnitRootPath
218               + File.separator + "META-INF" + File.separator + "jbi.xml";
219         initConfig(new File(jbiFilePath));
220
221         File databaseMappingFile = new File(databaseMappingFilepath);
222         if (!databaseMappingFile.isAbsolute()) {
223             databaseMappingFilepath = serviceUnitRootPath
224                   + File.separatorChar + databaseMappingFilepath;
225             databaseMappingFile = new File(databaseMappingFilepath);
226         }
227         if (!databaseMappingFile.exists() || databaseMappingFile.isDirectory()
228                 || !databaseMappingFile.canRead()) {
229             throw new DeploymentException("Unable to find database mapping file at "
230                 + databaseMappingFilepath);
231         }*/

232     }
233
234     public void stop(String JavaDoc serviceUnitName, PetalsServiceUnitManager epHandler)
235     throws DeploymentException {
236         super.stop(serviceUnitName, epHandler);
237     }
238
239     /**
240      * NOT USED FOR NOW, property file used instead
241      * jbiXML define attibuts to configure files to allow echange files.
242      * @param jbiXml
243      * @return properties
244      * properties has all attributs to call an internal Petals service,
245      * the source directory, destination directory, service and method to be called
246      */

247     protected void initConfig(File JavaDoc jbiXml) {
248         Properties JavaDoc configProperties = new Properties JavaDoc();
249         Document JavaDoc jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXml);
250         Node JavaDoc node = XMLHelper.findChild(jbiDoc, "services", true);
251         if (node != null) {
252
253             Node JavaDoc providesNode = XMLHelper.findChild(node, "provides", true);
254             if (providesNode != null) {
255                 Node JavaDoc databaseUrlNode = XMLHelper.findChild(node,
256                     "database_url", true);
257
258                 if (databaseUrlNode != null) {
259                     String JavaDoc databaseUrl = databaseUrlNode.getTextContent();
260                     configProperties.put(DATABASE_URL, databaseUrl);
261                 }
262                 
263                 // add other database & xquare properties too
264
}
265
266             Node JavaDoc consumesNode = XMLHelper.findChild(node, "consumes", true);
267             if (consumesNode != null) {
268
269                 String JavaDoc service = XMLHelper.getAttributeValue(consumesNode,
270                     "service-name");
271                 if (service != null) {
272                     configProperties.put(XQuareSUHandler.LISTENER_SERVICE_PROP, service);
273                 }
274
275                 String JavaDoc operation = XMLHelper.getAttributeValue(consumesNode,
276                     "operation-name");
277                 if (operation != null) {
278                     configProperties.put(XQuareSUHandler.LISTENER_OPERATION_PROP, operation);
279                 }
280
281                 // add delimiter props too
282
}
283
284         }
285     }
286
287 }
288
Popular Tags