KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > mdb > inflow > JBossMessageEndpointFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.mdb.inflow;
23
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 import javax.ejb.TransactionManagementType JavaDoc;
30 import javax.ejb.TransactionAttribute JavaDoc;
31 import javax.ejb.TransactionAttributeType JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.MalformedObjectNameException JavaDoc;
35 import javax.resource.spi.ActivationSpec JavaDoc;
36 import javax.resource.spi.UnavailableException JavaDoc;
37 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
38 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
39 import javax.transaction.xa.XAResource JavaDoc;
40
41 import org.jboss.deployment.DeploymentException;
42 import org.jboss.ejb3.Container;
43 import org.jboss.ejb3.KernelAbstractionFactory;
44 import org.jboss.ejb3.tx.TxUtil;
45 import org.jboss.ejb3.mdb.MessagingContainer;
46 import org.jboss.mx.util.JMXExceptionDecoder;
47 import org.jboss.logging.Logger;
48
49 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
50
51 /**
52  * EJBProxyFactory for inflow message driven beans
53  *
54  * @version <tt>$Revision: 55301 $</tt>
55  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
56  * @author <a HREF="mailto:bill@jboss.com">Bill Burke</a>
57  */

58 public class JBossMessageEndpointFactory implements MessageEndpointFactory JavaDoc
59 {
60    private static final Logger log = Logger.getLogger(JBossMessageEndpointFactory.class);
61
62    /** Whether trace is enabled */
63    protected boolean trace = log.isTraceEnabled();
64    
65    /** Our container */
66    protected MessagingContainer container;
67    
68    /** The activation properties */
69    protected HashMap JavaDoc properties = new HashMap JavaDoc();
70    
71    /** The messaging type class */
72    protected Class JavaDoc messagingTypeClass;
73    
74    /** The resource adapter name */
75    protected String JavaDoc resourceAdapterName;
76    
77    protected ObjectName JavaDoc resourceAdapterObjectName;
78      
79    /** The activation spec */
80    protected ActivationSpec JavaDoc activationSpec;
81    
82    /** The interfaces */
83    protected Class JavaDoc[] interfaces;
84
85    /** The next proxy id */
86    protected SynchronizedInt nextProxyId = new SynchronizedInt(0);
87     
88    // Static --------------------------------------------------------
89

90    /** The signature for createActivationSpec */
91    protected String JavaDoc[] createActivationSpecSig = new String JavaDoc[]
92    {
93       Class JavaDoc.class.getName(),
94       Collection JavaDoc.class.getName()
95    };
96    
97    /** The signature for activate/deactivateEndpint */
98    protected String JavaDoc[] activationSig = new String JavaDoc[]
99    {
100       MessageEndpointFactory JavaDoc.class.getName(),
101       ActivationSpec JavaDoc.class.getName()
102    };
103          
104    // Constructors --------------------------------------------------
105

106    public JBossMessageEndpointFactory()
107    {
108    }
109    
110    // Public --------------------------------------------------------
111

112    /**
113     * Get the message driven container
114     *
115     * @return the container
116     */

117    public MessagingContainer getContainer()
118    {
119       return container;
120    }
121    
122    // MessageEndpointFactory implementation -------------------------
123

124    public MessageEndpoint JavaDoc createEndpoint(XAResource JavaDoc resource) throws UnavailableException JavaDoc
125    {
126       trace = log.isTraceEnabled();
127       
128       if (trace)
129          log.trace("createEndpoint " + this + " xaResource=" + resource);
130           
131       MessageEndpoint JavaDoc endpoint = createProxy(resource);
132         
133       if (trace)
134          log.trace("Created endpoint " + endpoint + " from " + this);
135
136       return endpoint;
137    }
138    
139    protected MessageEndpoint JavaDoc createProxy(XAResource JavaDoc resource)
140    {
141       try
142       {
143          Class JavaDoc proxyClass = java.lang.reflect.Proxy.getProxyClass(container.getBeanClass().getClassLoader(), interfaces);
144             
145          final Class JavaDoc[] constructorParams = {InvocationHandler JavaDoc.class};
146          java.lang.reflect.Constructor JavaDoc proxyConstructor = proxyClass.getConstructor(constructorParams);
147          
148          MessageInflowLocalProxy proxy = new MessageInflowLocalProxy(container);
149          proxy.setXaResource(resource);
150          proxy.setMessageEndpointFactory(this);
151          
152          Object JavaDoc[] args = {proxy};
153          MessageEndpoint JavaDoc endpoint = (MessageEndpoint JavaDoc)proxyConstructor.newInstance(args);
154          return endpoint;
155          
156       } catch (Exception JavaDoc e)
157       {
158          e.printStackTrace();
159          return null;
160       }
161    }
162
163    public boolean isDeliveryTransacted(Method JavaDoc method) throws NoSuchMethodException JavaDoc
164    {
165       TransactionManagementType JavaDoc mtype = TxUtil.getTransactionManagementType(container);
166       if (mtype == javax.ejb.TransactionManagementType.BEAN) return false;
167
168
169       TransactionAttribute JavaDoc attr = (TransactionAttribute JavaDoc)container.resolveAnnotation(method, TransactionAttribute JavaDoc.class);
170       if (attr == null)
171       {
172          attr =(TransactionAttribute JavaDoc)container.resolveAnnotation(TransactionAttribute JavaDoc.class);
173       }
174       TransactionAttributeType JavaDoc type = TransactionAttributeType.REQUIRED;
175       if (attr != null) type = attr.value();
176       return type == javax.ejb.TransactionAttributeType.REQUIRED;
177    }
178    
179    // ServiceMBeanSupport overrides ---------------------------------
180

181    public void start() throws Exception JavaDoc
182    {
183       // Resolve the message listener
184
resolveMessageListener();
185       resolveResourceAdapterName();
186       // Resolve the resource adapter
187
resolveResourceAdapter();
188       // Create the activation config
189
createActivationSpec();
190       // Set up proxy parameters
191
// Set the interfaces
192
interfaces = new Class JavaDoc[] { MessageEndpoint JavaDoc.class, messagingTypeClass};
193       // Activate
194
activate();
195    }
196    
197    public void stop() throws Exception JavaDoc
198    {
199       // Deactivate
200
deactivate();
201    }
202    // ContainerService implementation -------------------------------
203

204    /**
205     * Set the container for which this is an invoker to.
206     *
207     * @param container The container for which this is an invoker to.
208     */

209    public void setContainer(final Container container)
210    {
211       this.container = (MessagingContainer) container;
212    }
213    
214    // Object overrides ----------------------------------------------
215

216    /**
217     * Return a string representation of the current config state.
218     */

219    public String JavaDoc toString()
220    {
221       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(100);
222       buffer.append(super.toString());
223       buffer.append("{ resourceAdapter=").append(resourceAdapterName);
224       buffer.append(", messagingType=").append(container.getMessagingType());
225       buffer.append(", ejbName=").append(container.getEjbName());
226       buffer.append(", activationConfig=").append(properties.values());
227       buffer.append(", activationSpec=").append(activationSpec);
228       buffer.append("}");
229       return buffer.toString();
230    }
231    
232    // Protected -----------------------------------------------------
233

234    /**
235     * Resolve message listener class
236     *
237     * @throws DeploymentException for any error
238     */

239    protected void resolveMessageListener() throws DeploymentException
240    {
241       messagingTypeClass = container.getMessagingType();
242    }
243
244    /**
245     * Resolve the resource adapter name
246     *
247     * @return the resource adapter name
248     * @throws DeploymentException for any error
249     */

250    protected void resolveResourceAdapterName() throws DeploymentException
251    {
252       resourceAdapterName = container.getResourceAdaptorName();
253    }
254
255    protected void resolveResourceAdapter()
256    {
257       try
258       {
259          resourceAdapterObjectName = new ObjectName JavaDoc("jboss.jca:service=RARDeployment,name='" + resourceAdapterName + "'");
260          // todo register with kernel and push dependencies to kernel
261
}
262       catch (MalformedObjectNameException JavaDoc e)
263       {
264          throw new RuntimeException JavaDoc(e);
265       }
266    }
267    
268    /**
269     * Create the activation spec
270     *
271     * @throws DeploymentException for any error
272     */

273    protected void createActivationSpec() throws DeploymentException
274    {
275       properties = new HashMap JavaDoc(container.getActivationConfigProperties());
276          
277       Object JavaDoc[] params = new Object JavaDoc[]
278       {
279          messagingTypeClass,
280          properties.values()
281       };
282     
283       try
284       {
285          activationSpec = (ActivationSpec JavaDoc) KernelAbstractionFactory.getInstance().invoke(resourceAdapterObjectName, "createActivationSpec", params, createActivationSpecSig);
286       }
287       catch (Throwable JavaDoc t)
288       {
289          t = JMXExceptionDecoder.decode(t);
290          DeploymentException.rethrowAsDeploymentException("Unable to create activation spec ra=" + resourceAdapterObjectName +
291                " messaging-type=" + messagingTypeClass.getName() + " properties=" + container.getActivationConfigProperties(), t);
292       }
293    }
294    
295    /**
296     * Activate
297     *
298     * @throws DeploymentException for any error
299     */

300    protected void activate() throws DeploymentException
301    {
302       Object JavaDoc[] params = new Object JavaDoc[] { this, activationSpec };
303       try
304       {
305          KernelAbstractionFactory.getInstance().invoke(resourceAdapterObjectName, "endpointActivation", params, activationSig);
306       }
307       catch (Throwable JavaDoc t)
308       {
309          t = JMXExceptionDecoder.decode(t);
310          DeploymentException.rethrowAsDeploymentException("Endpoint activation failed ra=" + resourceAdapterObjectName +
311                " activationSpec=" + activationSpec, t);
312       }
313    }
314    
315    /**
316     * Deactivate
317     */

318    protected void deactivate()
319    {
320       Object JavaDoc[] params = new Object JavaDoc[] { this, activationSpec };
321       try
322       {
323          KernelAbstractionFactory.getInstance().invoke(resourceAdapterObjectName, "endpointDeactivation", params, activationSig);
324       }
325       catch (Throwable JavaDoc t)
326       {
327          t = JMXExceptionDecoder.decode(t);
328          log.warn("Endpoint activation failed ra=" + resourceAdapterObjectName +
329                " activationSpec=" + activationSpec, t);
330       }
331    }
332 }
333
Popular Tags