001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.jmsmanager.renderers;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.Enumeration;
022: import java.util.Hashtable;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.jms.Destination;
027: import javax.jms.JMSException;
028: import javax.jms.Message;
029: import javax.jms.MessageListener;
030: import javax.jms.Queue;
031: import javax.jms.QueueBrowser;
032: import javax.jms.QueueConnection;
033: import javax.jms.QueueConnectionFactory;
034: import javax.jms.QueueSession;
035: import javax.jms.Topic;
036: import javax.management.ObjectName;
037: import javax.portlet.PortletException;
038: import javax.portlet.RenderRequest;
039: import javax.portlet.RenderResponse;
040:
041: import org.apache.geronimo.console.jmsmanager.AbstractJMSManager;
042: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
043: import org.apache.geronimo.gbean.AbstractName;
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: public class ViewMessagesRenderer extends AbstractJMSManager implements
048: PortletRenderer {
049:
050: private static final Log log = LogFactory
051: .getLog(ViewMessagesRenderer.class);
052:
053: private static final TopicListener topicListener = new TopicListener();
054:
055: public String render(RenderRequest request, RenderResponse response)
056: throws PortletException, IOException {
057: List messageList = getMessageList(request, response);
058: request.setAttribute("messages", messageList);
059: return "/WEB-INF/view/jmsmanager/viewmessages.jsp";
060: }
061:
062: public List getMessageList(RenderRequest request,
063: RenderResponse response) throws PortletException {
064: String destinationApplicationName = request
065: .getParameter("destinationApplicationName");
066: String destinationModuleName = request
067: .getParameter("destinationModuleName");
068: String destinationName = request
069: .getParameter("destinationName");
070:
071: List ret = new ArrayList();
072: try {
073: //TODO configid disabled
074: AbstractName adminObjectName = null;//NameFactory.getComponentName(null,
075: // null, destinationApplicationName, NameFactory.JCA_RESOURCE,
076: // destinationModuleName, destinationName, null, baseContext);
077: Destination dest = (Destination) kernel.invoke(
078: adminObjectName, "$getResource");
079: if (dest instanceof Queue) {
080: Queue queue = (Queue) dest;
081: QueueConnectionFactory qConFactory = null;
082: QueueConnection qConnection = null;
083: QueueSession qSession = null;
084: QueueBrowser qBrowser = null;
085: try {
086: qConFactory = (QueueConnectionFactory) kernel
087: .invoke(
088: JCA_MANAGED_CONNECTION_FACTORY_NAME,
089: "$getResource");
090: qConnection = qConFactory.createQueueConnection();
091: qSession = qConnection.createQueueSession(false,
092: QueueSession.AUTO_ACKNOWLEDGE);
093: qBrowser = qSession.createBrowser(queue);
094: qConnection.start();
095: for (Enumeration e = qBrowser.getEnumeration(); e
096: .hasMoreElements();) {
097: Object o = e.nextElement();
098: ret.add(o);
099: }
100: qConnection.stop();
101: } catch (Exception e) {
102: log.error(e.getMessage(), e);
103: } finally {
104: try {
105: if (qBrowser != null) {
106: qBrowser.close();
107: }
108: } catch (JMSException ignore) {
109: }
110: try {
111: if (qSession != null) {
112: qSession.close();
113: }
114: } catch (JMSException ignore) {
115: }
116: try {
117: if (qConnection != null) {
118: qConnection.close();
119: }
120: } catch (JMSException ignore) {
121: }
122: }
123: } else if (dest instanceof Topic) {
124: //TODO configid disabled
125: AbstractName tBrowserObjName = null;//NameFactory.getComponentName(null,
126: // null, destinationApplicationName,
127: // NameFactory.JCA_RESOURCE, destinationModuleName,
128: // destinationName, "TopicBrowser", baseContext);
129: ret = (List) kernel.invoke(tBrowserObjName,
130: "getMessages");
131: }
132: } catch (Exception e) {
133: log.error(e.getMessage(), e);
134: }
135: return ret;
136: }
137:
138: static class TopicListener implements MessageListener {
139: /**
140: * Hashtable to hold the messages for each topic. Messages are stored as
141: * Message/Topic key/value pairs.
142: */
143: private Map messagesMap = new Hashtable();
144:
145: public final String name = this .toString();
146:
147: public synchronized void onMessage(Message message) {
148: try {
149: Destination dest = message.getJMSDestination();
150: List messages = null;
151: if (!messagesMap.containsKey(dest)) {
152: register((Topic) dest);
153: }
154: messages = (List) messagesMap.get(dest);
155:
156: if (!messages.contains(message)) {
157: messages.add(message);
158: }
159: messagesMap.put(dest, messages);
160: } catch (JMSException e) {
161: log.error(e.getMessage(), e);
162: }
163: }
164:
165: public void register(Topic topic) {
166: if (!messagesMap.containsKey(topic)) {
167: List messages = new ArrayList();
168: messagesMap.put(topic, messages);
169: }
170: }
171:
172: public List getMessages(Topic topic) {
173: List ret;
174: if (messagesMap.containsKey(topic)) {
175: ret = (List) messagesMap.get(topic);
176: } else {
177: ret = Collections.EMPTY_LIST;
178: }
179: return Collections.unmodifiableList(ret);
180: }
181:
182: public boolean isListeningTo(Topic topic) {
183: return messagesMap.containsKey(topic);
184: }
185: }
186:
187: }
|