View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.core.messaging;
16  
17  import org.figure8.join.core.EntityObject;
18  import org.figure8.join.core.Configurable;
19  import org.figure8.join.core.InvalidParameterException;
20  import org.figure8.join.util.LogUtil;
21  
22  import org.apache.commons.logging.Log;
23  
24  import java.util.List;
25  import java.util.ArrayList;
26  /**
27   * This is an EntityObject for describing a {@link JMSConsumerBean}, its
28   * configurable properties & parameters and its activation specification.
29   *
30   * @see org.figure8.join.core.Configurable
31   *
32   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
33   * @version $Revision: 1.2 $
34   *
35   * @hibernate.class table="join_consumers"
36   *
37   * @hibernate.query name="join.consumer_findByName"
38   *    query="from JMSConsumerBeanInfo cons
39   *          left join fetch cons.consumerParameterInfos where cons.name = :consumerName"
40   */
41  public class JMSConsumerBeanInfo extends EntityObject{
42  
43     // Static -------------------------------------------------------------------
44  
45     /** Get a commons logger. */
46     private static final Log log = LogUtil.getLog(JMSConsumerBeanInfo.class);
47  
48  
49     // Attributes ---------------------------------------------------------------
50  
51     /** The name corresponding to the consumer. */
52     private String name;
53     /** The message selector expression to apply on consumer. */
54     private String selector;
55     /** The reference of destination this consumer get messages from. */
56     private String destination;
57     /** The Java class used for instanciating a JMS consumer */
58     private String consumerBeanClass;
59     /** Whether the wrapped consumer is active or not (it may have been temporarily disabled) */
60     private boolean active = true;
61     /** Whether the wrapped consumer bean is thread safe or not. Default is true. */
62     private boolean threadSafe = true;
63     /** A list of <code>JMSConsumerBeanParameterInfo</code>s if consumer is {@link org.figure8.join.core.Configurable}. */
64     private List consumerParameters = new ArrayList();
65  
66     /** Wrapped JMSConsumerBean instance */
67     private JMSConsumerBean consumerBean;
68  
69  
70     // Constructors -------------------------------------------------------------
71  
72     /** Creates a new instance of JMSConsumerBeanInfo. */
73     public JMSConsumerBeanInfo(){
74     }
75  
76     /**
77      * Creates a new instance of JMSConsumerBeanInfo with mandatory params.
78      * @param name Name of this consumer
79      * @param selector Selector expression for filtering messages
80      * @param destination Name of JMS destination to listen from
81      * @param consumerBeanClass FQN of Java class for getting a JMSConsumerBean instance
82      * @throws InvalidParameterException if <b>consumerBeanClass</b> is not a vlid one...
83      */
84     public JMSConsumerBeanInfo(String name, String selector, String destination, String consumerBeanClass)
85             throws InvalidParameterException{
86        this.name = name;
87        this.selector = selector;
88        this.destination = destination;
89        // Check and set consumer bean class.
90        setConsumerBeanClass(consumerBeanClass);
91     }
92  
93  
94     // Public -------------------------------------------------------------------
95  
96     /**
97      * @hibernate.property column="s_name"
98      *    not-null="true" unique="true"
99      *    length="60"
100     * @return The name of this consumer
101     */
102    public String getName(){
103       return name;
104    }
105    /** @param name The name of this consumer */
106    public void setName(String name){
107       this.name = name;
108    }
109 
110    /**
111     * @hibernate.property column="s_selector" length="100"
112     * @return The selector expression for filtering messages
113     */
114    public String getSelector(){
115       return selector;
116    }
117    /** @param selector The selector expression for filtering messages */
118    public void setSelector(String selector){
119       this.selector = selector;
120    }
121 
122    /**
123     * @hibernate.property column="s_destination"
124     *    not-null="true" length="100"
125     * @return The reference of the JMS destination this consumer should listen
126     */
127    public String getDestination(){
128       return destination;
129    }
130    /** @param destination The name of the JMS destination this consumer shoud listen */
131    public void setDestination(String destination){
132       this.destination = destination;
133    }
134 
135    /**
136     * @hibernate.property column="s_consumerclass"
137     *    not-null="true" length="200"
138     * @return The FQN of the Java class of wrapped JMSConsumerBean instance
139     */
140    public String getConsumerBeanClass(){
141       return consumerBeanClass;
142    }
143    /**
144     * Give the FQN of Java class used for instanciating a JMSConsumerBean
145     * @param consumerBeanClass The FQN of the Java class of wrapped JMSConsumerBean instance
146     * @throws InvalidParameterException if the <b>consumerBeanClass</b> is not an instance of {@link JMSConsumerBean}
147     */
148    public void setConsumerBeanClass(String consumerBeanClass) throws InvalidParameterException{
149       this.consumerBeanClass = consumerBeanClass;
150       // Try instanciating consumer bean now.
151       instanciateConsumerBean();
152    }
153 
154    /**
155     * @hibernate.property column="b_active"
156     *    type="boolean" not-null="true"
157     * @return Whether the wrapped {@link JMSConsumerBean} is active or not
158     */
159    public boolean isActive(){
160       return active;
161    }
162    /** @param active Flag telling whether wrapped JMSConsumerBean is active or not */
163    public void setActive(boolean active){
164       this.active = active;
165    }
166 
167    /**
168     * @hibernate.property column="b_threadsafe"
169     *    type="boolean" not-null="true"
170     * @return Whether the wrapped {@link JMSConsumerBean} is thread safe or not
171     */
172    public boolean isThreadSafe(){
173       return threadSafe;
174    }
175    /** @param threadSafe Flag telling whether wrapped JMSConsumerBean is thread safe or not */
176    public void setThreadSafe(boolean threadSafe){
177       this.threadSafe = threadSafe;
178    }
179 
180    /**
181     * @hibernate.bag cascade="all-delete-orphan" inverse="true" lazy="true"
182     * @hibernate.collection-key column="n_consumer_fk"
183     * @hibernate.collection-one-to-many class="org.figure8.join.core.messaging.JMSConsumerBeanParameterInfo"
184     * @return A set of {@code JMSConsumerBeanParameterInfo}s for this consumer
185     */
186    public List getConsumerParameterInfos(){
187       return consumerParameters;
188    }
189    /** @param paramInfos A set of {@code JMSConsumerBeanParameterInfo}s */
190    public void setConsumerParameterInfos(List paramInfos){
191       this.consumerParameters = paramInfos;
192    }
193 
194    /**
195     * Convenient method for adding a parameter info to a consumer.
196     * This method manages the 2 sides of the association.
197     * @param paramInfo The parameter info to add to consumer
198     */
199    public void addConsumerParameterInfo(JMSConsumerBeanParameterInfo paramInfo){
200       // Manage the 2 sides of relation.
201       paramInfo.setConsumerInfo(this);
202       consumerParameters.add(paramInfo);
203       // Reset consumer because it may need the new param.
204       consumerBean = null;
205    }
206 
207    /**
208     * Get the <code>JMSConsumerBean</code> wrapped within this infos container.
209     * @throws InvalidParameterException if <b>consumerBeanClass</b> is not valid
210     * @return The {@link JMSConsumerBean} instance wrapped by this info bean
211     */
212    public JMSConsumerBean getJMSConsumerBean() throws InvalidParameterException{
213       // Instanciate if not already done.
214       if (consumerBean == null)
215          instanciateConsumerBean();
216       return consumerBean;
217    }
218 
219 
220    // Protected ----------------------------------------------------------------
221 
222    /**
223     * Try to instanciate wrapped <b>consumerBean</b> and configure it.
224     * @throws InvalidParameterException if <b>consumerBeanClass</b> is not valid.
225     */
226    protected void instanciateConsumerBean() throws InvalidParameterException{
227       if (consumerBeanClass != null && consumerBeanClass.length() > 0){
228          try{
229             // Load class from context class loader and retrieve new instance.
230             Class clazz = Thread.currentThread().getContextClassLoader().loadClass(consumerBeanClass);
231             consumerBean = (JMSConsumerBean)clazz.newInstance();
232             consumerBean.setName(name);
233             // Add parameters if configurable.
234             if (consumerBean instanceof Configurable){
235                Configurable configurableConsumer = (Configurable)consumerBean;
236                for (int i=0; i<consumerParameters.size(); i++){
237                   JMSConsumerBeanParameterInfo parameter = (JMSConsumerBeanParameterInfo)consumerParameters.get(i);
238                   configurableConsumer.setParameter(parameter.getName(), parameter.getValue());
239                }
240             }
241          }
242          catch (Exception e){
243             // Log and wrap within an InvalidParameterException.
244             log.error("Exception while trying to instantiate consumerBean for consumer '" + name + "'");
245             log.error("Here's the exception message : " + e.getMessage());
246             throw new InvalidParameterException("'" + consumerBeanClass +
247                     "' cannot be found or does not extend JMSConsumerBean", e);
248          }
249       }
250    }
251 }