KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > edifact > EdifactSUHandler


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 : EdifactSUHandler.java 154 ${date} rbarraza
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.engine.edifact;
23
24 import java.io.File JavaDoc;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.jbi.component.ComponentContext;
32 import javax.jbi.management.DeploymentException;
33 import javax.xml.namespace.QName JavaDoc;
34
35 import org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler;
36 import org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager;
37 import org.objectweb.petals.component.common.util.ManagementMessageUtil;
38 import org.objectweb.petals.component.common.util.WSDLHelper;
39 import org.objectweb.petals.component.common.util.XMLHelper;
40
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43
44 /**
45  *
46  * @version $Rev: 250 $Date: {date}
47  * @since Petals 1.0
48  * @author Rocio BARRAZA - eBMWebsourcing
49  *
50  */

51 public class EdifactSUHandler extends PetalsServiceUnitHandler {
52
53     protected Map JavaDoc<QName JavaDoc, QName JavaDoc> mapEndpointXslt;
54
55     protected Map JavaDoc<String JavaDoc, QName JavaDoc> mapServiceUnitEndpoint;
56
57     public EdifactSUHandler(ComponentContext context,
58             Map JavaDoc<QName JavaDoc, QName JavaDoc> mapServiceXslt, Logger JavaDoc logger) {
59         this.context = context;
60         this.logger = logger;
61         this.mapEndpointXslt = mapServiceXslt;
62         this.mapServiceUnitEndpoint = new HashMap JavaDoc<String JavaDoc, QName JavaDoc>();
63         SERVICE_UNIT_TYPE = "EdiServiceUnit";
64     }
65
66     private EdifactSUHandler() {
67         SERVICE_UNIT_TYPE = "EdiServiceUnit";
68     }
69
70     public void init(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
71         throws DeploymentException {
72         logger.log(Level.FINE, "init serviceUnitName " + serviceUnitName);
73         this.serviceUnitInstallationRootPath.put(serviceUnitName,
74                 serviceUnitRootPath);
75     }
76
77     public void setContext(ComponentContext context) {
78         this.context = context;
79     }
80
81     public void setLogger(Logger JavaDoc logger) {
82         this.logger = logger;
83     }
84
85     public void shutDown(String JavaDoc serviceUnitName,
86             PetalsServiceUnitManager epHandler) throws DeploymentException {
87         QName JavaDoc service = this.mapServiceUnitEndpoint.remove(serviceUnitName);
88         this.mapEndpointXslt.remove(service);
89     }
90
91     public void start(String JavaDoc serviceUnitName, PetalsServiceUnitManager epHandler)
92         throws DeploymentException {
93         logger.log(Level.FINE, "start serviceUnitName " + serviceUnitName);
94         String JavaDoc serviceUnitRootPath = serviceUnitInstallationRootPath
95                 .get(serviceUnitName);
96         File JavaDoc[] files = new File JavaDoc(serviceUnitRootPath).listFiles();
97         Document JavaDoc serviceDesc = null;
98         QName JavaDoc service = null;
99         String JavaDoc xsltEndpoint = null;
100         File JavaDoc jbiXml = new File JavaDoc(serviceUnitRootPath + File.separator
101                 + "META-INF" + File.separator + "jbi.xml");
102
103         for (File JavaDoc file : files) {
104             if (file.getName().endsWith(".wsdl")) {
105                 serviceDesc = WSDLHelper.createDocumentFromWSDL(file);
106             }
107         }
108         if (serviceDesc != null) {
109             try {
110                 activateEndpointsFromJBIDescription(epHandler, serviceDesc,
111                         serviceUnitName, serviceUnitRootPath);
112
113                 xsltEndpoint = loadEndpointFromFile(jbiXml);
114                 service = getServiceNameFromJbiXml(jbiXml);
115
116             } catch (Exception JavaDoc ex) {
117                 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT + ex);
118                 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, ex);
119             }
120         } else {
121             throw new DeploymentException(INCOMPLETE_SERVICE_UNIT_PACKAGE);
122         }
123         if (service != null && xsltEndpoint != null) {
124             mapServiceUnitEndpoint.put(serviceUnitName, service);
125             mapEndpointXslt.put(service, QName.valueOf(xsltEndpoint));
126         }
127     }
128
129     public void stop(String JavaDoc serviceUnitName, PetalsServiceUnitManager epHandler)
130         throws DeploymentException {
131         /*
132          * When service unit is stopped, so properties of directory are deleted
133          */

134         QName JavaDoc endpoint = this.mapServiceUnitEndpoint.remove(serviceUnitName);
135         this.mapEndpointXslt.remove(endpoint);
136         try {
137             serviceUnitInstallationRootPath.remove(serviceUnitName);
138             deactivateEndpointsFromJBIDescription(serviceUnitName, epHandler);
139         } catch (Exception JavaDoc e) {
140             throw new DeploymentException(FAILED_DEACTIVATE_ENDPOINT, e);
141         }
142     }
143
144     public String JavaDoc undeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath)
145         throws DeploymentException {
146         return ManagementMessageUtil.getComponentTaskResult(context
147                 .getComponentName(), "undeploy",
148                 ManagementMessageUtil.TASK_RESULT_SUCCESS);
149     }
150
151     private String JavaDoc loadEndpointFromFile(File JavaDoc configFile) {
152         String JavaDoc value = null;
153         Document JavaDoc jbiDoc = WSDLHelper.createDocumentFromWSDL(configFile);
154         Node JavaDoc providesNode = XMLHelper.findChild(jbiDoc, "consumes", true);
155         value = XMLHelper.getAttributeValue(providesNode, "service-name");
156         return value;
157     }
158
159     @Override JavaDoc
160     public String JavaDoc deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitType,
161             String JavaDoc serviceUnitRootPath) throws DeploymentException {
162         if (SERVICE_UNIT_TYPE.equals(serviceUnitType)) {
163             logger.fine(" DEPLOY serviceUnitName " + serviceUnitName
164                     + " serviceUnitRootPath " + serviceUnitRootPath);
165             return ManagementMessageUtil.getComponentTaskResult(context
166                     .getComponentName(), "deploy",
167                     ManagementMessageUtil.TASK_RESULT_SUCCESS);
168         } else {
169             return null;
170         }
171     }
172
173 }
174
Popular Tags