KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > QueueBrowseServlet


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 package org.apache.activemq.web;
18
19 import org.apache.activemq.ActiveMQConnectionFactory;
20 import org.apache.activemq.util.FactoryFinder;
21 import org.apache.activemq.util.IntrospectionSupport;
22 import org.apache.activemq.web.view.MessageRenderer;
23
24 import javax.jms.Connection JavaDoc;
25 import javax.jms.ConnectionFactory JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.Queue JavaDoc;
28 import javax.jms.QueueBrowser JavaDoc;
29 import javax.jms.Session JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServlet JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import java.io.IOException JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.LinkedList JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * Renders the contents of a queue using some kind of view. The URI is assumed
43  * to be the queue. The following parameters can be used
44  *
45  * <ul>
46  * <li>view - specifies the type of the view such as simple, xml, rss</li>
47  * <li>selector - specifies the SQL 92 selector to apply to the queue</li>
48  * </ul>
49  *
50  * @version $Revision: $
51  */

52 public class QueueBrowseServlet extends HttpServlet JavaDoc {
53
54     private static FactoryFinder factoryFinder = new FactoryFinder("META-INF/services/org/apache/activemq/web/view/");
55
56     private ConnectionFactory connectionFactory;
57     private Connection JavaDoc connection;
58     private LinkedList JavaDoc sessions = new LinkedList JavaDoc();
59
60     public Connection JavaDoc getConnection() throws JMSException JavaDoc {
61         if (connection == null) {
62             connection = getConnectionFactory().createConnection();
63             connection.start();
64         }
65         return connection;
66     }
67
68     public void setConnection(Connection JavaDoc connection) {
69         this.connection = connection;
70     }
71
72     public ConnectionFactory getConnectionFactory() {
73         if (connectionFactory == null) {
74             // TODO support remote brokers too
75
connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
76         }
77         return connectionFactory;
78     }
79
80     public void setConnectionFactory(ConnectionFactory connectionFactory) {
81         this.connectionFactory = connectionFactory;
82     }
83
84     // Implementation methods
85
// -------------------------------------------------------------------------
86
protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
87         Session JavaDoc session = null;
88         try {
89             session = borrowSession();
90             Queue JavaDoc queue = getQueue(request, session);
91             if (queue == null) {
92                 throw new ServletException JavaDoc("No queue URI specified");
93             }
94             String JavaDoc selector = getSelector(request);
95             QueueBrowser JavaDoc browser = session.createBrowser(queue, selector);
96             MessageRenderer renderer = getMessageRenderer(request);
97             configureRenderer(request, renderer);
98             renderer.renderMessages(request, response, browser);
99         }
100         catch (JMSException JavaDoc e) {
101             throw new ServletException JavaDoc(e);
102         }
103         finally {
104             returnSession(session);
105         }
106     }
107
108     protected MessageRenderer getMessageRenderer(HttpServletRequest JavaDoc request) throws IOException JavaDoc, ServletException JavaDoc {
109         String JavaDoc style = request.getParameter("view");
110         if (style == null) {
111             style = "simple";
112         }
113         try {
114             return (MessageRenderer) factoryFinder.newInstance(style);
115         }
116         catch (IllegalAccessException JavaDoc e) {
117             throw new NoSuchViewStyleException(style, e);
118         }
119         catch (InstantiationException JavaDoc e) {
120             throw new NoSuchViewStyleException(style, e);
121         }
122         catch (ClassNotFoundException JavaDoc e) {
123             throw new NoSuchViewStyleException(style, e);
124         }
125     }
126
127     protected void configureRenderer(HttpServletRequest JavaDoc request, MessageRenderer renderer) {
128         Map JavaDoc properties = new HashMap JavaDoc();
129         for (Enumeration JavaDoc iter = request.getParameterNames(); iter.hasMoreElements(); ) {
130             String JavaDoc name = (String JavaDoc) iter.nextElement();
131             properties.put(name, request.getParameter(name));
132         }
133         IntrospectionSupport.setProperties(renderer, properties);
134     }
135
136     protected Session JavaDoc borrowSession() throws JMSException JavaDoc {
137         Session JavaDoc answer = null;
138         synchronized (sessions) {
139             if (sessions.isEmpty()) {
140                 answer = createSession();
141             }
142             else {
143                 answer = (Session JavaDoc) sessions.removeLast();
144             }
145         }
146         return answer;
147     }
148
149     protected void returnSession(Session JavaDoc session) {
150         if (session != null) {
151             synchronized (sessions) {
152                 sessions.add(session);
153             }
154         }
155     }
156
157     protected Session JavaDoc createSession() throws JMSException JavaDoc {
158         return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
159     }
160
161     protected String JavaDoc getSelector(HttpServletRequest JavaDoc request) {
162         return request.getParameter("selector");
163     }
164
165     protected Queue JavaDoc getQueue(HttpServletRequest JavaDoc request, Session JavaDoc session) throws JMSException JavaDoc {
166         String JavaDoc uri = request.getPathInfo();
167         if (uri == null)
168             return null;
169
170         // replace URI separator with JMS destination separator
171
if (uri.startsWith("/")) {
172             uri = uri.substring(1);
173             if (uri.length() == 0)
174                 return null;
175         }
176         uri = uri.replace('/', '.');
177
178         System.out.println("destination uri = " + uri);
179
180         return session.createQueue(uri);
181     }
182
183 }
184
Popular Tags