/** * Creates a new SyncedFileAttributes using the specifies 'time to live' value. * * @param ttl the attributes' 'time to live', in milliseconds * @param updateAttributesNow if <code>true</code>, attributes are automatically updated */ public SyncedFileAttributes(long ttl, boolean updateAttributesNow) { setTtl(ttl); // also sets the expiration date if(updateAttributesNow) checkForExpiration(true); // force attributes update }
/** * Updates the attributes' expiration date to 'now' + 'ttl' (as returned by {@link #getTtl()}. * This method is called after attributes have been automatically updated. It can also be called after attribute * values have been manually updated using the setter methods. */ public void updateExpirationDate() { setExpirationDate( ttl<0 ?Long.MAX_VALUE :System.currentTimeMillis()+getTtl()); }
/** * Checks if the attributes have expired and if they have, calls {@link #updateAttributes()} to refresh their * values. * * @param forceUpdate if true, attributes will systematically be updated, without checking the expiration date */ protected void checkForExpiration(boolean forceUpdate) { if(forceUpdate || hasExpired()) { // After this method is called, hasExpired() returns false so that implementations of updateAttributes() // can query attribute getters without entering a loop of death. setUpdating(true); // Updates attribute values updateAttributes(); // Update expiration date after the attribute have actually been updated, note that it may take a while // for remote file protocols to retrieve attributes. updateExpirationDate(); // OK we're done setUpdating(false); } }
/** * Overridden to trigger attributes update if the expiration date has been reached. */ @Override public String getOwner() { checkForExpiration(false); return super.getOwner(); }
/** * Sets the attributes' 'time to live', , i.e. the amount of time since the last update after which attributes will * be automatically updated when any of the getter method is called. * Note that setting the 'time to live' causes the expiration date to be updated with {@link #updateExpirationDate()}. * * @param ttl the attributes' 'time to live', in milliseconds */ public void setTtl(long ttl) { this.ttl = ttl; // update the expiration date updateExpirationDate(); }
/** * Returns <code>true</code> if attributes have expired, i.e. the {@link #getExpirationDate()} expiration date} has * passed, <code>false</code> if attributes are still 'fresh'. This method also returns <code>false</code> if * automatic attributes' update has been disabled ('time to live' set to a negative value), or if attributes are * currently being updated. * * @return <code>true</code> if attributes have expired */ public boolean hasExpired() { return ttl>=0 // prevents automatic updates if ttl is set to a negative value && !isUpdating() // causes getters to return the current value while attributes are being updated && System.currentTimeMillis()>expirationDate; }
/** * Overridden to trigger attributes update if the expiration date has been reached. */ @Override public boolean exists() { checkForExpiration(false); return super.exists(); }
/** * Overridden to trigger attributes update if the expiration date has been reached. */ @Override public long getDate() { checkForExpiration(false); return super.getDate(); }
/** * Overridden to trigger attributes update if the expiration date has been reached. */ @Override public long getSize() { checkForExpiration(false); return super.getSize(); }
/** * Overridden to trigger attributes update if the expiration date has been reached. */ @Override public String getPath() { checkForExpiration(false); return super.getPath(); }
/** * Overridden to trigger attributes update if the expiration date has been reached. */ @Override public boolean isDirectory() { checkForExpiration(false); return super.isDirectory(); }
/** * Overridden to trigger attributes update if the expiration date has been reached. */ @Override public FilePermissions getPermissions() { checkForExpiration(false); return super.getPermissions(); }
/** * Overridden to trigger attributes update if the expiration date has been reached. */ @Override public String getGroup() { checkForExpiration(false); return super.getGroup(); }