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.jivesoftware.smack.Chat;
036: import org.jivesoftware.smack.ConnectionConfiguration;
037: import org.jivesoftware.smack.XMPPConnection;
038: import org.jivesoftware.smack.XMPPException;
039:
040: import org.libresource.Libresource;
041: import org.libresource.LibresourceEvent;
042:
043: import org.libresource.core.CoreConstants;
044: import org.libresource.core.ejb.model.EventFilterValue;
045: import org.libresource.core.interfaces.LibresourceCoreService;
046:
047: import org.libresource.kernel.KernelConstants;
048: import org.libresource.kernel.interfaces.KernelService;
049:
050: import org.libresource.membership.MembershipConstants;
051: import org.libresource.membership.ejb.model.ProfileResourceValue;
052: import org.libresource.membership.interfaces.MembershipService;
053:
054: import java.net.URI;
055:
056: import javax.ejb.MessageDrivenBean;
057: import javax.ejb.MessageDrivenContext;
058:
059: import javax.jms.Message;
060: import javax.jms.MessageListener;
061:
062: import javax.mail.Session;
063:
064: /**
065: * @ejb.bean name="JabberNotificationsDispatcher" transaction-type="Container"
066: * reentrant="true" acknowledge-mode="Auto-acknowledge"
067: * destination-type="javax.jms.Topic"
068: * subscription-durability="NonDurable" message-selector="eventType
069: * NOT LIKE 'libresourceWeb%'"
070: *
071: * @jonas.bean ejb-name="JabberNotificationsDispatcher"
072: *
073: * @jonas.message-driven-destination jndi-name="libresource.events"
074: *
075: * @jboss.destination-jndi-name name="topic/libresource.events"
076: *
077: * @ejb.transaction type="NotSupported"
078: */
079: public class JabberNotificationsDispatcherBean implements
080: MessageDrivenBean, MessageListener {
081: private static final long serialVersionUID = 1L;
082: private transient static XMPPConnection connection = null;
083: private transient MessageDrivenContext mdbContext;
084: private transient LibresourceCoreService libresourceCoreService;
085: private transient MembershipService membershipService;
086: private transient Session session;
087:
088: public void onMessage(Message message) {
089: try {
090: KernelService kernelService = (KernelService) Libresource
091: .getService(KernelConstants.SERVICE);
092: LibresourceEvent libresourceEvent = LibresourceEvent
093: .toLibresourceEvent(message);
094: String host = kernelService.getUriHost();
095: String eventType = libresourceEvent.getEventType();
096: URI fromUri = libresourceEvent.getFromResource();
097: URI by = libresourceEvent.getThrowedBy();
098: String subject = "LS: http://" + host + fromUri + " - "
099: + by + " (" + eventType + ")";
100: EventFilterValue[] filters = getLibresourceCoreService()
101: .getInterestedEventFilters(eventType, fromUri,
102: libresourceEvent.getArgs());
103:
104: for (int i = 0; i < filters.length; i++) {
105: if (filters[i].getSendingType().indexOf("jabber") != -1) {
106: if (kernelService.checkSecurity(filters[i]
107: .getUser(), fromUri,
108: KernelConstants.SECURITY_READ)) {
109: ProfileResourceValue user = getMembershipService()
110: .systemGetProfile(filters[i].getUser());
111: String jabberId = user.getJabberId();
112:
113: if ((jabberId != null)
114: && (jabberId.trim().length() != 0)) {
115: //String truststore = System.getProperty("javax.net.ssl.trustStore");
116: //if (truststore != null) {
117: // System.getProperties().remove("javax.net.ssl.trustStore");
118: //}
119: // Send data
120: System.out.println("send to " + jabberId
121: + " " + subject);
122:
123: Chat chat = getConnection()
124: .getChatManager()
125: .createChat(
126: jabberId,
127: new org.jivesoftware.smack.MessageListener() {
128: public void processMessage(
129: Chat chat,
130: org.jivesoftware.smack.packet.Message message) {
131: }
132: });
133:
134: chat.sendMessage(subject);
135:
136: //if (truststore != null) {
137: // System.setProperty("javax.net.ssl.trustStore", truststore);
138: //}
139: }
140: }
141: }
142: }
143: } catch (Exception e) {
144: e.printStackTrace();
145: }
146: }
147:
148: private LibresourceCoreService getLibresourceCoreService()
149: throws Exception {
150: if (libresourceCoreService == null) {
151: libresourceCoreService = (LibresourceCoreService) Libresource
152: .getService(CoreConstants.SERVICE);
153: }
154:
155: return libresourceCoreService;
156: }
157:
158: private MembershipService getMembershipService() throws Exception {
159: if (membershipService == null) {
160: membershipService = (MembershipService) Libresource
161: .getService(MembershipConstants.SERVICE);
162: }
163:
164: return membershipService;
165: }
166:
167: private static XMPPConnection getConnection() throws XMPPException {
168: if (connection == null) {
169: ConnectionConfiguration config = new ConnectionConfiguration(
170: Libresource
171: .getLibresourceConfiguration("jabber.server"),
172: 5222);
173: config.setSASLAuthenticationEnabled(false);
174: connection = new XMPPConnection(config);
175: connection.connect();
176: connection
177: .login(
178: Libresource
179: .getLibresourceConfiguration("jabber.username"),
180: Libresource
181: .getLibresourceConfiguration("jabber.password"));
182: }
183:
184: return connection;
185: }
186:
187: // standard call back methods
188: public void setMessageDrivenContext(MessageDrivenContext ctx) {
189: mdbContext = ctx;
190: }
191:
192: public void ejbRemove() {
193: }
194:
195: public void ejbCreate() {
196: }
197: }
|