@Override public long lastmodified() { return delegate.lastmodified(); }
@Override public long lastModified() throws IOException { return resource.lastmodified(); }
/** @return info about the watched file */ public String getFileInfo() { SimpleDateFormat sdf = new SimpleDateFormat(); StringBuffer buff = new StringBuffer(path); buff.append(" last modified: "); buff.append(sdf.format(resource.lastmodified())); return buff.toString(); }
/** * Another method to avoid reloads if this object is registered * * @see GeoServerUserGroupService#registerUserGroupLoadedListener(UserGroupLoadedListener) */ @Override public void usersAndGroupsChanged(UserGroupLoadedEvent event) { // avoid unnecessary reloads setLastModified(resource.lastmodified()); LOGGER.info("Adjusted last modified for file: " + path); } }
/** Determines if the underlying file has been modified since the last check. */ public boolean isModified() { long now = System.currentTimeMillis(); if ((now - lastCheck) > 1000) { lastCheck = now; stale = (resource.getType() != Type.UNDEFINED) && (resource.lastmodified() != lastModified); } return stale; }
/** * Another method to avoid reloads if this object is registered * * @see GeoServerRoleService#registerRoleLoadedListener(RoleLoadedListener) */ @Override public void rolesChanged(RoleLoadedEvent event) { // avoid reloads setLastModified(resource.lastmodified()); LOGGER.info("Adjusted last modified for file: " + path); } }
/** * return true if a write lock is hold by this file watcher * * @throws IOException */ public boolean hasWriteLock() throws IOException { return Resources.exists(lockFile) && lockFile.lastmodified() == lockFileLastModified; }
/** * return true if a write lock is hold by another file watcher * * @throws IOException */ public boolean hasForeignWriteLock() throws IOException { return Resources.exists(lockFile) && lockFile.lastmodified() != lockFileLastModified; }
/** remove the lockfile */ public void writeUnLock() { if (Resources.exists(lockFile)) { if (lockFile.lastmodified() == lockFileLastModified) { lockFileLastModified = 0; lockFile.delete(); } else { LOGGER.warning("Tried to unlock foreign lock: " + lockFile.path()); } } else { LOGGER.warning("Tried to unlock not exisiting lock: " + lockFile.path()); } }
/** * Search for resources using pattern and last modified time. * * @param resource Resource indicated * @param lastModified time stamp to search from * @return list of modified resources */ public static List<Resource> search(Resource resource, long lastModified) { if (resource.getType() == Type.DIRECTORY) { ArrayList<Resource> results = new ArrayList<Resource>(); for (Resource child : resource.list()) { switch (child.getType()) { case RESOURCE: if (child.lastmodified() > lastModified) { results.add(child); } break; default: break; } } return results; } return Collections.emptyList(); }
/** * Reads the file updating the last check timestamp. * * <p>Subclasses can override {@link #parseFileContents(InputStream)} to do something when the * file is read. * * @return parsed file contents */ public T read() throws IOException { T result = null; if (resource.getType() == Type.RESOURCE) { InputStream is = null; try { is = resource.in(); result = parseFileContents(is); lastModified = resource.lastmodified(); lastCheck = System.currentTimeMillis(); stale = false; } finally { if (is != null) { is.close(); } } } return result; }
/** Test constellation and call {@link #doOnChange()} if necessary */ protected void checkAndConfigure() { boolean fileExists; try { fileExists = resource.getType() == Type.RESOURCE; } catch (SecurityException e) { LOGGER.warning("Was not allowed to read check file existance, file:[" + path + "]."); setTerminate(true); // there is no point in continuing return; } if (fileExists) { long l = resource.lastmodified(); // this can also throw a SecurityException if (testAndSetLastModified(l)) { // however, if we reached this point this doOnChange(); // is very unlikely. warnedAlready = false; } } else { if (!warnedAlready) { LOGGER.warning("[" + path + "] does not exist."); warnedAlready = true; } } }
/** * Try to get a lock * * @throws IOException */ public void writeLock() throws IOException { if (hasWriteLock()) return; // already locked if (Resources.exists(lockFile)) { LOGGER.warning("Cannot obtain lock: " + lockFile.path()); Properties props = new Properties(); try (InputStream in = lockFile.in()) { props.load(in); } throw new IOException(Util.convertPropsToString(props, "Already locked")); } else { // success writeLockFileContent(lockFile); lockFileLastModified = lockFile.lastmodified(); LOGGER.info("Successful lock: " + lockFile.path()); } }
@Theory public void theoryExtantHaveDate(String path) throws Exception { Resource res = getResource(path); assumeThat(res, defined()); long result = res.lastmodified(); assertThat(result, notNullValue()); }
new RoleFileWatcher(resource, service, resource.lastmodified()); service.registerRoleLoadedListener(watcher); watcher.start();
public void run() { long newLastModified = configFile.lastmodified(); if (lastModified == null || newLastModified != lastModified) { lastModified = newLastModified; loadConfiguration(); } } }
@Override public int compare(Resource o1, Resource o2) { return (int) (o1.lastmodified() - o2.lastmodified()); } });
/** Returns true if the backing file does not exist any more, or has been modified */ public boolean isStale() { return !Resources.exists(file) || (file.lastmodified() != lastModified); } }
@Theory public void theoryModifiedUpdated(String path) throws Exception { Resource res = getResource(path); assumeThat(res, not(directory())); long lastmod = res.lastmodified(); OutputStream out = res.out(); out.write(1234); out.close(); assertTrue(res.lastmodified() > lastmod); } }
@Theory public void theoryExtantHaveDate(String path) throws Exception { Resource res = getResource(path); assumeThat(res, defined()); long result = res.lastmodified(); assertThat(result, notNullValue()); }