1 3 package org.jgroups.conf; 4 5 import org.w3c.dom.Element ; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 9 import org.jgroups.ChannelException; 10 import org.jgroups.JChannel; 11 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileNotFoundException ; 17 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 21 import java.util.Properties ; 22 23 35 public class ConfiguratorFactory { 36 public static final String JAXP_MISSING_ERROR_MSG= 37 "JAXP Error: the required XML parsing classes are not available; " + 38 "make sure that JAXP compatible libraries are in the classpath."; 39 40 static final String FORCE_CONFIGURATION="force.properties"; 41 42 static final Log log=LogFactory.getLog(ConfiguratorFactory.class); 43 44 static String propertiesOverride=null; 45 46 static { 50 try { 51 Properties properties = System.getProperties(); 52 propertiesOverride = properties.getProperty(FORCE_CONFIGURATION); 53 } 54 catch (SecurityException e) { 55 propertiesOverride = null; 56 } 57 58 if(propertiesOverride != null && log.isInfoEnabled()) { 59 log.info("using properties override: " + propertiesOverride); 60 } 61 } 62 63 protected ConfiguratorFactory() { 64 } 65 66 78 public static ProtocolStackConfigurator getStackConfigurator(File file) 79 throws ChannelException { 80 ProtocolStackConfigurator returnValue; 81 82 if (propertiesOverride != null) { 83 returnValue = getStackConfigurator(propertiesOverride); 84 } 85 else { 86 checkForNullConfiguration(file); 87 checkJAXPAvailability(); 88 89 try { 90 returnValue= 91 XmlConfigurator.getInstance(new FileInputStream (file)); 92 } 93 catch (IOException ioe) { 94 throw createChannelConfigurationException(ioe); 95 } 96 } 97 98 return returnValue; 99 } 100 101 113 public static ProtocolStackConfigurator getStackConfigurator(URL url) 114 throws ChannelException { 115 ProtocolStackConfigurator returnValue; 116 117 if (propertiesOverride != null) { 118 returnValue = getStackConfigurator(propertiesOverride); 119 } 120 else { 121 checkForNullConfiguration(url); 122 checkJAXPAvailability(); 123 124 try { 125 returnValue=XmlConfigurator.getInstance(url); 126 } 127 catch (IOException ioe) { 128 throw createChannelConfigurationException(ioe); 129 } 130 } 131 132 return returnValue; 133 } 134 135 147 public static ProtocolStackConfigurator getStackConfigurator(Element element) 148 throws ChannelException { 149 ProtocolStackConfigurator returnValue; 150 151 if (propertiesOverride != null) { 152 returnValue = getStackConfigurator(propertiesOverride); 153 } 154 else { 155 checkForNullConfiguration(element); 156 157 163 try { 164 returnValue=XmlConfigurator.getInstance(element); 165 } 166 catch (IOException ioe) { 167 throw createChannelConfigurationException(ioe); 168 } 169 } 170 171 return returnValue; 172 } 173 174 184 public static ProtocolStackConfigurator getStackConfigurator(String properties) throws ChannelException { 185 if (propertiesOverride != null) { 186 properties = propertiesOverride; 187 } 188 189 if(properties == null) 191 properties=JChannel.DEFAULT_PROTOCOL_STACK; 192 193 checkForNullConfiguration(properties); 194 195 ProtocolStackConfigurator returnValue; 196 197 XmlConfigurator configurator = null; 200 201 try { 202 configurator=getXmlConfigurator(properties); 203 } 204 catch (IOException ioe) { 205 throw createChannelConfigurationException(ioe); 206 } 207 208 if (configurator != null) { 210 returnValue=configurator; 211 } 212 else { 213 returnValue=new PlainConfigurator(properties); 216 } 217 218 return returnValue; 219 } 220 221 233 public static ProtocolStackConfigurator getStackConfigurator(Object properties) throws IOException { 234 InputStream input=null; 235 236 if (propertiesOverride != null) { 237 properties = propertiesOverride; 238 } 239 240 if(properties == null) 242 properties=JChannel.DEFAULT_PROTOCOL_STACK; 243 244 if(properties instanceof URL ) { 245 try { 246 input=((URL )properties).openStream(); 247 } 248 catch(Throwable t) { 249 } 250 } 251 252 if(input == null && properties instanceof String ) { 254 try { 255 input=new URL ((String )properties).openStream(); 256 } 257 catch(Exception ignore) { 258 } 260 261 if(input == null && ((String )properties).endsWith("xml")) { 263 try { 264 ClassLoader classLoader=Thread.currentThread().getContextClassLoader(); 265 input=classLoader.getResourceAsStream((String )properties); 266 } 267 catch(Throwable ignore) { 268 } 269 } 270 271 276 if(input == null) { 277 try { 278 input=new FileInputStream ((String )properties); 279 } 280 catch(Throwable t) { 281 } 282 } 283 } 284 285 if(input == null && properties instanceof File ) { 287 try { 288 input=new FileInputStream ((File )properties); 289 } 290 catch(Throwable t) { 291 } 292 } 293 294 if(input != null) { 295 return XmlConfigurator.getInstance(input); 296 } 297 298 if(properties instanceof Element ) { 299 return XmlConfigurator.getInstance((Element )properties); 300 } 301 302 return new PlainConfigurator((String )properties); 303 } 304 305 306 319 static InputStream getConfigStream(String properties) throws IOException { 320 InputStream configStream = null; 321 322 try { 324 configStream=new URL (properties).openStream(); 325 } 326 catch (MalformedURLException mre) { 327 } 329 336 if(configStream == null && properties.endsWith("xml")) { 339 ClassLoader classLoader=Thread.currentThread().getContextClassLoader(); 340 configStream=classLoader.getResourceAsStream(properties); 341 } 342 343 if (configStream == null) { 345 try { 346 configStream=new FileInputStream (properties); 347 } 348 catch(FileNotFoundException fnfe) { 349 } 351 } 352 353 return configStream; 354 } 355 356 375 static XmlConfigurator getXmlConfigurator(String properties) throws IOException { 376 XmlConfigurator returnValue=null; 377 InputStream configStream=getConfigStream(properties); 378 379 if (configStream != null) { 380 checkJAXPAvailability(); 381 382 returnValue=XmlConfigurator.getInstance(configStream); 383 } 384 385 return returnValue; 386 } 387 388 395 static ChannelException createChannelConfigurationException(Throwable cause) { 396 return new ChannelException("unable to load the protocol stack", cause); 397 } 398 399 408 static void checkForNullConfiguration(Object properties) { 409 if (properties == null) { 410 final String msg = 411 "the specifed protocol stack configuration was null."; 412 413 throw new NullPointerException (msg); 414 } 415 } 416 417 423 static void checkJAXPAvailability() { 424 try { 425 XmlConfigurator.class.getName(); 429 } 430 catch (NoClassDefFoundError error) { 431 throw new NoClassDefFoundError (JAXP_MISSING_ERROR_MSG); 432 } 433 } 434 } 435 | Popular Tags |