1 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 ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 39 47 48 public class NotificationService implements Consumer { 49 private MessageService messageService; 50 private NotificationQueue queue; 51 private Thread thread; 52 53 private List notifiers; 54 55 public NotificationService() { 56 this((MessageService) Components.getComponent(MessageService.class)); 57 } 58 59 public NotificationService(MessageService service) { 60 64 queue = new NotificationQueue(); 65 notifiers = new ArrayList (); 66 notifiers.add(new JabberNotifier("leo@snipsnap.org")); 67 notifiers.add(new JabberNotifier("funzel@snipsnap.org")); 68 69 thread = new Thread () { 70 public void run() { 71 while (true) { 72 while (queue.hasItems()) { 73 String message = queue.remove(); 74 sendNotifiers(message); 75 } 76 try { 77 synchronized (this) { 78 wait(); 79 } 80 } catch (InterruptedException 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 buffer = new StringBuffer (); 94 buffer.append("new snip '"); 95 buffer.append(((Snip) messsage.getValue()).getName()); 96 buffer.append("'"); 97 notify(buffer); 98 } 99 } 101 102 public void notify(StringBuffer buffer) { 103 buffer.append(" by "); 104 buffer.append(Application.get().getUser().getLogin()); 105 notify(buffer.toString()); 106 } 107 108 public void notify(String 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 message) { 118 Iterator iterator = notifiers.iterator(); 119 while (iterator.hasNext()) { 120 Notifier notifier = (Notifier) iterator.next(); 121 notifier.notify(message); 122 } 123 } 124 } 125 | Popular Tags |