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.wayf;
    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
    31  /** Callback that may be registered for a watch logback configuration file. */
    32  public class LogbackConfigurationChangeListener implements ResourceChangeListener {
    33
    34      /** {@inheritDoc} */
    35      public void onResourceCreate(Resource resource) {
    36          configureLogback(resource);
    37      }
    38
    39      /** {@inheritDoc} */
    40      public void onResourceDelete(Resource resource) {
    41          // do nothing
    42      }
    43
    44      /** {@inheritDoc} */
    45      public void onResourceUpdate(Resource resource) {
    46          configureLogback(resource);
    47      }
    48
    49      /**
    50       * Configures logback using the given resource as the Joran configuration file.
    51       *
    52       * @param configuration logback configuration file
    53       */
    54      protected void configureLogback(Resource configuration) {
    55          LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    56          StatusManager statusManager = loggerContext.getStatusManager();
    57          statusManager.add(new InfoStatus("Loading logging configuration file: " + configuration.getLocation(), this));
    58          try {
    59              loggerContext.reset();
    60              JoranConfigurator configurator = new JoranConfigurator();
    61              configurator.setContext(loggerContext);
    62              configurator.doConfigure(configuration.getInputStream());
    63              loggerContext.start();
    64          } catch (JoranException e) {
    65              statusManager.add(new ErrorStatus("Error loading logging configuration file: "
    66                      + configuration.getLocation(), this, e));
    67          } catch (ResourceException e) {
    68              statusManager.add(new ErrorStatus("Error loading logging configuration file: "
    69                      + configuration.getLocation(), this, e));
    70          }
    71      }
    72  }