1 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 ; 25 import javax.jms.ConnectionFactory ; 26 import javax.jms.JMSException ; 27 import javax.jms.Queue ; 28 import javax.jms.QueueBrowser ; 29 import javax.jms.Session ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServlet ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import java.io.IOException ; 36 import java.util.Enumeration ; 37 import java.util.HashMap ; 38 import java.util.LinkedList ; 39 import java.util.Map ; 40 41 52 public class QueueBrowseServlet extends HttpServlet { 53 54 private static FactoryFinder factoryFinder = new FactoryFinder("META-INF/services/org/apache/activemq/web/view/"); 55 56 private ConnectionFactory connectionFactory; 57 private Connection connection; 58 private LinkedList sessions = new LinkedList (); 59 60 public Connection getConnection() throws JMSException { 61 if (connection == null) { 62 connection = getConnectionFactory().createConnection(); 63 connection.start(); 64 } 65 return connection; 66 } 67 68 public void setConnection(Connection connection) { 69 this.connection = connection; 70 } 71 72 public ConnectionFactory getConnectionFactory() { 73 if (connectionFactory == null) { 74 connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); 76 } 77 return connectionFactory; 78 } 79 80 public void setConnectionFactory(ConnectionFactory connectionFactory) { 81 this.connectionFactory = connectionFactory; 82 } 83 84 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 87 Session session = null; 88 try { 89 session = borrowSession(); 90 Queue queue = getQueue(request, session); 91 if (queue == null) { 92 throw new ServletException ("No queue URI specified"); 93 } 94 String selector = getSelector(request); 95 QueueBrowser browser = session.createBrowser(queue, selector); 96 MessageRenderer renderer = getMessageRenderer(request); 97 configureRenderer(request, renderer); 98 renderer.renderMessages(request, response, browser); 99 } 100 catch (JMSException e) { 101 throw new ServletException (e); 102 } 103 finally { 104 returnSession(session); 105 } 106 } 107 108 protected MessageRenderer getMessageRenderer(HttpServletRequest request) throws IOException , ServletException { 109 String style = request.getParameter("view"); 110 if (style == null) { 111 style = "simple"; 112 } 113 try { 114 return (MessageRenderer) factoryFinder.newInstance(style); 115 } 116 catch (IllegalAccessException e) { 117 throw new NoSuchViewStyleException(style, e); 118 } 119 catch (InstantiationException e) { 120 throw new NoSuchViewStyleException(style, e); 121 } 122 catch (ClassNotFoundException e) { 123 throw new NoSuchViewStyleException(style, e); 124 } 125 } 126 127 protected void configureRenderer(HttpServletRequest request, MessageRenderer renderer) { 128 Map properties = new HashMap (); 129 for (Enumeration iter = request.getParameterNames(); iter.hasMoreElements(); ) { 130 String name = (String ) iter.nextElement(); 131 properties.put(name, request.getParameter(name)); 132 } 133 IntrospectionSupport.setProperties(renderer, properties); 134 } 135 136 protected Session borrowSession() throws JMSException { 137 Session answer = null; 138 synchronized (sessions) { 139 if (sessions.isEmpty()) { 140 answer = createSession(); 141 } 142 else { 143 answer = (Session ) sessions.removeLast(); 144 } 145 } 146 return answer; 147 } 148 149 protected void returnSession(Session session) { 150 if (session != null) { 151 synchronized (sessions) { 152 sessions.add(session); 153 } 154 } 155 } 156 157 protected Session createSession() throws JMSException { 158 return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE); 159 } 160 161 protected String getSelector(HttpServletRequest request) { 162 return request.getParameter("selector"); 163 } 164 165 protected Queue getQueue(HttpServletRequest request, Session session) throws JMSException { 166 String uri = request.getPathInfo(); 167 if (uri == null) 168 return null; 169 170 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 |