KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > jmsmanager > renderers > ViewMessagesRenderer


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.jmsmanager.renderers;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.jms.Destination JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.MessageListener JavaDoc;
32 import javax.jms.Queue JavaDoc;
33 import javax.jms.QueueBrowser JavaDoc;
34 import javax.jms.QueueConnection JavaDoc;
35 import javax.jms.QueueConnectionFactory JavaDoc;
36 import javax.jms.QueueSession JavaDoc;
37 import javax.jms.Topic JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.portlet.PortletException;
40 import javax.portlet.RenderRequest;
41 import javax.portlet.RenderResponse;
42
43 import org.apache.geronimo.console.jmsmanager.AbstractJMSManager;
44 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
45 import org.apache.geronimo.gbean.AbstractName;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49 public class ViewMessagesRenderer extends AbstractJMSManager implements
50         PortletRenderer {
51
52     private static final Log log = LogFactory.getLog(ViewMessagesRenderer.class);
53
54     private static final TopicListener topicListener = new TopicListener();
55
56     public String JavaDoc render(RenderRequest request, RenderResponse response)
57             throws PortletException, IOException JavaDoc {
58         List JavaDoc messageList = getMessageList(request, response);
59         request.setAttribute("messages", messageList);
60         return "/WEB-INF/view/jmsmanager/viewmessages.jsp";
61     }
62
63     public List JavaDoc getMessageList(RenderRequest request, RenderResponse response)
64             throws PortletException {
65         String JavaDoc destinationApplicationName = request
66                 .getParameter("destinationApplicationName");
67         String JavaDoc destinationModuleName = request
68                 .getParameter("destinationModuleName");
69         String JavaDoc destinationName = request.getParameter("destinationName");
70
71         List JavaDoc ret = new ArrayList JavaDoc();
72         try {
73             //TODO configid disabled
74
AbstractName adminObjectName = null;//NameFactory.getComponentName(null,
75
// null, destinationApplicationName, NameFactory.JCA_RESOURCE,
76
// destinationModuleName, destinationName, null, baseContext);
77
Destination JavaDoc dest = (Destination JavaDoc) kernel.invoke(adminObjectName,
78                     "$getResource");
79             if (dest instanceof Queue JavaDoc) {
80                 Queue JavaDoc queue = (Queue JavaDoc) dest;
81                 QueueConnectionFactory JavaDoc qConFactory = null;
82                 QueueConnection JavaDoc qConnection = null;
83                 QueueSession JavaDoc qSession = null;
84                 QueueBrowser JavaDoc qBrowser = null;
85                 try {
86                     qConFactory = (QueueConnectionFactory JavaDoc) kernel.invoke(
87                             JCA_MANAGED_CONNECTION_FACTORY_NAME,
88                             "$getResource");
89                     qConnection = qConFactory.createQueueConnection();
90                     qSession = qConnection.createQueueSession(false,
91                             QueueSession.AUTO_ACKNOWLEDGE);
92                     qBrowser = qSession.createBrowser(queue);
93                     qConnection.start();
94                     for (Enumeration JavaDoc e = qBrowser.getEnumeration(); e
95                             .hasMoreElements();) {
96                         Object JavaDoc o = e.nextElement();
97                         ret.add(o);
98                     }
99                     qConnection.stop();
100                 } catch (Exception JavaDoc e) {
101                     log.error(e.getMessage(), e);
102                 } finally {
103                     try {
104                         if (qBrowser != null) {
105                             qBrowser.close();
106                         }
107                     } catch (JMSException JavaDoc ignore) {
108                     }
109                     try {
110                         if (qSession != null) {
111                             qSession.close();
112                         }
113                     } catch (JMSException JavaDoc ignore) {
114                     }
115                     try {
116                         if (qConnection != null) {
117                             qConnection.close();
118                         }
119                     } catch (JMSException JavaDoc ignore) {
120                     }
121                 }
122             } else if (dest instanceof Topic JavaDoc) {
123                 //TODO configid disabled
124
AbstractName tBrowserObjName = null;//NameFactory.getComponentName(null,
125
// null, destinationApplicationName,
126
// NameFactory.JCA_RESOURCE, destinationModuleName,
127
// destinationName, "TopicBrowser", baseContext);
128
ret = (List JavaDoc) kernel.invoke(tBrowserObjName, "getMessages");
129             }
130         } catch (Exception JavaDoc e) {
131             log.error(e.getMessage(), e);
132         }
133         return ret;
134     }
135
136     static class TopicListener implements MessageListener JavaDoc {
137         /**
138          * Hashtable to hold the messages for each topic. Messages are stored as
139          * Message/Topic key/value pairs.
140          */

141         private Map JavaDoc messagesMap = new Hashtable JavaDoc();
142
143         public final String JavaDoc name = this.toString();
144
145         public synchronized void onMessage(Message JavaDoc message) {
146             try {
147                 Destination JavaDoc dest = message.getJMSDestination();
148                 List JavaDoc messages = null;
149                 if (!messagesMap.containsKey(dest)) {
150                     register((Topic JavaDoc) dest);
151                 }
152                 messages = (List JavaDoc) messagesMap.get(dest);
153
154                 if (!messages.contains(message)) {
155                     messages.add(message);
156                 }
157                 messagesMap.put(dest, messages);
158             } catch (JMSException JavaDoc e) {
159                 log.error(e.getMessage(), e);
160             }
161         }
162
163         public void register(Topic JavaDoc topic) {
164             if (!messagesMap.containsKey(topic)) {
165                 List JavaDoc messages = new ArrayList JavaDoc();
166                 messagesMap.put(topic, messages);
167             }
168         }
169
170         public List JavaDoc getMessages(Topic JavaDoc topic) {
171             List JavaDoc ret;
172             if (messagesMap.containsKey(topic)) {
173                 ret = (List JavaDoc) messagesMap.get(topic);
174             } else {
175                 ret = Collections.EMPTY_LIST;
176             }
177             return Collections.unmodifiableList(ret);
178         }
179
180         public boolean isListeningTo(Topic JavaDoc topic) {
181             return messagesMap.containsKey(topic);
182         }
183     }
184
185 }
186
Popular Tags