View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.system.logging.log4j;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.io.FileNotFoundException;
24  import java.net.URL;
25  import java.net.URLConnection;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.log4j.LogManager;
30  import org.apache.log4j.PropertyConfigurator;
31  import org.apache.log4j.spi.Configurator;
32  import org.apache.log4j.spi.LoggerRepository;
33  import org.apache.log4j.xml.DOMConfigurator;
34  
35  /**
36   * Handles the details of configuring Log4j from a URL.
37   *
38   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
39   */
40  public class URLConfigurator implements Configurator {
41      private static final Log log = LogFactory.getLog(URLConfigurator.class);
42  
43      public static void configure(final URL url) {
44          new URLConfigurator().doConfigure(url, LogManager.getLoggerRepository());
45      }
46  
47      private Configurator getConfigurator(final URL url) throws FileNotFoundException {
48          String contentType = null;
49  
50          // Get the content type to see if it is XML or not
51          URLConnection connection = null;
52          try {
53              connection = url.openConnection();
54              contentType = connection.getContentType();
55              if (log.isTraceEnabled()) {
56                  log.trace("Content type: " + contentType);
57              }
58          } catch (FileNotFoundException e) {
59              throw e;
60          } catch (IOException e) {
61              log.warn("Could not determine content type from URL; ignoring", e);
62          }
63          if (contentType != null) {
64              if (contentType.toLowerCase().endsWith("/xml")) {
65                  return new DOMConfigurator();
66              }
67          }
68  
69          // Check thr file name
70          String filename = url.getFile().toLowerCase();
71          if (filename.endsWith(".xml")) {
72              return new DOMConfigurator();
73          } else if (filename.endsWith(".properties")) {
74              return new PropertyConfigurator();
75          }
76  
77          // Check for <?xml in content
78          if (connection != null) {
79              try {
80                  BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
81                  try {
82                      String head = reader.readLine();
83                      if (head.startsWith("<?xml")) {
84                          return new DOMConfigurator();
85                      }
86                  } finally {
87                      reader.close();
88                  }
89              } catch (IOException e) {
90                  log.warn("Failed to check content header; ignoring", e);
91              }
92          }
93  
94          log.warn("Unable to determine content type, using property configurator");
95          return new PropertyConfigurator();
96      }
97  
98      public void doConfigure(final URL url, final LoggerRepository repo) {
99          if (log.isDebugEnabled()) {
100             log.debug("Configuring from URL: " + url);
101         }
102 
103         // Get the config delegate and target repository to config with
104         Configurator delegate = null;
105         try {
106             delegate = getConfigurator(url);
107         } catch (FileNotFoundException e) {
108             return;
109         }
110 
111         if (log.isTraceEnabled()) {
112             log.trace("Configuring Log4j using configurator: " +
113                     delegate + ", repository: " + repo);
114         }
115 
116         // Now actually configure Log4j
117         delegate.doConfigure(url, repo);
118     }
119 }