1 16 17 package org.apache.catalina.cluster.deploy; 18 19 import java.io.File ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Iterator ; 23 24 35 36 public class WarWatcher { 37 38 39 public static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 40 .getLog(WarWatcher.class); 41 42 43 46 protected File watchDir = null; 47 48 51 protected FileChangeListener listener = null; 52 53 56 protected Map currentStatus = new HashMap (); 57 58 59 60 public WarWatcher() { 61 } 62 63 public WarWatcher(FileChangeListener listener, File watchDir) { 64 this.listener = listener; 65 this.watchDir = watchDir; 66 } 67 68 69 70 73 public void check() { 74 if (log.isInfoEnabled()) 75 log.info("check cluster wars at " + watchDir); 76 File [] list = watchDir.listFiles(new WarFilter()); 77 if (list == null) 78 list = new File [0]; 79 for (int i = 0; i < list.length; i++) { 81 addWarInfo(list[i]); 82 } 84 for (Iterator i = currentStatus.entrySet().iterator(); i.hasNext();) { 86 Map.Entry entry = (Map.Entry ) i.next(); 87 WarInfo info = (WarInfo) entry.getValue(); 88 int check = info.check(); 89 if (check == 1) { 90 listener.fileModified(info.getWar()); 91 } else if (check == -1) { 92 listener.fileRemoved(info.getWar()); 93 currentStatus.remove(info.getWar()); 95 } } 98 } 99 100 104 protected void addWarInfo(File warfile) { 105 WarInfo info = (WarInfo) currentStatus.get(warfile.getAbsolutePath()); 106 if (info == null) { 107 info = new WarInfo(warfile); 108 info.setLastState(-1); currentStatus.put(warfile.getAbsolutePath(), info); 110 } 111 } 112 113 116 public void clear() { 117 currentStatus.clear(); 118 } 119 120 123 public File getWatchDir() { 124 return watchDir; 125 } 126 127 131 public void setWatchDir(File watchDir) { 132 this.watchDir = watchDir; 133 } 134 135 138 public FileChangeListener getListener() { 139 return listener; 140 } 141 142 146 public void setListener(FileChangeListener listener) { 147 this.listener = listener; 148 } 149 150 151 152 155 protected class WarFilter implements java.io.FilenameFilter { 156 public boolean accept(File path, String name) { 157 if (name == null) 158 return false; 159 return name.endsWith(".war"); 160 } 161 } 162 163 166 protected class WarInfo { 167 protected File war = null; 168 169 protected long lastChecked = 0; 170 171 protected long lastState = 0; 172 173 public WarInfo(File war) { 174 this.war = war; 175 this.lastChecked = war.lastModified(); 176 if (!war.exists()) 177 lastState = -1; 178 } 179 180 public boolean modified() { 181 return war.exists() && war.lastModified() > lastChecked; 182 } 183 184 public boolean exists() { 185 return war.exists(); 186 } 187 188 194 public int check() { 195 int result = 0; 197 198 if (modified()) { 199 result = 1; 201 lastState = result; 202 } else if ((!exists()) && (!(lastState == -1))) { 203 result = -1; 205 lastState = result; 206 } else if ((lastState == -1) && exists()) { 207 result = 1; 209 lastState = result; 210 } 211 this.lastChecked = System.currentTimeMillis(); 212 return result; 213 } 214 215 public File getWar() { 216 return war; 217 } 218 219 public int hashCode() { 220 return war.getAbsolutePath().hashCode(); 221 } 222 223 public boolean equals(Object other) { 224 if (other instanceof WarInfo) { 225 WarInfo wo = (WarInfo) other; 226 return wo.getWar().equals(getWar()); 227 } else { 228 return false; 229 } 230 } 231 232 protected void setLastState(int lastState) { 233 this.lastState = lastState; 234 } 235 236 } 237 238 } | Popular Tags |