001: /*
002: * 02/01/2002 - 15:24:07
003: *
004: * NotificationBean.java -
005: * Copyright (C) 2002 Ecoo Team
006: * charoy@loria.fr
007: *
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022: */
023:
024: package hero.message;
025:
026: // MDB imports
027: import hero.interfaces.BnUserLocal;
028: import hero.interfaces.BnUserPropertyLocal;
029: import hero.interfaces.BnProjectLocal;
030: import hero.interfaces.BnProjectLocalHome;
031: import hero.interfaces.BnProjectUtil;
032: import hero.interfaces.UserServiceUtil;
033: import hero.util.EventConstants;
034: import hero.interfaces.UserServiceLocalHome;
035: import hero.interfaces.UserServiceLocal;
036:
037: import java.util.Collection;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.ArrayList;
041: import java.util.Map;
042:
043: import javax.ejb.EJBException;
044: import javax.ejb.MessageDrivenBean;
045: import javax.ejb.MessageDrivenContext;
046: import javax.jms.Message;
047: import javax.jms.MessageListener;
048:
049: import hero.util.MailNotification;
050:
051: import javax.management.MBeanServer;
052: import javax.management.MBeanServerFactory;
053: import javax.management.ObjectName;
054:
055: /**
056: * This is a Message Driven Bean
057: *
058: * @ejb:message-driven
059: * @ejb:bean name="NotificationBean"
060: * transaction-type="Container"
061: * acknowledge-mode="Auto-acknowledge"
062: * destination-type="javax.jms.Topic"
063: * subscription-durability="NonDurable"
064: *
065: * @jboss:destination-jndi-name name="topic/testTopic"
066: * @ejb.security-identity run-as="SuperAdmin"
067: * @ejb.permission unchecked="yes"
068: *
069: *
070: * @jonas.message-driven-destination jndi-name="testTopic"
071: * @jonas.bean ejb-name="NotificationBean"
072: * jndi-name="testTopic"
073: * @ejb.transaction type= "NotSupported"
074: *
075: * @jboss.container-configuration name="Standard Message Driven Bean for Bonita"
076: *
077: * @author $Author: brice $
078: * @version $Revision: 1.12 $
079: */
080:
081: public class NotificationBean implements MessageDrivenBean,
082: MessageListener {
083:
084: //private MessageDrivenContext ctx = null;
085: private transient MessageDrivenContext ctx;
086: private MailNotification mailNotif = new MailNotification();
087:
088: private void sendJabber(String toAccount, String subject,
089: String message) throws Exception {
090: ObjectName jabber = new ObjectName(
091: "bonita:type=bonitaservice,name=Jabber");
092: List list = MBeanServerFactory.findMBeanServer(null);
093: MBeanServer mServer = (MBeanServer) list.iterator().next();
094: Object id = (Object) mServer.invoke(jabber, "sendMessage",
095: new Object[] { toAccount, //JID
096: null, //Resource
097: subject, //Subject
098: message //message
099: }, new String[] { "".getClass().getName(),
100: "".getClass().getName(),
101: "".getClass().getName(),
102: "".getClass().getName() });
103: }
104:
105: public void setMessageDrivenContext(MessageDrivenContext ctx)
106: throws EJBException {
107: this .ctx = ctx;
108: }
109:
110: private Collection usersRegistered(String project, String event)
111: throws Exception {
112: try {
113: BnProjectLocalHome phome = BnProjectUtil.getLocalHome();
114: BnProjectLocal plocal = phome.findByName(project);
115: if (plocal != null)
116: return (plocal.getBnUsers());
117: else
118: return (new ArrayList());
119: } catch (Exception usersRegistered) {
120: Collection users = new ArrayList();
121: return (users);
122: }
123: }
124:
125: public void ejbCreate() {
126: }
127:
128: public void ejbRemove() {
129: // ctx=null;
130: }
131:
132: public void onMessage(Message message) {
133: /* TODO This code has been commented out because of its bad performances.
134: When rewritten, we should take into account the fact that
135: usersRegistered might return non human users such as SuperAdmin
136: and therefore ul.getUserInfos() should not be called in that case.
137: BnUserLocal mUser;
138: UserServiceLocalHome ushome;
139: UserServiceLocal ul;
140: try{
141: ushome = UserServiceUtil.getLocalHome();
142: ul=ushome.create();
143:
144: String event= (String)message.getObjectProperty(EventConstants.EVENT);
145: String projectName = (String)message.getObjectProperty(EventConstants.PROJECTNAME);
146: if((event.equalsIgnoreCase(EventConstants.START) || event.equalsIgnoreCase(EventConstants.TERMINATED))&&!event.equalsIgnoreCase(EventConstants.DELETEPROJECT)){
147: Collection c=usersRegistered(projectName,event);
148: for(Iterator i=c.iterator();i.hasNext();){
149: mUser=(BnUserLocal)i.next();
150: Map infos = ul.getUserInfos(mUser.getName());
151: Collection props=mUser.getBnProperties();
152: for(Iterator it=props.iterator();it.hasNext();){
153: BnUserPropertyLocal pValue= (BnUserPropertyLocal)it.next();
154: if((pValue.getTheKey()).equalsIgnoreCase(event+"Mail") && pValue.getTheValue()!= null){
155: if(infos.get("email")!=null)
156: mailNotif.sendMail((String)infos.get("email"),"Event Notification","BnProject:"+projectName+" Event:"+event);
157: }
158: if((pValue.getTheKey()).equalsIgnoreCase(event+"Jabber") && pValue.getTheValue()!= null ){
159: if(infos.get("jabber")!=null){
160: sendJabber((String)infos.get("jabber"),"Event Notification","Event Notification:"+projectName+" Event:"+event);
161: }
162: }
163: }
164: }
165: }
166: }catch(Exception e1){
167: ctx.setRollbackOnly();
168: e1.printStackTrace();}
169: */
170: }
171:
172: }
|