KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > axis2 > Axis2BCSUListener


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 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.axis2;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30 import javax.jbi.component.ComponentContext;
31 import javax.jbi.servicedesc.ServiceEndpoint;
32 import javax.xml.namespace.QName JavaDoc;
33
34 import org.apache.axis2.AxisFault;
35 import org.apache.axis2.Constants;
36 import org.apache.axis2.context.ConfigurationContext;
37 import org.apache.axis2.description.AxisService;
38 import org.apache.axis2.description.Parameter;
39 import org.apache.axis2.engine.AxisConfiguration;
40 import org.objectweb.petals.binding.axis2.listener.soap.PetalsAxisService;
41 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
42 import org.objectweb.petals.component.common.su.ServiceUnitListener;
43 import org.objectweb.petals.component.common.util.PetalsExtensionsUtil;
44 import org.objectweb.petals.tools.jbicommon.descriptor.Consumes;
45 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
46
47 /**
48  * A service unit listener used to register new service into Axis Engine during
49  * SU deployment.
50  *
51  * @author Christophe HAMERLING - eBMWebSourcing
52  *
53  */

54 public class Axis2BCSUListener implements ServiceUnitListener {
55
56     private ConfigurationContext axisConfigurationContext;
57
58     private ComponentContext componentContext;
59
60     private Logger JavaDoc logger;
61
62     private Map JavaDoc<String JavaDoc, JBIDescriptor> descriptors;
63
64     /**
65      * Creates a new instance of {@link Axis2BCSUListener}
66      *
67      * @param axisConfigurationContext
68      */

69     public Axis2BCSUListener(ConfigurationContext axisConfigurationContext,
70         ComponentContext componentContext, Logger JavaDoc logger) {
71         this.axisConfigurationContext = axisConfigurationContext;
72         this.componentContext = componentContext;
73         this.logger = logger;
74         this.descriptors = new HashMap JavaDoc<String JavaDoc, JBIDescriptor>();
75     }
76
77     /*
78      * (non-Javadoc)
79      *
80      * @see org.objectweb.petals.component.common.su.ServiceUnitListener#onSUDeployed(java.lang.String,
81      * java.lang.String,
82      * org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor)
83      */

84     public void onSUDeployed(String JavaDoc serviceUnitName, String JavaDoc suRootPath,
85         JBIDescriptor descriptor) throws PEtALSComponentSDKException {
86         logger.log(Level.FINE, "Deploying a new AxisService for SU "
87             + serviceUnitName);
88
89         descriptors.put(serviceUnitName, descriptor);
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.objectweb.petals.component.common.su.ServiceUnitListener#onSUStarted(java.lang.String)
96      */

97     public void onSUStarted(String JavaDoc serviceUnitName)
98         throws PEtALSComponentSDKException {
99
100         JBIDescriptor descriptor = descriptors.get(serviceUnitName);
101         if (descriptor != null) {
102             List JavaDoc<Consumes> consumesList = descriptor.getServices()
103                 .getConsumes();
104             for (Consumes consumes : consumesList) {
105                 registerAxisService(consumes);
106             }
107         }
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see org.objectweb.petals.component.common.su.ServiceUnitListener#onSUStopped(java.lang.String)
114      */

115     public void onSUStopped(String JavaDoc serviceUnitName)
116         throws PEtALSComponentSDKException {
117
118         JBIDescriptor descriptor = descriptors.get(serviceUnitName);
119         if (descriptor != null) {
120             // delete registered axis services
121
List JavaDoc<Consumes> consumesList = descriptor.getServices()
122                 .getConsumes();
123             for (Consumes consumes : consumesList) {
124                 unregisterAxisService(consumes);
125             }
126         }
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.objectweb.petals.component.common.su.ServiceUnitListener#onSUUndeployed(java.lang.String,
133      * java.lang.String,
134      * org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor)
135      */

136     public void onSUUndeployed(String JavaDoc serviceUnitName, String JavaDoc suRootPath,
137         JBIDescriptor descriptor) throws PEtALSComponentSDKException {
138
139         descriptors.remove(serviceUnitName);
140     }
141
142     /**
143      *
144      * @param serviceQName
145      * @throws PEtALSComponentSDKException
146      */

147     private void registerAxisService(Consumes consumes)
148         throws PEtALSComponentSDKException {
149
150         QName JavaDoc serviceQName = consumes.getServiceName();
151         String JavaDoc endPointName = PetalsExtensionsUtil
152             .extractValueFromKeyValueExtension(consumes.getExtensions(),
153                 PetalsExtensionsUtil.ADDRESS);
154
155         logger.log(Level.INFO, "Registering Axis Service : " + endPointName);
156
157         AxisConfiguration axisConfig = axisConfigurationContext
158             .getAxisConfiguration();
159
160         try {
161             AxisService axisService = null;
162             String JavaDoc consumedEndpoint = consumes.getEndpointName();
163
164             // try to find if the axisService has already been registered
165
axisService = axisConfig.getService(endPointName);
166
167             if (axisService == null) {
168                 // create a new axisService, according to the JBI
169
// servicedescription. The endpoint name is the value given in
170
// the address extension, the namespace is provided in
171
// serviceQName and the endpoint is the one that will be really
172
// consumed.
173
ServiceEndpoint endpoint = componentContext.getEndpoint(
174                     serviceQName, consumedEndpoint);
175
176                 axisService = new PetalsAxisService(endPointName,
177                     componentContext, endpoint);
178                 axisService.setTargetNamespace(serviceQName.getNamespaceURI());
179                 axisService.setEndpoint(consumedEndpoint);
180
181                 axisService.addParameter(new Parameter(Constants.SERVICE_CLASS,
182                     "PetalsReceiver"));
183
184                 // add service
185
axisConfig.addService(axisService);
186             } else {
187                 logger.log(Level.WARNING,
188                     "This service is already registered in Axis : "
189                         + axisService.getName());
190             }
191         } catch (AxisFault e) {
192             throw new PEtALSComponentSDKException(
193                 "Can not register Service into Axis context", e);
194         }
195     }
196
197     /**
198      * Unregister the service from Axis
199      *
200      * @param serviceQName
201      * @throws PEtALSComponentSDKException
202      */

203     private void unregisterAxisService(Consumes consumes)
204         throws PEtALSComponentSDKException {
205
206         // QName serviceQName = consumes.getServiceName();
207
String JavaDoc endPointName = PetalsExtensionsUtil
208             .extractValueFromKeyValueExtension(consumes.getExtensions(),
209                 PetalsExtensionsUtil.ADDRESS);
210
211         logger.log(Level.INFO, "Removing Axis service from list : "
212             + endPointName);
213
214         try {
215             // register an axis service to axis engine
216
AxisService axisService = axisConfigurationContext
217                 .getAxisConfiguration().getService(endPointName);
218
219             if (axisService != null) {
220                 axisConfigurationContext.getAxisConfiguration().removeService(
221                     axisService.getName());
222             }
223
224         } catch (AxisFault e) {
225             throw new PEtALSComponentSDKException(
226                 "Can not remove service from Axis context", e);
227         }
228     }
229
230 }
231
Popular Tags