JAL-3048 test updated for AlignExportSettings changes
[jalview.git] / srcjar2 / org / apache / log4j / LogManager.java
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.log4j;
19
20 import org.apache.log4j.spi.LoggerRepository;
21 import org.apache.log4j.spi.LoggerFactory;
22 import org.apache.log4j.spi.RepositorySelector;
23 import org.apache.log4j.spi.DefaultRepositorySelector;
24 import org.apache.log4j.spi.RootLogger;
25 import org.apache.log4j.spi.NOPLoggerRepository;
26 import org.apache.log4j.helpers.Loader;
27 import org.apache.log4j.helpers.OptionConverter;
28 import org.apache.log4j.helpers.LogLog;
29
30 import java.net.URL;
31 import java.net.MalformedURLException;
32
33
34 import java.util.Enumeration;
35 import java.io.StringWriter;
36 import java.io.PrintWriter;
37
38 /**
39  * Gets {@link Logger} instances and operates on the current {@link LoggerRepository}.
40  * 
41  * <p>
42  * When the <code>LogManager</code> class is loaded into memory the default initialization procedure runs. The default initialization
43  * procedure</a> is described in the <a href="../../../../manual.html#defaultInit">short log4j manual</a>.
44  * </p>
45  * 
46  * @author Ceki G&uuml;lc&uuml;
47  */
48 public class LogManager {
49
50   /**
51    * @deprecated This variable is for internal use only. It will
52    * become package protected in future versions.
53    * */
54   static public final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
55   
56   static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";  
57    
58   /**
59    * @deprecated This variable is for internal use only. It will
60    * become private in future versions.
61    * */
62   static final public String DEFAULT_CONFIGURATION_KEY="log4j.configuration";
63
64   /**
65    * @deprecated This variable is for internal use only. It will
66    * become private in future versions.
67    * */
68   static final public String CONFIGURATOR_CLASS_KEY="log4j.configuratorClass";
69
70   /**
71   * @deprecated This variable is for internal use only. It will
72   * become private in future versions.
73   */
74   public static final String DEFAULT_INIT_OVERRIDE_KEY = 
75                                                  "log4j.defaultInitOverride";
76
77
78   static private Object guard = null;
79   static private RepositorySelector repositorySelector;
80
81   static {
82     // By default we use a DefaultRepositorySelector which always returns 'h'.
83     Hierarchy h = new Hierarchy(new RootLogger(Level.DEBUG));
84     repositorySelector = new DefaultRepositorySelector(h);
85
86     /** Search for the properties file log4j.properties in the CLASSPATH.  */
87     String override =OptionConverter.getSystemProperty(DEFAULT_INIT_OVERRIDE_KEY,
88                                                        null);
89
90     // if there is no default init override, then get the resource
91     // specified by the user or the default config file.
92     if(override == null || "false".equalsIgnoreCase(override)) {
93
94       String configurationOptionStr = OptionConverter.getSystemProperty(
95                                                           DEFAULT_CONFIGURATION_KEY, 
96                                                           null);
97
98       String configuratorClassName = OptionConverter.getSystemProperty(
99                                                    CONFIGURATOR_CLASS_KEY, 
100                                                    null);
101
102       URL url = null;
103
104       // if the user has not specified the log4j.configuration
105       // property, we search first for the file "log4j.xml" and then
106       // "log4j.properties"
107       if(configurationOptionStr == null) {      
108         url = Loader.getResource(DEFAULT_XML_CONFIGURATION_FILE);
109         if(url == null) {
110           url = Loader.getResource(DEFAULT_CONFIGURATION_FILE);
111         }
112       } else {
113         try {
114           url = new URL(configurationOptionStr);
115         } catch (MalformedURLException ex) {
116           // so, resource is not a URL:
117           // attempt to get the resource from the class path
118           url = Loader.getResource(configurationOptionStr); 
119         }       
120       }
121       
122       // If we have a non-null url, then delegate the rest of the
123       // configuration to the OptionConverter.selectAndConfigure
124       // method.
125       if(url != null) {
126             LogLog.debug("Using URL ["+url+"] for automatic log4j configuration.");
127         try {
128             OptionConverter.selectAndConfigure(url, configuratorClassName,
129                                            LogManager.getLoggerRepository());
130         } catch (NoClassDefFoundError e) {
131             LogLog.warn("Error during default initialization", e);
132         }
133       } else {
134             LogLog.debug("Could not find resource: ["+configurationOptionStr+"].");
135       }
136     } else {
137         LogLog.debug("Default initialization of overridden by " + 
138             DEFAULT_INIT_OVERRIDE_KEY + "property."); 
139     }  
140   } 
141
142   /**
143      Sets <code>LoggerFactory</code> but only if the correct
144      <em>guard</em> is passed as parameter.
145      
146      <p>Initally the guard is null.  If the guard is
147      <code>null</code>, then invoking this method sets the logger
148      factory and the guard. Following invocations will throw a {@link
149      IllegalArgumentException}, unless the previously set
150      <code>guard</code> is passed as the second parameter.
151
152      <p>This allows a high-level component to set the {@link
153      RepositorySelector} used by the <code>LogManager</code>.
154      
155      <p>For example, when tomcat starts it will be able to install its
156      own repository selector. However, if and when Tomcat is embedded
157      within JBoss, then JBoss will install its own repository selector
158      and Tomcat will use the repository selector set by its container,
159      JBoss.  */
160   static
161   public
162   void setRepositorySelector(RepositorySelector selector, Object guard) 
163                                                  throws IllegalArgumentException {
164     if((LogManager.guard != null) && (LogManager.guard != guard)) {
165       throw new IllegalArgumentException(
166            "Attempted to reset the LoggerFactory without possessing the guard.");
167     }
168
169     if(selector == null) {
170       throw new IllegalArgumentException("RepositorySelector must be non-null.");
171     }
172
173     LogManager.guard = guard;
174     LogManager.repositorySelector = selector;
175   }
176
177
178     /**
179      * This method tests if called from a method that
180      * is known to result in class members being abnormally
181      * set to null but is assumed to be harmless since the
182      * all classes are in the process of being unloaded.
183      *
184      * @param ex exception used to determine calling stack.
185      * @return true if calling stack is recognized as likely safe.
186      */
187   private static boolean isLikelySafeScenario(final Exception ex) {
188       StringWriter stringWriter = new StringWriter();
189       ex.printStackTrace(new PrintWriter(stringWriter));
190       String msg = stringWriter.toString();
191       return msg.indexOf("org.apache.catalina.loader.WebappClassLoader.stop") != -1;
192   }
193
194   static
195   public
196   LoggerRepository getLoggerRepository() {
197     if (repositorySelector == null) {
198         repositorySelector = new DefaultRepositorySelector(new NOPLoggerRepository());
199         guard = null;
200         Exception ex = new IllegalStateException("Class invariant violation");
201         String msg =
202                 "log4j called after unloading, see http://logging.apache.org/log4j/1.2/faq.html#unload.";
203         if (isLikelySafeScenario(ex)) {
204             LogLog.debug(msg, ex);
205         } else {
206             LogLog.error(msg, ex);
207         }
208     }
209     return repositorySelector.getLoggerRepository();
210   }
211
212   /**
213      Retrieve the appropriate root logger.
214    */
215   public
216   static 
217   Logger getRootLogger() {
218      // Delegate the actual manufacturing of the logger to the logger repository.
219     return getLoggerRepository().getRootLogger();
220   }
221
222   /**
223      Retrieve the appropriate {@link Logger} instance.  
224   */
225   public
226   static 
227   Logger getLogger(final String name) {
228      // Delegate the actual manufacturing of the logger to the logger repository.
229     return getLoggerRepository().getLogger(name);
230   }
231
232  /**
233      Retrieve the appropriate {@link Logger} instance.  
234   */
235   public
236   static 
237   Logger getLogger(final Class clazz) {
238      // Delegate the actual manufacturing of the logger to the logger repository.
239     return getLoggerRepository().getLogger(clazz.getName());
240   }
241
242
243   /**
244      Retrieve the appropriate {@link Logger} instance.  
245   */
246   public
247   static 
248   Logger getLogger(final String name, final LoggerFactory factory) {
249      // Delegate the actual manufacturing of the logger to the logger repository.
250     return getLoggerRepository().getLogger(name, factory);
251   }  
252
253   public
254   static
255   Logger exists(final String name) {
256     return getLoggerRepository().exists(name);
257   }
258
259   public
260   static
261   Enumeration getCurrentLoggers() {
262     return getLoggerRepository().getCurrentLoggers();
263   }
264
265   public
266   static
267   void shutdown() {
268     getLoggerRepository().shutdown();
269   }
270
271   public
272   static
273   void resetConfiguration() {
274     getLoggerRepository().resetConfiguration();
275   }
276 }
277