| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.config; |
| 34 | | | |
| 35 | | | import java.util.Enumeration; |
| 36 | | | import java.util.HashSet; |
| 37 | | | import java.util.Iterator; |
| 38 | | | import java.util.MissingResourceException; |
| 39 | | | import java.util.ResourceBundle; |
| 40 | | | import java.util.Set; |
| 41 | | | import java.util.logging.Level; |
| 42 | | | import java.util.logging.Logger; |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | (1) | public final class ConfigurationCacheByPropertiesImpl |
| 49 | | | implements ConfigurationCacheInterface |
| 50 | | | { |
| 51 | | | |
| 52 | | | private static final String BUNDLE_NAME |
| 53 | | | = "org.jcoderz.commons.config.configuration"; |
| 54 | | | |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | 0 | | private static final String CLASSNAME |
| 59 | | | = ConfigurationCacheByPropertiesImpl.class.getName(); |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | 0 | | private ResourceBundle mResourceBundle = null; |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | |
| 77 | | | |
| 78 | 0 | | private Set mConfigurationKeys = null; |
| 79 | | | |
| 80 | | | |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | 0 | | private final Set mRegisteredServices = new HashSet(); |
| 87 | | | |
| 88 | | | private boolean mIsInitialized; |
| 89 | | | private Exception mInitException; |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | |
| 94 | | | @see |
| 95 | | | |
| 96 | | | private ConfigurationCacheByPropertiesImpl () |
| 97 | 0 | | { |
| 98 | | | try |
| 99 | | | { |
| 100 | 0 | | init(); |
| 101 | 0 | | mIsInitialized = true; |
| 102 | 0 | | mInitException = null; |
| 103 | | | } |
| 104 | 0 | | catch (Exception x) |
| 105 | | | { |
| 106 | 0 | | mIsInitialized = false; |
| 107 | 0 | | mInitException = x; |
| 108 | 0 | | final ConfigurationInitializationFailedException sysEvt |
| 109 | | | = new ConfigurationInitializationFailedException(x); |
| 110 | 0 | | sysEvt.log(); |
| 111 | 0 | | } |
| 112 | 0 | | } |
| 113 | | | |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | @return |
| 120 | | | |
| 121 | | | public static ConfigurationCacheByPropertiesImpl current () |
| 122 | | | { |
| 123 | 0 | | final ConfigurationCacheByPropertiesImpl result |
| 124 | | | = ConfigurationCacheHolder.INSTANCE; |
| 125 | 0 | | if (! result.mIsInitialized) |
| 126 | | | { |
| 127 | 0 | | throw new ConfigurationInitializationFailedException( |
| 128 | | | result.mInitException); |
| 129 | | | } |
| 130 | 0 | | return result; |
| 131 | | | } |
| 132 | | | |
| 133 | | | |
| 134 | | | |
| 135 | | | |
| 136 | | | |
| 137 | | | private void init () |
| 138 | | | { |
| 139 | 0 | | final String method = "init"; |
| 140 | | | |
| 141 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 142 | | | { |
| 143 | 0 | | logger.entering(CLASSNAME, method); |
| 144 | | | } |
| 145 | | | |
| 146 | 0 | | reloadCache(); |
| 147 | | | |
| 148 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 149 | | | { |
| 150 | 0 | | logger.exiting(CLASSNAME, method); |
| 151 | | | } |
| 152 | 0 | | } |
| 153 | | | |
| 154 | | | |
| 155 | | | @see |
| 156 | | | |
| 157 | | | @param |
| 158 | | | @return |
| 159 | | | @throws |
| 160 | | | |
| 161 | | | |
| 162 | | | public String getString (String key) |
| 163 | | | throws ConfigurationValueNotFoundException |
| 164 | | | { |
| 165 | | | final String result; |
| 166 | | | try |
| 167 | | | { |
| 168 | 0 | | result = mResourceBundle.getString(key); |
| 169 | 0 | | CfgLogMessage.ConfigurationValueRead.log(key, result); |
| 170 | | | } |
| 171 | 0 | | catch (MissingResourceException e) |
| 172 | | | { |
| 173 | 0 | | throw new ConfigurationValueNotFoundException(key, e); |
| 174 | 0 | | } |
| 175 | 0 | | return result; |
| 176 | | | } |
| 177 | | | |
| 178 | | | {@inheritDoc} |
| 179 | | | public Set getKeys () |
| 180 | | | { |
| 181 | 0 | | return mConfigurationKeys; |
| 182 | | | } |
| 183 | | | |
| 184 | | | |
| 185 | | | {@inheritDoc} |
| 186 | | | public void addConfigurationListener (ConfigurationListener newListener) |
| 187 | | | { |
| 188 | 0 | | mRegisteredServices.add(newListener); |
| 189 | 0 | | } |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | |
| 194 | | | |
| 195 | | | |
| 196 | | | |
| 197 | | | public void reloadCache () |
| 198 | | | { |
| 199 | 0 | | final String method = "reloadCache"; |
| 200 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 201 | | | { |
| 202 | 0 | | logger.entering(CLASSNAME, method); |
| 203 | | | } |
| 204 | | | |
| 205 | | | try |
| 206 | | | { |
| 207 | 0 | | mResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME); |
| 208 | 0 | | final Enumeration keys = mResourceBundle.getKeys(); |
| 209 | 0 | | mConfigurationKeys = new HashSet(java.util.Collections.list(keys)); |
| 210 | | | } |
| 211 | 0 | | catch (Exception e) |
| 212 | | | { |
| 213 | 0 | | final ConfigurationInitializationFailedException cife |
| 214 | | | = new ConfigurationInitializationFailedException(e); |
| 215 | | (2) | |
| 216 | 0 | | cife.addParameter("DETAIL", |
| 217 | | | "Exception while updating ConfigurationCache."); |
| 218 | 0 | | throw cife; |
| 219 | 0 | | } |
| 220 | | | |
| 221 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 222 | | | { |
| 223 | 0 | | logger.exiting(CLASSNAME, method); |
| 224 | | | } |
| 225 | 0 | | notifyServices(); |
| 226 | 0 | | } |
| 227 | | | |
| 228 | | | |
| 229 | | | |
| 230 | | | |
| 231 | | | private void notifyServices () |
| 232 | | | { |
| 233 | 0 | | final Iterator it = mRegisteredServices.iterator(); |
| 234 | 0 | | final ConfigUpdateEvent event = new ConfigUpdateEvent(this, |
| 235 | | | ConfigUpdateEvent.CACHE_UPDATED); |
| 236 | 0 | | while (it.hasNext()) |
| 237 | | | { |
| 238 | 0 | | ((ConfigurationListener) it.next()).updateConfiguration(event); |
| 239 | | | } |
| 240 | 0 | | } |
| 241 | | | |
| 242 | | | |
| 243 | | | |
| 244 | | | |
| 245 | | | |
| 246 | | | |
| 247 | 0 | (3) | private static final class ConfigurationCacheHolder |
| 248 | | | { |
| 249 | 0 | | private static final ConfigurationCacheByPropertiesImpl INSTANCE |
| 250 | | | = new ConfigurationCacheByPropertiesImpl(); |
| 251 | | | } |
| 252 | | | } |