KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > message > NotificationBean


1 /*
2 * 02/01/2002 - 15:24:07
3 *
4 * NotificationBean.java -
5 * Copyright (C) 2002 Ecoo Team
6 * charoy@loria.fr
7 *
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */

23
24 package hero.message;
25
26 // MDB imports
27
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 JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import javax.ejb.EJBException JavaDoc;
45 import javax.ejb.MessageDrivenBean JavaDoc;
46 import javax.ejb.MessageDrivenContext JavaDoc;
47 import javax.jms.Message JavaDoc;
48 import javax.jms.MessageListener JavaDoc;
49
50 import hero.util.MailNotification;
51
52 import javax.management.MBeanServer JavaDoc;
53 import javax.management.MBeanServerFactory JavaDoc;
54 import javax.management.ObjectName JavaDoc;
55
56
57 /**
58  * This is a Message Driven Bean
59  *
60  * @ejb:message-driven
61  * @ejb:bean name="NotificationBean"
62  * transaction-type="Container"
63  * acknowledge-mode="Auto-acknowledge"
64  * destination-type="javax.jms.Topic"
65  * subscription-durability="NonDurable"
66  *
67  * @jboss:destination-jndi-name name="testTopic"
68  * @ejb.security-identity run-as="SuperAdmin"
69  * @ejb.permission unchecked="yes"
70  *
71  *
72  * @jonas.message-driven-destination jndi-name="testTopic"
73  * @jonas.bean ejb-name="NotificationBean"
74  * jndi-name="testTopic"
75  * @ejb.transaction type= "NotSupported"
76  *
77  * @author $Author: mvaldes $
78  * @version $Revision: 1.7 $
79  */

80
81 public class NotificationBean implements MessageDrivenBean JavaDoc, MessageListener JavaDoc{
82
83     //private MessageDrivenContext ctx = null;
84
private transient MessageDrivenContext JavaDoc ctx;
85     private MailNotification mailNotif = new MailNotification();
86    
87     private void sendJabber(String JavaDoc toAccount, String JavaDoc subject, String JavaDoc message) throws Exception JavaDoc{
88     //ObjectName jabber = new ObjectName("jboss:service=Jabber");
89
ObjectName JavaDoc jabber = new ObjectName JavaDoc("jonas:type=service,name=Jabber");
90     List JavaDoc list = MBeanServerFactory.findMBeanServer(null);
91     MBeanServer JavaDoc mServer = (MBeanServer JavaDoc)list.iterator().next();
92     Object JavaDoc id = (Object JavaDoc) mServer.invoke( jabber,
93                          "sendMessage",
94                          new Object JavaDoc[] {
95                              toAccount, //JID
96
null , //Resource
97
subject, //Subject
98
message //message
99
},
100                          new String JavaDoc[] {
101                              "".getClass().getName(),
102                              "".getClass().getName(),
103                              "".getClass().getName(),
104                              "".getClass().getName()
105                          }
106                           );
107     }
108
109
110     public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx) throws EJBException JavaDoc
111     {
112         this.ctx = ctx;
113     }
114
115     private Collection JavaDoc usersRegistered(String JavaDoc project, String JavaDoc event) throws Exception JavaDoc {
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 JavaDoc());
123         }catch(Exception JavaDoc usersRegistered) {
124             Collection JavaDoc users = new ArrayList JavaDoc();
125             return (users);
126         }
127     }
128
129     public void ejbCreate()
130     {
131     }
132
133     public void ejbRemove()
134     {
135     // ctx=null;
136
}
137     
138     public void onMessage(Message JavaDoc message)
139     {
140     BnUserLocal mUser;
141     UserServiceLocalHome ushome;
142     UserServiceLocal ul;
143     try{
144         ushome = UserServiceUtil.getLocalHome();
145         ul=ushome.create();
146         
147         String JavaDoc event= (String JavaDoc)message.getObjectProperty(EventConstants.EVENT);
148         String JavaDoc projectName = (String JavaDoc)message.getObjectProperty(EventConstants.PROJECTNAME);
149         if((event.equalsIgnoreCase(EventConstants.START) || event.equalsIgnoreCase(EventConstants.TERMINATED))&&!event.equalsIgnoreCase(EventConstants.DELETEPROJECT)){
150         Collection JavaDoc c=usersRegistered(projectName,event);
151         for(Iterator JavaDoc i=c.iterator();i.hasNext();){
152             mUser=(BnUserLocal)i.next();
153             Map JavaDoc infos = ul.getUserInfos(mUser.getName());
154             Collection JavaDoc props=mUser.getBnProperties();
155             for(Iterator JavaDoc 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 JavaDoc)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 JavaDoc)infos.get("jabber"),"Event Notification","Event Notification:"+projectName+" Event:"+event);
164                 }
165             }
166             }
167         }
168         }
169     }catch(Exception JavaDoc e1){
170         ctx.setRollbackOnly();
171         e1.printStackTrace();}
172     }
173     
174 }
175
Popular Tags