1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kcb.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.Map.Entry;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.log4j.Logger;
27 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
28 import org.kuali.rice.kcb.bo.MessageDelivery;
29 import org.kuali.rice.kcb.deliverer.MessageDeliverer;
30 import org.kuali.rice.kcb.deliverer.impl.AOLInstantMessageDeliverer;
31 import org.kuali.rice.kcb.deliverer.impl.EmailMessageDeliverer;
32 import org.kuali.rice.kcb.deliverer.impl.MockMessageDeliverer;
33 import org.kuali.rice.kcb.deliverer.impl.SMSMessageDeliverer;
34 import org.kuali.rice.kcb.service.MessageDelivererRegistryService;
35
36
37
38
39
40 public class MessageDelivererRegistryServiceImpl implements MessageDelivererRegistryService {
41 private static Logger LOG = Logger.getLogger(MessageDelivererRegistryServiceImpl.class);
42
43
44 private HashMap<String, Class<? extends MessageDeliverer>> messageDelivererTypes;
45
46
47
48
49
50
51
52
53 public MessageDelivererRegistryServiceImpl() {
54 EmailMessageDeliverer email = new EmailMessageDeliverer();
55 SMSMessageDeliverer sms = new SMSMessageDeliverer();
56 AOLInstantMessageDeliverer aim = new AOLInstantMessageDeliverer();
57 MockMessageDeliverer mock = new MockMessageDeliverer();
58
59 messageDelivererTypes = new HashMap<String, Class<? extends MessageDeliverer>>(4);
60 messageDelivererTypes.put(email.getName().toLowerCase(), email.getClass());
61 messageDelivererTypes.put(sms.getName().toLowerCase(), sms.getClass());
62 messageDelivererTypes.put(aim.getName().toLowerCase(), aim.getClass());
63 messageDelivererTypes.put(mock.getName().toLowerCase(), mock.getClass());
64 }
65
66
67
68
69 public Collection<String> getAllDelivererTypes() {
70 Collection<MessageDeliverer> deliverers = getAllDeliverers();
71 Set<String> types = new HashSet<String>(deliverers.size());
72 for (MessageDeliverer deliverer: deliverers) {
73 types.add(deliverer.getName().toLowerCase());
74 }
75 return types;
76 }
77
78
79
80
81
82
83 public Collection<MessageDeliverer> getAllDeliverers() {
84 ArrayList<MessageDeliverer> delivererTypes = new ArrayList<MessageDeliverer>();
85
86 Set<Entry<String, Class<? extends MessageDeliverer>>> registeredTypes = messageDelivererTypes.entrySet();
87
88
89 for(Entry<String, Class<? extends MessageDeliverer>> entry: registeredTypes ) {
90 try {
91 delivererTypes.add(entry.getValue().newInstance());
92 } catch (InstantiationException e) {
93 LOG.error(e.getStackTrace());
94 } catch (IllegalAccessException e) {
95 LOG.error(e.getStackTrace());
96 }
97 }
98
99 return delivererTypes;
100 }
101
102
103
104
105
106 public MessageDeliverer getDeliverer(MessageDelivery messageDelivery) {
107 if (messageDelivery == null) {
108 throw new RiceIllegalArgumentException("messageDelivery is null");
109 }
110
111 MessageDeliverer nmd = getDelivererByName(messageDelivery.getDelivererTypeName());
112 if (nmd == null) {
113 LOG.error("The message deliverer type ('" + messageDelivery.getDelivererTypeName() + "') " +
114 "associated with message delivery id='" + messageDelivery.getId() + "' was not found in the message deliverer registry. This deliverer " +
115 "plugin is not in the system.");
116 }
117 return nmd;
118 }
119
120
121
122
123
124
125 public MessageDeliverer getDelivererByName(String messageDelivererName) {
126 if (StringUtils.isBlank(messageDelivererName)) {
127 throw new RiceIllegalArgumentException("messageDelivererName is null or blank");
128 }
129
130 Class<? extends MessageDeliverer> clazz = messageDelivererTypes.get(messageDelivererName.toLowerCase());
131
132 if(clazz == null) {
133 LOG.error("The message deliverer type ('" + messageDelivererName + "') " +
134 " was not found in the message deliverer registry. This deliverer " +
135 "plugin is not in the system.");
136 return null;
137 }
138
139 MessageDeliverer messageDeliverer = null;
140 try {
141 messageDeliverer = clazz.newInstance();
142 } catch (InstantiationException e) {
143 LOG.error(e.getStackTrace());
144 } catch (IllegalAccessException e) {
145 LOG.error(e.getStackTrace());
146 }
147
148 return messageDeliverer;
149 }
150 }