1 20 package org.apache.cactus.internal.configuration; 21 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 25 import java.util.Enumeration ; 26 import java.util.MissingResourceException ; 27 import java.util.PropertyResourceBundle ; 28 import java.util.ResourceBundle ; 29 30 import org.apache.cactus.internal.util.ClassLoaderUtils; 31 import org.apache.cactus.util.ChainedRuntimeException; 32 33 39 public class ConfigurationInitializer 40 { 41 45 private static final String DEFAULT_CONFIG_NAME = "cactus"; 46 47 52 private static final String CACTUS_CONFIG_PROPERTY = "cactus.config"; 53 54 58 private static final String CACTUS_LOGGING_CONFIG_PROPERTY = 59 "cactus.logging.config"; 60 61 64 private static boolean isInitialized; 65 66 72 public static synchronized void initialize(boolean isReinitialization) 73 { 74 if (!isInitialized) 75 { 76 initializeConfig(isReinitialization); 77 initializeLoggingConfig(isReinitialization); 78 isInitialized = true; 79 } 80 } 81 82 85 public static synchronized void initialize() 86 { 87 initialize(false); 88 } 89 90 100 private static void initializeConfig(boolean isReinitialization) 101 { 102 ResourceBundle config; 103 104 String configOverride = System.getProperty(CACTUS_CONFIG_PROPERTY); 107 108 if (configOverride == null) 109 { 110 try 113 { 114 config = ClassLoaderUtils.loadPropertyResourceBundle( 115 DEFAULT_CONFIG_NAME, ConfigurationInitializer.class); 116 } 117 catch (MissingResourceException e) 118 { 119 return; 121 } 122 } 123 else 124 { 125 try 127 { 128 config = new PropertyResourceBundle ( 129 new FileInputStream (configOverride)); 130 } 131 catch (IOException e) 132 { 133 throw new ChainedRuntimeException( 134 "Cannot read cactus configuration file [" 135 + configOverride + "]", e); 136 } 137 } 138 139 addSystemProperties(config, isReinitialization); 140 } 141 142 148 private static void initializeLoggingConfig(boolean isReinitialization) 149 { 150 String logConfig = System.getProperty(CACTUS_LOGGING_CONFIG_PROPERTY); 151 if (logConfig != null) 152 { 153 ResourceBundle bundle; 154 try 155 { 156 bundle = new PropertyResourceBundle ( 157 new FileInputStream (logConfig)); 158 } 159 catch (IOException e) 160 { 161 throw new ChainedRuntimeException("Failed to load logging " 162 + "configuration file [" + logConfig + "]"); 163 } 164 addSystemProperties(bundle, isReinitialization); 165 } 166 } 167 168 177 private static void addSystemProperties(ResourceBundle theBundle, 178 boolean isReinitialization) 179 { 180 Enumeration keys = theBundle.getKeys(); 181 182 while (keys.hasMoreElements()) 183 { 184 String key = (String ) keys.nextElement(); 185 186 if ((System.getProperty(key) == null) || isReinitialization) 191 { 192 System.setProperty(key, theBundle.getString(key)); 193 } 194 } 195 } 196 } 197 | Popular Tags |