KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > view > SimpleMessageRenderer


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.view;
18
19 import javax.jms.JMSException JavaDoc;
20 import javax.jms.Message JavaDoc;
21 import javax.jms.QueueBrowser JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Enumeration JavaDoc;
29
30 /**
31  * A simple rendering of the contents of a queue appear as a list of message
32  * elements which just contain an ID attribute.
33  *
34  * @version $Revision: $
35  */

36 public class SimpleMessageRenderer implements MessageRenderer {
37
38     private String JavaDoc contentType = "text/xml";
39     private int maxMessages = 0;
40
41     public void renderMessages(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, QueueBrowser JavaDoc browser)
42             throws IOException JavaDoc, JMSException JavaDoc, ServletException JavaDoc {
43         // lets use XML by default
44
response.setContentType(getContentType());
45         PrintWriter JavaDoc writer = response.getWriter();
46         printHeader(writer, browser, request);
47
48         Enumeration JavaDoc iter = browser.getEnumeration();
49         for (int counter = 0; iter.hasMoreElements() && (maxMessages <= 0 || counter < maxMessages); counter++) {
50             Message JavaDoc message = (Message JavaDoc) iter.nextElement();
51             renderMessage(writer, request, response, browser, message);
52         }
53
54         printFooter(writer, browser, request);
55     }
56
57     public void renderMessage(PrintWriter JavaDoc writer, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
58             QueueBrowser JavaDoc browser, Message JavaDoc message) throws JMSException JavaDoc, ServletException JavaDoc {
59         // lets just write the message IDs for now
60
writer.print("<message id='");
61         writer.print(message.getJMSMessageID());
62         writer.println("'/>");
63     }
64
65     // Properties
66
// -------------------------------------------------------------------------
67
public int getMaxMessages() {
68         return maxMessages;
69     }
70
71     public void setMaxMessages(int maxMessages) {
72         this.maxMessages = maxMessages;
73     }
74
75     public String JavaDoc getContentType() {
76         return contentType;
77     }
78
79     public void setContentType(String JavaDoc contentType) {
80         this.contentType = contentType;
81     }
82
83     // Implementation methods
84
// -------------------------------------------------------------------------
85

86     protected void printHeader(PrintWriter JavaDoc writer, QueueBrowser JavaDoc browser, HttpServletRequest JavaDoc request)
87             throws IOException JavaDoc, JMSException JavaDoc, ServletException JavaDoc {
88         writer.println("");
89         writer.print("<messages queue='");
90         writer.print(browser.getQueue());
91         writer.print("'");
92         String JavaDoc selector = browser.getMessageSelector();
93         if (selector != null) {
94             writer.print(" selector='");
95             writer.print(selector);
96             writer.print("'");
97         }
98         writer.println(">");
99     }
100
101     protected void printFooter(PrintWriter JavaDoc writer, QueueBrowser JavaDoc browser, HttpServletRequest JavaDoc request)
102             throws IOException JavaDoc, JMSException JavaDoc, ServletException JavaDoc {
103         writer.println("</messages>");
104     }
105
106 }
107
Popular Tags