KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > crm > ejb > CrmBean


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

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 JavaDoc;
51
52 import com.sun.j2ee.blueprints.opc.mailer.*;
53
54 /**
55  * MailCompletedOrderMDB receives a JMS message containing an Order
56  * xml message for orders that are COMPLETELY shipped. It builds a mail
57  * message that it then sends to the mailer service so that
58  * the customer gets email
59  */

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   /**
72    * Receive a JMS Message containing the Order that is completed,
73    * generate Mail xml messages for the customer
74    * The Mail xml mesages contain html presentation
75    */

76   public void onMessage(Message recvMsg) {
77     TextMessage recdTM = null;
78     String JavaDoc recdText = null;
79     String JavaDoc 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     /**
98       * Process the message and do the right thing.
99       */

100     private String JavaDoc doWork(String JavaDoc message) throws JMSException {
101
102         Document doc = getDocument(message);
103         String JavaDoc recipient = getTagValue(doc.getDocumentElement(), "Address");
104         String JavaDoc xmlMessage = getTagValue(doc.getDocumentElement(), "Content");
105         String JavaDoc 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 JavaDoc ex) {
114             System.err.println("CrmBean: caught=" + ex);
115             ex.printStackTrace();
116         }
117         return "success";
118       }
119       
120     private Document getDocument(String JavaDoc 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 JavaDoc pce) {
130                 System.err.println(pce);
131             }
132           InputSource is = new InputSource(new StringReader(xmlText));
133             doc = db.parse(is);
134         } catch (Exception JavaDoc e) {
135             System.err.println("CrmBean::getDocument error loading XML Document " + e);
136         }
137         return doc;
138     }
139     
140     private String JavaDoc getTagValue(Element root, String JavaDoc tagName) {
141         String JavaDoc 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