KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > activitysupplier > powebservice > ActivityPOEndpointBean


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

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

64     public String JavaDoc submitActivityReservationDetails(String JavaDoc xmlPO)
65          throws InvalidOrderException, OrderSubmissionException,
66                                                    RemoteException {
67
68   // Do Interaction layer processing
69
ActivityOrder order = preProcessInput(xmlPO);
70
71   // Submit request thro JMS Queue
72
submitRequest(order);
73
74   // Return reference id - dummy ID for now
75
return("ACT1234");
76     }
77
78     private ActivityOrder preProcessInput(String JavaDoc po)
79                         throws InvalidOrderException {
80   // XML doc should be ideally validated against its schema here;
81
// Similar scenario already shown in OPC module;
82
// Here it is skipped in this sample - we will convert doc to obj
83
return(ActivityOrder.fromXML(po));
84     }
85
86     private void submitRequest(ActivityOrder act)
87                         throws OrderSubmissionException {
88
89   ConnectionFactory jmsConnectionFactory = null;
90   Destination jmsDest = null;
91   Connection jmsConnection = null;
92   Session jmsSession = null;
93   MessageProducer jmsSender = null;
94
95   try {
96       ServiceLocator sl = new ServiceLocator();
97       jmsConnectionFactory = (ConnectionFactory)
98       sl.getJMSConnectionFactory(JNDINames.ACT_QUEUECONNECTIONFACTORY);
99       jmsDest = sl.getJMSDestination(JNDINames.ACT_QUEUE);
100       jmsConnection = jmsConnectionFactory.createConnection();
101       jmsSession = jmsConnection.createSession(true,
102         Session.AUTO_ACKNOWLEDGE);
103       jmsSender = jmsSession.createProducer(jmsDest);
104
105       ObjectMessage message = jmsSession.createObjectMessage();
106       message.setObject(act);
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     public void setSessionContext(SessionContext sc) {
141         this.sc = sc;
142     }
143     
144     public void ejbRemove() throws RemoteException {}
145     
146     //empty for Stateless EJBs
147
public void ejbActivate() {}
148
149     //empty for Stateless EJBs
150
public void ejbPassivate() {}
151 }
152
Popular Tags