View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.log4j.Logger;
26  import org.kuali.rice.kcb.bo.MessageDelivery;
27  import org.kuali.rice.kcb.deliverer.MessageDeliverer;
28  import org.kuali.rice.kcb.deliverer.impl.AOLInstantMessageDeliverer;
29  import org.kuali.rice.kcb.deliverer.impl.EmailMessageDeliverer;
30  import org.kuali.rice.kcb.deliverer.impl.MockMessageDeliverer;
31  import org.kuali.rice.kcb.deliverer.impl.SMSMessageDeliverer;
32  import org.kuali.rice.kcb.service.MessageDelivererRegistryService;
33  
34  /**
35   * MessageDelivererRegistryService implementation - for now we use a HashMap to do this registration, in the future we'll use resource loading.
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class MessageDelivererRegistryServiceImpl implements MessageDelivererRegistryService {
39      private static Logger LOG = Logger.getLogger(MessageDelivererRegistryServiceImpl.class);
40  
41      // holds information about the registered deliverer types
42      private HashMap<String, Class<? extends MessageDeliverer>> messageDelivererTypes;
43  
44      /**
45       * Constructs an instance of the MessageDelivererRegistryServiceImpl class and sets up the 
46       * registered MessageDeliverers in the system. These are the hardcoded message deliverers 
47       * that we support out of the box.
48       * 
49       * TODO: we'll need to implement a plugin registry discovery mechanism long term.
50       */
51      public MessageDelivererRegistryServiceImpl() {
52          //KEWActionListMessageDeliverer kewActionList = new KEWActionListMessageDeliverer();
53          EmailMessageDeliverer email = new EmailMessageDeliverer();
54          SMSMessageDeliverer sms = new SMSMessageDeliverer();
55          AOLInstantMessageDeliverer aim = new AOLInstantMessageDeliverer();
56          MockMessageDeliverer mock = new MockMessageDeliverer();
57  
58          messageDelivererTypes = new HashMap<String, Class<? extends MessageDeliverer>>(4);
59          //messageDelivererTypes.put(kewActionList.getName(), kewActionList.getClass());
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       * @see org.kuali.rice.kcb.service.MessageDelivererRegistryService#getAllDelivererTypes()
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       * Implements by constructing instances of each registered class and adding to an ArrayList that
80       * gets passed back to the calling method.
81       * @see MessageDelivererRegistryService#getAllDelivererTypes()
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          // iterate over each type and add an instance of each to the returning ArrayList
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      * Implements by calling getDelivererByName for the delivery type name within the messageDelivery object.
104      * @see MessageDelivererRegistryService#getDeliverer(MessageDelivery)
105      */
106     public MessageDeliverer getDeliverer(MessageDelivery messageDelivery) {
107         MessageDeliverer nmd = getDelivererByName(messageDelivery.getDelivererTypeName());
108         if (nmd == null) {
109             LOG.error("The message deliverer type ('" + messageDelivery.getDelivererTypeName() + "') " +
110                       "associated with message delivery id='" + messageDelivery.getId() + "' was not found in the message deliverer registry.  This deliverer " +
111                       "plugin is not in the system.");
112         }
113         return nmd;
114     }
115 
116     /**
117      * Implements by doing a key lookup in the hashmap that acts as the deliverer plugin registry.  The deliverer name is the key in the hashmap for 
118      * all registered deliverers.
119      * @see MessageDelivererRegistryService#getDelivererByName(String)
120      */
121     public MessageDeliverer getDelivererByName(String messageDelivererName) {
122         Class<? extends MessageDeliverer> clazz = messageDelivererTypes.get(messageDelivererName.toLowerCase());
123 
124         if(clazz == null) {
125             LOG.error("The message deliverer type ('" + messageDelivererName + "') " +
126                       " was not found in the message deliverer registry.  This deliverer " +
127                       "plugin is not in the system.");
128             return null;
129         }
130 
131         MessageDeliverer messageDeliverer = null;
132         try {
133             messageDeliverer = clazz.newInstance();
134         } catch (InstantiationException e) {
135             LOG.error(e.getStackTrace());
136         } catch (IllegalAccessException e) {
137             LOG.error(e.getStackTrace());
138         }
139 
140         return messageDeliverer;
141     }
142 }