KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > airlinesupplier > powebservice > AirlinePOEndpointBean


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.powebservice;
39
40 import javax.ejb.*;
41 import java.rmi.*;
42 import javax.naming.*;
43 import javax.jms.*;
44 import com.sun.j2ee.blueprints.airlinesupplier.JNDINames;
45 import com.sun.j2ee.blueprints.servicelocator.*;
46 import com.sun.j2ee.blueprints.servicelocator.ejb.*;
47
48 /**
49  * This class is the entry point for purchase orders submitted
50  * by OPC to the airline suppliers;
51  */

52 public class AirlinePOEndpointBean implements SessionBean {
53
54     private SessionContext sc;
55  
56     public AirlinePOEndpointBean(){}
57     
58     public void ejbCreate() throws CreateException {}
59
60     /**
61      * Receive an order, preprocess the request, submit request to workflow
62      * and return the order id so that the caller can have a correlation id
63      * for the order
64      */

65     public String JavaDoc submitAirlineReservationDetails(String JavaDoc xmlPO)
66   throws InvalidOrderException, OrderSubmissionException,
67     RemoteException {
68
69   // Do Interaction layer processing
70
AirlineOrder flightObj = preProcessInput(xmlPO);
71
72   // Submit request to JMS Queue
73
submitRequest(flightObj);
74
75   // Return Co-relation ID - dummy ID for now
76
return("FLIGHT1234");
77     }
78
79     private AirlineOrder preProcessInput(String JavaDoc po)
80   throws InvalidOrderException {
81   // XML doc should be ideally validated against its schema here;
82
// Similar scenario shown in OPC module;
83
// Here it is skipped in this sample - we will convert doc to obj
84
return(AirlineOrder.fromXML(po));
85     }
86
87     private void submitRequest(AirlineOrder flight)
88                               throws OrderSubmissionException {
89
90   ConnectionFactory jmsConnectionFactory = null;
91   Destination dest = null;
92   Connection jmsConnection = null;
93   Session jmsSession = null;
94   MessageProducer jmsSender = null;
95
96   try {
97       ServiceLocator sl = new ServiceLocator();
98             jmsConnectionFactory = (ConnectionFactory)
99     sl.getJMSConnectionFactory(JNDINames.AIRLINE_QUEUECONNECTIONFACTORY);
100             dest = sl.getJMSDestination(JNDINames.AIRLINE_QUEUE);
101             jmsConnection = jmsConnectionFactory.createConnection();
102             jmsSession = jmsConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
103             jmsSender = jmsSession.createProducer(dest);
104
105             ObjectMessage message = jmsSession.createObjectMessage();
106             message.setObject(flight);
107             jmsSender.send(message);
108   } catch (ServiceLocatorException se) {
109       throw new OrderSubmissionException("Error while sending a message:"
110             + se.getMessage());
111   } catch (JMSException e) {
112         throw new OrderSubmissionException("Error while sending a message:"
113                                                 + e.getMessage());
114   } finally {
115             // close all JMS resources
116
if (jmsSender != null) {
117     try {
118                     jmsSender.close();
119     } catch (JMSException e) {
120                     throw new OrderSubmissionException("Error sender close");
121     }
122             }
123             if (jmsSession != null) {
124     try {
125                     jmsSession.close();
126     } catch (JMSException e) {
127                     throw new OrderSubmissionException("Error session close");
128     }
129             }
130             if (jmsConnection != null) {
131     try {
132                     jmsConnection.close();
133     } catch (JMSException e) {
134         throw new OrderSubmissionException("Error Connection close");
135     }
136             }
137   }
138     }
139
140         
141     public void setSessionContext(SessionContext sc) {
142         this.sc = sc;
143     }
144     
145     public void ejbRemove() throws RemoteException {}
146     
147
148     //empty for Stateless EJBs
149
public void ejbActivate() {}
150
151     //empty for Stateless EJBs
152
public void ejbPassivate() {}
153 }
154
Popular Tags