| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.configuration.reloading.FileChangedReloadingStrategy |
|
|
| 1 | /* |
|
| 2 | * Copyright 2001-2005 The Apache Software Foundation. |
|
| 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 org.apache.commons.configuration.reloading; |
|
| 18 | ||
| 19 | import org.apache.commons.configuration.FileConfiguration; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * A reloading strategy that will reload the configuration every time its |
|
| 23 | * underlying file is changed. The file is not reloaded more than once |
|
| 24 | * every 5 seconds by default, this time can be changed by setting the refresh |
|
| 25 | * delay. This strategy only works with FileConfiguration instances. |
|
| 26 | * |
|
| 27 | * @author Emmanuel Bourg |
|
| 28 | * @version $Revision: 155945 $, $Date: 2005-03-02 20:29:39 +0100 (Mi, 02 Mrz 2005) $ |
|
| 29 | * @since 1.1 |
|
| 30 | */ |
|
| 31 | 42 | public class FileChangedReloadingStrategy implements ReloadingStrategy |
| 32 | { |
|
| 33 | 9 | protected FileConfiguration configuration; |
| 34 | ||
| 35 | /** The last time the configuration file was modified. */ |
|
| 36 | protected long lastModified; |
|
| 37 | ||
| 38 | /** The last time the file was checked for changes. */ |
|
| 39 | protected long lastChecked; |
|
| 40 | ||
| 41 | /** The minimum delay in milliseconds between checks. */ |
|
| 42 | 42 | protected long refreshDelay = 5000; |
| 43 | ||
| 44 | 9 | public void setConfiguration(FileConfiguration configuration) |
| 45 | { |
|
| 46 | 28 | this.configuration = configuration; |
| 47 | 28 | } |
| 48 | 9 | |
| 49 | 9 | public void init() |
| 50 | { |
|
| 51 | 28 | updateLastModified(); |
| 52 | 28 | } |
| 53 | 9 | |
| 54 | 9 | public boolean reloadingRequired() |
| 55 | { |
|
| 56 | 84 | boolean reloading = false; |
| 57 | ||
| 58 | 120 | long now = System.currentTimeMillis(); |
| 59 | ||
| 60 | 120 | if ((now > lastChecked + refreshDelay) && hasChanged()) |
| 61 | { |
|
| 62 | 64 | lastChecked = now; |
| 63 | 28 | reloading = true; |
| 64 | 18 | } |
| 65 | 18 | |
| 66 | 84 | return reloading; |
| 67 | } |
|
| 68 | 36 | |
| 69 | public void reloadingPerformed() |
|
| 70 | { |
|
| 71 | 28 | updateLastModified(); |
| 72 | 28 | } |
| 73 | 18 | |
| 74 | 18 | /** |
| 75 | * Return the minimal time in milliseconds between two reloadings. |
|
| 76 | */ |
|
| 77 | public long getRefreshDelay() |
|
| 78 | { |
|
| 79 | 14 | return refreshDelay; |
| 80 | } |
|
| 81 | ||
| 82 | /** |
|
| 83 | * Set the minimal time between two reloadings. |
|
| 84 | * |
|
| 85 | * @param refreshDelay refresh delay in milliseconds |
|
| 86 | */ |
|
| 87 | public void setRefreshDelay(long refreshDelay) |
|
| 88 | { |
|
| 89 | 42 | this.refreshDelay = refreshDelay; |
| 90 | 42 | } |
| 91 | ||
| 92 | /** |
|
| 93 | * Update the last modified time. |
|
| 94 | */ |
|
| 95 | protected void updateLastModified() |
|
| 96 | { |
|
| 97 | 56 | lastModified = configuration.getFile().lastModified(); |
| 98 | 56 | } |
| 99 | 27 | |
| 100 | 27 | /** |
| 101 | 27 | * Check if the configuration has changed since the last time it was loaded. |
| 102 | */ |
|
| 103 | protected boolean hasChanged() |
|
| 104 | { |
|
| 105 | 56 | if (!configuration.getFile().exists()) |
| 106 | { |
|
| 107 | 14 | return false; |
| 108 | } |
|
| 109 | 18 | |
| 110 | 42 | return (configuration.getFile().lastModified() > lastModified); |
| 111 | } |
|
| 112 | ||
| 113 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |