KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > activitysupplier > pomessagebean > ActivityMessageBean


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 package com.sun.j2ee.blueprints.activitysupplier.pomessagebean;
38 import javax.ejb.*;
39 import javax.jms.*;
40 import javax.xml.rpc.*;
41 import javax.naming.*;
42
43 import com.sun.j2ee.blueprints.activitysupplier.JNDINames;
44 import com.sun.j2ee.blueprints.activitysupplier.powebservice.*;
45 import com.sun.j2ee.blueprints.activitysupplier.purchaseorder.ejb.*;
46 import com.sun.j2ee.blueprints.servicelocator.*;
47 import com.sun.j2ee.blueprints.servicelocator.ejb.*;
48
49 public class ActivityMessageBean implements
50         MessageDrivenBean, MessageListener {
51     
52     private transient MessageDrivenContext mdc = null;
53     
54     /**
55      * Default constructor.
56      */

57     public ActivityMessageBean() {}
58     
59     /**
60      * Sets the context for this bean.
61      */

62     public void setMessageDrivenContext(MessageDrivenContext mdc) {
63         this.mdc = mdc;
64     }
65     
66     /**
67      * Casts the incoming message to an ObjectMessage.
68      */

69     public void onMessage(Message message) {
70         ActivityOrder ao = null;
71         
72         try {
73             String JavaDoc messageID = message.getJMSMessageID();
74             if (message instanceof ObjectMessage) {
75                 ObjectMessage msg = (ObjectMessage)message;
76                 ao = (ActivityOrder)msg.getObject();
77             } else {
78                 System.out.println("Wrong type message: "
79                         + message.getClass().getName());
80             }
81         } catch (JMSException e) {
82             // Proper exception handling as in OPC module has to be
83
// implemented here later
84
e.printStackTrace();
85         }
86         
87         try {
88             doWork(ao);
89         } catch (OrderSubmissionException oe) {
90             // Proper exception handling as in OPC module has to be
91
// implemented here later
92
oe.printStackTrace();
93         }
94         
95     }
96     
97     private void doWork(ActivityOrder act) throws OrderSubmissionException {
98         try {
99             persistOrder(act);
100         } catch (Exception JavaDoc e) {
101             // Proper exception handling as in OPC module has to be
102
// implemented here later
103
e.printStackTrace();
104         }
105         sendInvoice(act);
106     }
107     
108     private void sendInvoice(ActivityOrder act) {
109         Invoice inv = new Invoice("1234", act.getOrderId(), "ACTIVITY_INVOICE",
110                 act, "COMPLETED");
111         try {
112             InitialContext ic = new InitialContext();
113             WebServiceBroker svc = (WebServiceBroker)
114             ic.lookup(JNDINames.BROKER_SERVICE_NAME);
115             String JavaDoc endpointURI = (String JavaDoc)
116             ic.lookup(JNDINames.BROKER_SERVICE_URL);
117             BrokerServiceIntf port= (BrokerServiceIntf)
118             svc.getPort(BrokerServiceIntf.class);
119             
120             // Required because we build the stubs using static WSDL
121
((Stub)port)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,
122                     endpointURI);
123             port.submitDocument(inv.toXML());
124         } catch (Exception JavaDoc ne) {
125             // Proper exception handling as in OPC module has to be
126
// implemented here later
127
ne.printStackTrace();
128         }
129     }
130     
131     /**
132      * Persists the ActivityOrder
133      */

134     public void persistOrder(ActivityOrder act)
135     throws OrderSubmissionException {
136         
137         try {
138             ServiceLocator sl = new ServiceLocator();
139             
140             ActivityPurchaseOrderLocalHome actLocalHome =
141                     (ActivityPurchaseOrderLocalHome)
142                     sl.getLocalHome(JNDINames.ACTIVITY_PURCHASEORDER_EJB);
143             ActivityPurchaseOrderLocal actLocal =
144                     (ActivityPurchaseOrderLocal) actLocalHome.create(act);
145         } catch (ServiceLocatorException je) {
146             throw new OrderSubmissionException("Error while persisting order:"
147                     + je.getMessage());
148         } catch(CreateException ce) {
149             throw new OrderSubmissionException("Error while persisting order:"
150                     + ce.getMessage());
151         }
152     }
153     
154     
155     /**
156      * Creates a bean.
157      */

158     public void ejbCreate() {}
159     
160     /**
161      * Removes this bean.
162      */

163     public void ejbRemove() {
164         mdc = null;
165     }
166 }
167
Popular Tags