1 25 package org.objectweb.carol.util.configuration; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.net.URL ; 30 import java.net.URLConnection ; 31 import java.util.Enumeration ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 import java.util.Set ; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 import org.objectweb.carol.util.mbean.MBeanUtils; 42 43 48 public class ConfigurationRepository { 49 50 53 private static Log logger = LogFactory.getLog(ConfigurationRepository.class); 54 55 58 private static Properties defaultProperties = null; 59 60 63 private static ServerConfiguration serverConfiguration = null; 64 65 68 private static Map managedProtocols = null; 69 70 73 private static Map managedConfigurations = null; 74 75 78 private static InheritableThreadLocal threadLocal = null; 79 80 84 private static ProtocolConfiguration defaultConfiguration = null; 85 86 89 private static Properties properties = null; 90 91 94 private static boolean initDone = false; 95 96 99 private ConfigurationRepository() { 100 } 101 102 105 protected static void checkInitialized() { 106 if (!initDone) { 107 try { 108 if (logger.isDebugEnabled()) { 109 logger.debug("Do the configuration as the configuration was not yet done!"); 110 } 111 init(); 112 } catch (ConfigurationException ce) { 113 IllegalStateException ise = new IllegalStateException ( 114 "Configuration of carol was not done and when trying to initialize it, it fails."); 115 ise.initCause(ce); 116 throw ise; 117 } 118 } 119 } 120 121 124 protected static void checkConfigured() { 125 checkInitialized(); 127 if (managedConfigurations == null) { 128 throw new IllegalStateException ("Cannot find a configuration, carol was not configured"); 129 } 130 } 131 132 135 public static ProtocolConfiguration[] getConfigurations() { 136 checkConfigured(); 137 Set set = managedConfigurations.keySet(); 138 ProtocolConfiguration[] configs = new ProtocolConfiguration[set.size()]; 139 int c = 0; 140 for (Iterator it = set.iterator(); it.hasNext();) { 141 String key = (String ) it.next(); 142 configs[c] = (ProtocolConfiguration) managedConfigurations.get(key); 143 c++; 144 } 145 return configs; 146 147 } 148 149 154 public static ProtocolConfiguration getConfiguration(String configName) { 155 checkConfigured(); 156 return (ProtocolConfiguration) managedConfigurations.get(configName); 157 } 158 159 164 public static Protocol getProtocol(String protocolName) { 165 checkConfigured(); 166 return (Protocol) managedProtocols.get(protocolName); 167 } 168 169 176 public static ProtocolConfiguration newConfiguration(String configurationName, String protocolName) 177 throws ConfigurationException { 178 checkConfigured(); 179 Protocol p = null; 180 181 if (managedConfigurations.get(configurationName) != null) { 183 throw new ConfigurationException("There is an existing configuration with the name '" + configurationName 184 + "'. Use another name."); 185 } 186 187 if (managedProtocols != null) { 189 p = (Protocol) managedProtocols.get(protocolName); 190 } 191 if (p == null) { 192 throw new ConfigurationException("Protocol '" + protocolName + "' doesn't exists in carol. Cannot build"); 193 } 194 195 return new ProtocolConfigurationImpl(configurationName, p, new Properties ()); 196 } 197 198 203 public static ProtocolConfiguration setCurrentConfiguration(ProtocolConfiguration config) { 204 checkConfigured(); 205 ProtocolConfiguration old = getCurrentConfiguration(); 206 threadLocal.set(config); 207 return old; 208 } 209 210 213 public static ProtocolConfiguration getCurrentConfiguration() { 214 checkConfigured(); 215 Object o = threadLocal.get(); 216 if (o != null) { 217 return (ProtocolConfiguration) o; 218 } else { 219 return defaultConfiguration; 220 } 221 } 222 223 228 public static void init(URL carolPropertiesFileURL) throws ConfigurationException { 229 init(carolPropertiesFileURL, null, null); 230 } 231 232 238 public static void init(String idMbeanServer, String serverName) throws ConfigurationException { 239 init(Thread.currentThread().getContextClassLoader().getResource(CarolDefaultValues.CAROL_CONFIGURATION_FILE), 240 idMbeanServer, serverName); 241 } 242 243 250 public static void init(URL carolPropertiesFileURL, String idMbeanServer, String serverName) 251 throws ConfigurationException { 252 if (initDone) { 253 return; 254 } 255 Properties carolDefaultProperties = getDefaultProperties(); 256 Properties carolProperties = getPropertiesFromURL(carolPropertiesFileURL); 257 258 262 properties = mergeProperties(carolDefaultProperties, carolProperties); 263 264 serverConfiguration = new ServerConfiguration(properties); 266 267 managedProtocols = new HashMap (); 269 managedConfigurations = new HashMap (); 270 271 threadLocal = new InheritableThreadLocal (); 273 274 int propertyBeginLength = CarolDefaultValues.CAROL_PREFIX.length(); 276 int propertyEndLength = CarolDefaultValues.FACTORY_PREFIX.length(); 277 for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) { 278 String key = (String ) e.nextElement(); 279 if (key.startsWith(CarolDefaultValues.CAROL_PREFIX) && key.endsWith(CarolDefaultValues.FACTORY_PREFIX)) { 282 String protocolName = key.substring(propertyBeginLength + 1, key.length() - propertyEndLength - 1); 284 if (logger.isDebugEnabled()) { 286 logger.debug("Build protocol object for protocol name found '" + protocolName + "'."); 287 } 288 Protocol protocol = new Protocol(protocolName, properties); 289 managedProtocols.put(protocolName, protocol); 290 291 if (protocolName.equals("cmi")) { 293 try { 294 org.objectweb.carol.cmi.Config.setProperties(properties); 295 } catch (NoClassDefFoundError ncdfe) { 296 if (logger.isDebugEnabled()) { 297 logger.debug("Cmi is not available, don't configure it."); 298 } 299 } catch (Exception ex) { 300 TraceCarol.error("Cannot set the cmi configuration.", ex); 301 throw new ConfigurationException("Cannot set the cmi configuration.", ex); 302 } 303 } 304 } 305 } 306 307 311 312 String protocols = properties.getProperty(CarolDefaultValues.PROTOCOLS_KEY); 314 String defaultProtocol = properties.getProperty(CarolDefaultValues.DEFAULT_PROTOCOLS_KEY); 315 if (defaultProtocol == null) { 316 throw new ConfigurationException("No default protocol defined with property '" 317 + CarolDefaultValues.DEFAULT_PROTOCOLS_KEY + "', check your carol configuration."); 318 } 319 if (protocols == null) { 320 logger.info("No protocols were defined for property '" + CarolDefaultValues.PROTOCOLS_KEY 321 + "', trying with default protocol = '" + defaultProtocol + "'."); 322 protocols = defaultProtocol; 323 } 324 325 String [] protocolsArray = protocols.split(","); 327 328 for (int p = 0; p < protocolsArray.length; p++) { 329 String pName = protocolsArray[p]; 331 332 Protocol protocol = (Protocol) managedProtocols.get(pName); 334 if (protocol == null) { 335 throw new ConfigurationException("Cannot find a protocol with name '" + pName 336 + "' in the list of available protocols."); 337 } 338 ProtocolConfiguration protoConfig = new ProtocolConfigurationImpl(pName, protocol, properties); 339 managedConfigurations.put(pName, protoConfig); 340 } 341 342 if (protocolsArray[0] != null) { 344 defaultConfiguration = (ProtocolConfiguration) managedConfigurations.get(protocolsArray[0]); 345 } 346 347 if (idMbeanServer != null && serverName != null) { 348 initMbeans(idMbeanServer, serverName); 349 } 350 351 initDone = true; 352 } 353 354 359 public static void addConfiguration(ProtocolConfiguration protocolConfiguration) throws ConfigurationException { 360 String protocolConfigName = protocolConfiguration.getName(); 361 if (managedConfigurations.get(protocolConfigName) != null) { 362 throw new ConfigurationException("The configuration named '" + protocolConfigName + "' already exist."); 363 } 364 managedConfigurations.put(protocolConfigName, protocolConfiguration); 365 } 366 367 372 public static ServerConfiguration getServerConfiguration() { 373 checkConfigured(); 374 return serverConfiguration; 375 } 376 377 383 protected static Properties mergeProperties(Properties defaultValues, Properties values) { 384 Properties p = new Properties (); 385 p.putAll(defaultValues); 386 p.putAll(values); 388 return p; 389 } 390 391 396 public static void init() throws ConfigurationException { 397 init(Thread.currentThread().getContextClassLoader().getResource(CarolDefaultValues.CAROL_CONFIGURATION_FILE)); 398 } 399 400 405 protected static Properties getDefaultProperties() throws ConfigurationException { 406 if (defaultProperties == null) { 407 URL defaultConfigurationFile = Thread.currentThread().getContextClassLoader().getResource( 409 CarolDefaultValues.CAROL_DEFAULT_CONFIGURATION_FILE); 410 411 defaultProperties = getPropertiesFromURL(defaultConfigurationFile); 412 } 413 return defaultProperties; 414 415 } 416 417 423 protected static Properties getPropertiesFromURL(URL url) throws ConfigurationException { 424 if (url == null) { 425 if (logger.isDebugEnabled()) { 426 logger.debug("Return empty properties, URL is null"); 427 } 428 return new Properties (); 429 } 430 431 InputStream is = null; 433 try { 434 URLConnection urlConnect = null; 435 urlConnect = url.openConnection(); 436 urlConnect.setDefaultUseCaches(false); 438 439 is = urlConnect.getInputStream(); 440 } catch (IOException ioe) { 441 throw new ConfigurationException("Invalid URL '" + url + "' : " + ioe.getMessage(), ioe); 442 } 443 444 if (is == null) { 446 throw new ConfigurationException("No inputstream for URL '" + url + "'."); 447 } 448 449 Properties p = new Properties (); 451 try { 452 p.load(is); 453 } catch (IOException ioe) { 454 throw new ConfigurationException("Could not load input stream of URL '" + url + "' : " + ioe.getMessage()); 455 } 456 457 try { 459 is.close(); 460 } catch (IOException ioe) { 461 throw new ConfigurationException("Cannot close inputStream", ioe); 462 } 463 464 return p; 465 } 466 467 470 public static ProtocolConfiguration getDefaultConfiguration() { 471 checkConfigured(); 472 return defaultConfiguration; 473 } 474 475 478 public static Properties getProperties() { 479 checkConfigured(); 480 return properties; 481 } 482 483 486 public static int getActiveConfigurationsNumber() { 487 checkConfigured(); 488 if (managedConfigurations != null) { 489 return managedConfigurations.size(); 490 } else { 491 return 0; 492 } 493 } 494 495 501 public static void addInterceptors(String protocolName, String interceptorInitializer) 502 throws ConfigurationException { 503 checkConfigured(); 504 Protocol protocol = getProtocol(protocolName); 505 if (protocol == null) { 506 throw new ConfigurationException("Cannot add interceptor on an unknown protocol '" + protocolName + "'."); 507 } 508 protocol.addInterceptor(interceptorInitializer); 509 } 510 511 517 protected static void initMbeans(String idMbeanServer, String serverName) throws ConfigurationException { 518 519 for (Iterator it = managedConfigurations.keySet().iterator(); it.hasNext();) { 520 String key = (String ) it.next(); 521 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) managedConfigurations.get(key); 522 if (protocolConfiguration instanceof ProtocolConfigurationImplMBean) { 523 MBeanUtils.registerProtocolConfigurationMBean((ProtocolConfigurationImplMBean) protocolConfiguration, 524 logger, idMbeanServer, serverName); 525 } 526 } 527 528 } 529 530 } 531 | Popular Tags |