KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > notification > NotificationService


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.notification;
27
28 import org.snipsnap.app.Application;
29 import org.snipsnap.config.Configuration;
30 import org.snipsnap.notification.jabber.JabberNotifier;
31 import org.snipsnap.snip.Snip;
32 import org.snipsnap.user.User;
33 import org.snipsnap.container.Components;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38
39 /**
40  * Notification service receives notifications, stores
41  * them in a queue and delivers them with different notifiers,
42  * e.g. JabberNotifier, IRCNotifier or MailNotitifier
43  *
44  * @author Stephan J. Schmidt
45  * @version $Id: NotificationService.java 1256 2003-12-11 13:24:57Z leo $
46  */

47
48 public class NotificationService implements Consumer {
49   private MessageService messageService;
50   private NotificationQueue queue;
51   private Thread JavaDoc thread;
52
53   private List JavaDoc notifiers;
54
55   public NotificationService() {
56     this((MessageService) Components.getComponent(MessageService.class));
57   }
58
59   public NotificationService(MessageService service) {
60 // (MessageService service) {
61
// this.messageService = service;
62
// service.register(this);
63

64     queue = new NotificationQueue();
65     notifiers = new ArrayList JavaDoc();
66     notifiers.add(new JabberNotifier("leo@snipsnap.org"));
67     notifiers.add(new JabberNotifier("funzel@snipsnap.org"));
68
69     thread = new Thread JavaDoc() {
70       public void run() {
71         while (true) {
72           while (queue.hasItems()) {
73             String JavaDoc message = queue.remove();
74             sendNotifiers(message);
75           }
76           try {
77             synchronized (this) {
78               wait();
79             }
80           } catch (InterruptedException JavaDoc e) {
81             continue;
82           }
83         }
84       }
85     };
86     thread.start();
87
88     service.register(this);
89   }
90
91   public void consume(Message messsage) {
92     if (Message.SNIP_CREATE.equals(messsage.getType())) {
93       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
94         buffer.append("new snip '");
95         buffer.append(((Snip) messsage.getValue()).getName());
96         buffer.append("'");
97       notify(buffer);
98     }
99     // do something
100
}
101
102   public void notify(StringBuffer JavaDoc buffer) {
103     buffer.append(" by ");
104     buffer.append(Application.get().getUser().getLogin());
105     notify(buffer.toString());
106   }
107
108   public void notify(String JavaDoc message) {
109     queue.add(message);
110     synchronized (thread) {
111       if (Application.get().getConfiguration().allow(Configuration.APP_PERM_NOTIFICATION)) {
112         thread.notify();
113       }
114     }
115   }
116
117   public void sendNotifiers(String JavaDoc message) {
118     Iterator JavaDoc iterator = notifiers.iterator();
119     while (iterator.hasNext()) {
120       Notifier notifier = (Notifier) iterator.next();
121       notifier.notify(message);
122     }
123   }
124 }
125
Popular Tags