001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.core.ejb;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceEvent;
037:
038: import org.libresource.core.CoreConstants;
039: import org.libresource.core.ejb.model.EventFilterValue;
040: import org.libresource.core.interfaces.LibresourceCoreService;
041: import org.libresource.core.notification.EventFormatterManager;
042:
043: import org.libresource.kernel.KernelConstants;
044: import org.libresource.kernel.interfaces.KernelService;
045:
046: import org.libresource.membership.MembershipConstants;
047: import org.libresource.membership.ejb.model.ProfileResourceValue;
048: import org.libresource.membership.interfaces.MembershipService;
049:
050: import java.net.URI;
051:
052: import javax.ejb.MessageDrivenBean;
053: import javax.ejb.MessageDrivenContext;
054:
055: import javax.jms.Message;
056: import javax.jms.MessageListener;
057:
058: import javax.mail.Session;
059: import javax.mail.Transport;
060: import javax.mail.internet.MimeMessage;
061:
062: import javax.naming.InitialContext;
063:
064: import javax.rmi.PortableRemoteObject;
065:
066: /**
067: * @ejb.bean name="MailNotificationsDispatcher" transaction-type="Container"
068: * reentrant="true" acknowledge-mode="Auto-acknowledge"
069: * destination-type="javax.jms.Topic"
070: * subscription-durability="NonDurable" message-selector="eventType
071: * NOT LIKE 'libresourceWeb%'"
072: *
073: * @jonas.bean ejb-name="MailNotificationsDispatcher"
074: *
075: * @jonas.message-driven-destination jndi-name="libresource.events"
076: *
077: * @jboss.destination-jndi-name name="topic/libresource.events"
078: *
079: * @ejb.transaction type="NotSupported"
080: */
081: public class MailNotificationsDispatcherBean implements
082: MessageDrivenBean, MessageListener {
083: private transient MessageDrivenContext mdbContext;
084: private transient KernelService kernelService;
085: private transient LibresourceCoreService libresourceCoreService;
086: private transient MembershipService membershipService;
087: private transient Session session;
088:
089: public void onMessage(Message message) {
090: try {
091: LibresourceEvent libresourceEvent = LibresourceEvent
092: .toLibresourceEvent(message);
093: String host = getKernelService().getUriHost();
094: String eventType = libresourceEvent.getEventType();
095: URI fromUri = libresourceEvent.getFromResource();
096: EventFilterValue[] filters = getLibresourceCoreService()
097: .getInterestedEventFilters(eventType, fromUri,
098: libresourceEvent.getArgs());
099:
100: for (int i = 0; i < filters.length; i++) {
101: if (filters[i].getSendingType().indexOf("mail") != -1) {
102: if (getKernelService().checkSecurity(
103: filters[i].getUser(), fromUri,
104: KernelConstants.SECURITY_READ)) {
105: String subject = EventFormatterManager
106: .getInstance().formatEventSubject(
107: libresourceEvent);
108: StringBuffer email = new StringBuffer();
109: email.append("You have received an event from "
110: + host + ".\r\n");
111: email.append("\r\n");
112: email.append(EventFormatterManager
113: .getInstance().formatEventBody(
114: libresourceEvent));
115:
116: ProfileResourceValue user = getMembershipService()
117: .systemGetProfile(filters[i].getUser());
118: getKernelService().sendEmail(user.getId(),
119: email.toString(), subject);
120: }
121: }
122: }
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: }
127:
128: private KernelService getKernelService() throws Exception {
129: if (kernelService == null) {
130: kernelService = (KernelService) Libresource
131: .getService(KernelConstants.SERVICE);
132: }
133:
134: return kernelService;
135: }
136:
137: private LibresourceCoreService getLibresourceCoreService()
138: throws Exception {
139: if (libresourceCoreService == null) {
140: libresourceCoreService = (LibresourceCoreService) Libresource
141: .getService(CoreConstants.SERVICE);
142: }
143:
144: return libresourceCoreService;
145: }
146:
147: private MembershipService getMembershipService() throws Exception {
148: if (membershipService == null) {
149: membershipService = (MembershipService) Libresource
150: .getService(MembershipConstants.SERVICE);
151: }
152:
153: return membershipService;
154: }
155:
156: // standard call back methods
157: public void setMessageDrivenContext(MessageDrivenContext ctx) {
158: mdbContext = ctx;
159: }
160:
161: public void ejbRemove() {
162: }
163:
164: public void ejbCreate() {
165: }
166: }
|