KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > airlinesupplier > pomessagebean > AirlineMessageBean


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.airlinesupplier.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.airlinesupplier.JNDINames;
48 import com.sun.j2ee.blueprints.airlinesupplier.powebservice.*;
49 import com.sun.j2ee.blueprints.airlinesupplier.purchaseorder.ejb.*;
50 import com.sun.j2ee.blueprints.servicelocator.*;
51 import com.sun.j2ee.blueprints.servicelocator.ejb.*;
52
53 public class AirlineMessageBean implements MessageDrivenBean, MessageListener {
54     
55     private transient MessageDrivenContext mdc = null;
56     
57     /**
58      * Default constructor.
59      */

60     public AirlineMessageBean() {}
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         AirlineOrder alo = null;
74         
75         try {
76             String JavaDoc messageID = message.getJMSMessageID();
77             if (message instanceof ObjectMessage) {
78                 ObjectMessage msg = (ObjectMessage)message;
79                 alo = (AirlineOrder)msg.getObject();
80             } else {
81                 System.out.println("Wrong type message for AL order: "
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         try {
90             doWork(alo);
91         } catch (OrderSubmissionException oe) {
92             // Proper exception handling as in OPC module has to be
93
// implemented here later
94
oe.printStackTrace();
95         }
96         
97     }
98     
99     public void doWork(AirlineOrder flight) throws OrderSubmissionException {
100         try {
101             persistOrder(flight);
102         } catch (Exception JavaDoc e) {
103             // Proper exception handling as in OPC module has to be
104
// implemented here later
105
e.printStackTrace();
106         }
107         sendInvoice(flight);
108     }
109     
110     public void sendInvoice(AirlineOrder flight) {
111         Invoice inv = new Invoice("1234", flight.getOrderId(),
112                 "AIRLINE_INVOICE", "AGENT-001234",
113                 flight.getDepFlightDate(),
114                 flight.getRetFlightDate(),
115                 flight.getDepFlightId(),
116                 flight.getRetFlightId(),
117                 flight.getHeadCount(),
118                 "COMPLETED",
119                 "48 Hours Prior to Flight");
120         try {
121             InitialContext ic = new InitialContext();
122             WebServiceBroker svc = (WebServiceBroker)
123             ic.lookup(JNDINames.BROKER_SERVICE_NAME);
124             String JavaDoc endpointURI = (String JavaDoc)
125             ic.lookup(JNDINames.BROKER_SERVICE_URL);
126             BrokerServiceIntf port= (BrokerServiceIntf)
127             svc.getPort(BrokerServiceIntf.class);
128             
129             // Required because we build the stubs using static WSDL
130
((Stub)port)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,
131                     endpointURI);
132             port.submitDocument(inv.toXML());
133         } catch (Exception JavaDoc ne) {
134             // Proper exception handling as in OPC module has to be
135
// implemented here later
136
ne.printStackTrace();
137         }
138     }
139     
140     
141     /**
142      * Persists the AirlineOrder
143      */

144     public void persistOrder(AirlineOrder flight)
145     throws OrderSubmissionException {
146         
147         try {
148             ServiceLocator sl = new ServiceLocator();
149             
150             AirlineOrderLocalHome flightLocalHome = (AirlineOrderLocalHome)
151             sl.getLocalHome(JNDINames.AIRLINE_ORDER_EJB);
152             AirlineOrderLocal flightLocal =
153                     (AirlineOrderLocal) flightLocalHome.create(flight);
154         } catch (ServiceLocatorException je) {
155             throw new OrderSubmissionException("Error AL persisting order:"
156                     + je.getMessage());
157         } catch(CreateException ce) {
158             throw new OrderSubmissionException("Error AL persisting order:"
159                     + ce.getMessage());
160         }
161     }
162     
163     
164     /**
165      * Creates a bean.
166      */

167     public void ejbCreate() {}
168     
169     /**
170      * Removes this bean.
171      */

172     public void ejbRemove() {
173         mdc = null;
174     }
175 }
176
Popular Tags