KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > csv > CsvSUHandler


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: CsvSUHandler.java 1168 2006-10-12 15:50:18Z alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.engine.csv;
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.Map JavaDoc;
29 import java.util.Properties 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.management.DeploymentException;
35 import javax.xml.namespace.QName JavaDoc;
36
37 import org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler;
38 import org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager;
39 import org.objectweb.petals.component.common.util.ManagementMessageUtil;
40 import org.objectweb.petals.component.common.util.WSDLHelper;
41
42 import org.w3c.dom.Document JavaDoc;
43
44 /**
45  * This is the SU handler for the CSV service engine.
46  *
47  * At startup, lookups for a single *.wsdl file and a single
48  * *.properties file. The first provides the service name, and the
49  * second provides properties that are given to the CSV
50  * component and from there to the CsvProcessor to use (look at
51  * the processor's documentation to learn which properties
52  * may be set).
53  *
54  * @version $Rev: 1068 $Date: {date}
55  * @since Petals 1.0
56  * @author Marc Dutoo - Open Wide
57  *
58  */

59 public class CsvSUHandler extends PetalsServiceUnitHandler {
60
61     /** csv SE map */
62     protected Map JavaDoc<String JavaDoc, Properties JavaDoc> mapServiceCsv;
63     /** csv SU map. Needed to be able to remove csv SE */
64     protected Map JavaDoc<String JavaDoc, String JavaDoc> mapServiceUnitCsv;
65
66
67     private CsvSUHandler() {
68         super();
69         SERVICE_UNIT_TYPE="csvServiceUnit";
70     }
71
72     public CsvSUHandler(ComponentContext context,
73             HashMap JavaDoc<String JavaDoc, Properties JavaDoc> mapServiceCsv, Logger JavaDoc logger) {
74         super(context, logger);
75         this.mapServiceCsv= mapServiceCsv;
76         this.mapServiceUnitCsv = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
77         SERVICE_UNIT_TYPE="csvServiceUnit";
78     }
79     
80
81     /*
82      * (non-Javadoc)
83      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#deploy(java.lang.String, java.lang.String, java.lang.String)
84      */

85     public String JavaDoc deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitType,
86             String JavaDoc serviceUnitRootPath) {
87         if (SERVICE_UNIT_TYPE.equals(serviceUnitType)) {
88             logger.info(" DEPLOY serviceUnitName " + serviceUnitName
89                     + " serviceUnitRootPath " + serviceUnitRootPath);
90             return ManagementMessageUtil.getComponentTaskResult(context
91                     .getComponentName(), "deploy",
92                     ManagementMessageUtil.TASK_RESULT_SUCCESS);
93         } else {
94             return null;
95         }
96     }
97
98     /* (non-Javadoc)
99      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#init(java.lang.String, java.lang.String)
100      */

101     public void init(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
102         throws DeploymentException {
103         this.serviceUnitInstallationRootPath.put(serviceUnitName, serviceUnitRootPath);
104     }
105
106     /* (non-Javadoc)
107      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#undeploy(java.lang.String, java.lang.String)
108      */

109     public String JavaDoc undeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
110         throws DeploymentException {
111         return ManagementMessageUtil.getComponentTaskResult(context
112                 .getComponentName(), "undeploy",
113                 ManagementMessageUtil.TASK_RESULT_SUCCESS);
114     }
115     
116     /* (non-Javadoc)
117      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#shutDown(java.lang.String, org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager)
118      */

119     public void shutDown(String JavaDoc serviceUnitName, PetalsServiceUnitManager epHandler) throws DeploymentException {
120         String JavaDoc csv = this.mapServiceUnitCsv.remove(serviceUnitName);
121         this.mapServiceCsv.remove(csv);
122     }
123
124     
125     /* (non-Javadoc)
126      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#start(java.lang.String, org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager)
127      */

128     public void start(String JavaDoc serviceUnitName, PetalsServiceUnitManager epHandler) throws DeploymentException {
129         logger.log(Level.FINE, "start serviceUnitName " + serviceUnitName);
130         
131         // looking for configuration files : one *.wsdl and one *.properties
132
// TODO use special tags in jbi.xml to set component properties instead
133
String JavaDoc serviceUnitRootPath = serviceUnitInstallationRootPath.get(serviceUnitName);
134         File JavaDoc[] files = new File JavaDoc(serviceUnitRootPath).listFiles();
135         Document JavaDoc serviceDesc = null;
136         String JavaDoc propFileName = null;
137         QName JavaDoc service= null;
138         
139         for (File JavaDoc file : files) {
140             if (file.getName().endsWith(".wsdl")) {
141                 // found the service definition
142
serviceDesc = WSDLHelper.createDocumentFromWSDL(file);
143             } else if (file.getName().endsWith(".properties")){
144                 // found the service configuration
145
propFileName = file.getAbsolutePath();
146             }
147         }
148         
149         // activating found endpoints
150
if (serviceDesc != null) {
151             try {
152                 activateEndpointsFromJBIDescription(epHandler, serviceDesc,
153                         serviceUnitName, serviceUnitRootPath);
154                 service = WSDLHelper.getServiceNameFromWSDLDocument(serviceDesc)[0];
155             } catch (Exception JavaDoc ex) {
156                 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT + ex);
157                 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, ex);
158             }
159         } else {
160             throw new DeploymentException(INCOMPLETE_SERVICE_UNIT_PACKAGE);
161         }
162         
163         // configuring the component for the service & properties found
164
if (service!=null && propFileName != null) {
165             Properties JavaDoc csvReaderProps = new Properties JavaDoc();
166             try {
167                 csvReaderProps.load(new FileInputStream JavaDoc(propFileName));
168             } catch (IOException JavaDoc e) {
169                 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT, e);
170                 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, e);
171             }
172             mapServiceUnitCsv.put(serviceUnitName, service.getLocalPart());
173             mapServiceCsv.put(service.getLocalPart(), csvReaderProps);
174         }
175     }
176
177     
178     /* (non-Javadoc)
179      * @see org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler#stop(java.lang.String, org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager)
180      */

181     public void stop(String JavaDoc serviceUnitName, PetalsServiceUnitManager epHandler) throws DeploymentException {
182         // When service unit is stopped, so properties of directory are deleted
183
String JavaDoc csv = this.mapServiceUnitCsv.remove(serviceUnitName);
184         this.mapServiceCsv.remove(csv);
185     }
186 }
187
Popular Tags