KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > deployment > api > MessageDrivenDesc


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@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: MessageDrivenDesc.java,v 1.25 2005/02/03 08:18:47 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.deployment.api;
27
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ListIterator JavaDoc;
31
32 import org.objectweb.jonas_ejb.deployment.xml.ActivationConfig;
33 import org.objectweb.jonas_ejb.deployment.xml.ActivationConfigProperty;
34 import org.objectweb.jonas_ejb.deployment.xml.AssemblyDescriptor;
35 import org.objectweb.jonas_ejb.deployment.xml.JonasMessageDriven;
36 import org.objectweb.jonas_ejb.deployment.xml.MessageDriven;
37 import org.objectweb.jonas_ejb.deployment.xml.MessageDrivenDestination;
38 import org.objectweb.jonas_ejb.lib.BeanNaming;
39
40 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
41
42 import org.objectweb.jonas_lib.deployment.xml.JLinkedList;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 /**
47  * Class to hold meta-information related to a message driven bean
48  *
49  * @author Christophe Ney [cney@batisseurs.com] : Initial developer
50  * @author Helene Joanin
51  */

52 public class MessageDrivenDesc extends BeanDesc {
53
54     public final static int AUTO_ACKNOWLEDGE = 1;
55     public final static int DUPS_OK_ACKNOWLEDGE = 2;
56     protected static final String JavaDoc[] ACKMODE = {null, "AUTO_ACKNOWLEDGE",
57             "DUPS_OK_ACKNOWLEDGE"};
58
59     public final static int SUBS_DURABLE = 1;
60     public final static int SUBS_NON_DURABLE = 2;
61     protected static final String JavaDoc[] SUBS_DURABILITY = {null,
62             "SUBSCRIPTION_DURABLE", "SUBSCRIPTION_NON_DURABLE"};
63
64     public final static int DEFAULT_MAX_MESSAGES = 1;
65
66     protected String JavaDoc selector = null;
67     protected int acknowledgeMode;
68     protected int subscriptionDurability = SUBS_NON_DURABLE;
69     protected Class JavaDoc destinationType = null;
70     protected int txAttribute = MethodDesc.TX_NOT_SET; // for onMessage method
71
protected boolean isTopicDestination = false;
72     protected int transactionType;
73     protected String JavaDoc destinationJndiName = null;
74     protected String JavaDoc destinationLink = null;
75     
76     // Used with JMS Rars
77
protected String JavaDoc destination = null;
78
79     protected ActivationConfigDesc mdActivationConfigDesc = null;
80
81     protected ActivationConfigDesc mdJonasActivationConfigDesc = null;
82
83     /**
84      * package protected constructor used by API
85      */

86     MessageDrivenDesc(ClassLoader JavaDoc classLoader, MessageDriven md,
87             AssemblyDescriptor asd, JonasMessageDriven jMd,
88             JLinkedList jMDRList, String JavaDoc fileName)
89             throws DeploymentDescException {
90
91         super(classLoader, md, jMd, asd, jMDRList, fileName);
92         mdActivationConfigDesc = new ActivationConfigDesc(md.getActivationConfig());
93         mdJonasActivationConfigDesc = new ActivationConfigDesc(jMd.getActivationConfig());
94
95         // min-pool-size
96
if (jMd.getMinPoolSize() != null) {
97             String JavaDoc tstr = jMd.getMinPoolSize();
98             Integer JavaDoc tval = new Integer JavaDoc(tstr);
99             poolMin = tval.intValue();
100         }
101
102         // max-cache-size
103
if (jMd.getMaxCacheSize() != null) {
104             String JavaDoc tstr = jMd.getMaxCacheSize();
105             Integer JavaDoc tval = new Integer JavaDoc(tstr);
106             cacheMax = tval.intValue();
107         }
108
109         // necessary check
110
if (md.getEjbName() == null) {
111             throw new Error JavaDoc("No ejb-name specified for a message-driven bean");
112         }
113
114         // transaction-type
115
if (md.getTransactionType().equals("Bean")) {
116             transactionType = BEAN_TRANSACTION_TYPE;
117         } else if (md.getTransactionType().equals("Container")) {
118             transactionType = CONTAINER_TRANSACTION_TYPE;
119         } else {
120             throw new DeploymentDescException(
121                     "Invalid transaction-type content for ejb-name " + ejbName);
122         }
123
124         // message driven destination
125
if (jMd.getJonasMessageDrivenDestination() != null) {
126             destinationJndiName = jMd.getJonasMessageDrivenDestination()
127                     .getJndiName();
128         }
129
130         // Set values can be from old 2.0 way, ActivationConfig in std xml,
131
// or ActivationConfig in jonas xml
132

133         // message selector
134
selector = md.getMessageSelector();
135         // acknowledge mode
136
if (md.getAcknowledgeMode() == null) {
137             acknowledgeMode = AUTO_ACKNOWLEDGE;
138         } else {
139             if (md.getAcknowledgeMode().equals("Auto-acknowledge")) {
140                 acknowledgeMode = AUTO_ACKNOWLEDGE;
141             } else if (md.getAcknowledgeMode().equals("Dups-ok-acknowledge")) {
142                 acknowledgeMode = DUPS_OK_ACKNOWLEDGE;
143             } else {
144                 throw new DeploymentDescException("Invalid acknowledge-mode content for ejb-name " + ejbName);
145             }
146         }
147         MessageDrivenDestination d = md.getMessageDrivenDestination();
148         if (d != null && d.getDestinationType() != null) {
149             if (d.getDestinationType().equals("javax.jms.Queue")) {
150                 destinationType = javax.jms.Queue JavaDoc.class;
151             } else if (d.getDestinationType().equals("javax.jms.Topic")) {
152                 destinationType = javax.jms.Topic JavaDoc.class;
153                 isTopicDestination = true;
154             } else {
155                 try {
156                     destinationType = classLoader.loadClass(d.getDestinationType());
157                 } catch (Exception JavaDoc ex) {
158                     throw new DeploymentDescException("Invalid destination-type for ejb-name " + ejbName);
159                 }
160             }
161             if (d.getSubscriptionDurability() != null) {
162                 if (destinationType.equals(javax.jms.Queue JavaDoc.class)) {
163                     throw new DeploymentDescException("subscription-durability of message-driven-destination for ejb-name "
164                                                       + ejbName + " defined");
165                 }
166                 if (d.getSubscriptionDurability().equals("Durable")) {
167                     subscriptionDurability = SUBS_DURABLE;
168                 } else if (d.getSubscriptionDurability().equals("NonDurable")) {
169                     subscriptionDurability = SUBS_NON_DURABLE;
170                 } else {
171                     throw new DeploymentDescException("Invalid subscription-durability content for ejb-name " + ejbName);
172                 }
173             } else {
174                 // non-durable subscription default value for topic
175
if (destinationType.equals(javax.jms.Topic JavaDoc.class)) {
176                     subscriptionDurability = SUBS_NON_DURABLE;
177                 }
178             }
179
180         }
181
182         destinationLink = md.getMessageDestinationLink();
183
184         if( mdActivationConfigDesc != null) {
185             configureAC(mdActivationConfigDesc, classLoader);
186         }
187         
188         if( mdJonasActivationConfigDesc != null) {
189             configureAC(mdJonasActivationConfigDesc, classLoader);
190         }
191         
192         if (destinationJndiName == null) {
193             throw new Error JavaDoc("No destination specified for message-driven bean " + ejbName);
194         }
195
196         // cache TxAttribute for onMessage and ejbTimeout
197
for (Iterator JavaDoc i = getMethodDescIterator(); i.hasNext();) {
198             MethodDesc methd = (MethodDesc) i.next();
199             if (methd.getMethod().getName().equals("onMessage")) {
200                 txAttribute = methd.getTxAttribute();
201             }
202             if (methd.getMethod().getName().equals("ejbTimeout")) {
203                 timerTxAttribute = methd.getTxAttribute();
204                 ejbTimeoutSignature = BeanNaming.getSignature(getEjbName(), methd.getMethod());
205             }
206         }
207     }
208
209     /**
210      * Get transaction management type of the message driven.
211      *
212      * @return transaction type value within
213      * BEAN_TRANSACTION_TYPE,CONTAINER_TRANSACTION_TYPE
214      */

215     public int getTransactionType() {
216         return transactionType;
217     }
218
219     /**
220      * Return the transaction attribute for the onMessage method of Message
221      * driven bean
222      *
223      */

224     public int getTxAttribute() {
225         return txAttribute;
226     }
227
228     /**
229      * @return true if BEAN_TRANSACTION_TYPE
230      */

231     public boolean isBeanManagedTransaction() {
232         return (transactionType == BeanDesc.BEAN_TRANSACTION_TYPE);
233     }
234
235     /**
236      * Get the the destination name of the message driven bean.
237      * @return name of the destination of the message driven bean.
238      */

239     public String JavaDoc getDestination() {
240         return(destination);
241     }
242
243     /**
244      * Get the the destination JNDI name of the message driven bean.
245      *
246      * @return JNDI name of the destination of the message driven bean.
247      */

248     public String JavaDoc getDestinationJndiName() {
249         return (destinationJndiName);
250     }
251
252     /**
253      * Get the the destination link name of the message driven bean.
254      *
255      * @return link name of the destination of the message driven bean.
256      */

257     public String JavaDoc getDestinationLink() {
258         return (destinationLink);
259     }
260
261     /**
262      * Get the the destination type of the message driven bean.
263      *
264      * @return type of the destination of the message driven bean.
265      */

266     public Class JavaDoc getDestinationType() {
267         return (destinationType);
268     }
269
270     /**
271      * Return true if it is a Topic destination
272      */

273     public boolean isTopicDestination() {
274         return (isTopicDestination);
275     }
276
277     /**
278      * Assessor for existence of a message-selector for the message driven bean
279      *
280      * @return true if message-selector is defined for the bean
281      */

282     public boolean hasSelector() {
283         return (selector != null);
284     }
285
286     /**
287      * Get the message-selector value of the message driven bean.
288      *
289      * @return value of the message selector return null if no selector
290      */

291     public String JavaDoc getSelector() {
292         return (selector);
293     }
294
295     /**
296      * Get the acknowledge-mode of the message driven bean.
297      *
298      * @return acknowledge-mode value within AUTO_ACKNOWLEDGE,
299      * DUPS_OK_ACKNOWLEDGE
300      */

301     public int getAcknowledgeMode() {
302         return (acknowledgeMode);
303     }
304
305     /**
306      * Get the the durability of the subscription of the message driven bean.
307      *
308      * @return durability of the subscription value within SUBS_DURABLE,
309      * SUBS_NON_DURABLE
310      */

311     public int getSubscriptionDurability() {
312         return (subscriptionDurability);
313     }
314
315     public boolean isSubscriptionDurable() {
316         return (subscriptionDurability == SUBS_DURABLE);
317     }
318
319     /**
320      * Return true if tx attribute for onMessage is Required
321      */

322     public boolean isRequired() {
323         return (txAttribute == MethodDesc.TX_REQUIRED);
324     }
325
326     /**
327      * @return the maximum number of messages that can be assigned to a server
328      * session at one time. will be configurable in the future
329      */

330     public int getMaxMessages() {
331         return DEFAULT_MAX_MESSAGES;
332     }
333
334     /**
335      * check that trans-attribute is valid for bean
336      */

337     protected void checkTxAttribute(MethodDesc md)
338             throws DeploymentDescException {
339         java.lang.reflect.Method JavaDoc m = md.getMethod();
340         if (getTransactionType() == CONTAINER_TRANSACTION_TYPE) {
341             if (md.getTxAttribute() == MethodDesc.TX_NOT_SET) {
342                 // trans-attribute not set !
343
// trace a warning and set the tx-attribute with the default value
344
logger.log(BasicLevel.WARN,
345                            "trans-attribute missing for method "
346                            + m.toString() + " in message driven bean "
347                            + getEjbName()
348                            + " (set to the default value "
349                            + MethodDesc.TX_STR_DEFAULT_VALUE_4_MDB
350                            + ")");
351                 md.setTxAttribute(MethodDesc.TX_STR_DEFAULT_VALUE_4_MDB);
352             }
353             if ((md.getTxAttribute() != MethodDesc.TX_REQUIRED)
354                     && (md.getTxAttribute() != MethodDesc.TX_NOT_SUPPORTED)) {
355                 if (!"ejbTimeout".equals(md.getMethod().getName())) {
356                     throw new DeploymentDescException(md.getTxAttributeName()
357                             + " is not a valid trans-attribute for method "
358                             + m.toString() + " in message driven bean "
359                             + getEjbName());
360                 }
361             }
362         } else {
363             if (md.getTxAttribute() != MethodDesc.TX_NOT_SET) {
364                 throw new DeploymentDescException(
365                         "trans-attribute for message driven bean "
366                                 + getEjbName()
367                                 + " must not be defined (transaction bean managed)");
368             }
369         }
370     }
371
372     /**
373      * Check that the message diven bean descriptor is valid
374      *
375      * @exception DeploymentDescException
376      * thrown for non-valid bean
377      */

378     public void check() throws DeploymentDescException {
379         super.check();
380     }
381
382     /**
383      * String representation of the object for test purpose
384      *
385      * @return String representation of this object
386      */

387     public String JavaDoc toString() {
388         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
389         ret.append(super.toString());
390         ret.append("\ngetTransactionType()=" + TRANS[getTransactionType()]);
391         if (hasSelector()) {
392             ret.append("\ngetSelector()=\"" + getSelector() + "\"");
393         }
394         ret.append("\ngetAcknowledgeMode()=" + ACKMODE[getAcknowledgeMode()]);
395         if (getDestinationType() != null) {
396             ret.append("\ngetDestinationType()="
397                     + getDestinationType().getName());
398         }
399         ret.append("\ngetSubscriptionDurability()="
400                 + SUBS_DURABILITY[getSubscriptionDurability()]);
401         ret.append("\ngetDestinationJndiName()=" + getDestinationJndiName());
402         return ret.toString();
403     }
404
405
406     /**
407      * @return the MessageDriven ActivationConfigDesc object
408      */

409     public ActivationConfigDesc getMdActivationConfigDesc() {
410         return mdActivationConfigDesc;
411     }
412
413     /**
414      * @return the JOnAS MessageDriven ActivationConfigDesc object
415      */

416     public ActivationConfigDesc getJonasMdActivationConfigDesc() {
417         return mdJonasActivationConfigDesc;
418     }
419     
420     private void configureAC(ActivationConfigDesc ac, ClassLoader JavaDoc curLoader) throws DeploymentDescException{
421         try {
422             List JavaDoc acpl = ac.getActivationConfigPropertyList();
423             for (ListIterator JavaDoc lit = acpl.listIterator();lit.hasNext();) {
424                 ActivationConfigPropertyDesc el = (ActivationConfigPropertyDesc)lit.next();
425                 if (el.getActivationConfigPropertyName().equals("destinationType")) {
426                     if (el.getActivationConfigPropertyValue().equals("javax.jms.Queue")) {
427                         destinationType = javax.jms.Queue JavaDoc.class;
428                     } else if (el.getActivationConfigPropertyValue().equals("javax.jms.Topic")) {
429                         destinationType = javax.jms.Topic JavaDoc.class;
430                         isTopicDestination = true;
431                     } else {
432                         try {
433                             destinationType = curLoader.loadClass(el.getActivationConfigPropertyValue());
434                         } catch (Exception JavaDoc ex) {
435                             throw new DeploymentDescException("Invalid destination-type of "
436                                                   + el.getActivationConfigPropertyValue()
437                                                   + "for ejb-name" + ejbName);
438                         }
439                     }
440                 } else if (el.getActivationConfigPropertyName().equals("messageSelector")) {
441                     selector = el.getActivationConfigPropertyValue();
442                 } else if (el.getActivationConfigPropertyName().equals("acknowledgeMode")) {
443                     if (el.getActivationConfigPropertyValue().equals("Auto-acknowledge")) {
444                         acknowledgeMode = AUTO_ACKNOWLEDGE;
445                     } else if (el.getActivationConfigPropertyValue().equals("Dups-ok-acknowledge")) {
446                         acknowledgeMode = DUPS_OK_ACKNOWLEDGE;
447                     }
448                 } else if (el.getActivationConfigPropertyName().equals("subscriptionDurability")) {
449                     if (el.getActivationConfigPropertyValue().equals("Durable")) {
450                         subscriptionDurability = SUBS_DURABLE;
451                     } else {
452                         subscriptionDurability = SUBS_NON_DURABLE;
453                     }
454                 } else if (el.getActivationConfigPropertyName().equals("destination")) {
455                     destination = el.getActivationConfigPropertyValue();
456                 }
457             }
458         } catch (Exception JavaDoc ex) {
459             ex.printStackTrace();
460         }
461     }
462
463     
464 }
465
466
Popular Tags