1 package org.objectweb.alarm; 3 4 import java.io.*; 5 import javax.jms.*; 6 import javax.servlet.*; 7 import javax.servlet.http.*; 8 import javax.naming.Context ; 9 import javax.naming.InitialContext ; 10 import javax.naming.NamingException ; 11 import javax.rmi.PortableRemoteObject ; 12 13 16 public class Sender extends HttpServlet { 17 18 public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { 19 20 res.setContentType("text/html"); 21 PrintWriter out = res.getWriter(); 22 String device = req.getParameter("device"); 23 String level = req.getParameter("level"); 24 String message = req.getParameter("message"); 25 String sloop = req.getParameter("number"); 26 String stimeout = req.getParameter("timeout"); 27 int severity; 28 String error = ""; 29 30 out.write("<html><head><title>Alarm Sender Servlet</title></head><body>"); 31 if (level.startsWith("S")) { 32 severity = 1; 33 } else if (level.startsWith("W")) { 34 severity = 2; 35 } else { 36 severity = 3; 37 } 38 int loop = (new Integer (sloop)).intValue(); 39 int timeout = (new Integer (stimeout)).intValue(); 40 41 Context ictx = null; 42 try { 43 ictx = new InitialContext (); 44 } catch (Exception e) { 45 error += "Cannot get initial context:"+e; 46 } 47 48 TopicConnectionFactory tcf = null; 50 Topic mytopic = null; 51 TopicConnection mytc = null; 52 try { 53 tcf = (TopicConnectionFactory) ictx.lookup("java:comp/env/jms/TopicFactory"); 55 mytopic = (Topic) ictx.lookup("java:comp/env/jms/AlarmTopic"); 57 } catch (NamingException e) { 58 error += "Cannot lookup JMS Resources:"+ e; 59 } 60 61 try { 63 mytc = tcf.createTopicConnection(); 64 } catch (Exception e) { 65 error += "Cannot create JMS Connection:"+ e; 66 } 67 68 TopicSession session = null; 70 TopicPublisher tp = null; 71 try { 72 session = mytc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 73 tp = session.createPublisher(mytopic); 74 } catch (Exception e) { 75 error += "Cannot create JMS Publisher:"+ e; 76 } 77 78 try { 80 for (int i = 0 ; i < loop; i++) { 81 if (timeout > 0) { 82 Thread.currentThread().sleep(1000*timeout); 83 } 84 MapMessage mess = session.createMapMessage(); 85 mess.setInt("Severity", severity); 86 mess.setString("From", device); 87 mess.setString("Reason", message); 88 tp.publish(mess); 89 } 90 } catch (JMSException e) { 91 String l = e.getLinkedException().toString(); 92 error += " Exception while creating or sending message: "+ e; 93 error += " Linked Exception: "+l; 94 } catch (java.lang.InterruptedException e) { 95 out.println("Alarm Generator Interrupted"); 96 } 97 98 if (error.equals("")) { 99 out.println("Message sent"); 100 } else { 101 out.println(error); 102 } 103 out.println("<p>return to <a HREF=\"../generator.html\">Alarm Generator</a>"); 104 out.println("<p>go to <a HREF=\"list.jsp\">Alarm List</a>"); 105 106 try { 108 mytc.close(); 109 } catch (Exception e) { 110 } 111 112 out.println("</body>"); 113 out.println("</html>"); 114 } 115 } 116 | Popular Tags |