KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > session > JMSServicesSessionBean


1 package hero.session;
2
3 /**
4 *
5 * Bonita
6 * Copyright (C) 1999 Bull S.A.
7 * Bull 68 route de versailles 78434 Louveciennes Cedex France
8 * Further information: bonita@objectweb.org
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 *
26 --------------------------------------------------------------------------
27 * $Id: JMSServicesSessionBean.java,v 1.8 2005/03/18 14:54:29 mvaldes Exp $
28 *
29 --------------------------------------------------------------------------
30 */

31
32 import hero.util.EventConstants;
33 import hero.util.BonitaConfig;
34 import hero.util.BonitaServiceException;
35 import javax.ejb.CreateException JavaDoc;
36 import javax.ejb.SessionBean JavaDoc;
37 import javax.ejb.SessionContext JavaDoc;
38
39 import javax.jms.TopicConnectionFactory JavaDoc;
40 import javax.jms.Topic JavaDoc;
41 import javax.jms.TopicConnection JavaDoc;
42 import javax.jms.TopicSession JavaDoc;
43 import javax.jms.TopicSubscriber JavaDoc;
44 import javax.jms.TopicPublisher JavaDoc;
45 import javax.jms.TextMessage JavaDoc;
46 import javax.jms.ObjectMessage JavaDoc;
47 import javax.jms.JMSException JavaDoc;
48 import javax.jms.Session JavaDoc;
49
50 import java.io.InputStreamReader JavaDoc;
51 import java.util.Hashtable JavaDoc;
52 import java.util.Enumeration JavaDoc;
53
54 import hero.util.BonitaServiceLocator;
55 import hero.util.HeroException;
56
57 /**
58  * Session Bean Template
59  *
60  * @ejb:bean name="JMSServicesSession"
61  * display-name="JMSServices Bean"
62  * type="Stateful"
63  * transaction-type="Container"
64  * jndi-name="ejb/hero/JMSServicesSession"
65  * local-jndi-name="ejb/hero/JMSServicesSession_L"
66  *
67  * @ejb:transaction type="Required"
68  * @ejb.permission unchecked="yes"
69  * @ejb.security-identity run-as="SuperAdmin"
70  * @jonas.bean
71  * ejb-name="JMSServicesSession"
72  * jndi-name="ejb/hero/JMSServicesSession"
73  *
74  *
75  **/

76
77 public class JMSServicesSessionBean implements SessionBean JavaDoc, EventConstants {
78
79     private TopicConnectionFactory JavaDoc topicConnectionFactory = null;
80     private TopicSession JavaDoc topicSession = null;
81     private Topic JavaDoc topic = null;
82     private TopicConnection JavaDoc topicConnection = null;
83     private TopicSubscriber JavaDoc topicSubscriber = null;
84     private TopicPublisher JavaDoc topicPublisher = null;
85     private TextMessage JavaDoc message = null;
86     private ObjectMessage JavaDoc omessage = null;
87     private InputStreamReader JavaDoc inputStreamReader = null;
88     private BonitaConfig bonitaConfig;
89
90     // -------------------------------------------------------------------------
91
// Members
92
// -------------------------------------------------------------------------
93

94     private SessionContext JavaDoc mContext;
95
96     // -------------------------------------------------------------------------
97
// Methods
98
// -------------------------------------------------------------------------
99

100     /**
101     * Send messages, varying text slightly.
102     *
103     * @param evt Hashtable that includes the message
104     * @param type Jms type
105     * @ejb.permission unchecked="yes"
106     * @ejb:interface-method view-type="both"
107     *
108     **/

109     public void send(Hashtable JavaDoc evt, String JavaDoc type) throws Exception JavaDoc {
110         try{
111             if (((String JavaDoc)evt.get(EVENT)).equals(DELETEPROJECT) || bonitaConfig.getProcessJMS((String JavaDoc)evt.get(PROJECTNAME)))
112             {
113                 StringBuffer JavaDoc subs = new StringBuffer JavaDoc("");
114                 
115                 try {
116                     topicConnection = topicConnectionFactory.createTopicConnection();
117                     topicSession =topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
118                 } catch (JMSException JavaDoc e) {
119                     System.out.println("Exception occurred: " + e.toString());
120                     throw new CreateException JavaDoc(e.getMessage());
121                 }
122                 
123                 try {
124                     topicPublisher = topicSession.createPublisher(topic);
125                     message = topicSession.createTextMessage();
126                     Enumeration JavaDoc keys = evt.keys();
127                     while (keys.hasMoreElements()) {
128                         String JavaDoc key = (String JavaDoc) keys.nextElement();
129                         String JavaDoc value = (String JavaDoc) evt.get(key);
130                         message.setStringProperty(key, value);
131                         subs.append(" " + key + " = '" + value + "' AND ");
132                         evt.put(key, value);
133                     }
134                     message.setJMSType(type);
135                     
136                     String JavaDoc res = subs.toString();
137                     String JavaDoc result = res.substring(0, res.length() - 4);
138                     message.setText(result);
139                     
140                     topicPublisher.publish(message);
141                     topicSession.close();
142                     topicConnection.close();
143                     
144                 } catch (JMSException JavaDoc e) {
145                     System.out.println("Exception occurred: " + e.toString());
146                     throw new Exception JavaDoc(e.getMessage());
147                 }
148             }
149         } catch (HeroException he){}// remove project consistency
150
}
151
152     /**
153     * Send Node Event.
154     *
155     * @param event The name of the event
156     * @param projectName The name of the project
157     * @param nodeName The name of the node
158     * @param type The type of the node
159     * @param state The state of the node
160     * @param userName The name of the user
161     * @ejb.permission unchecked="yes"
162     * @ejb:interface-method view-type="both"
163     *
164     **/

165     public void sendNodeEvent(String JavaDoc event,String JavaDoc projectName,String JavaDoc nodeName,int type,int state,String JavaDoc userName)
166     {
167     try{
168         Hashtable JavaDoc evt = new Hashtable JavaDoc();
169         evt.put(EVENT, event);
170         evt.put(PROJECTNAME, projectName);
171         evt.put(NODENAME, nodeName);
172         evt.put(NODETYPE, (new Integer JavaDoc(type)).toString());
173         evt.put(NODESTATE, (new Integer JavaDoc(state)).toString());
174         evt.put(USERNAME, userName);
175         this.send(evt, NODE);
176     }catch(Exception JavaDoc e){e.printStackTrace();}
177     }
178     /**
179     * Send Edge Event.
180     *
181     * @param event The name of the event
182     * @param projectName The name of the project
183     * @param edgeName The name of the edge
184     * @param nodeIn The name of the in node
185     * @param nodeOut The name of the out node
186     * @param userName The name of the user
187     * @ejb.permission unchecked="yes"
188     * @ejb:interface-method view-type="both"
189     *
190     **/

191     public void sendEdgeEvent(String JavaDoc event,String JavaDoc projectName,String JavaDoc edgeName,String JavaDoc nodeIn,String JavaDoc nodeOut,String JavaDoc userName)
192     {
193      try{
194         Hashtable JavaDoc evt = new Hashtable JavaDoc();
195         evt.put(EVENT, event);
196         evt.put(PROJECTNAME, projectName);
197         evt.put(EDGENAME, edgeName);
198         evt.put(NODEIN, nodeIn);
199         evt.put(NODEOUT, nodeOut);
200         evt.put(USERNAME, userName);
201         this.send(evt, EDGE);
202       }catch(Exception JavaDoc e){e.printStackTrace();}
203     }
204     /**
205     * Send Project Event.
206     *
207     * @param event The name of the event
208     * @param projectName The name of the project
209     * @ejb.permission unchecked="yes"
210     * @ejb:interface-method view-type="both"
211     *
212     **/

213     public void sendProjectEvent(String JavaDoc event, String JavaDoc projectName)
214     {
215      try{
216         Hashtable JavaDoc evt = new Hashtable JavaDoc();
217         evt.put(EVENT, event);
218         evt.put(PROJECTNAME, projectName);
219         this.send(evt, PROJECT);
220       }catch(Exception JavaDoc e){e.printStackTrace();}
221     }
222     
223     /**
224     * Send Project Event.
225     *
226     * @param event The name of the event
227     * @param projectName The name of the project
228     * @param userName The name of the user
229     * @ejb.permission unchecked="yes"
230     * @ejb:interface-method view-type="both"
231     *
232     **/

233
234     public void sendProjectEvent(String JavaDoc event,String JavaDoc projectName,String JavaDoc userName)
235     {
236     try{
237         Hashtable JavaDoc evt = new Hashtable JavaDoc();
238         evt.put(EVENT, event);
239         evt.put(PROJECTNAME, projectName);
240         evt.put(USERNAME, userName);
241         this.send(evt, PROJECT);
242     }catch(Exception JavaDoc e){e.printStackTrace();}
243     }
244     /**
245     * Send Project Event.
246     *
247     * @param event The name of the event
248     * @param projectName The name of the project
249     * @param nodeName The name of the node
250     * @param userName The name of the user
251     * @ejb.permission unchecked="yes"
252     * @ejb:interface-method view-type="both"
253     *
254     **/

255     public void sendProjectEvent(String JavaDoc event,String JavaDoc projectName,String JavaDoc nodeName,String JavaDoc userName)
256     {
257     try{
258         Hashtable JavaDoc evt = new Hashtable JavaDoc();
259         evt.put(EVENT, event);
260         evt.put(PROJECTNAME, projectName);
261         evt.put(NODENAME, nodeName);
262         evt.put(USERNAME, userName);
263         this.send(evt, PROJECT);
264     }catch(Exception JavaDoc e){e.printStackTrace();}
265     }
266     
267     /**
268     * Send Iteration Event.
269     *
270     * @param event The name of the event
271     * @param projectName The name of the project
272     * @ejb.permission unchecked="yes"
273     * @ejb:interface-method view-type="both"
274     *
275     **/

276     public void sendIterationEvent(String JavaDoc event, String JavaDoc projectName, String JavaDoc from, String JavaDoc to)
277     {
278      try{
279         Hashtable JavaDoc evt = new Hashtable JavaDoc();
280         evt.put(EVENT, event);
281         evt.put(PROJECTNAME, projectName);
282         evt.put(FROM, from);
283         evt.put(TO, to);
284         this.send(evt, ITERATION);
285       }catch(Exception JavaDoc e){e.printStackTrace();}
286     }
287     
288     /**
289     * Send User Event.
290     *
291     * @param event The name of the event
292     * @param projectName The name of the project
293     * @param userName The name of the user
294     * @ejb.permission unchecked="yes"
295     * @ejb:interface-method view-type="both"
296     *
297     **/

298     public void sendUserEvent(String JavaDoc event,String JavaDoc projectName,String JavaDoc userName)
299     {
300      try{
301         Hashtable JavaDoc evt = new Hashtable JavaDoc();
302         evt.put(EVENT, event);
303         evt.put(PROJECTNAME, projectName);
304         evt.put(USERNAME, userName);
305         this.send(evt, USER);
306      }catch(Exception JavaDoc e){e.printStackTrace();}
307     }
308     /**
309     * Send Role Event.
310     *
311     * @param event The name of the event
312     * @param projectName The name of the project
313     * @param roleName The name of the role
314     * @ejb.permission unchecked="yes"
315     * @ejb:interface-method view-type="both"
316     *
317     **/

318     public void sendRoleEvent(String JavaDoc event,String JavaDoc projectName,String JavaDoc roleName)
319     {
320       try{
321         Hashtable JavaDoc evt = new Hashtable JavaDoc();
322         evt.put(EVENT, event);
323         evt.put(PROJECTNAME, projectName);
324         evt.put(ROLENAME, roleName);
325         this.send(evt, ROLE);
326       }catch(Exception JavaDoc e){e.printStackTrace();}
327     }
328     /**
329     * Send User Role Event.
330     *
331     * @param event The name of the event
332     * @param projectName The name of the project
333     * @param userName The name of the user
334     * @param roleName The name of the role
335     * @ejb.permission unchecked="yes"
336     * @ejb:interface-method view-type="both"
337     *
338     **/

339     public void sendUserRoleEvent(String JavaDoc event,String JavaDoc projectName,String JavaDoc userName,String JavaDoc roleName)
340     {
341       try{
342         Hashtable JavaDoc evt = new Hashtable JavaDoc();
343         evt.put(EVENT, event);
344         evt.put(PROJECTNAME, projectName);
345         evt.put(USERNAME, userName);
346         evt.put(ROLENAME, roleName);
347         this.send(evt, USERROLE);
348       }catch(Exception JavaDoc e){e.printStackTrace();}
349     }
350
351     private void initJMS() throws CreateException JavaDoc {
352         try {
353             BonitaServiceLocator serviceLocator = BonitaServiceLocator.getInstance();
354             topicConnectionFactory = (TopicConnectionFactory JavaDoc) serviceLocator.getResource(BonitaServiceLocator.Services.TOPIC_CONNECTION_FACTORY);
355             topic = (Topic JavaDoc)serviceLocator.getResource(BonitaServiceLocator.Services.TOPIC);
356         } catch (BonitaServiceException e) {
357             throw new CreateException JavaDoc(e.getMessage());
358         }
359
360     }
361
362     /**
363     * Create the All Projects Session Bean
364     *
365     * @throws CreateException
366     *
367     * @ejb.permission unchecked="yes"
368     * @ejb:create-method view-type="both"
369     **/

370
371     public void ejbCreate() throws CreateException JavaDoc {
372         initJMS();
373         try {
374             this.bonitaConfig = new BonitaConfig();
375         }catch(HeroException e) {throw new CreateException JavaDoc();}
376         
377     }
378
379     public void setSessionContext(final javax.ejb.SessionContext JavaDoc context) {
380         mContext = context;
381     }
382
383     public void ejbRemove() {
384     }
385
386     public void ejbActivate() {
387     }
388
389     public void ejbPassivate() {
390     }
391
392 }
393
Popular Tags