1 package hero.session; 2 3 31 32 import hero.util.EventConstants; 33 import hero.util.BonitaConfig; 34 import hero.util.BonitaServiceException; 35 import javax.ejb.CreateException ; 36 import javax.ejb.SessionBean ; 37 import javax.ejb.SessionContext ; 38 39 import javax.jms.TopicConnectionFactory ; 40 import javax.jms.Topic ; 41 import javax.jms.TopicConnection ; 42 import javax.jms.TopicSession ; 43 import javax.jms.TopicSubscriber ; 44 import javax.jms.TopicPublisher ; 45 import javax.jms.TextMessage ; 46 import javax.jms.ObjectMessage ; 47 import javax.jms.JMSException ; 48 import javax.jms.Session ; 49 50 import java.io.InputStreamReader ; 51 import java.util.Hashtable ; 52 import java.util.Enumeration ; 53 54 import hero.util.BonitaServiceLocator; 55 import hero.util.HeroException; 56 57 76 77 public class JMSServicesSessionBean implements SessionBean , EventConstants { 78 79 private TopicConnectionFactory topicConnectionFactory = null; 80 private TopicSession topicSession = null; 81 private Topic topic = null; 82 private TopicConnection topicConnection = null; 83 private TopicSubscriber topicSubscriber = null; 84 private TopicPublisher topicPublisher = null; 85 private TextMessage message = null; 86 private ObjectMessage omessage = null; 87 private InputStreamReader inputStreamReader = null; 88 private BonitaConfig bonitaConfig; 89 90 94 private SessionContext mContext; 95 96 100 109 public void send(Hashtable evt, String type) throws Exception { 110 try{ 111 if (((String )evt.get(EVENT)).equals(DELETEPROJECT) || bonitaConfig.getProcessJMS((String )evt.get(PROJECTNAME))) 112 { 113 StringBuffer subs = new StringBuffer (""); 114 115 try { 116 topicConnection = topicConnectionFactory.createTopicConnection(); 117 topicSession =topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); 118 } catch (JMSException e) { 119 System.out.println("Exception occurred: " + e.toString()); 120 throw new CreateException (e.getMessage()); 121 } 122 123 try { 124 topicPublisher = topicSession.createPublisher(topic); 125 message = topicSession.createTextMessage(); 126 Enumeration keys = evt.keys(); 127 while (keys.hasMoreElements()) { 128 String key = (String ) keys.nextElement(); 129 String value = (String ) 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 res = subs.toString(); 137 String 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 e) { 145 System.out.println("Exception occurred: " + e.toString()); 146 throw new Exception (e.getMessage()); 147 } 148 } 149 } catch (HeroException he){} } 151 152 165 public void sendNodeEvent(String event,String projectName,String nodeName,int type,int state,String userName) 166 { 167 try{ 168 Hashtable evt = new Hashtable (); 169 evt.put(EVENT, event); 170 evt.put(PROJECTNAME, projectName); 171 evt.put(NODENAME, nodeName); 172 evt.put(NODETYPE, (new Integer (type)).toString()); 173 evt.put(NODESTATE, (new Integer (state)).toString()); 174 evt.put(USERNAME, userName); 175 this.send(evt, NODE); 176 }catch(Exception e){e.printStackTrace();} 177 } 178 191 public void sendEdgeEvent(String event,String projectName,String edgeName,String nodeIn,String nodeOut,String userName) 192 { 193 try{ 194 Hashtable evt = new Hashtable (); 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 e){e.printStackTrace();} 203 } 204 213 public void sendProjectEvent(String event, String projectName) 214 { 215 try{ 216 Hashtable evt = new Hashtable (); 217 evt.put(EVENT, event); 218 evt.put(PROJECTNAME, projectName); 219 this.send(evt, PROJECT); 220 }catch(Exception e){e.printStackTrace();} 221 } 222 223 233 234 public void sendProjectEvent(String event,String projectName,String userName) 235 { 236 try{ 237 Hashtable evt = new Hashtable (); 238 evt.put(EVENT, event); 239 evt.put(PROJECTNAME, projectName); 240 evt.put(USERNAME, userName); 241 this.send(evt, PROJECT); 242 }catch(Exception e){e.printStackTrace();} 243 } 244 255 public void sendProjectEvent(String event,String projectName,String nodeName,String userName) 256 { 257 try{ 258 Hashtable evt = new Hashtable (); 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 e){e.printStackTrace();} 265 } 266 267 276 public void sendIterationEvent(String event, String projectName, String from, String to) 277 { 278 try{ 279 Hashtable evt = new Hashtable (); 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 e){e.printStackTrace();} 286 } 287 288 298 public void sendUserEvent(String event,String projectName,String userName) 299 { 300 try{ 301 Hashtable evt = new Hashtable (); 302 evt.put(EVENT, event); 303 evt.put(PROJECTNAME, projectName); 304 evt.put(USERNAME, userName); 305 this.send(evt, USER); 306 }catch(Exception e){e.printStackTrace();} 307 } 308 318 public void sendRoleEvent(String event,String projectName,String roleName) 319 { 320 try{ 321 Hashtable evt = new Hashtable (); 322 evt.put(EVENT, event); 323 evt.put(PROJECTNAME, projectName); 324 evt.put(ROLENAME, roleName); 325 this.send(evt, ROLE); 326 }catch(Exception e){e.printStackTrace();} 327 } 328 339 public void sendUserRoleEvent(String event,String projectName,String userName,String roleName) 340 { 341 try{ 342 Hashtable evt = new Hashtable (); 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 e){e.printStackTrace();} 349 } 350 351 private void initJMS() throws CreateException { 352 try { 353 BonitaServiceLocator serviceLocator = BonitaServiceLocator.getInstance(); 354 topicConnectionFactory = (TopicConnectionFactory ) serviceLocator.getResource(BonitaServiceLocator.Services.TOPIC_CONNECTION_FACTORY); 355 topic = (Topic )serviceLocator.getResource(BonitaServiceLocator.Services.TOPIC); 356 } catch (BonitaServiceException e) { 357 throw new CreateException (e.getMessage()); 358 } 359 360 } 361 362 370 371 public void ejbCreate() throws CreateException { 372 initJMS(); 373 try { 374 this.bonitaConfig = new BonitaConfig(); 375 }catch(HeroException e) {throw new CreateException ();} 376 377 } 378 379 public void setSessionContext(final javax.ejb.SessionContext 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 |