View | Details | Raw Unified | Return to bug 43619
Collapse All | Expand All

(-)org/apache/log4j/helpers/OptionConverter.java (-6 / +3 lines)
Lines 25-30 Link Here
25
import org.apache.log4j.PropertyConfigurator;
25
import org.apache.log4j.PropertyConfigurator;
26
26
27
// Contributors:   Avy Sharell (sharell@online.fr)
27
// Contributors:   Avy Sharell (sharell@online.fr)
28
import org.apache.log4j.sysprops.PropertyResolver;
28
//                 Matthieu Verbert (mve@zurich.ibm.com)
29
//                 Matthieu Verbert (mve@zurich.ibm.com)
29
//                 Colin Sampaleanu
30
//                 Colin Sampaleanu
30
31
Lines 401-413 Link Here
401
	} else {
402
	} else {
402
	  j += DELIM_START_LEN;
403
	  j += DELIM_START_LEN;
403
	  String key = val.substring(j, k);
404
	  String key = val.substring(j, k);
404
	  // first try in System properties
405
	  String replacement = getSystemProperty(key, null);
406
	  // then try props parameter
407
	  if(replacement == null && props != null) {
408
	    replacement =  props.getProperty(key);
409
	  }
410
405
406
          String replacement = PropertyResolver.getInstance().resolveProperty(key, props);
407
411
	  if(replacement != null) {
408
	  if(replacement != null) {
412
	    // Do variable substitution on the replacement string
409
	    // Do variable substitution on the replacement string
413
	    // such that we can solve "Hello ${x2}" as "Hello p1" 
410
	    // such that we can solve "Hello ${x2}" as "Hello p1" 
(-)org/apache/log4j/sysprops/DefaultPropertyResolver.java (+38 lines)
Line 0 Link Here
1
2
package org.apache.log4j.sysprops;
3
4
import java.util.Properties;
5
import org.apache.log4j.helpers.OptionConverter;
6
7
/**
8
 * Implements default behaviour: 1st check system properties, 2nd check given
9
 * properties.
10
 * @author LBlaze
11
 */
12
public class DefaultPropertyResolver extends PropertyResolver
13
{
14
  
15
  /**
16
   * 
17
   */
18
  public String resolveProperty(String key, Properties localProps, String def)
19
  {
20
    String systemTry = OptionConverter.getSystemProperty(key, null);
21
    if( systemTry != null )
22
    {
23
      return systemTry;
24
    }
25
    
26
    if( localProps != null )
27
    {
28
      String localTry = localProps.getProperty(key);
29
      if( localTry != null )
30
      {
31
        return localTry;
32
      }
33
    }
34
    
35
    return def;
36
  }
37
  
38
}
(-)org/apache/log4j/sysprops/Jndi1stSystem2ndPropertyResolver.java (+62 lines)
Line 0 Link Here
1
2
package org.apache.log4j.sysprops;
3
4
import java.util.Properties;
5
import javax.naming.Context;
6
import javax.naming.InitialContext;
7
import org.apache.log4j.helpers.OptionConverter;
8
9
/**
10
 *
11
 * @author LBlaze
12
 */
13
public class Jndi1stSystem2ndPropertyResolver
14
extends PropertyResolver
15
{
16
17
  public String resolveProperty(String key, Properties localProps, String def)
18
  {
19
    String jndiTry = resolveJndiProperty(key);
20
    if( jndiTry != null )
21
    {
22
      return jndiTry;
23
    }
24
    
25
    String systemTry = OptionConverter.getSystemProperty(key, null);
26
    if( systemTry != null )
27
    {
28
      return systemTry;
29
    }
30
    
31
    if( localProps != null )
32
    {
33
      String localTry = localProps.getProperty(key);
34
      if( localTry != null )
35
      {
36
        return localTry;
37
      }
38
    }
39
    
40
    return def;
41
  }
42
  
43
  
44
  protected String resolveJndiProperty(String key)
45
  {
46
    try
47
    {
48
      Context initCtx = new InitialContext();
49
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
50
      String value = (String)envCtx.lookup("log4j/" + key);
51
      return value;
52
    }
53
    catch(Exception e)
54
    {
55
      e.printStackTrace();
56
      return null;
57
    }
58
  }
59
60
  
61
  
62
}
(-)org/apache/log4j/sysprops/PropertyResolver.java (+50 lines)
Line 0 Link Here
1
2
package org.apache.log4j.sysprops;
3
4
import java.util.Properties;
5
import org.apache.log4j.helpers.LogLog;
6
7
/**
8
 *
9
 * @author LBlaze
10
 */
11
public abstract class PropertyResolver
12
{
13
14
  private static PropertyResolver instance = null;
15
16
  public static PropertyResolver getInstance()
17
  {
18
    if (instance == null) {
19
      instance = createInstance();
20
    }
21
    return instance;
22
  }
23
24
  protected static PropertyResolver createInstance()
25
  {
26
    String log4jPropertyResolver = "";
27
    try {
28
      log4jPropertyResolver = System.getProperty("log4j.propertyResolver", null);
29
      if (log4jPropertyResolver != null && !log4jPropertyResolver.equals("")) {
30
        Object o = Class.forName(log4jPropertyResolver).newInstance();
31
        if (o instanceof PropertyResolver) {
32
          LogLog.debug("Using PropertyResolver " + log4jPropertyResolver);
33
          return (PropertyResolver) o;
34
        }
35
      } else {
36
        LogLog.debug("Using default PropertyResolver");
37
      }
38
    } catch (Exception e) {
39
      LogLog.error("Problem instantiating PropertyResolver " + log4jPropertyResolver, e);
40
    }
41
    return new DefaultPropertyResolver();
42
  }
43
44
  public String resolveProperty(String key, Properties localProps)
45
  {
46
    return resolveProperty(key, localProps, null);
47
  }
48
49
  public abstract String resolveProperty(String key, Properties localProps, String def);
50
}

Return to bug 43619