001: // "plain/text"
002: package org.objectweb.alarm;
003:
004: import java.io.*;
005: import javax.jms.*;
006: import javax.servlet.*;
007: import javax.servlet.http.*;
008: import javax.naming.Context;
009: import javax.naming.InitialContext;
010: import javax.naming.NamingException;
011: import javax.rmi.PortableRemoteObject;
012:
013: /**
014: * This servlet is used to send Alarms on a JMS topic
015: */
016: public class Sender extends HttpServlet {
017:
018: public void doGet(HttpServletRequest req, HttpServletResponse res)
019: throws IOException, ServletException {
020:
021: res.setContentType("text/html");
022: PrintWriter out = res.getWriter();
023: String device = req.getParameter("device");
024: String level = req.getParameter("level");
025: String message = req.getParameter("message");
026: String sloop = req.getParameter("number");
027: String stimeout = req.getParameter("timeout");
028: int severity;
029: String error = "";
030:
031: out
032: .write("<html><head><title>Alarm Sender Servlet</title></head><body>");
033: if (level.startsWith("S")) {
034: severity = 1;
035: } else if (level.startsWith("W")) {
036: severity = 2;
037: } else {
038: severity = 3;
039: }
040: int loop = (new Integer(sloop)).intValue();
041: int timeout = (new Integer(stimeout)).intValue();
042:
043: Context ictx = null;
044: try {
045: ictx = new InitialContext();
046: } catch (Exception e) {
047: error += "Cannot get initial context:" + e;
048: }
049:
050: // Lookup JMS resources
051: TopicConnectionFactory tcf = null;
052: Topic mytopic = null;
053: TopicConnection mytc = null;
054: try {
055: // lookup the TopicConnectionFactory through its JNDI name
056: tcf = (TopicConnectionFactory) ictx
057: .lookup("java:comp/env/jms/TopicFactory");
058: // lookup the Topic through its JNDI name
059: mytopic = (Topic) ictx
060: .lookup("java:comp/env/jms/AlarmTopic");
061: } catch (NamingException e) {
062: error += "Cannot lookup JMS Resources:" + e;
063: }
064:
065: // Create Connection
066: try {
067: mytc = tcf.createTopicConnection();
068: } catch (Exception e) {
069: error += "Cannot create JMS Connection:" + e;
070: }
071:
072: // Create Session + Publisher
073: TopicSession session = null;
074: TopicPublisher tp = null;
075: try {
076: session = mytc.createTopicSession(false,
077: Session.AUTO_ACKNOWLEDGE);
078: tp = session.createPublisher(mytopic);
079: } catch (Exception e) {
080: error += "Cannot create JMS Publisher:" + e;
081: }
082:
083: // publish messages to the topic
084: try {
085: for (int i = 0; i < loop; i++) {
086: if (timeout > 0) {
087: Thread.currentThread().sleep(1000 * timeout);
088: }
089: MapMessage mess = session.createMapMessage();
090: mess.setInt("Severity", severity);
091: mess.setString("From", device);
092: mess.setString("Reason", message);
093: tp.publish(mess);
094: }
095: } catch (JMSException e) {
096: String l = e.getLinkedException().toString();
097: error += " Exception while creating or sending message: "
098: + e;
099: error += " Linked Exception: " + l;
100: } catch (java.lang.InterruptedException e) {
101: out.println("Alarm Generator Interrupted");
102: }
103:
104: if (error.equals("")) {
105: out.println("Message sent");
106: } else {
107: out.println(error);
108: }
109: out
110: .println("<p>return to <a href=\"../generator.html\">Alarm Generator</a>");
111: out.println("<p>go to <a href=\"list.jsp\">Alarm List</a>");
112:
113: // close connection
114: try {
115: mytc.close();
116: } catch (Exception e) {
117: }
118:
119: out.println("</body>");
120: out.println("</html>");
121: }
122: }
|