1 37 38 package com.sun.j2ee.blueprints.opc.crm.ejb; 39 40 import java.util.*; 41 import javax.ejb.*; 42 import javax.naming.*; 43 import javax.jms.*; 44 import java.io.*; 45 import org.w3c.dom.*; 46 import org.xml.sax.*; 47 import javax.xml.parsers.*; 48 import javax.xml.transform.*; 49 import javax.xml.transform.dom.*; 50 import javax.xml.transform.stream.StreamResult ; 51 52 import com.sun.j2ee.blueprints.opc.mailer.*; 53 54 60 public class CrmBean implements MessageDrivenBean, MessageListener { 61 62 private Context context; 63 private MessageDrivenContext mdc; 64 65 public CrmBean() { 66 } 67 68 public void ejbCreate() { 69 } 70 71 76 public void onMessage(Message recvMsg) { 77 TextMessage recdTM = null; 78 String recdText = null; 79 String result = null; 80 81 try { 82 recdTM = (TextMessage)recvMsg; 83 recdText = recdTM.getText(); 84 result = doWork(recdText); 85 } catch (JMSException je) { 86 throw new EJBException(je); 87 } 88 } 89 90 public void setMessageDrivenContext(MessageDrivenContext mdc) { 91 this.mdc = mdc; 92 } 93 94 public void ejbRemove() { 95 } 96 97 100 private String doWork(String message) throws JMSException { 101 102 Document doc = getDocument(message); 103 String recipient = getTagValue(doc.getDocumentElement(), "Address"); 104 String xmlMessage = getTagValue(doc.getDocumentElement(), "Content"); 105 String title = getTagValue(doc.getDocumentElement(), "Subject"); 106 if (recipient == null) return "failed"; 107 try { 108 MailHelper mh = new MailHelper(); 109 mh.createAndSendMail(recipient, title, xmlMessage, Locale.getDefault()); 110 } catch (MailerException mx) { 111 System.err.println("CrmBean: caught=" + mx); 112 mx.printStackTrace(); 113 } catch (Exception ex) { 114 System.err.println("CrmBean: caught=" + ex); 115 ex.printStackTrace(); 116 } 117 return "success"; 118 } 119 120 private Document getDocument(String xmlText) { 121 Document doc = null; 122 try { 123 DocumentBuilderFactory docBuilderFactory = null; 124 DocumentBuilder db = null; 125 try { 126 docBuilderFactory = DocumentBuilderFactory.newInstance(); 127 if (docBuilderFactory != null) 128 db = docBuilderFactory.newDocumentBuilder(); 129 } catch ( javax.xml.parsers.ParserConfigurationException pce) { 130 System.err.println(pce); 131 } 132 InputSource is = new InputSource(new StringReader(xmlText)); 133 doc = db.parse(is); 134 } catch (Exception e) { 135 System.err.println("CrmBean::getDocument error loading XML Document " + e); 136 } 137 return doc; 138 } 139 140 private String getTagValue(Element root, String tagName) { 141 String returnString = ""; 142 NodeList list = root.getElementsByTagName(tagName); 143 for (int loop = 0; loop < list.getLength(); loop++) { 144 Node node = list.item(loop); 145 if (node != null) { 146 Node child = node.getFirstChild(); 147 if ((child != null) && child.getNodeValue() != null) return child.getNodeValue(); 148 } 149 } 150 return returnString; 151 } 152 } 153 154 | Popular Tags |