[prev in list] [next in list] [prev in thread] [next in thread] 

List:       log4j-dev
Subject:    PATCH: modify config file loading to allow custom URL types
From:       "Colin Sampaleanu" <colinml1 () exis ! com>
Date:       2001-06-29 14:20:32
[Download RAW message or body]

(same patch as before, but diffed using -u option...)

> As per my previous email with Ceki, this patch modifies the configuration 
> file loading code so that while still allowing a 'ref' or anchor portion 
> of a URL to specify a class name for a custom configurator, it can also 
> work with custom URL schemes such as used in WebLogic to specify a file 
> within an archive, e.g.
> zip:/part1/part2/whatever.war#WEB-INF/classes/log4j.properties  
> 
> With the new code, for the ref portion of a URL to be considered a 
> classname, it must consist of at least one package element and one 
> classname element (e.g. mypackage.Myclass), and must not have the file 
> separator character in it. Also, when the URL comes from a classloader as 
> opposed to a system properly, the code will never consider the ref to be a 
> classname, since that wouldn't make sense (e.g. in this case the ref will 
> always be part of a custom URL). 
 

Index: Category.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-log4j/src/java/org/apache/log4j/Category.java,v
retrieving revision 1.35
diff -w -u -r1.35 Category.java
 --- Category.java	2001/06/26 19:40:05	1.35
+++ Category.java	2001/06/29 14:12:44
@@ -131,12 +131,16 @@
                                                   
DEFAULT_CONFIGURATION_KEY,
						   DEFAULT_CONFIGURATION_FILE);
      URL url = null;
+      boolean urlRefMayBeConfigurator = true;
      try {
 -	// so, resource is not a URL:
 -	// attempt to get the resource from the class path
	url = new URL(resource);
      } catch (MalformedURLException ex) {
+	// if resource is not a URL then
+	// attempt to get the resource from the class path
	url = Loader.getResource(resource, Category.class);
+        // any ref portion in URL returned by classload is specific to that 
URL,
+        // not a Configurator class name
+        urlRefMayBeConfigurator = false;
      }	

      // If we have a non-null url, then delegate the rest of the
@@ -144,7 +148,7 @@
      // method.
      if(url != null) {
	LogLog.debug("Using URL ["+url+"] for automatic log4j configuration.");
 -	OptionConverter.selectAndConfigure(url, defaultHierarchy);
+	OptionConverter.selectAndConfigure(url, urlRefMayBeConfigurator, 
defaultHierarchy);
      } else {
	LogLog.debug("Could not find resource: ["+resource+"].");
      }
Index: helpers/OptionConverter.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-log4j/src/java/org/apache/log4j/helpers/OptionConver 
ter.java,v
retrieving revision 1.17
diff -w -u -r1.17 OptionConverter.java
 --- helpers/OptionConverter.java	2001/06/20 07:59:46	1.17
+++ helpers/OptionConverter.java	2001/06/29 14:12:45
@@ -399,9 +399,16 @@
  /**
     Configure log4j given a URL. 

 -     <p>The URL format is important. Its <em>reference</em> part is
 -     taken as the class name of the configurator. For example, if you
 -     invoke your application using the command line
+     <p>The URL format is important. If the 
<em>urlRefMayBeConfigurator</em>
+     param is true, then its <em>reference</em> part (if any) is 
potentially
+     considered to be the class name of the configurator. Since the ref 
part
+     may also be used by some custom URL types (e.g. to qualify a specific 
file
+     within another archive file), for the ref string to be considered a 
class
+     name, it must be a fully qualified class name including at least one
+     package, and must not contain any file separator characters such as 
'/' or
+     '\' since those would never be part of a valid class name, and are a 
strong
+     indication of a custom URL type. For example, if you invoke your
+     application using the command line 

     <pre> java 
 -Dlog4j.configuration=file:/temp/myconfig.xyz#com.myCompany.myConfigurator
     </pre>
@@ -412,8 +419,8 @@
     configurator you specify <em>must</em> implement the {@link
     Configurator} interface. 

 -     <p>If the URL has no reference part, then the {@link
 -     PropertyConfigurator} will parse the URL. However, if the URL
+     <p>If no configurator is specified via a URL reference part, then the
+     {@link PropertyConfigurator} will parse the URL. However, if the URL
     ends with a ".xml" extension, then the {@link DOMConfigurator}
     will be used to parse the URL. 

@@ -425,12 +432,29 @@
     @since 1.0 */
  static
  public
 -  void selectAndConfigure(URL url, Hierarchy hierarchy) {
 -    String clazz = url.getRef();
+  void selectAndConfigure(URL url, boolean urlRefMayBeConfigurator,
+                          Hierarchy hierarchy) {
+
+    // assume a 'ref' portion of a URL may be Configurator class
+    String clazz = null;
+    if (urlRefMayBeConfigurator) {
+      clazz = url.getRef();
+      if (clazz != null) {
+        String fileSep = "/";
+        try { fileSep = System.getProperty("file.separator"); } 
catch(Exception e) {}
+        // it's only a class name if it has a period somewhere in it, and 
it's
+        // not a class name if it has a file separator (explicitly check 
both
+        // unix and windows versions since Java itself will always allow 
them
+        // regardless of actual platform)
+        if (clazz.indexOf('.') == -1 || clazz.indexOf(fileSep) != -1 ||
+            clazz.indexOf('/') != -1 || clazz.indexOf('\\') != -1)
+          clazz = null;
+      }
+    } 

    Configurator configurator = null; 

 -    if(clazz != null) {
+    if (clazz != null && clazz.length() > 0) {
      LogLog.debug("Preferred configurator class: " + clazz);
      configurator = (Configurator) instantiateByClassName(clazz,
							   Configurator.class,
@@ -440,6 +464,11 @@
	return;
      }
    } else {
+      // hmm, this test is actually no longer valid if we have a custom URL 
type
+      // such as:
+      // 'zip:/a/b/c/d.war#e/f/log4j.properties'
+      // which is what a custom classloader like WebLogic's may return as a 
URL
+      // reference to a file inside of an archive
      String filename = url.getFile();
      if(filename != null && filename.endsWith(".xml")) {
	try {
@@ -455,4 +484,16 @@ 

    configurator.doConfigure(url, hierarchy);
  }
+
+
+  /**
+     Configure log4j given a URL.
+     This method signature maintained for compatibility
+   */
+  static
+  public
+  void selectAndConfigure(URL url, Hierarchy hierarchy) {
+    selectAndConfigure(url, true, hierarchy);
+  }
+
} 

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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic