@Test public void locateProperty() throws Exception { assertEquals("king", ConfigUtil.locateProperty("find.me", "king")); System.setProperty("solr.find.me", "iamfound"); assertEquals("iamfound", ConfigUtil.locateProperty("find.me", "king")); System.clearProperty("solr.find.me"); assertEquals("king", ConfigUtil.locateProperty("find.me", "king")); System.setProperty("find.me", "iamfoundagain"); assertEquals("iamfoundagain", ConfigUtil.locateProperty("find.me", "king")); System.clearProperty("find.me"); }
public static String locateProperty(String propertyName, String defaultValue) { String propertyValue = null; String propertyKey = propertyName; String jndiKey = convertPropertyNameToJNDIPath(propertyKey); String envVar = convertPropertyNameToEnvironmentParam(propertyKey); try { Context c = new InitialContext(); propertyValue = (String) c.lookup(jndiKey); log.debug("Using JNDI key: "+jndiKey+": "+propertyValue ); return propertyValue; } catch (NoInitialContextException e) { log.debug("JNDI not configured (NoInitialContextEx)"); } catch (NamingException e) { log.debug("No "+jndiKey+" in JNDI"); } catch( RuntimeException ex ) { log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage()); } propertyValue = System.getProperty(propertyKey); if( propertyValue != null ) { log.debug("Using system property "+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getProperty("solr."+propertyKey); if( propertyValue != null ) { log.debug("Using system property "+"solr."+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getenv(envVar); if( propertyValue != null ) { log.debug("Using environment variable "+envVar+": " + propertyValue ); return propertyValue; } log.debug("Using default value for variable "+propertyName+": " + defaultValue ); return defaultValue; }
ConfigUtil { public static String locateProperty(String propertyName, String defaultValue) { String propertyValue = null; String propertyKey = propertyName; String jndiKey = convertPropertyNameToJNDIPath(propertyKey); String envVar = convertPropertyNameToEnvironmentParam(propertyKey); try { Context c = new InitialContext(); propertyValue = (String) c.lookup(jndiKey); log.debug("Using JNDI key: "+jndiKey+": "+propertyValue ); return propertyValue; } catch (NoInitialContextException e) { log.debug("JNDI not configured (NoInitialContextEx)"); } catch (NamingException e) { log.debug("No "+jndiKey+" in JNDI"); } catch( RuntimeException ex ) { log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage()); } propertyValue = System.getProperty(propertyKey); if( propertyValue != null ) { log.debug("Using system property "+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getProperty("solr."+propertyKey); if( propertyValue != null ) { log.debug("Using system property "+"solr."+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getenv(envVar); if( propertyValue != null ) { log.debug("Using environment variable "+envVar+": " + propertyValue ); return propertyValue; } log.debug("Using default value for variable "+propertyName+": " + defaultValue ); return defaultValue; } }
ConfigUtil { public static String locateProperty(String propertyName, String defaultValue) { String propertyValue = null; String propertyKey = propertyName; String jndiKey = convertPropertyNameToJNDIPath(propertyKey); String envVar = convertPropertyNameToEnvironmentParam(propertyKey); try { Context c = new InitialContext(); propertyValue = (String) c.lookup(jndiKey); log.debug("Using JNDI key: "+jndiKey+": "+propertyValue ); return propertyValue; } catch (NoInitialContextException e) { log.debug("JNDI not configured (NoInitialContextEx)"); } catch (NamingException e) { log.debug("No "+jndiKey+" in JNDI"); } catch( RuntimeException ex ) { log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage()); } propertyValue = System.getProperty(propertyKey); if( propertyValue != null ) { log.debug("Using system property "+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getProperty("solr."+propertyKey); if( propertyValue != null ) { log.debug("Using system property "+"solr."+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getenv(envVar); if( propertyValue != null ) { log.debug("Using environment variable "+envVar+": " + propertyValue ); return propertyValue; } log.debug("Using default value for variable "+propertyName+": " + defaultValue ); return defaultValue; }  }
ConfigUtil { public static String locateProperty(String propertyName, String defaultValue) { String propertyValue = null; String propertyKey = propertyName; String jndiKey = convertPropertyNameToJNDIPath(propertyKey); String envVar = convertPropertyNameToEnvironmentParam(propertyKey); try { Context c = new InitialContext(); propertyValue = (String) c.lookup(jndiKey); log.debug("Using JNDI key: "+jndiKey+": "+propertyValue ); return propertyValue; } catch (NoInitialContextException e) { log.debug("JNDI not configured (NoInitialContextEx)"); } catch (NamingException e) { log.debug("No "+jndiKey+" in JNDI"); } catch( RuntimeException ex ) { log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage()); } propertyValue = System.getProperty(propertyKey); if( propertyValue != null ) { log.debug("Using system property "+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getProperty("solr."+propertyKey); if( propertyValue != null ) { log.debug("Using system property "+"solr."+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getenv(envVar); if( propertyValue != null ) { log.debug("Using environment variable "+envVar+": " + propertyValue ); return propertyValue; } log.debug("Using default value for variable "+propertyName+": " + defaultValue ); return defaultValue; }  static String locateProperty(String propertyName, String defaultValue); }
ConfigUtil { public static String locateProperty(String propertyName, String defaultValue) { String propertyValue = null; String propertyKey = propertyName; String jndiKey = convertPropertyNameToJNDIPath(propertyKey); String envVar = convertPropertyNameToEnvironmentParam(propertyKey); try { Context c = new InitialContext(); propertyValue = (String) c.lookup(jndiKey); log.debug("Using JNDI key: "+jndiKey+": "+propertyValue ); return propertyValue; } catch (NoInitialContextException e) { log.debug("JNDI not configured (NoInitialContextEx)"); } catch (NamingException e) { log.debug("No "+jndiKey+" in JNDI"); } catch( RuntimeException ex ) { log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage()); } propertyValue = System.getProperty(propertyKey); if( propertyValue != null ) { log.debug("Using system property "+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getProperty("solr."+propertyKey); if( propertyValue != null ) { log.debug("Using system property "+"solr."+propertyKey+": " + propertyValue ); return propertyValue; } propertyValue = System.getenv(envVar); if( propertyValue != null ) { log.debug("Using environment variable "+envVar+": " + propertyValue ); return propertyValue; } log.debug("Using default value for variable "+propertyName+": " + defaultValue ); return defaultValue; }  static String locateProperty(String propertyName, String defaultValue);  }