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 | 0 | 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 | 0 | public MessageDelivererRegistryServiceImpl() { |
54 | |
|
55 | 0 | EmailMessageDeliverer email = new EmailMessageDeliverer(); |
56 | 0 | SMSMessageDeliverer sms = new SMSMessageDeliverer(); |
57 | 0 | AOLInstantMessageDeliverer aim = new AOLInstantMessageDeliverer(); |
58 | 0 | MockMessageDeliverer mock = new MockMessageDeliverer(); |
59 | |
|
60 | 0 | messageDelivererTypes = new HashMap<String, Class<? extends MessageDeliverer>>(4); |
61 | |
|
62 | 0 | messageDelivererTypes.put(email.getName().toLowerCase(), email.getClass()); |
63 | 0 | messageDelivererTypes.put(sms.getName().toLowerCase(), sms.getClass()); |
64 | 0 | messageDelivererTypes.put(aim.getName().toLowerCase(), aim.getClass()); |
65 | 0 | messageDelivererTypes.put(mock.getName().toLowerCase(), mock.getClass()); |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public Collection<String> getAllDelivererTypes() { |
72 | 0 | Collection<MessageDeliverer> deliverers = getAllDeliverers(); |
73 | 0 | Set<String> types = new HashSet<String>(deliverers.size()); |
74 | 0 | for (MessageDeliverer deliverer: deliverers) { |
75 | 0 | types.add(deliverer.getName().toLowerCase()); |
76 | |
} |
77 | 0 | return types; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public Collection<MessageDeliverer> getAllDeliverers() { |
86 | 0 | ArrayList<MessageDeliverer> delivererTypes = new ArrayList<MessageDeliverer>(); |
87 | |
|
88 | 0 | Set<Entry<String, Class<? extends MessageDeliverer>>> registeredTypes = messageDelivererTypes.entrySet(); |
89 | |
|
90 | |
|
91 | 0 | for(Entry<String, Class<? extends MessageDeliverer>> entry: registeredTypes ) { |
92 | |
try { |
93 | 0 | delivererTypes.add(entry.getValue().newInstance()); |
94 | 0 | } catch (InstantiationException e) { |
95 | 0 | LOG.error(e.getStackTrace()); |
96 | 0 | } catch (IllegalAccessException e) { |
97 | 0 | LOG.error(e.getStackTrace()); |
98 | 0 | } |
99 | |
} |
100 | |
|
101 | 0 | return delivererTypes; |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public MessageDeliverer getDeliverer(MessageDelivery messageDelivery) { |
109 | 0 | if (messageDelivery == null) { |
110 | 0 | throw new RiceIllegalArgumentException("messageDelivery is null"); |
111 | |
} |
112 | |
|
113 | 0 | MessageDeliverer nmd = getDelivererByName(messageDelivery.getDelivererTypeName()); |
114 | 0 | if (nmd == null) { |
115 | 0 | LOG.error("The message deliverer type ('" + messageDelivery.getDelivererTypeName() + "') " + |
116 | |
"associated with message delivery id='" + messageDelivery.getId() + "' was not found in the message deliverer registry. This deliverer " + |
117 | |
"plugin is not in the system."); |
118 | |
} |
119 | 0 | return nmd; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public MessageDeliverer getDelivererByName(String messageDelivererName) { |
128 | 0 | if (StringUtils.isBlank(messageDelivererName)) { |
129 | 0 | throw new RiceIllegalArgumentException("messageDelivererName is null or blank"); |
130 | |
} |
131 | |
|
132 | 0 | Class<? extends MessageDeliverer> clazz = messageDelivererTypes.get(messageDelivererName.toLowerCase()); |
133 | |
|
134 | 0 | if(clazz == null) { |
135 | 0 | LOG.error("The message deliverer type ('" + messageDelivererName + "') " + |
136 | |
" was not found in the message deliverer registry. This deliverer " + |
137 | |
"plugin is not in the system."); |
138 | 0 | return null; |
139 | |
} |
140 | |
|
141 | 0 | MessageDeliverer messageDeliverer = null; |
142 | |
try { |
143 | 0 | messageDeliverer = clazz.newInstance(); |
144 | 0 | } catch (InstantiationException e) { |
145 | 0 | LOG.error(e.getStackTrace()); |
146 | 0 | } catch (IllegalAccessException e) { |
147 | 0 | LOG.error(e.getStackTrace()); |
148 | 0 | } |
149 | |
|
150 | 0 | return messageDeliverer; |
151 | |
} |
152 | |
} |