KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > basic > binding > SimpleBindingComponentServiceUnitManager


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: SimpleBindingComponentServiceUnitManager.java 601 2006-06-13 12:53:50Z wjoseph $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.basic.binding;
23
24 import java.io.File JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set 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.jbi.servicedesc.ServiceEndpoint;
35 import javax.xml.namespace.QName JavaDoc;
36
37 import org.objectweb.petals.component.common.basic.AbstractServiceUnitManager;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40
41 /**
42  * Generic service unit manager. Only handles one type of service unit.
43  * Endpoints are activated by this manager. An abstract deploy method allows you
44  * to add extra information in the service unit descriptor.
45  *
46  * @version $Rev: 250 $ $Date: 2006-04-21 14:20:57 +0200 (ven, 21 avr 2006) $
47  * @since Petals 1.0
48  * @author wjoseph - eBMWebsourcing
49  */

50 public class SimpleBindingComponentServiceUnitManager extends
51     AbstractServiceUnitManager {
52
53     protected static final String JavaDoc NO_ENDPOINT_FOR_CONSUMES_NODE = "No endpoint was found matching information from the consumes node of the JBI descriptor";
54
55     protected Map JavaDoc<ServiceEndpoint, String JavaDoc> endpointAddress;
56
57     protected Map JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> extAddressesForSU;
58
59     protected ExternalListenerManager externalListenerManager;
60
61     /**
62      * Default contructor
63      */

64     public SimpleBindingComponentServiceUnitManager() {
65         super();
66         endpointAddress = new HashMap JavaDoc<ServiceEndpoint, String JavaDoc>();
67         extAddressesForSU = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>>();
68     }
69
70     public void setExternalListenerManager(
71         ExternalListenerManager externalListenerManager) {
72         this.externalListenerManager = externalListenerManager;
73     }
74
75     /**
76      * Create a new SimpleBindingComponentServiceUnitManager
77      *
78      * @param context
79      * Component's context
80      * @param log
81      * Component's logger
82      *
83      * @throws DeploymentException
84      */

85     public SimpleBindingComponentServiceUnitManager(ComponentContext context,
86         Logger JavaDoc log)
87         throws DeploymentException {
88         super(context, log);
89         endpointAddress = new HashMap JavaDoc<ServiceEndpoint, String JavaDoc>();
90         extAddressesForSU = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>>();
91     }
92
93     /**
94      * Returns the distant address for an endpoint. May be null if this endpoint
95      * has no address registered for it
96      *
97      * @param ep
98      * an Endpoint
99      * @return the distant address for this endpoint, or null
100      */

101     public String JavaDoc getAddressForEndpoint(ServiceEndpoint ep) {
102         return endpointAddress.get(ep);
103     }
104
105     /**
106      * Return the endpoint associated with a distant address. May be null if
107      * this address does not match any endpoint
108      *
109      * @param address
110      * The distant address
111      * @return the endpoint associated with the distant address, else null
112      */

113     public ServiceEndpoint getEndpointForAddress(String JavaDoc address) {
114         for (Map.Entry JavaDoc<ServiceEndpoint, String JavaDoc> entry : endpointAddress.entrySet()) {
115             if (entry.getValue() != null && entry.getValue().equals(address)){
116                 return entry.getKey();
117             }
118         }
119         return null;
120     }
121
122     /**
123      * Same process as for AbstractServiceUnitManager except the registered
124      * endpoint is stored with the distant address in a Map (non-Javadoc)
125      *
126      * @see org.objectweb.petals.component.common.basic.AbstractServiceUnitManager#start(java.lang.String)
127      */

128     @Override JavaDoc
129     public void start(String JavaDoc serviceUnitName) throws DeploymentException {
130         logger.log(Level.FINE, "start serviceUnitName " + serviceUnitName);
131         String JavaDoc suRootPath = serviceUnitInstallationRootPath
132             .get(serviceUnitName);
133         Document JavaDoc serviceDesc = getServiceUnitWSDL(suRootPath);
134         try {
135             ServiceEndpoint ep = activateEndpointsFromJBIDescription(
136                 serviceDesc, serviceUnitName, suRootPath);
137             registerAddressForEndpoint(ep, suRootPath);
138             String JavaDoc address = registerEndpointForAddress(suRootPath,
139                 serviceUnitName);
140             if (address != null) {
141                 externalListenerManager.startListening(address);
142             }
143         } catch (Exception JavaDoc ex) {
144             logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT + ex, ex);
145             throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, ex);
146         }
147     }
148
149     /**
150      * The distant address registered for this endpoint is removed (non-Javadoc)
151      *
152      * @see org.objectweb.petals.component.common.basic.AbstractServiceUnitManager#stop(java.lang.String)
153      */

154     public void stop(String JavaDoc serviceUnitName) throws DeploymentException {
155         try {
156             serviceUnitInstallationRootPath.remove(serviceUnitName);
157             deactivateEndpointsFromJBIDescription(serviceUnitName);
158             ServiceEndpoint ep = serviceUnitMap.get(serviceUnitName);
159             endpointAddress.remove(ep);
160             Set JavaDoc<String JavaDoc> addresses = extAddressesForSU.get(serviceUnitName);
161             if (addresses != null) {
162                 for (String JavaDoc address : addresses) {
163                     ServiceEndpoint seToRemove = getEndpointForAddress(address);
164                     if(seToRemove!= null){
165                         endpointAddress.remove(seToRemove);
166                     }
167                     externalListenerManager.stopListening(address);
168                 }
169
170             }
171         } catch (Exception JavaDoc e) {
172             throw new DeploymentException(FAILED_DEACTIVATE_ENDPOINT, e);
173         }
174         logger.log(Level.FINE, "stop serviceUnitName " + serviceUnitName);
175     }
176
177     /*
178      * (non-Javadoc)
179      *
180      * @see org.objectweb.petals.component.common.basic.AbstractServiceUnitManager#deploy(java.util.Map)
181      */

182     @Override JavaDoc
183     protected void deploy(Map JavaDoc<String JavaDoc, String JavaDoc> extensions) {
184     }
185
186     /**
187      * Register a distant address for the new endpoint. The address is found in
188      * the jbiXml file as an extension element.
189      *
190      * @param ep
191      * the new endpoint
192      * @param suRootPath
193      * the service unit's root path
194      */

195     protected void registerAddressForEndpoint(ServiceEndpoint ep,
196         String JavaDoc suRootPath) {
197         File JavaDoc jbiXmlFile = new File JavaDoc(suRootPath + File.separator + "META-INF"
198             + File.separator + "jbi.xml");
199         Node JavaDoc providesNode = getNode(jbiXmlFile, "provides");
200         if (providesNode != null) {
201             Map JavaDoc<String JavaDoc, String JavaDoc> extensions = getExtensions(providesNode);
202             endpointAddress.put(ep, extensions.get("address"));
203         }
204     }
205
206     /**
207      * Register an endpoint for the distant address. The address is found in the
208      * jbiXml file as an extension element. We search in the container if there
209      * is an endpoint for the serviceName and endpointName found in the consumes
210      * node. If no endpoint is found, we search for endpoints for the Interface
211      * Name found in the consumes node. If still no endpoint is foud an
212      * Exception is raised
213      *
214      * @param suRootPath
215      * the service unit's root path
216      * @param serviceUnitName
217      * the service unit name
218      * @return the registered address (listened address). Can be null if no
219      * "consumes" node can be found in the JBI descriptor or if no
220      * external address is provided
221      * @throws Exception
222      * If bo endpoint is found for this serviceName or Interface
223      * Name
224      */

225     protected String JavaDoc registerEndpointForAddress(String JavaDoc suRootPath,
226         String JavaDoc serviceUnitName) throws Exception JavaDoc {
227         String JavaDoc address = null;
228
229         File JavaDoc jbiXmlFile = new File JavaDoc(suRootPath + File.separator + "META-INF"
230             + File.separator + "jbi.xml");
231         Node JavaDoc consumesNode = getNode(jbiXmlFile, "consumes");
232         if (consumesNode != null) {
233             Map JavaDoc<String JavaDoc, String JavaDoc> extensions = getExtensions(consumesNode);
234             address = extensions.get("address");
235             if (address != null) {
236                 QName JavaDoc interfaceName = getInterfaceNameFromJbiXml(consumesNode);
237                 String JavaDoc endpointName = getEndpointNameFromJbiXml(consumesNode);
238                 QName JavaDoc serviceName = getServiceNameFromJbiXml(consumesNode);
239                 ServiceEndpoint endpoint = context.getEndpoint(serviceName,
240                     endpointName);
241                 if (endpoint == null) {
242                     ServiceEndpoint[] endpoints = context
243                         .getEndpoints(interfaceName);
244                     if (endpoints.length == 0) {
245                         throw new Exception JavaDoc(NO_ENDPOINT_FOR_CONSUMES_NODE);
246                     } else {
247                         endpointAddress.put(endpoints[0],address);
248                     }
249                 } else {
250                     endpointAddress.put(endpoint,address);
251                 }
252                 registerAddressForSU(address, serviceUnitName);
253             }
254         }
255         return address;
256     }
257
258     /**
259      * Add this address to the su (identified by its su name) address set
260      *
261      * @param address
262      * the address to register
263      * @param serviceUnitName
264      * the service unit name
265      */

266     protected void registerAddressForSU(String JavaDoc address, String JavaDoc serviceUnitName) {
267         // Retrieve the address set for this suName
268
Set JavaDoc<String JavaDoc> addresses = extAddressesForSU.get(serviceUnitName);
269
270         if (addresses == null) {
271             addresses = new HashSet JavaDoc<String JavaDoc>();
272             addresses.add(address);
273             extAddressesForSU.put(serviceUnitName, addresses);
274         } else {
275             // Add this address to the su set
276
addresses.add(address);
277         }
278     }
279 }
280
Popular Tags