1 16 17 package org.apache.commons.configuration.reloading; 18 19 import org.apache.commons.configuration.FileConfiguration; 20 21 31 public class FileChangedReloadingStrategy implements ReloadingStrategy 32 { 33 protected FileConfiguration configuration; 34 35 36 protected long lastModified; 37 38 39 protected long lastChecked; 40 41 42 protected long refreshDelay = 5000; 43 44 public void setConfiguration(FileConfiguration configuration) 45 { 46 this.configuration = configuration; 47 } 48 49 public void init() 50 { 51 updateLastModified(); 52 } 53 54 public boolean reloadingRequired() 55 { 56 boolean reloading = false; 57 58 long now = System.currentTimeMillis(); 59 60 if ((now > lastChecked + refreshDelay) && hasChanged()) 61 { 62 lastChecked = now; 63 reloading = true; 64 } 65 66 return reloading; 67 } 68 69 public void reloadingPerformed() 70 { 71 updateLastModified(); 72 } 73 74 77 public long getRefreshDelay() 78 { 79 return refreshDelay; 80 } 81 82 87 public void setRefreshDelay(long refreshDelay) 88 { 89 this.refreshDelay = refreshDelay; 90 } 91 92 95 protected void updateLastModified() 96 { 97 lastModified = configuration.getFile().lastModified(); 98 } 99 100 103 protected boolean hasChanged() 104 { 105 if (!configuration.getFile().exists()) 106 { 107 return false; 108 } 109 110 return (configuration.getFile().lastModified() > lastModified); 111 } 112 113 } 114 | Popular Tags |