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     */
017    
018    package org.apache.geronimo.console.jmsmanager.renderers;
019    
020    import java.io.IOException;
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.Enumeration;
024    import java.util.Hashtable;
025    import java.util.List;
026    import java.util.Map;
027    
028    import javax.jms.Destination;
029    import javax.jms.JMSException;
030    import javax.jms.Message;
031    import javax.jms.MessageListener;
032    import javax.jms.Queue;
033    import javax.jms.QueueBrowser;
034    import javax.jms.QueueConnection;
035    import javax.jms.QueueConnectionFactory;
036    import javax.jms.QueueSession;
037    import javax.jms.Topic;
038    import javax.management.ObjectName;
039    import javax.portlet.PortletException;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import org.apache.geronimo.console.jmsmanager.AbstractJMSManager;
044    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
045    import org.apache.geronimo.gbean.AbstractName;
046    import org.apache.commons.logging.Log;
047    import org.apache.commons.logging.LogFactory;
048    
049    public class ViewMessagesRenderer extends AbstractJMSManager implements
050            PortletRenderer {
051    
052        private static final Log log = LogFactory.getLog(ViewMessagesRenderer.class);
053    
054        private static final TopicListener topicListener = new TopicListener();
055    
056        public String render(RenderRequest request, RenderResponse response)
057                throws PortletException, IOException {
058            List messageList = getMessageList(request, response);
059            request.setAttribute("messages", messageList);
060            return "/WEB-INF/view/jmsmanager/viewmessages.jsp";
061        }
062    
063        public List getMessageList(RenderRequest request, RenderResponse response)
064                throws PortletException {
065            String destinationApplicationName = request
066                    .getParameter("destinationApplicationName");
067            String destinationModuleName = request
068                    .getParameter("destinationModuleName");
069            String destinationName = request.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(adminObjectName,
078                        "$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.invoke(
087                                JCA_MANAGED_CONNECTION_FACTORY_NAME,
088                                "$getResource");
089                        qConnection = qConFactory.createQueueConnection();
090                        qSession = qConnection.createQueueSession(false,
091                                QueueSession.AUTO_ACKNOWLEDGE);
092                        qBrowser = qSession.createBrowser(queue);
093                        qConnection.start();
094                        for (Enumeration e = qBrowser.getEnumeration(); e
095                                .hasMoreElements();) {
096                            Object o = e.nextElement();
097                            ret.add(o);
098                        }
099                        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    }