KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > lodgingsupplier > pomessagebean > LodgingMessageBean


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

60     public LodgingMessageBean() {}
61
62     /**
63      * Sets the context for this bean.
64      */

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

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

141     public void persistOrder(LodgingOrder lodge)
142   throws OrderSubmissionException {
143
144   try {
145             ServiceLocator sl = new ServiceLocator();
146
147             LodgingOrderLocalHome lodgeLocalHome = (LodgingOrderLocalHome)
148                 sl.getLocalHome(JNDINames.LODGE_ORDER_EJB);
149             LodgingOrderLocal lodgeLocal =
150                 (LodgingOrderLocal) lodgeLocalHome.create(lodge);
151         } catch (ServiceLocatorException je) {
152             throw new OrderSubmissionException("Error LODGE persisting order:"
153                                                + je.getMessage());
154         } catch(CreateException ce) {
155             throw new OrderSubmissionException("Error LODGE persisting order:"
156                                                + ce.getMessage());
157         }
158     }
159
160
161     /**
162      * Creates a bean.
163      */

164     public void ejbCreate() {}
165
166     /**
167      * Removes this bean.
168      */

169     public void ejbRemove() {
170         mdc = null;
171     }
172 }
173
Popular Tags