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 java.util.Timer;
20  
21  import org.opensaml.util.resource.FilesystemResource;
22  import org.opensaml.util.resource.Resource;
23  import org.opensaml.util.resource.ResourceChangeWatcher;
24  import org.opensaml.util.resource.ResourceException;
25  import org.slf4j.LoggerFactory;
26  
27  import ch.qos.logback.classic.LoggerContext;
28  import ch.qos.logback.core.status.ErrorStatus;
29  import ch.qos.logback.core.status.StatusManager;
30  
31  /**
32   * Simple logging service that watches for logback configuration file changes and reloads the file when a change occurs.
33   */
34  public class LogbackLoggingService {
35      
36      /** Timer used periodically read the logging configuration file. */
37      private Timer taskTimer;
38  
39      /**
40       * Constructor.
41       *
42       * @param loggingConfigurationFile logback configuration file
43       * @param pollingFrequency frequency the configuration file should be checked for changes
44       */
45      public LogbackLoggingService(String loggingConfigurationFile, long pollingFrequency) {        
46          LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
47          StatusManager statusManager = loggerContext.getStatusManager();
48          
49          try{
50              Resource configResource = new FilesystemResource(loggingConfigurationFile);
51              LogbackConfigurationChangeListener configChangeListener = new LogbackConfigurationChangeListener();
52              configChangeListener.onResourceCreate(configResource);
53              
54              ResourceChangeWatcher resourceWatcher = new ResourceChangeWatcher(configResource, pollingFrequency, 5);
55              resourceWatcher.getResourceListeners().add(configChangeListener);
56              
57              taskTimer = new Timer(true);
58              taskTimer.schedule(resourceWatcher, 0, pollingFrequency);
59          }catch(ResourceException e){
60              statusManager.add(new ErrorStatus("Error loading logging configuration file: "
61                      + loggingConfigurationFile, this, e));
62          }
63      }
64  }