View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with 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 edu.internet2.middleware.shibboleth.common.log;
19  
20  import org.opensaml.util.resource.Resource;
21  import org.opensaml.util.resource.ResourceChangeListener;
22  import org.opensaml.util.resource.ResourceException;
23  import org.slf4j.LoggerFactory;
24  
25  import ch.qos.logback.classic.LoggerContext;
26  import ch.qos.logback.classic.joran.JoranConfigurator;
27  import ch.qos.logback.core.joran.spi.JoranException;
28  import ch.qos.logback.core.status.ErrorStatus;
29  import ch.qos.logback.core.status.InfoStatus;
30  import ch.qos.logback.core.status.StatusManager;
31  import ch.qos.logback.core.util.StatusPrinter;
32  
33  /** Callback that may be registered for a watch logback configuration file. */
34  public class LogbackConfigurationChangeListener implements ResourceChangeListener {
35  
36      /** {@inheritDoc} */
37      public void onResourceCreate(Resource resource) {
38          configureLogback(resource);
39      }
40  
41      /** {@inheritDoc} */
42      public void onResourceDelete(Resource resource) {
43          // do nothing
44      }
45  
46      /** {@inheritDoc} */
47      public void onResourceUpdate(Resource resource) {
48          configureLogback(resource);
49      }
50  
51      /**
52       * Configures logback using the given resource as the Joran configuration file.
53       * 
54       * @param configuration logback configuration file
55       */
56      protected void configureLogback(Resource configuration) {
57          LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
58          StatusManager statusManager = loggerContext.getStatusManager();
59          statusManager.add(new InfoStatus("Loading logging configuration file: " + configuration.getLocation(), this));
60          try {
61              //loggerContext.stop();
62              loggerContext.reset();
63              JoranConfigurator configurator = new JoranConfigurator();
64              configurator.setContext(loggerContext);
65              configurator.doConfigure(configuration.getInputStream());
66              loggerContext.start();
67          } catch (JoranException e) {
68              statusManager.add(new ErrorStatus("Error loading logging configuration file: "
69                      + configuration.getLocation(), this, e));
70          } catch (ResourceException e) {
71              statusManager.add(new ErrorStatus("Error loading logging configuration file: "
72                      + configuration.getLocation(), this, e));
73          }
74          StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); 
75      }
76  }