View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.log;
18  
19  import org.opensaml.util.resource.Resource;
20  import org.opensaml.util.resource.ResourceChangeListener;
21  import org.opensaml.util.resource.ResourceException;
22  import org.slf4j.LoggerFactory;
23  
24  import ch.qos.logback.classic.LoggerContext;
25  import ch.qos.logback.classic.joran.JoranConfigurator;
26  import ch.qos.logback.core.joran.spi.JoranException;
27  import ch.qos.logback.core.status.ErrorStatus;
28  import ch.qos.logback.core.status.InfoStatus;
29  import ch.qos.logback.core.status.StatusManager;
30  import ch.qos.logback.core.util.StatusPrinter;
31  
32  /** Callback that may be registered for a watch logback configuration file. */
33  public class LogbackConfigurationChangeListener implements ResourceChangeListener {
34  
35      /** {@inheritDoc} */
36      public void onResourceCreate(Resource resource) {
37          configureLogback(resource);
38      }
39  
40      /** {@inheritDoc} */
41      public void onResourceDelete(Resource resource) {
42          // do nothing
43      }
44  
45      /** {@inheritDoc} */
46      public void onResourceUpdate(Resource resource) {
47          configureLogback(resource);
48      }
49  
50      /**
51       * Configures logback using the given resource as the Joran configuration file.
52       * 
53       * @param configuration logback configuration file
54       */
55      protected void configureLogback(Resource configuration) {
56          LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
57          StatusManager statusManager = loggerContext.getStatusManager();
58          statusManager.add(new InfoStatus("Loading logging configuration file: " + configuration.getLocation(), this));
59          try {
60              //loggerContext.stop();
61              loggerContext.reset();
62              JoranConfigurator configurator = new JoranConfigurator();
63              configurator.setContext(loggerContext);
64              configurator.doConfigure(configuration.getInputStream());
65              loggerContext.start();
66          } catch (JoranException e) {
67              statusManager.add(new ErrorStatus("Error loading logging configuration file: "
68                      + configuration.getLocation(), this, e));
69          } catch (ResourceException e) {
70              statusManager.add(new ErrorStatus("Error loading logging configuration file: "
71                      + configuration.getLocation(), this, e));
72          }
73          StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); 
74      }
75  }