KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > mdb > MDBMessageEndPointFactory


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: MDBMessageEndPointFactory.java 940 2006-07-26 09:05:27Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container.mdb;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.List JavaDoc;
31
32 import javax.ejb.ActivationConfigProperty JavaDoc;
33 import javax.resource.ResourceException JavaDoc;
34 import javax.resource.spi.ActivationSpec JavaDoc;
35 import javax.resource.spi.InvalidPropertyException JavaDoc;
36 import javax.resource.spi.ResourceAdapter JavaDoc;
37 import javax.resource.spi.UnavailableException JavaDoc;
38 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
39 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
40 import javax.transaction.xa.XAResource JavaDoc;
41
42 import org.objectweb.easybeans.api.EZBContainer;
43 import org.objectweb.easybeans.api.FactoryException;
44 import org.objectweb.easybeans.api.bean.EasyBeansMDB;
45 import org.objectweb.easybeans.api.pool.PoolException;
46
47 /**
48  * Defines a class that will manage the message end point factory for the MDB.
49  * The super class will manage the pool of message end point.
50  * @author Florent Benoit
51  */

52 public class MDBMessageEndPointFactory extends MDBFactory implements MessageEndpointFactory JavaDoc {
53
54     /**
55      * Default name of the activation spec (JORAM).
56      */

57     public static final String JavaDoc DEFAULT_ACTIVATION_SPEC_NAME = "joramActivationSpec";
58
59     /**
60      * ActivationSpec object linked to this factory (used to activate or
61      * deactive an endpoint factory (us).
62      */

63     private ActivationSpec JavaDoc activationSpec = null;
64
65     /**
66      * Resource adapter that provides the activation spec implementation.
67      */

68     private ResourceAdapter JavaDoc resourceAdapter = null;
69
70     /**
71      * Default constructor (delegate to super class).
72      * @param className name of this factory (name of class that is managed)
73      * @param container the root component of this factory.
74      * @param activationSpec the activation Spec object used for
75      * activating/deactivating.
76      * @param resourceAdapter the resource adapter used to activate/deactivate
77      * ourself.
78      * @throws FactoryException if super constructor fails
79      */

80     public MDBMessageEndPointFactory(final String JavaDoc className, final EZBContainer container,
81             final ActivationSpec JavaDoc activationSpec, final ResourceAdapter JavaDoc resourceAdapter) throws FactoryException {
82         super(className, container);
83         this.activationSpec = activationSpec;
84         this.resourceAdapter = resourceAdapter;
85     }
86
87     /**
88      * Init the factory.
89      * @throws FactoryException if the initialization fails.
90      */

91     @Override JavaDoc
92     public void init() throws FactoryException {
93         initActivationSpec();
94
95         validateActivationSpec();
96
97         activate();
98     }
99
100
101     /**
102      * Call setters method on the activation spec object.
103      * @throws FactoryException if activation spec object is not configured.
104      */

105     private void initActivationSpec() throws FactoryException {
106         // Call setter method for each property found in the activation
107
// properties
108
List JavaDoc<ActivationConfigProperty JavaDoc> properties = getMessageDrivenInfo().getActivationConfigProperties();
109         if (properties != null) {
110             for (ActivationConfigProperty JavaDoc property : properties) {
111                 // get property name
112
String JavaDoc key = property.propertyName();
113                 // and value
114
String JavaDoc value = property.propertyValue();
115
116                 // define setter method name
117
String JavaDoc methodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
118
119                 // get Method (reflection)
120
Method JavaDoc m = null;
121                 try {
122                     m = activationSpec.getClass().getMethod(methodName, new Class JavaDoc[] {String JavaDoc.class});
123                 } catch (SecurityException JavaDoc e) {
124                     throw new FactoryException("Cannot get a method named '" + methodName
125                             + "' on activation spec object '" + activationSpec + "'.", e);
126                 } catch (NoSuchMethodException JavaDoc e) {
127                     throw new FactoryException("Cannot get a method named '" + methodName
128                             + "' on activation spec object '" + activationSpec + "'.", e);
129                 }
130
131                 // invoke method
132
try {
133                     m.invoke(activationSpec, value);
134                 } catch (IllegalArgumentException JavaDoc e) {
135                     throw new FactoryException("Cannot invoke method named '" + methodName + "' with value '" + value
136                             + "' on activation spec object '" + activationSpec + "'.", e);
137                 } catch (IllegalAccessException JavaDoc e) {
138                     throw new FactoryException("Cannot invoke method named '" + methodName + "' with value '" + value
139                             + "' on activation spec object '" + activationSpec + "'.", e);
140                 } catch (InvocationTargetException JavaDoc e) {
141                     throw new FactoryException("Cannot invoke method named '" + methodName + "' with value '" + value
142                             + "' on activation spec object '" + activationSpec + "'.", e);
143                 }
144
145             }
146         }
147
148     }
149
150     /**
151      * Validate the configuration used p, tje activation spec object.
152      * @throws FactoryException if the validation of the activation spec
153      * implementation object fails.
154      */

155     private void validateActivationSpec() throws FactoryException {
156         try {
157             activationSpec.validate();
158         } catch (InvalidPropertyException JavaDoc e) {
159             throw new FactoryException("Cannot validate the validation spec object", e);
160         }
161     }
162
163     /**
164      * Activate this endpoint factory on resource adapter with the activation
165      * spec object.
166      * @throws FactoryException if the activation fails.
167      */

168     private void activate() throws FactoryException {
169         try {
170             resourceAdapter.endpointActivation(this, activationSpec);
171         } catch (ResourceException JavaDoc e) {
172             throw new FactoryException(
173                     "Cannot activate the activationspec object and us (MessageEndPointFactory) on the resource adapter",
174                     e);
175         }
176     }
177
178     /**
179      * This is used to create a message endpoint. The message endpoint is
180      * expected to implement the correct message listener type.
181      * @param xaResource an optional XAResource instance used to get transaction
182      * notifications when the message delivery is transacted.
183      * @return a message endpoint instance.
184      * @throws UnavailableException indicates a transient failure in creating a
185      * message endpoint. Subsequent attempts to create a message
186      * endpoint might succeed.
187      */

188     public MessageEndpoint JavaDoc createEndpoint(final XAResource JavaDoc xaResource) throws UnavailableException JavaDoc {
189         // Instance that implements the MessageEndpoint interface
190
MDBMessageEndPoint messageEndpoint = null;
191
192         // get an instance of MDB
193
EasyBeansMDB easyBeansMDB = null;
194         try {
195             easyBeansMDB = getPool().get();
196         } catch (PoolException e) {
197             throw new UnavailableException JavaDoc("Cannot get instance in the pool", e);
198         }
199
200         // Build a wrapper around this mdb instance
201
// TODO: For now, this is only for JMS, needs to be changed.
202
messageEndpoint = new MDBMessageListenerEndPoint(this, easyBeansMDB);
203
204         // Set XAResource of the message endpoint.
205
messageEndpoint.setXaResource(xaResource);
206
207         return messageEndpoint;
208     }
209
210     /**
211      * Release an endpoint created by this factory.
212      * @param mdbMessageEndPoint the endpoint to release.
213      */

214     protected void releaseEndPoint(final MDBMessageEndPoint mdbMessageEndPoint) {
215         // Release the wrapped message driven bean
216
try {
217             getPool().release(mdbMessageEndPoint.getEasyBeansMDB());
218         } catch (PoolException e) {
219             throw new IllegalStateException JavaDoc("Cannot release the given message end point", e);
220         }
221     }
222
223     /**
224      * This is used to find out whether message deliveries to a target method on
225      * a message listener interface that is implemented by a message endpoint
226      * will be transacted or not. The message endpoint may indicate its
227      * transacted delivery preferences (at a per method level) through its
228      * deployment descriptor. The message delivery preferences must not change
229      * during the lifetime of a message endpoint.
230      * @param method description of a target method. This information about the
231      * intended target method allows an application server to find out
232      * whether the target method call will be transacted or not.
233      * @return boolean whether the specified method is transacted
234      * @throws NoSuchMethodException exception to throw
235      */

236     public boolean isDeliveryTransacted(final Method JavaDoc method) throws NoSuchMethodException JavaDoc {
237         // TODO : Not yet implemented
238
return false;
239     }
240
241     /**
242      * Stops the factory.
243      */

244     @Override JavaDoc
245     public void stop() {
246         // stop the pool.
247
super.stop();
248
249         // deactivate this factory
250
resourceAdapter.endpointDeactivation(this, activationSpec);
251
252     }
253 }
254
Popular Tags