1 23 24 package hero.message; 25 26 import hero.interfaces.BnUserLocal; 28 import hero.interfaces.BnUserPropertyLocal; 29 import hero.interfaces.BnProjectLocal; 30 import hero.interfaces.BnProjectLocalHome; 31 import hero.interfaces.BnProjectUtil; 32 import hero.interfaces.UserServiceUtil; 33 import hero.util.EventConstants; 34 import hero.interfaces.UserServiceLocalHome; 35 import hero.interfaces.UserServiceLocal; 36 37 38 import java.util.Collection ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.ArrayList ; 42 import java.util.Map ; 43 44 import javax.ejb.EJBException ; 45 import javax.ejb.MessageDrivenBean ; 46 import javax.ejb.MessageDrivenContext ; 47 import javax.jms.Message ; 48 import javax.jms.MessageListener ; 49 50 import hero.util.MailNotification; 51 52 import javax.management.MBeanServer ; 53 import javax.management.MBeanServerFactory ; 54 import javax.management.ObjectName ; 55 56 57 80 81 public class NotificationBean implements MessageDrivenBean , MessageListener { 82 83 private transient MessageDrivenContext ctx; 85 private MailNotification mailNotif = new MailNotification(); 86 87 private void sendJabber(String toAccount, String subject, String message) throws Exception { 88 ObjectName jabber = new ObjectName ("jonas:type=service,name=Jabber"); 90 List list = MBeanServerFactory.findMBeanServer(null); 91 MBeanServer mServer = (MBeanServer )list.iterator().next(); 92 Object id = (Object ) mServer.invoke( jabber, 93 "sendMessage", 94 new Object [] { 95 toAccount, null , subject, message }, 100 new String [] { 101 "".getClass().getName(), 102 "".getClass().getName(), 103 "".getClass().getName(), 104 "".getClass().getName() 105 } 106 ); 107 } 108 109 110 public void setMessageDrivenContext(MessageDrivenContext ctx) throws EJBException 111 { 112 this.ctx = ctx; 113 } 114 115 private Collection usersRegistered(String project, String event) throws Exception { 116 try{ 117 BnProjectLocalHome phome=BnProjectUtil.getLocalHome(); 118 BnProjectLocal plocal = phome.findByName(project); 119 if (plocal!=null) 120 return (plocal.getBnUsers()); 121 else 122 return(new ArrayList ()); 123 }catch(Exception usersRegistered) { 124 Collection users = new ArrayList (); 125 return (users); 126 } 127 } 128 129 public void ejbCreate() 130 { 131 } 132 133 public void ejbRemove() 134 { 135 } 137 138 public void onMessage(Message message) 139 { 140 BnUserLocal mUser; 141 UserServiceLocalHome ushome; 142 UserServiceLocal ul; 143 try{ 144 ushome = UserServiceUtil.getLocalHome(); 145 ul=ushome.create(); 146 147 String event= (String )message.getObjectProperty(EventConstants.EVENT); 148 String projectName = (String )message.getObjectProperty(EventConstants.PROJECTNAME); 149 if((event.equalsIgnoreCase(EventConstants.START) || event.equalsIgnoreCase(EventConstants.TERMINATED))&&!event.equalsIgnoreCase(EventConstants.DELETEPROJECT)){ 150 Collection c=usersRegistered(projectName,event); 151 for(Iterator i=c.iterator();i.hasNext();){ 152 mUser=(BnUserLocal)i.next(); 153 Map infos = ul.getUserInfos(mUser.getName()); 154 Collection props=mUser.getBnProperties(); 155 for(Iterator it=props.iterator();it.hasNext();){ 156 BnUserPropertyLocal pValue= (BnUserPropertyLocal)it.next(); 157 if((pValue.getTheKey()).equalsIgnoreCase(event+"Mail") && pValue.getTheValue()!= null){ 158 if(infos.get("email")!=null) 159 mailNotif.sendMail((String )infos.get("email"),"Event Notification","BnProject:"+projectName+" Event:"+event); 160 } 161 if((pValue.getTheKey()).equalsIgnoreCase(event+"Jabber") && pValue.getTheValue()!= null ){ 162 if(infos.get("jabber")!=null){ 163 sendJabber((String )infos.get("jabber"),"Event Notification","Event Notification:"+projectName+" Event:"+event); 164 } 165 } 166 } 167 } 168 } 169 }catch(Exception e1){ 170 ctx.setRollbackOnly(); 171 e1.printStackTrace();} 172 } 173 174 } 175
| Popular Tags
|