1 29 30 package com.caucho.make; 31 32 import com.caucho.loader.DynamicClassLoader; 33 import com.caucho.util.Alarm; 34 import com.caucho.util.Log; 35 import com.caucho.vfs.Dependency; 36 37 import java.util.ArrayList ; 38 import java.util.logging.Logger ; 39 40 43 public class DependencyContainer implements Dependency 44 { 45 private static Logger _log; 46 47 private ArrayList <Dependency> _dependencyList = new ArrayList <Dependency>(); 48 49 private boolean _isModified; 51 52 private long _checkInterval = 2000L; 54 55 private long _lastCheckTime = 0; 57 58 private volatile boolean _isChecking; 59 60 public DependencyContainer() 61 { 62 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 63 64 _checkInterval = DynamicClassLoader.getGlobalDependencyCheckInterval(); 65 66 for (; loader != null; loader = loader.getParent()) { 67 if (loader instanceof DynamicClassLoader) { 68 _checkInterval = ((DynamicClassLoader) loader).getDependencyCheckInterval(); 69 break; 70 } 71 } 72 } 73 74 77 public DependencyContainer add(Dependency dependency) 78 { 79 if (dependency == this) 80 throw new IllegalArgumentException ("Can't add self as a dependency."); 81 82 if (! _dependencyList.contains(dependency)) 83 _dependencyList.add(dependency); 84 85 88 return this; 89 } 90 91 94 public DependencyContainer remove(Dependency dependency) 95 { 96 if (dependency == this) 97 throw new IllegalArgumentException ("Can't remove self as a dependency."); 98 99 _dependencyList.remove(dependency); 100 101 return this; 102 } 103 104 110 public void setCheckInterval(long checkInterval) 111 { 112 if (checkInterval < 0 || checkInterval > Long.MAX_VALUE / 2) 113 _checkInterval = Long.MAX_VALUE / 2; 114 else 115 _checkInterval = checkInterval; 116 117 _lastCheckTime = 0; 118 } 119 120 124 public long getCheckInterval() 125 { 126 return _checkInterval; 127 } 128 129 132 public void setModified(boolean isModified) 133 { 134 _isModified = isModified; 135 _lastCheckTime = 0; 136 } 137 138 141 public void resetDependencyCheckInterval() 142 { 143 _lastCheckTime = 0; 144 } 145 146 149 public void clearModified() 150 { 151 _isModified = false; 152 _lastCheckTime = Alarm.getCurrentTime(); 153 } 154 155 158 public boolean isModified() 159 { 160 synchronized (this) { 161 if (_isChecking || _isModified) { 162 return _isModified; 163 } 164 165 _isChecking = true; 166 } 167 168 try { 169 long now = Alarm.getCurrentTime(); 170 171 if (now < _lastCheckTime + _checkInterval) 172 return _isModified; 173 174 _lastCheckTime = now; 175 176 for (int i = _dependencyList.size() - 1; i >= 0; i--) { 177 Dependency dependency = _dependencyList.get(i); 178 179 if (dependency.isModified()) { 180 log().fine(dependency + " is modified"); 181 182 _isModified = true; 183 184 return _isModified; 185 } 186 } 187 188 190 return _isModified; 191 } finally { 192 _isChecking = false; 193 } 194 } 195 196 199 public boolean isModifiedNow() 200 { 201 _lastCheckTime = 0; 202 203 return isModified(); 204 } 205 206 private Logger log() 207 { 208 if (_log == null) 209 _log = Log.open(DependencyContainer.class); 210 211 return _log; 212 } 213 214 public String toString() 215 { 216 return "DependencyContainer" + _dependencyList; 217 } 218 } 219 | Popular Tags |