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; 21 import java.util.ArrayList; 22 import java.util.Collections; 23 import java.util.Enumeration; 24 import java.util.Hashtable; 25 import java.util.List; 26 import java.util.Map; 27 28 import javax.jms.Destination; 29 import javax.jms.JMSException; 30 import javax.jms.Message; 31 import javax.jms.MessageListener; 32 import javax.jms.Queue; 33 import javax.jms.QueueBrowser; 34 import javax.jms.QueueConnection; 35 import javax.jms.QueueConnectionFactory; 36 import javax.jms.QueueSession; 37 import javax.jms.Topic; 38 import javax.management.ObjectName; 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.slf4j.Logger; 47 import org.slf4j.LoggerFactory; 48 49 public class ViewMessagesRenderer extends AbstractJMSManager implements 50 PortletRenderer { 51 52 private static final Logger log = LoggerFactory.getLogger(ViewMessagesRenderer.class); 53 54 private static final TopicListener topicListener = new TopicListener(); 55 56 public String render(RenderRequest request, RenderResponse response) 57 throws PortletException, IOException { 58 List messageList = getMessageList(request, response); 59 request.setAttribute("messages", messageList); 60 return "/WEB-INF/view/jmsmanager/viewmessages.jsp"; 61 } 62 63 public List getMessageList(RenderRequest request, RenderResponse response) 64 throws PortletException { 65 String destinationApplicationName = request 66 .getParameter("destinationApplicationName"); 67 String destinationModuleName = request 68 .getParameter("destinationModuleName"); 69 String destinationName = request.getParameter("destinationName"); 70 71 List ret = new ArrayList(); 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 dest = (Destination) kernel.invoke(adminObjectName, 78 "$getResource"); 79 if (dest instanceof Queue) { 80 Queue queue = (Queue) dest; 81 QueueConnectionFactory qConFactory = null; 82 QueueConnection qConnection = null; 83 QueueSession qSession = null; 84 QueueBrowser qBrowser = null; 85 try { 86 qConFactory = (QueueConnectionFactory) 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 e = qBrowser.getEnumeration(); e 95 .hasMoreElements();) { 96 Object o = e.nextElement(); 97 ret.add(o); 98 } 99 qConnection.stop(); 100 } catch (Exception e) { 101 log.error(e.getMessage(), e); 102 } finally { 103 try { 104 if (qBrowser != null) { 105 qBrowser.close(); 106 } 107 } catch (JMSException ignore) { 108 } 109 try { 110 if (qSession != null) { 111 qSession.close(); 112 } 113 } catch (JMSException ignore) { 114 } 115 try { 116 if (qConnection != null) { 117 qConnection.close(); 118 } 119 } catch (JMSException ignore) { 120 } 121 } 122 } else if (dest instanceof Topic) { 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) kernel.invoke(tBrowserObjName, "getMessages"); 129 } 130 } catch (Exception e) { 131 log.error(e.getMessage(), e); 132 } 133 return ret; 134 } 135 136 static class TopicListener implements MessageListener { 137 /** 138 * Hashtable to hold the messages for each topic. Messages are stored as 139 * Message/Topic key/value pairs. 140 */ 141 private Map messagesMap = new Hashtable(); 142 143 public final String name = this.toString(); 144 145 public synchronized void onMessage(Message message) { 146 try { 147 Destination dest = message.getJMSDestination(); 148 List messages = null; 149 if (!messagesMap.containsKey(dest)) { 150 register((Topic) dest); 151 } 152 messages = (List) messagesMap.get(dest); 153 154 if (!messages.contains(message)) { 155 messages.add(message); 156 } 157 messagesMap.put(dest, messages); 158 } catch (JMSException e) { 159 log.error(e.getMessage(), e); 160 } 161 } 162 163 public void register(Topic topic) { 164 if (!messagesMap.containsKey(topic)) { 165 List messages = new ArrayList(); 166 messagesMap.put(topic, messages); 167 } 168 } 169 170 public List getMessages(Topic topic) { 171 List ret; 172 if (messagesMap.containsKey(topic)) { 173 ret = (List) messagesMap.get(topic); 174 } else { 175 ret = Collections.EMPTY_LIST; 176 } 177 return Collections.unmodifiableList(ret); 178 } 179 180 public boolean isListeningTo(Topic topic) { 181 return messagesMap.containsKey(topic); 182 } 183 } 184 185 }