KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > logging > log4j > URLConfigurator


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
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 JavaDoc url) {
44         new URLConfigurator().doConfigure(url, LogManager.getLoggerRepository());
45     }
46
47     private Configurator getConfigurator(final URL JavaDoc url) throws FileNotFoundException JavaDoc {
48         String JavaDoc contentType = null;
49
50         // Get the content type to see if it is XML or not
51
URLConnection JavaDoc 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 JavaDoc e) {
59             throw e;
60         } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(connection.getInputStream()));
81                 try {
82                     String JavaDoc head = reader.readLine();
83                     if (head.startsWith("<?xml")) {
84                         return new DOMConfigurator();
85                     }
86                 } finally {
87                     reader.close();
88                 }
89             } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc 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 }
120
Popular Tags