KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jndi > browser > model > jms > TopicProxy


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.jndi.browser.model.jms;
8
9 import java.beans.beancontext.BeanContextServices JavaDoc;
10
11 import javax.jms.JMSException JavaDoc;
12 import javax.jms.Session JavaDoc;
13 import javax.jms.TextMessage JavaDoc;
14 import javax.jms.Topic JavaDoc;
15 import javax.jms.TopicConnection JavaDoc;
16 import javax.jms.TopicConnectionFactory JavaDoc;
17 import javax.jms.TopicPublisher JavaDoc;
18 import javax.jms.TopicSession JavaDoc;
19 import javax.naming.Context JavaDoc;
20 import javax.rmi.PortableRemoteObject JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.ejtools.jndi.browser.model.JNDIEntry;
24 import org.ejtools.jndi.browser.model.service.JMSConnectionService;
25
26 /**
27  * Description of the Class
28  *
29  * @author letiemble
30  * @created 13 décembre 2001
31  * @version $Revision: 1.2 $
32  * @todo Javadoc to complete
33  * @todo Add log4j logs
34  * @javabean:class displayName="JMS Topic"
35  * shortDescription="JMS Topic"
36  * @javabean:icons color16="/toolbarButtonGraphics/development/JMSTopic16.gif"
37  * @javabean:property name="name"
38  * class="java.lang.String"
39  * displayName="Name"
40  * shortDescription="Name of the entry"
41  * @javabean:property name="className"
42  * class="java.lang.String"
43  * displayName="Class"
44  * shortDescription="Class of the entry"
45  */

46 public class TopicProxy extends JNDIEntry
47 {
48    /** Description of the Field */
49    private Topic JavaDoc topic = null;
50    /** Description of the Field */
51    private static Logger logger = Logger.getLogger(TopicProxy.class);
52
53
54    /**
55     * Constructor for the JMSTopic object
56     *
57     * @param context Description of the Parameter
58     * @param jndiName Description of the Parameter
59     * @exception Exception Description of Exception
60     */

61    public TopicProxy(Context JavaDoc context, String JavaDoc jndiName)
62       throws Exception JavaDoc
63    {
64       // Try to narrow to an Topic class
65
Object JavaDoc o = context.lookup(jndiName);
66
67       setName(jndiName);
68       setClassName(o.getClass().getName());
69
70       this.topic = (Topic JavaDoc) PortableRemoteObject.narrow(o, Topic JavaDoc.class);
71    }
72
73
74    /**
75     * Description of the Method
76     *
77     * @param text Description of Parameter
78     * @javabean:method name="createTextMessage" displayName="Create Text Message" shortDescription="Create a text message"
79     * @javabean:param name="text" displayName="Text"
80     */

81    public void createTextMessage(String JavaDoc text)
82    {
83       // Get a connection on the default factory
84
TopicConnection JavaDoc topicConnection = getTopicConnection();
85       if (topicConnection != null)
86       {
87          try
88          {
89             TextMessage JavaDoc message = null;
90
91             TopicSession JavaDoc topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
92             TopicPublisher JavaDoc topicPublisher = topicSession.createPublisher(topic);
93
94             message = topicSession.createTextMessage();
95             message.setText(text);
96             topicPublisher.publish(message);
97
98             topicSession.close();
99          }
100          catch (JMSException JavaDoc e)
101          {
102             logger.warn("Exception occurred: " + e.toString());
103          }
104          finally
105          {
106             if (topicConnection != null)
107             {
108                try
109                {
110                   topicConnection.close();
111                }
112                catch (JMSException JavaDoc e)
113                {
114                }
115             }
116          }
117       }
118    }
119
120
121    /**
122     * @return The value
123     * @todo Add Log4j log
124     */

125    protected TopicConnection JavaDoc getTopicConnection()
126    {
127       TopicConnection JavaDoc topicConnection = null;
128       TopicConnectionFactory JavaDoc topicConnectionFactory = getTopicConnectionFactory();
129
130       if (topicConnectionFactory != null)
131       {
132          try
133          {
134             topicConnection = topicConnectionFactory.createTopicConnection();
135          }
136          catch (JMSException JavaDoc e)
137          {
138             logger.warn("No Connection: " + e.toString());
139          }
140       }
141
142       return topicConnection;
143    }
144
145
146    /**
147     * @return The value
148     * @todo Add Log4j log
149     */

150    protected TopicConnectionFactory JavaDoc getTopicConnectionFactory()
151    {
152       TopicConnectionFactory JavaDoc topicConnectionFactory = null;
153
154       try
155       {
156          BeanContextServices JavaDoc context = (BeanContextServices JavaDoc) getBeanContext();
157          JMSConnectionService service = (JMSConnectionService) context.getService(this, this, JMSConnectionService.class, this, this);
158          topicConnectionFactory = service.getDefaultTopicConnectionFactory();
159       }
160       catch (Exception JavaDoc e)
161       {
162          logger.warn("No Factory: " + e.toString());
163       }
164
165       return topicConnectionFactory;
166    }
167 }
168
169
Popular Tags