View Javadoc

1   /*
2    * Copyright 2004-2005 Germinus XXI
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  package com.germinus.easyconf;
17  
18  import java.io.File;
19  import java.net.URL;
20  
21  import org.apache.commons.configuration.FileConfiguration;
22  import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /***
27   * Reloads the configuration file even if a base path wasn't originally 
28   * specified. This happens for example when it has been loaded from
29   * the classpath. It works too for files that are inside JAR files.
30   *
31   */
32  public class FileConfigurationChangedReloadingStrategy extends
33          FileChangedReloadingStrategy {
34      private URL sourceURL;
35      private static final Log log = LogFactory.getLog(FileConfigurationChangedReloadingStrategy.class);
36      
37      public void setConfiguration(FileConfiguration configuration) {
38          super.setConfiguration(configuration);
39          setSourceURL(configuration.getURL());
40       }
41      
42      protected void setSourceURL(URL url) {
43          sourceURL = url;        
44      }
45      
46      protected URL getSourceURL() {
47          return sourceURL;
48      }
49  
50      /***
51       * Update the last modified time.
52       */
53      protected void updateLastModified() {
54          lastModified = getFile().lastModified();
55      }
56  
57      /***
58       * Check if the configuration has changed since the last time it was loaded.
59       */
60      protected boolean hasChanged() {
61          File file = getFile();
62          if (!file.exists()) {
63  			if (log.isDebugEnabled()) {
64  				log.debug("File does not exist: " + file);
65  			}
66              return false;
67          }
68          boolean result = (file.lastModified() > lastModified);
69  		lastChecked = System.currentTimeMillis();
70  		return result;
71          
72      }
73  
74      protected File getFile() {        
75          if ("file".equals(sourceURL.getProtocol())) {
76              return new File(sourceURL.getPath());
77          } else if ("jar".equals(sourceURL.getProtocol())) {
78              String path = sourceURL.getPath();
79              String jarFilePath = path.substring("file:".length(), path.indexOf('!'));
80              return new File(jarFilePath);
81          } else if (configuration != null) {
82              return configuration.getFile();
83          } else if ("classloader".equals(sourceURL.getProtocol())) {
84  			if (log.isDebugEnabled()) {
85  				log.debug("Reloading will not work for files loaded by the classloader: " + sourceURL);
86  			}
87  			return new File(sourceURL.getFile());
88          }
89  		log.warn("Cannot determine the filesystem file which contains the " +
90  				"configuration file for: " + sourceURL);
91  		return new File(sourceURL.getFile());
92      }
93  }