KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > EjbMessageBean


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.cfg;
31
32 import com.caucho.bytecode.JClass;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.config.ConfigException;
35 import com.caucho.config.types.JndiBuilder;
36 import com.caucho.ejb.AbstractServer;
37 import com.caucho.ejb.EjbServerManager;
38 import com.caucho.ejb.message.MessageServer;
39 import com.caucho.java.gen.JavaClassGenerator;
40 import com.caucho.management.j2ee.J2EEManagedObject;
41 import com.caucho.util.L10N;
42
43 import javax.annotation.PostConstruct;
44 import javax.ejb.MessageDrivenBean JavaDoc;
45 import javax.jms.ConnectionFactory JavaDoc;
46 import javax.jms.Destination JavaDoc;
47 import javax.jms.MessageListener JavaDoc;
48 import javax.jms.Session JavaDoc;
49 import javax.naming.NamingException JavaDoc;
50 import java.lang.reflect.Modifier JavaDoc;
51
52 /**
53  * Configuration for an ejb entity bean.
54  */

55 public class EjbMessageBean extends EjbBean {
56   private static L10N L = new L10N(EjbMessageBean.class);
57
58   private ConnectionFactory JavaDoc _connectionFactory;
59   private Destination JavaDoc _destination;
60   private boolean _isContainerTransaction = true;
61   private int _acknowledgeMode = Session.AUTO_ACKNOWLEDGE;
62   private String JavaDoc _selector;
63   private String JavaDoc _subscriptionName;
64   private String JavaDoc _destinationType;
65   private String JavaDoc _destinationLink;
66   private int _consumerMax = 5;
67
68   /**
69    * Creates a new message bean configuration.
70    */

71   public EjbMessageBean(EjbConfig config, String JavaDoc ejbModuleName)
72   {
73     super(config, ejbModuleName);
74
75     _consumerMax = config.getEJBManager().getMessageConsumerMax();
76   }
77
78   /**
79    * Returns the kind of bean.
80    */

81   public String JavaDoc getEJBKind()
82   {
83     return "message";
84   }
85
86   /**
87    * Sets the ejb implementation class.
88    */

89   public void setEJBClass(Class JavaDoc ejbClass)
90     throws ConfigException
91   {
92     super.setEJBClass(ejbClass);
93
94     if (! MessageListener JavaDoc.class.isAssignableFrom(ejbClass))
95       throw error(L.l("'{0}' must implement javax.jms.MessageListener. Every message-driven bean must implement MessageListener.", ejbClass.getName()));
96
97     if (! MessageDrivenBean JavaDoc.class.isAssignableFrom(ejbClass) &&
98     ! isAllowPOJO())
99       throw error(L.l("'{0}' must implement javax.ejb.MessageDrivenBean. Every message-driven bean must implement MessageDrivenBean.", ejbClass.getName()));
100
101
102     if (Modifier.isAbstract(ejbClass.getModifiers()))
103       throw error(L.l("'{0}' must not be abstract. Every message-driven bean must be a fully-implemented class.",
104               ejbClass.getName()));
105
106     JMethod create = getMethod(getEJBClassWrapper(), "ejbCreate", new JClass[] {});
107
108     if (create == null) {
109       if (! isAllowPOJO()) {
110     throw error(L.l("{0}: ejbCreate() method is missing. Every message-driven bean must have an ejbCreate() method.",
111               ejbClass.getName()));
112       }
113     }
114     else if (! create.isPublic()) {
115       throw error(L.l("{0}: ejbCreate() must be public. Every message-driven bean must have a public ejbCreate method.",
116               ejbClass.getName()));
117     }
118   }
119
120   /**
121    * Creates the old EJB 2.0 message-driven-destination
122    */

123   public MessageDrivenDestination createMessageDrivenDestination()
124   {
125     return new MessageDrivenDestination();
126   }
127
128   /**
129    * Sets the JMS destination.
130    */

131   public void setDestination(JndiBuilder destination)
132     throws ConfigException, NamingException JavaDoc
133   {
134     if (! (destination.getObject() instanceof Destination JavaDoc))
135       throw new ConfigException(L.l("`{0}' needs to implement javax.jms.Destination.",
136                     destination.getObject()));
137     
138     _destination = (Destination JavaDoc) destination.getObject();
139   }
140
141   /**
142    * Returns the destination.
143    */

144   public Destination JavaDoc getDestination()
145   {
146     return _destination;
147   }
148
149   /**
150    * Sets the JMS destination type.
151    */

152   public void setMessageDestinationType(String JavaDoc type)
153     throws ConfigException, NamingException JavaDoc
154   {
155     _destinationType = type;
156   }
157
158   /**
159    * Sets the JMS destination link
160    */

161   public void setMessageDestinationLink(String JavaDoc link)
162     throws ConfigException, NamingException JavaDoc
163   {
164     _destinationLink = link;
165   }
166
167   /**
168    * Sets the connection factory.
169    */

170   public void setConnectionFactory(JndiBuilder factory)
171     throws ConfigException, NamingException JavaDoc
172   {
173     if (! (factory.getObject() instanceof ConnectionFactory JavaDoc))
174       throw new ConfigException(L.l("`{0}' needs to implement javax.jms.ConnectionFactory.",
175                     factory.getObject()));
176     
177     _connectionFactory = (ConnectionFactory JavaDoc) factory.getObject();
178   }
179
180   /**
181    * Returns the destination.
182    */

183   public ConnectionFactory JavaDoc getConnectionFactory()
184   {
185     return _connectionFactory;
186   }
187
188   /**
189    * Returns true if the container handles transactions.
190    */

191   public boolean getContainerTransaction()
192   {
193     return _isContainerTransaction;
194   }
195
196   /**
197    * Set true if the container handles transactions.
198    */

199   public void setContainerTransaction(boolean isContainerTransaction)
200   {
201     _isContainerTransaction = isContainerTransaction;
202   }
203
204   /**
205    * Returns the acknowledge mode.
206    */

207   public int getAcknowledgeMode()
208   {
209     return _acknowledgeMode;
210   }
211
212   /**
213    * Set the acknowledge mode.
214    */

215   public void setAcknowledgeMode(int acknowledgeMode)
216   {
217     _acknowledgeMode = acknowledgeMode;
218   }
219
220   /**
221    * Returns the message selector
222    */

223   public String JavaDoc getSelector()
224   {
225     return _selector;
226   }
227
228   /**
229    * Set the message selector.
230    */

231   public void setSelector(String JavaDoc selector)
232   {
233     _selector = selector;
234   }
235
236   /**
237    * Returns the durable subscription name
238    */

239   public String JavaDoc getSubscriptionName()
240   {
241     return _subscriptionName;
242   }
243
244   /**
245    * Set the message selector.
246    */

247   public void setSubscriptionName(String JavaDoc subscriptionName)
248   {
249     _subscriptionName = subscriptionName;
250   }
251
252   /**
253    * Set true if the container handles transactions.
254    */

255   public void setTransactionType(String JavaDoc type)
256     throws ConfigException
257   {
258     if (type.equals("Container")) {
259     }
260     else if (type.equals("Bean")) {
261     }
262     else
263       throw new ConfigException(L.l("`{0}' is an unknown transaction-type. transaction-type must be `Bean' or `Container'.", type));
264   }
265
266   public void setSecurityIdentity(SecurityIdentity identity)
267   {
268   }
269
270   /**
271    * Adds the activation config.
272    */

273   public ActivationConfig createActivationConfig()
274   {
275     return new ActivationConfig();
276   }
277
278   /**
279    * Sets the number of message consumers.
280    */

281   public void setMessageConsumerMax(int consumerMax)
282     throws ConfigException
283   {
284     _consumerMax = consumerMax;
285   }
286
287   /**
288    * Initialize
289    */

290   @PostConstruct
291   public void init()
292     throws ConfigException
293   {
294     J2EEManagedObject.register(new com.caucho.management.j2ee.MessageDrivenBean(this));
295   }
296
297   /**
298    * Deploys the bean.
299    */

300   public AbstractServer deployServer(EjbServerManager ejbManager,
301                      JavaClassGenerator javaGen)
302     throws ClassNotFoundException JavaDoc
303   {
304     MessageServer server = new MessageServer(ejbManager);
305
306     server.setEJBName(getEJBName());
307
308     //Class contextImplClass = javaGen.loadClass(getSkeletonName());
309
//server.setContextImplClass(contextImplClass);
310

311     server.setContextImplClass(getEJBClass());
312     server.setDestination(_destination);
313     server.setConsumerMax(_consumerMax);
314     
315     server.setInitProgram(getInitProgram());
316
317     return server;
318   }
319
320   public class ActivationConfig {
321     public void addActivationConfigProperty(ActivationConfigProperty prop)
322     {
323     }
324   }
325
326   public static class ActivationConfigProperty {
327     String JavaDoc _name;
328     String JavaDoc _value;
329
330     public void setActivationConfigPropertyName(String JavaDoc name)
331     {
332       _name = name;
333     }
334
335     public void setActivationConfigPropertyValue(String JavaDoc value)
336     {
337       _value = value;
338     }
339   }
340
341   public class MessageDrivenDestination {
342     public void setDestinationType(String JavaDoc value)
343       throws ConfigException, NamingException JavaDoc
344     {
345       setMessageDestinationType(value);
346     }
347     
348     public void setSubscriptionDurability(String JavaDoc durability)
349     {
350     }
351     
352     public void setJndiName(JndiBuilder destination)
353       throws ConfigException, NamingException JavaDoc
354     {
355       setDestination(destination);
356     }
357   }
358 }
359
Popular Tags