View Javadoc

1   /*
2    * Copyright 2007-2009 Hippo
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, 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 nl.hippo.client.event.service;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import nl.hippo.client.event.EventLogger;
25  
26  import org.apache.commons.lang.StringUtils;
27  
28  public class UpdateNotificationConfig {
29  
30      public static final String JMS_CONNECTION_FACTORY = "hippo.client.jms.factory";
31      public static final String JMS_TOPIC = "hippo.client.jms.topic";
32      public static final String JMS_USERNAME = "hippo.client.jms.username";
33      public static final String JMS_PASSWORD = "hippo.client.jms.password";
34      public static final String JMS_RECONNECT_DELAY = "hippo.client.jms.reconnect.delay";
35  
36      public static final String JNDI_INITIAL_CONTEXT_FACTORY = "java.naming.factory.initial";
37      public static final String JNDI_PROVIDER_URL = "java.naming.provider.url";
38      
39      public static final String JMS_LOGLABEL = "hippo.client.loglabel";
40  
41      private Properties parameters;
42  
43      public UpdateNotificationConfig(Properties parameters) {
44          if (!sanityCheck(parameters)) {
45              throw new IllegalArgumentException("Incorrect parameters for UpdateNotificationConfig");
46          }
47          this.parameters = parameters;
48      }
49  
50      public UpdateNotificationConfig(InputStream stream) {
51          parameters = new Properties();
52          try {
53              parameters.load(stream);
54          } catch (IOException e) {
55              throw new IllegalArgumentException("Failed to load configuration parameters from InputStream: "
56                      + e.getMessage());
57          }
58          if (!sanityCheck(parameters)) {
59              throw new IllegalArgumentException("Incorrect parameters for UpdateNotificationConfig");
60          }
61      }
62  
63      public String getConnectionFactory() {
64          return parameters.getProperty(JMS_CONNECTION_FACTORY);
65      }
66  
67      public String getTopic() {
68          return parameters.getProperty(JMS_TOPIC);
69      }
70  
71      public String getUserName() {
72          return parameters.getProperty(JMS_USERNAME);
73      }
74  
75      public String getPassword() {
76          return parameters.getProperty(JMS_PASSWORD);
77      }
78  
79      public long getReconnectDelay() {
80          return Long.valueOf(parameters.getProperty(JMS_RECONNECT_DELAY)).longValue();
81      }
82      
83      public String getLoglabel() {
84          return parameters.getProperty(JMS_LOGLABEL, "");
85      }
86  
87      public Properties getJndiProperties() {
88          Properties result = new Properties();
89          Iterator it = parameters.entrySet().iterator();
90          while (it.hasNext()) {
91              Map.Entry entry = (Map.Entry) it.next();
92              String key = (String) entry.getKey();
93              result.put(key, entry.getValue());
94          }
95          return result;
96      }
97  
98      private boolean sanityCheck(Properties parameters) {
99          boolean allSet = true;
100         if (parameters.getProperty(JMS_CONNECTION_FACTORY) == null) {
101             EventLogger.log.error("Mandatory parameter " + JMS_CONNECTION_FACTORY + " is missing");
102             allSet = false;
103         }
104         if (parameters.getProperty(JMS_TOPIC) == null) {
105             EventLogger.log.error("Mandatory parameter " + JMS_TOPIC + " is missing");
106             allSet = false;
107         }
108         if (parameters.getProperty(JMS_RECONNECT_DELAY) == null) {
109             EventLogger.log.error("Mandatory parameter " + JMS_RECONNECT_DELAY + " is missing");
110             allSet = false;
111         } else {
112             try {
113                 String reconnectDelay = StringUtils.trim(parameters.getProperty(JMS_RECONNECT_DELAY));
114                 Long.valueOf(reconnectDelay);
115                 parameters.put(JMS_RECONNECT_DELAY, reconnectDelay);
116             } catch (NumberFormatException e) {
117                 EventLogger.log.error("Parameter " + JMS_RECONNECT_DELAY + " should be a number, but is: "
118                         + parameters.getProperty(JMS_RECONNECT_DELAY));
119                 allSet = false;
120             }
121         }
122         return allSet;
123     }
124 
125 }