1 29 30 package com.caucho.vfs; 31 32 import com.caucho.util.Alarm; 33 34 import java.util.ArrayList ; 35 import java.util.logging.Logger ; 36 37 40 public class BasicDependencyContainer implements Dependency 41 { 42 private static Logger _log; 43 44 private ArrayList <Dependency> _dependencyList = new ArrayList <Dependency>(); 45 46 private boolean _isModified; 48 49 private long _checkInterval = 2000L; 51 52 private long _lastCheckTime = 0; 54 55 private volatile boolean _isChecking; 56 57 60 public BasicDependencyContainer add(Dependency dependency) 61 { 62 if (dependency == this) 63 throw new IllegalArgumentException ("Can't add self as a dependency."); 64 65 if (! _dependencyList.contains(dependency)) 66 _dependencyList.add(dependency); 67 68 71 return this; 72 } 73 74 77 public BasicDependencyContainer remove(Dependency dependency) 78 { 79 if (dependency == this) 80 throw new IllegalArgumentException ("Can't remove self as a dependency."); 81 82 _dependencyList.remove(dependency); 83 84 return this; 85 } 86 87 93 public void setCheckInterval(long checkInterval) 94 { 95 if (checkInterval < 0 || checkInterval > Long.MAX_VALUE / 2) 96 _checkInterval = Long.MAX_VALUE / 2; 97 else 98 _checkInterval = checkInterval; 99 100 _lastCheckTime = 0; 101 } 102 103 107 public long getCheckInterval() 108 { 109 return _checkInterval; 110 } 111 112 115 public void setModified(boolean isModified) 116 { 117 _isModified = isModified; 118 _lastCheckTime = 0; 119 } 120 121 124 public void resetDependencyCheckInterval() 125 { 126 _lastCheckTime = 0; 127 } 128 129 132 public void clearModified() 133 { 134 _isModified = false; 135 _lastCheckTime = Alarm.getCurrentTime(); 136 } 137 138 141 public boolean isModified() 142 { 143 synchronized (this) { 144 if (_isChecking || _isModified) { 145 return _isModified; 146 } 147 148 _isChecking = true; 149 } 150 151 try { 152 long now = Alarm.getCurrentTime(); 153 154 if (now < _lastCheckTime + _checkInterval) 155 return _isModified; 156 157 _lastCheckTime = now; 158 159 for (int i = _dependencyList.size() - 1; i >= 0; i--) { 160 Dependency dependency = _dependencyList.get(i); 161 162 if (dependency.isModified()) { 163 log().fine(dependency + " is modified"); 164 165 _isModified = true; 166 167 return _isModified; 168 } 169 } 170 171 173 return _isModified; 174 } finally { 175 _isChecking = false; 176 } 177 } 178 179 182 public boolean isModifiedNow() 183 { 184 _lastCheckTime = 0; 185 186 return isModified(); 187 } 188 189 private Logger log() 190 { 191 if (_log == null) 192 _log = Logger.getLogger(BasicDependencyContainer.class.getName()); 193 194 return _log; 195 } 196 197 public String toString() 198 { 199 return "BasicDependencyContainer" + _dependencyList; 200 } 201 } 202 | Popular Tags |