KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > notification > Notifier


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.notification;
21
22 import java.io.DataInputStream JavaDoc;
23 import java.io.DataOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileFilter JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.text.DateFormat JavaDoc;
30 import java.text.SimpleDateFormat JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.TreeMap JavaDoc;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 import com.sslexplorer.core.CoreAttributeConstants;
46 import com.sslexplorer.core.CoreEvent;
47 import com.sslexplorer.core.CoreEventConstants;
48 import com.sslexplorer.core.CoreServlet;
49 import com.sslexplorer.core.UserDatabaseManager;
50 import com.sslexplorer.policyframework.Policy;
51 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
52 import com.sslexplorer.policyframework.Principal;
53 import com.sslexplorer.security.LogonControllerFactory;
54 import com.sslexplorer.security.Role;
55 import com.sslexplorer.security.User;
56 import com.sslexplorer.security.UserDatabase;
57
58 public class Notifier {
59     final static Log log = LogFactory.getLog(Notifier.class);
60     private List JavaDoc<MessageWrapper> messages;
61     private boolean started;
62     private File JavaDoc queueDirectory;
63     private long messageId;
64     private MessageConsumer messageConsumer;
65     private boolean stop;
66     private List JavaDoc<MessageSink> sinks;
67     private HashMap JavaDoc<String JavaDoc,Boolean JavaDoc> sinkEnabled;
68
69     public Notifier(File JavaDoc queueDirectory) throws IOException JavaDoc {
70         messages = new ArrayList JavaDoc<MessageWrapper>();
71         this.queueDirectory = queueDirectory;
72         sinks = new ArrayList JavaDoc<MessageSink>();
73         sinkEnabled = new HashMap JavaDoc<String JavaDoc,Boolean JavaDoc>();
74         loadFromDisk();
75     }
76
77     public List JavaDoc<MessageSink> getSinks() {
78         return sinks;
79     }
80     
81     public Collection JavaDoc<MessageSink> getEnabledSinks() {
82         Collection JavaDoc<MessageSink> enabledSinks = new ArrayList JavaDoc<MessageSink>();
83         for (MessageSink sink : sinks) {
84             if(isEnabled(sink.getName())) {
85                 enabledSinks.add(sink);
86             }
87         }
88         return enabledSinks;
89     }
90
91     public void addSink(MessageSink sink, boolean enabled) {
92         if(enabled && started) {
93         
94             try {
95             sink.start(this);
96             } catch(Exception JavaDoc ex) {
97                 log.error("Failed to start " + sink.getName() + " message sink", ex);
98                 return;
99             }
100         }
101         
102         sinks.add(sink);
103         sinkEnabled.put(sink.getName(), Boolean.valueOf(enabled));
104     }
105
106     public void removeSink(MessageSink sink) {
107         sinks.remove(sink);
108         sinkEnabled.remove(sink.getName());
109     }
110
111     public boolean isEnabled(String JavaDoc sinkName) {
112         return Boolean.TRUE.equals(sinkEnabled.get(sinkName));
113     }
114
115     public void start() throws IllegalStateException JavaDoc {
116         if (started) {
117             throw new IllegalStateException JavaDoc("Already started.");
118         }
119         messageConsumer = new MessageConsumer();
120         if (sinks.size() == 0) {
121             throw new IllegalStateException JavaDoc(
122                             "At least one message sink must have been registered for the notfication queue to be started.");
123         }
124         for (Iterator JavaDoc i = sinks.iterator(); i.hasNext();) {
125             MessageSink sink = (MessageSink) i.next();
126             try {
127                 if (log.isDebugEnabled())
128                     log.debug("Starting message sink " + sink.getName());
129                 sink.start(this);
130             } catch (Exception JavaDoc e) {
131                 log.error("Failed to start sink " + sink.getName() + ".", e);
132             }
133         }
134         started = true;
135         messageConsumer.start();
136         if (log.isInfoEnabled())
137             log.info("Notifier started");
138     }
139
140     void loadFromDisk() throws IOException JavaDoc {
141         File JavaDoc[] f = queueDirectory.listFiles(new FileFilter JavaDoc() {
142             public boolean accept(File JavaDoc f) {
143                 return f.getName().endsWith(".msg");
144             }
145         });
146         // TODO better error handling in parsing of message files. Report on
147
// non-existant / unreadable directory
148
if (f == null) {
149             throw new IOException JavaDoc("Could not list queue directory " + queueDirectory.getAbsolutePath());
150         }
151         for (int i = 0; i < f.length; i++) {
152             FileInputStream JavaDoc fin = new FileInputStream JavaDoc(f[i]);
153             try {
154                 DataInputStream JavaDoc din = new DataInputStream JavaDoc(fin);
155                 long id = din.readLong();
156                 String JavaDoc sinkName = din.readUTF();
157                 messageId = Math.max(id, messageId);
158                 boolean urgent = din.readBoolean();
159                 String JavaDoc subject = din.readUTF();
160                 List JavaDoc<Recipient> recipientList = new ArrayList JavaDoc<Recipient>();
161                 while (true) {
162                     int recipientType = din.readInt();
163                     if (recipientType == Recipient.EOF) {
164                         break;
165                     } else {
166                         String JavaDoc recipientAlias = din.readUTF();
167                         String JavaDoc realmName = din.readUTF();
168                         Recipient recipient = new Recipient(recipientType, recipientAlias, realmName);
169                         recipientList.add(recipient);
170                     }
171                 }
172                 Properties JavaDoc parameters = new Properties JavaDoc();
173                 while (true) {
174                     int parameterType = din.readInt();
175                     if (parameterType < 1) {
176                         break;
177                     } else {
178                         String JavaDoc key = din.readUTF();
179                         String JavaDoc val = din.readUTF();
180                         parameters.setProperty(key, val);
181                     }
182                 }
183                 String JavaDoc content = din.readUTF();
184                 String JavaDoc lastMessage = din.readUTF();
185                 Message msg = new Message(subject, content, urgent);
186                 msg.setId(id);
187                 msg.setRecipients(recipientList);
188                 msg.setSinkName(sinkName);
189                 msg.setLastMessage(lastMessage);
190                 queue(msg);
191             } finally {
192                 fin.close();
193             }
194         }
195     }
196
197     public void stop() throws IllegalStateException JavaDoc {
198         if (!started) {
199             throw new IllegalStateException JavaDoc("Notifier is not started.");
200         }
201         stop = true;
202         queueNotify();
203         started = false;
204         if (log.isInfoEnabled())
205             log.info("Waiting for up to 120 seconds message consumer to stop");
206         try {
207             messageConsumer.join(120000);
208         } catch (InterruptedException JavaDoc ie) {
209         }
210         for (Iterator JavaDoc i = sinks.iterator(); i.hasNext();) {
211             MessageSink sink = (MessageSink) i.next();
212             try {
213                 sink.stop();
214             } catch (Exception JavaDoc e) {
215                 log.error("Failed to stop sink " + sink.getName() + ".", e);
216             }
217         }
218         if (log.isInfoEnabled())
219             log.info("Notifier stopped");
220     }
221
222     public boolean isStarted() {
223         return started;
224     }
225
226     public void clearAllMessages() {
227         synchronized (messages) {
228             try {
229                 messages.clear();
230                 File JavaDoc[] f = queueDirectory.listFiles(new FileFilter JavaDoc() {
231                     public boolean accept(File JavaDoc f) {
232                         return f.getName().endsWith(".msg");
233                     }
234                 });
235                 if (f != null) {
236                     for (int i = 0; i < f.length; i++) {
237                         f[i].delete();
238                     }
239                 }
240                 CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, CoreEventConstants.MESSAGE_QUEUE_CLEARED, null, null, CoreEvent.STATE_SUCCESSFUL));
241             } catch (Exception JavaDoc e) {
242                 log.error("Failed to clear messages from queue", e);
243                 CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, CoreEventConstants.MESSAGE_QUEUE_CLEARED, null, null, CoreEvent.STATE_UNSUCCESSFUL));
244             }
245         }
246     }
247
248     public void sendToAll(Message message) {
249         sendToSink("*", message);
250     }
251
252     public void sendToAllExcept(Message message, String JavaDoc except) {
253         sendToSink("!" + except, message);
254     }
255
256     public void sendToFirst(Message message) {
257         sendToSink("^", message);
258     }
259
260     public void sendToSink(String JavaDoc sinkName, Message message) throws IllegalArgumentException JavaDoc {
261         messageId++;
262         message.setSinkName(sinkName);
263         message.setId(messageId);
264         if (log.isDebugEnabled())
265             log.debug("Sending message " + message.getId() + " '" + message.getSubject() + "' to sink '" + sinkName + "'");
266         queue(message);
267         try {
268             write(message);
269         } catch (IOException JavaDoc ioe) {
270         }
271     }
272
273     public void setEnabled(String JavaDoc sinkName, boolean enabled) {
274         sinkEnabled.put(sinkName, Boolean.valueOf(enabled));
275         if (enabled) {
276             queueNotify();
277         }
278     }
279
280     public MessageWrapper getMessage(long messageId) {
281         for (MessageWrapper wrapper : getMessages()) {
282             if(wrapper.getMessage().getId() == messageId)
283                 return wrapper;
284         }
285         return null;
286     }
287     
288     public List JavaDoc<MessageWrapper> getMessages() {
289         return messages;
290     }
291
292     public List JavaDoc getFullRecipientListAsUsers(List JavaDoc recipients) throws Exception JavaDoc {
293         Map JavaDoc<String JavaDoc,Recipient> h = new TreeMap JavaDoc<String JavaDoc,Recipient>();
294         for (Iterator JavaDoc i = recipients.iterator(); i.hasNext();) {
295             Recipient r = (Recipient) i.next();
296             if (r.getRecipientType() == Recipient.USER) {
297                 h.put(r.getRecipientAlias(), r);
298             } else if (r.getRecipientType() == Recipient.POLICY) {
299                 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(r.getRealmName());
300                 User[] users = udb.listAllUsers("*");
301                 Policy pol = PolicyDatabaseFactory.getInstance().getPolicyByName(r.getRecipientAlias(), udb.getRealm().getResourceId());
302                 List JavaDoc principals = PolicyDatabaseFactory.getInstance().getPrincipalsGrantedPolicy(pol, udb.getRealm());
303                 for (Iterator JavaDoc j = principals.iterator(); j.hasNext();) {
304                     Principal p = (Principal) j.next();
305                     if (p instanceof Role) {
306                         for (int x = 0; x < users.length; x++) {
307                             Role[] roles = users[x].getRoles();
308                             for (int y = 0; y < roles.length; y++) {
309                                 if (roles[y].getPrincipalName().equals(p.getPrincipalName())) {
310                                     h.put(users[x].getPrincipalName(), new Recipient(Recipient.USER, users[x].getPrincipalName(), users[x].getRealm().getResourceName()));
311                                     break;
312                                 }
313                             }
314                         }
315                     } else {
316                         h.put(p.getPrincipalName(), new Recipient(Recipient.USER, p.getPrincipalName(), p.getRealm().getResourceName()));
317                     }
318                 }
319             } else if (r.getRecipientType() == Recipient.ROLE) {
320                 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(r.getRealmName());
321                 Role role = udb.getRole(r.getRecipientAlias());
322                 User[] users = udb.listAllUsers("*");
323                 for (int x = 0; x < users.length; x++) {
324                     Role[] roles = users[x].getRoles();
325                     for (int y = 0; y < roles.length; y++) {
326                         if (roles[y].getPrincipalName().equals(role.getPrincipalName())) {
327                             h.put(users[x].getPrincipalName(), new Recipient(Recipient.USER, users[x].getPrincipalName(), users[x].getRealm().getResourceName()));
328                             break;
329                         }
330                     }
331                 }
332             } else if (r.getRecipientType() == Recipient.ADMINS) {
333                 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(r.getRealmName());
334                 User[] users = udb.listAllUsers("*");
335                 for (int j = 0; j < users.length; j++) {
336                     if (LogonControllerFactory.getInstance().isAdministrator(users[j])) {
337                         h.put(users[j].getPrincipalName(), new Recipient(Recipient.USER, users[j].getPrincipalName(), users[j].getRealm().getResourceName()));
338                     }
339                 }
340             }
341         }
342         List JavaDoc<Recipient> l = new ArrayList JavaDoc<Recipient>();
343         for (Map.Entry JavaDoc<String JavaDoc, Recipient> entry : h.entrySet()) {
344             l.add(entry.getValue());
345         }
346         return l;
347     }
348
349     MessageSink getSink(String JavaDoc sinkName) {
350         MessageSink s;
351         for (Iterator JavaDoc i = sinks.iterator(); i.hasNext();) {
352             s = (MessageSink) i.next();
353             if (s.getName().equals(sinkName)) {
354                 return s;
355             }
356         }
357         return null;
358     }
359
360     void write(Message message) throws IOException JavaDoc {
361         if (log.isDebugEnabled())
362             log.debug("Writing message " + message.getId() + " '" + message.getSubject() + "' to disk");
363         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(new File JavaDoc(queueDirectory, String.valueOf(message.getId()) + ".msg"));
364         try {
365             DataOutputStream JavaDoc dout = new DataOutputStream JavaDoc(fout);
366             dout.writeLong(message.getId());
367             dout.writeUTF(message.getSinkName());
368             dout.writeBoolean(message.isUrgent());
369             dout.writeUTF(message.getSubject());
370             for (Iterator JavaDoc i = message.getRecipients().iterator(); i.hasNext();) {
371                 Recipient r = (Recipient) i.next();
372                 dout.writeInt(r.getRecipientType());
373                 dout.writeUTF(r.getRecipientAlias() == null ? "" : r.getRecipientAlias());
374                 dout.writeUTF(r.getRealmName() == null ? "" : r.getRealmName());
375             }
376             dout.writeInt(0);
377             for (Iterator JavaDoc i = message.getParameterNames(); i.hasNext();) {
378                 String JavaDoc key = (String JavaDoc) i.next();
379                 dout.writeInt(1);
380                 dout.writeUTF(key);
381                 dout.writeUTF(message.getParameter(key));
382             }
383             dout.writeInt(0);
384             dout.writeUTF(message.getContent());
385             dout.writeUTF(message.getLastMessage());
386         } finally {
387             fout.close();
388         }
389     }
390
391     void queue(Message message) {
392         if (log.isDebugEnabled())
393             log.debug("Queueing message " + message.getId() + " '" + message.getSubject() + "'");
394         MessageWrapper wrapper = new MessageWrapper(message);
395         if (message.isUrgent()) {
396             messages.add(0, wrapper);
397             fireQueuedEvent(message);
398             queueNotify();
399         } else {
400             messages.add(wrapper);
401             fireQueuedEvent(message);
402         }
403     }
404
405     void fireQueuedEvent(Message message) {
406         fireMessageEvent(message, new CoreEvent(this, CoreEventConstants.MESSAGE_QUEUED, message, null, CoreEvent.STATE_SUCCESSFUL));
407     }
408
409     void fireSentEvent(Message message, int state) {
410         fireMessageEvent(message, new CoreEvent(this, CoreEventConstants.MESSAGE_SENT, message, null, state));
411     }
412
413     void fireMessageEvent(Message message, CoreEvent evt) {
414         List JavaDoc l = message.getRecipients();
415         StringBuffer JavaDoc userR = new StringBuffer JavaDoc();
416         StringBuffer JavaDoc roleR = new StringBuffer JavaDoc();
417         StringBuffer JavaDoc policyR = new StringBuffer JavaDoc();
418         for (Iterator JavaDoc i = l.iterator(); i.hasNext();) {
419             Recipient r = (Recipient) i.next();
420             StringBuffer JavaDoc rb = userR;
421             if (r.getRecipientType() == Recipient.ROLE) {
422                 rb = roleR;
423             } else if (r.getRecipientType() == Recipient.POLICY) {
424                 rb = policyR;
425             }
426             if (rb.length() > 0) {
427                 rb.append(",");
428             }
429             rb.append(r.getRecipientAlias());
430         }
431         CoreServlet.getServlet().fireCoreEvent(
432                         evt.addAttribute(CoreAttributeConstants.EVENT_ATTR_MESSAGE_ID, String.valueOf(message.getId()))
433                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_MESSAGE_URGENT,
434                                                         String.valueOf(message.isUrgent())).addAttribute(
435                                                         CoreAttributeConstants.EVENT_ATTR_MESSAGE_SUBJECT,
436                                                         String.valueOf(message.getSubject()))
437                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_MESSAGE_ROLE_RECIPIENTS, roleR.toString())
438                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_MESSAGE_POLICY_RECIPIENTS,
439                                                         policyR.toString()));
440     }
441
442     void queueNotify() {
443         if (log.isDebugEnabled())
444             log.debug("Notify queue");
445         synchronized (messages) {
446             messages.notifyAll();
447         }
448         if (log.isDebugEnabled())
449             log.debug("Queue notified");
450     }
451
452     public boolean doSend(String JavaDoc sinkName, Message message) {
453         message.setSinkName(sinkName);
454         return doSend(message);
455     }
456
457     boolean doSend(Message message) {
458         
459         if(log.isDebugEnabled())
460             log.debug("Sending message with subject of " + message.getSubject() + " urgent = " + message.isUrgent());
461
462         if (log.isDebugEnabled()) {
463             for (Iterator JavaDoc i = message.getRecipients().iterator(); i.hasNext();) {
464                 Recipient r = (Recipient) i.next();
465                 log.debug(" " + r.getRecipientType() + "/" + r.getRecipientAlias());
466             }
467             log.debug("Content = " + message.getContent());
468             log.debug("Sink name = " + message.getSinkName());
469         }
470
471         boolean sent = false;
472         if (message.getSinkName().equals("*")) {
473             for (Iterator JavaDoc i = sinks.iterator(); i.hasNext();) {
474                 MessageSink sink = (MessageSink) i.next();
475                 if (Boolean.TRUE == sinkEnabled.get(sink.getName())) {
476                     try {
477                         if (sink.send(message)) {
478                             sent = true;
479                         }
480                     } catch (Exception JavaDoc e) {
481                         log.error("Failed to send message " + message.getId() + ".", e);
482                     }
483                 }
484             }
485         } else if (message.getSinkName().startsWith("!")) {
486             String JavaDoc[] except = message.getSinkName().substring(1).split(",");
487             for (Iterator JavaDoc i = sinks.iterator(); i.hasNext();) {
488                 MessageSink sink = (MessageSink) i.next();
489                 boolean found = false;
490                 for (int j = 0; j < except.length && !found; j++) {
491                     if (sink.getName().equals(except[j])) {
492                         found = true;
493                     }
494                 }
495                 if (!found && Boolean.TRUE == sinkEnabled.get(sink.getName())) {
496                     try {
497                         if (sink.send(message)) {
498                             sent = true;
499                         }
500                     } catch (Exception JavaDoc e) {
501                         log.error("Failed to send message " + message.getId() + ".", e);
502                     }
503                 }
504             }
505         } else if (message.getSinkName().equals("^")) {
506             for (Iterator JavaDoc i = sinks.iterator(); !sent && i.hasNext();) {
507                 MessageSink sink = (MessageSink) i.next();
508                 if (Boolean.TRUE == sinkEnabled.get(sink.getName())) {
509                     try {
510                         if (sink.send(message)) {
511                             sent = true;
512                         }
513                     } catch (Exception JavaDoc e) {
514                         log.error("Failed to send message " + message.getId() + ".", e);
515                     }
516                 }
517             }
518         } else {
519             MessageSink s = getSink(message.getSinkName());
520             if (s == null) {
521                 log.error("No message sink named " + message.getSinkName());
522             } else {
523                 if (Boolean.TRUE == sinkEnabled.get(s.getName())) {
524                     try {
525                         sent = s.send(message);
526                     } catch (Exception JavaDoc e) {
527                         message.setLastMessage(e.getMessage());
528                         log.error("Failed to send message " + message.getId() + ".", e);
529                         ;
530                     }
531                 }
532             }
533         }
534         if (!sent) {
535             log.error("No message sink sent message " + message.getId());
536             fireSentEvent(message, CoreEvent.STATE_UNSUCCESSFUL);
537         }
538         else {
539             fireSentEvent(message, CoreEvent.STATE_SUCCESSFUL);
540         }
541         return sent;
542     }
543
544     class MessageConsumer extends Thread JavaDoc {
545         MessageConsumer() {
546             super("Notification Message Consumer");
547         }
548
549         public void run() {
550             stop = false;
551             MessageWrapper msg = null;
552             int valid = -1;
553             boolean waitOnce = false;
554             while (!stop) {
555                 synchronized (messages) {
556                     while ((waitOnce || messages.size() == 0) && !stop) {
557                         try {
558                             messages.wait(30000);
559                         } catch (InterruptedException JavaDoc ie) {
560                             log.error("MessageConsumer interrupted.", ie);
561                         }
562                         waitOnce = false;
563                     }
564                     if (!stop) {
565                         if (log.isDebugEnabled())
566                             log.debug("Checking message queue");
567                         int i = 0;
568                         valid = -1;
569                         while (i < messages.size()) {
570                             msg = (MessageWrapper) messages.get(i);
571                             if (log.isDebugEnabled())
572                                 log.debug("Checking if message " + msg.getMessage().getId() + " is valid");
573                             // If the message has been attempted before, check
574
// whether its ready for retry
575
if (msg.attempt == 0 || (msg.attempt > 0 && (msg.time.getTime() + 60000) < System.currentTimeMillis())) {
576                                 valid = i;
577                                 break;
578                             } else {
579                                 i++;
580                             }
581                         }
582                         if (valid != -1) {
583                             messages.remove(valid);
584                         }
585                     }
586                 }
587                 if (!stop) {
588                     boolean sent = false;
589                     if (valid != -1) {
590                         sent = doSend(msg.message);
591                         if (!sent) {
592                             msg.attempt++;
593                             msg.time = new Date JavaDoc();
594                             try {
595                                 Thread.sleep(1);
596                             } catch (InterruptedException JavaDoc ie) {
597                             }
598                             log.error("Failed to send message. " + msg.getMessage().getLastMessage());
599                             messages.add(msg);
600                         } else {
601                             new File JavaDoc(queueDirectory, msg.message.getId() + ".msg").delete();
602                         }
603                     } else {
604                         waitOnce = true;
605                     }
606                 }
607             }
608         }
609     }
610
611     public class MessageWrapper {
612         private final Message message;
613         private Date JavaDoc time;
614         private int attempt;
615
616         private MessageWrapper(Message message) {
617             this.message = message;
618             this.time = new Date JavaDoc();
619         }
620
621         public Message getMessage() {
622             return message;
623         }
624
625         public int getAttempt() {
626             return attempt;
627         }
628
629         public Date JavaDoc getTime() {
630             return time;
631         }
632         
633         public String JavaDoc getFormattedTime() {
634             DateFormat JavaDoc sdf = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
635             return sdf.format(getTime());
636         }
637         
638         public String JavaDoc getUserRecipients() {
639             return getRecipients(Recipient.USER);
640         }
641         
642         public String JavaDoc getRoleRecipients() {
643             return getRecipients(Recipient.ROLE);
644         }
645         
646         public String JavaDoc getPolicyRecipients() {
647             return getRecipients(Recipient.POLICY);
648         }
649         
650         private String JavaDoc getRecipients(int type) {
651             List JavaDoc<String JavaDoc> recipients = new ArrayList JavaDoc<String JavaDoc>();
652             for (Recipient recipient : message.getRecipients()) {
653                 if(recipient.getRecipientType() == type) {
654                     recipients.add(recipient.getRecipientAlias());
655                 }
656             }
657             Collections.sort(recipients);
658
659             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
660             for (String JavaDoc value : recipients) {
661                 buffer.append(value).append(", ");
662             }
663             return (buffer.length() == 0 ) ? "" : buffer.substring(0, buffer.length() - 2);
664         }
665     }
666 }
Popular Tags