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.InfrastructureException;
18  import org.figure8.join.core.InvalidParameterException;
19  
20  import javax.jms.Destination;
21  /**
22   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
23   * @version $Revision: 1.1 $
24   */
25  public class ResolverJMSProducerBean extends JMSProducerBean{
26  
27     // Attributes ---------------------------------------------------------------
28  
29     /** Destination resolution policy flag. Default is first topic, then queue */
30     private boolean topicFirst = true;
31     /** The name of destination to get using resolver and to produce messages onto */
32     private String destinationName;
33     /** The resolver of JMS destinations to use for getting destination to produce messages onto */
34     private JMSDestinationResolver resolver;
35  
36  
37     // Constructors -------------------------------------------------------------
38  
39     /** Creates a new instance of ResolverJMSProducerBean */
40     public ResolverJMSProducerBean(){
41     }
42  
43     /**
44      * Creates a new instance of ResolverJMSProducerBean
45      * @param destinationName The name of destination to resolve using resolver
46      * @param resolver The resolver for getting the required destination
47      */
48     public ResolverJMSProducerBean(String destinationName, JMSDestinationResolver resolver){
49        this.destinationName = destinationName;
50        this.resolver = resolver;
51     }
52  
53  
54     // Public -------------------------------------------------------------------
55  
56     /** @return Destination resolution policy flag. True means first topic, then queue */
57     public boolean isTopicFirst(){
58        return topicFirst;
59     }
60     /** @param topicFirst Destination resolution policy flag. Set to true if first topic, then queue */
61     public void setTopicFirst(boolean topicFirst){
62        this.topicFirst = topicFirst;
63     }
64  
65     /** @return The name of destination to get using resolver and to produce messages onto */
66     public String getDestinationName(){
67        return destinationName;
68     }
69     /** @param destinationName The name of destination to get using resolver and to produce messages onto */
70     public void setDestinationName(String destinationName){
71        this.destinationName = destinationName;
72     }
73  
74     /** @return The resolver of JMS destinations to use for getting destination to produce messages onto */
75     public JMSDestinationResolver getResolver(){
76        return resolver;
77     }
78     /** @param resolver The resolver of JMS destinations to use for getting destination to produce messages onto */
79     public void setResolver(JMSDestinationResolver resolver){
80        this.resolver = resolver;
81     }
82  
83  
84     // Implementation of JMSProducerBean ----------------------------------------
85  
86     /**
87      * Used the provided JMS destination resolver for remote destinations ...
88      * @return The JMS destination to use when sending messages.
89      */
90     public Destination getDestination(){
91        // Check resolver presence.
92        if (resolver == null)
93           throw new IllegalStateException("A JMSDestinationResolver has to be provided for getting destination !");
94  
95        Destination destination = null;
96        try{
97           // Depending on flag, try first a topic.
98           if (topicFirst) destination = resolver.resolveTopic(destinationName);
99           else destination = resolver.resolveQueue(destinationName);
100       }
101       catch (InvalidParameterException ipe){
102          // Log and try the other kind of destination.
103          log.warn("Destination cannot be resolved at first try: " + ipe.getMessage());
104          try{
105             if (topicFirst) destination = resolver.resolveQueue(destinationName);
106             else destination = resolver.resolveTopic(destinationName);
107          }
108          catch (InvalidParameterException ipe2){
109             // Log and raise an infrastructure runtime exception.
110             log.error("The destination called '" + destinationName + "' cannot be resolved");
111             log.error("Here's the detailed message: " + ipe2.getMessage());
112             throw new InfrastructureException("The destination called '" + destinationName + "' cannot be resolved", ipe);
113          }
114       }
115       return destination;
116    }
117 }