KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > lodgingsupplier > powebservice > LodgingPOEndpointBean


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.powebservice;
39
40 import javax.ejb.*;
41 import java.rmi.*;
42 import javax.naming.*;
43 import javax.jms.*;
44 import com.sun.j2ee.blueprints.lodgingsupplier.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 lodging suppliers;
51  */

52 public class LodgingPOEndpointBean implements SessionBean {
53     
54     private SessionContext sc;
55     
56     public LodgingPOEndpointBean(){}
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 submitLodgingReservationDetails(String JavaDoc xmlPO)
66     throws InvalidOrderException, OrderSubmissionException,
67             RemoteException {
68         
69         // Do Interaction layer processing
70
LodgingOrder lodgeObj = preProcessInput(xmlPO);
71         
72         // Submit request to JMS Queue
73
submitRequest(lodgeObj);
74         
75         // Return co-relation ID - dummy ID for now
76
return("LODGE1234");
77     }
78     
79     private LodgingOrder 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(LodgingOrder.fromXML(po));
85     }
86     
87     private void submitRequest(LodgingOrder lodge)
88     throws OrderSubmissionException {
89         
90         ConnectionFactory jmsConnectionFactory = null;
91         Destination jmsDest = 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.LODGE_QUEUECONNECTIONFACTORY);
100             jmsDest = sl.getJMSDestination(JNDINames.LODGE_QUEUE);
101             jmsConnection = jmsConnectionFactory.createConnection();
102             jmsSession = jmsConnection.createSession(true,
103                     Session.AUTO_ACKNOWLEDGE);
104             jmsSender = jmsSession.createProducer(jmsDest);
105             
106             ObjectMessage message = jmsSession.createObjectMessage();
107             message.setObject(lodge);
108             jmsSender.send(message);
109             
110         } catch (ServiceLocatorException se) {
111             throw new OrderSubmissionException("Error while sending a message:"
112                     + se.getMessage());
113         } catch (JMSException e) {
114             throw new OrderSubmissionException("Error while sending a message:"
115                     + e.getMessage());
116         } finally {
117             // close all JMS resources
118
if (jmsSender != null) {
119                 try {
120                     jmsSender.close();
121                 } catch (JMSException e) {
122                     throw new OrderSubmissionException("Error sender close");
123                 }
124             }
125             if (jmsSession != null) {
126                 try {
127                     jmsSession.close();
128                 } catch (JMSException e) {
129                     throw new OrderSubmissionException("Error session close");
130                 }
131             }
132             if (jmsConnection != null) {
133                 try {
134                     jmsConnection.close();
135                 } catch (JMSException e) {
136                     throw new OrderSubmissionException("Error Connection close");
137                 }
138             }
139         }
140     }
141     
142     
143     public void setSessionContext(SessionContext sc) {
144         this.sc = sc;
145     }
146     
147     public void ejbRemove() throws RemoteException {}
148     
149     
150     //empty for Stateless EJBs
151
public void ejbActivate() {}
152     
153     //empty for Stateless EJBs
154
public void ejbPassivate() {}
155 }
156
Popular Tags