logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Will Sargent" <will.sarg...@gmail.com>
Subject Re: JNDI in addition to sys props, part 2
Date Sat, 13 Oct 2007 23:43:50 GMT
+1

This is not a hard feature to implement, and I'd be happy to help out
if the team needs engineering resources here.

Will.

On 10/13/07, Lilianne E. Blaze <lilianne_blaze@tlen.pl> wrote:
> Hello,
> I wrote a simple patch which makes property resolving pluggable. It can
> be used, for example, to check JNDI env-entries in addition to system
> properties, or to specify the priority in which they are resolved
> (allowing or disallowing sys props to override local props, etc). Could
> you please review it? Could something like that be included in Log4j? If
> yes, what changes (the current version works, but is pretty rudimentary)
> are required?
>
> Greetings, Lilianne E. Blaze
>
> # This patch file was generated by NetBeans IDE
> # Following Index: paths are relative to: D:\Work\Java\Open_Source\custom-log4j\src
> # This patch can be applied using context Tools: Patch action on respective folder.
> # It uses platform neutral UTF-8 encoding and \n newlines.
> # Above lines and this line are ignored by the patching process.
> Index: main/java/org/apache/log4j/helpers/OptionConverter.java
> --- main/java/org/apache/log4j/helpers/OptionConverter.java Base (BASE)
> +++ main/java/org/apache/log4j/helpers/OptionConverter.java Locally Modified (Based On
LOCAL)
> @@ -25,6 +25,7 @@
>  import org.apache.log4j.PropertyConfigurator;
>
>  // Contributors:   Avy Sharell (sharell@online.fr)
> +import org.apache.log4j.sysprops.PropertyResolver;
>  //                 Matthieu Verbert (mve@zurich.ibm.com)
>  //                 Colin Sampaleanu
>
> @@ -401,12 +402,16 @@
>         } else {
>           j += DELIM_START_LEN;
>           String key = val.substring(j, k);
> +
> +          /*
>           // first try in System properties
>           String replacement = getSystemProperty(key, null);
>           // then try props parameter
>           if(replacement == null && props != null) {
>             replacement =  props.getProperty(key);
>           }
> +          */
> +          String replacement = PropertyResolver.getInstance().resolveProperty(key, props,
null);
>
>           if(replacement != null) {
>             // Do variable substitution on the replacement string
> Index: main/java/org/apache/log4j/sysprops/DefaultPropertyResolver.java
> --- main/java/org/apache/log4j/sysprops/DefaultPropertyResolver.java Locally New
> +++ main/java/org/apache/log4j/sysprops/DefaultPropertyResolver.java Locally New
> @@ -0,0 +1,43 @@
> +/*
> + *  Copyright 2007 LBlaze.
> + *
> + *  Licensed under the Apache License, Version 2.0 (the "License");
> + *  you may not use this file except in compliance with the License.
> + *  You may obtain a copy of the License at
> + *
> + *       http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing, software
> + *  distributed under the License is distributed on an "AS IS" BASIS,
> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + *  See the License for the specific language governing permissions and
> + *  limitations under the License.
> + *  under the License.
> + */
> +package org.apache.log4j.sysprops;
> +
> +import java.util.Properties;
> +import org.apache.log4j.helpers.OptionConverter;
> +
> +/**
> + * Implements default behaviour: 1st check system properties, 2nd check given
> + * properties.
> + * @author LBlaze
> + */
> +public class DefaultPropertyResolver extends PropertyResolver
> +{
> +
> +  /**
> +   *
> +   */
> +  public String resolveProperty(String key, Properties localProps, String def)
> +  {
> +    String replacement = OptionConverter.getSystemProperty(key, null);
> +    // then try props parameter
> +    if(replacement == null && localProps != null) {
> +      replacement =  localProps.getProperty(key);
> +    }
> +    return replacement;
> +  }
> +
> +}
> \ No newline at end of file
> Index: main/java/org/apache/log4j/sysprops/Jndi1stSystem2ndPropertyResolver.java
> --- main/java/org/apache/log4j/sysprops/Jndi1stSystem2ndPropertyResolver.java Locally
New
> +++ main/java/org/apache/log4j/sysprops/Jndi1stSystem2ndPropertyResolver.java Locally
New
> @@ -0,0 +1,68 @@
> +/*
> + *  Copyright 2007 LBlaze.
> + *
> + *  Licensed under the Apache License, Version 2.0 (the "License");
> + *  you may not use this file except in compliance with the License.
> + *  You may obtain a copy of the License at
> + *
> + *       http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing, software
> + *  distributed under the License is distributed on an "AS IS" BASIS,
> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + *  See the License for the specific language governing permissions and
> + *  limitations under the License.
> + *  under the License.
> + */
> +package org.apache.log4j.sysprops;
> +
> +import java.util.Properties;
> +import javax.naming.Context;
> +import javax.naming.InitialContext;
> +import org.apache.log4j.helpers.OptionConverter;
> +
> +/**
> + *
> + * @author LBlaze
> + */
> +public class Jndi1stSystem2ndPropertyResolver
> +extends PropertyResolver
> +{
> +
> +  public String resolveProperty(String key, Properties localProps, String def)
> +  {
> +    String jndiTry = resolveJndiProperty(key);
> +    if( jndiTry != null )
> +    {
> +      return jndiTry;
> +    }
> +
> +    String systemTry = OptionConverter.getSystemProperty(key, "");
> +    if( systemTry != null )
> +    {
> +      return systemTry;
> +    }
> +
> +    return localProps.getProperty(key);
> +  }
> +
> +
> +  protected String resolveJndiProperty(String key)
> +  {
> +    try
> +    {
> +      Context initCtx = new InitialContext();
> +      Context envCtx = (Context) initCtx.lookup("java:comp/env");
> +      String value = (String)envCtx.lookup("log4j/" + key);
> +      return value;
> +    }
> +    catch(Exception e)
> +    {
> +      e.printStackTrace();
> +      return null;
> +    }
> +  }
> +
> +
> +
> +}
> Index: main/java/org/apache/log4j/sysprops/PropertyResolver.java
> --- main/java/org/apache/log4j/sysprops/PropertyResolver.java Locally New
> +++ main/java/org/apache/log4j/sysprops/PropertyResolver.java Locally New
> @@ -0,0 +1,81 @@
> +/*
> + *  Copyright 2007 LBlaze.
> + *
> + *  Licensed under the Apache License, Version 2.0 (the "License");
> + *  you may not use this file except in compliance with the License.
> + *  You may obtain a copy of the License at
> + *
> + *       http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing, software
> + *  distributed under the License is distributed on an "AS IS" BASIS,
> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + *  See the License for the specific language governing permissions and
> + *  limitations under the License.
> + *  under the License.
> + */
> +package org.apache.log4j.sysprops;
> +
> +import java.util.Properties;
> +import org.apache.log4j.helpers.LogLog;
> +import org.apache.log4j.helpers.OptionConverter;
> +
> +/**
> + *
> + * @author LBlaze
> + */
> +public abstract class PropertyResolver
> +{
> +
> +  private static PropertyResolver instance = null;
> +
> +  /*static {
> +  String log4jPropertyResolver = "";
> +  try {
> +  log4jPropertyResolver = System.getProperty("log4j.propertyResolver", null);
> +  if (log4jPropertyResolver != null || !log4jPropertyResolver.equals("")) {
> +  Object o = Class.forName(log4jPropertyResolver).newInstance();
> +  instance = (PropertyResolver) o;
> +  LogLog.debug("Using PropertyResolver " + log4jPropertyResolver);
> +  } else {
> +  LogLog.debug("Using default PropertyResolver");
> +  }
> +  } catch (Exception e) {
> +  LogLog.error("Problem instantiating PropertyResolver " + log4jPropertyResolver, e);
> +  }
> +  }*/
> +  public static PropertyResolver getInstance()
> +  {
> +    if (instance == null) {
> +      instance = createInstance();
> +    }
> +    return instance;
> +  }
> +
> +  protected static PropertyResolver createInstance()
> +  {
> +    String log4jPropertyResolver = "";
> +    try {
> +      log4jPropertyResolver = System.getProperty("log4j.propertyResolver", null);
> +      if (log4jPropertyResolver != null && !log4jPropertyResolver.equals(""))
{
> +        Object o = Class.forName(log4jPropertyResolver).newInstance();
> +        if (o instanceof PropertyResolver) {
> +          LogLog.debug("Using PropertyResolver " + log4jPropertyResolver);
> +          return (PropertyResolver) o;
> +        }
> +      } else {
> +        LogLog.debug("Using default PropertyResolver");
> +      }
> +    } catch (Exception e) {
> +      LogLog.error("Problem instantiating PropertyResolver " + log4jPropertyResolver,
e);
> +    }
> +    return new DefaultPropertyResolver();
> +  }
> +
> +  public String resolveProperty(String key, Properties localProps)
> +  {
> +    return resolveProperty(key, localProps, null);
> +  }
> +
> +  public abstract String resolveProperty(String key, Properties localProps, String def);
> +}
> \ No newline at end of file
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message