Example #1
0
  /**
   * Returns the last modified time for the path.
   *
   * @return the last modified time of the jar in milliseconds.
   */
  private boolean isCacheValid() {
    long now = CurrentTime.currentTime();

    if ((now - _lastTime < 100) && !CurrentTime.isTest()) return true;

    long oldLastModified = _lastModified;
    long oldLength = _length;

    long newLastModified = getBacking().getLastModified();
    long newLength = getBacking().length();

    _lastTime = now;

    if (newLastModified == oldLastModified && newLength == oldLength) {
      _lastTime = now;
      return true;
    } else {
      _changeSequence.incrementAndGet();

      // If the file has changed, close the old file
      clearCache();

      _zipEntryCache.clear();

      _lastModified = newLastModified;
      _length = newLength;

      _lastTime = now;

      return false;
    }
  }
  /** Start a new connection. Returns true if the connection can be started. */
  public boolean startConnection() {
    State state = _state.get();

    if (state.isActive()) {
      // when active, always start a connection

      _connectionCount.incrementAndGet();

      return true;
    }

    long now = CurrentTime.currentTime();
    long lastFailTime = _lastFailTime;
    long recoverTimeout = _dynamicRecoverTimeout.get();

    if (now < lastFailTime + recoverTimeout) {
      // if the fail recover hasn't timed out, return false
      return false;
    }

    // when fail, only start a single connection
    int count;

    do {
      count = _connectionCount.get();

      if (count > 0) {
        return false;
      }
    } while (!_connectionCount.compareAndSet(count, count + 1));

    return true;
  }
  /** Sets the modified. */
  public void setModified(boolean isModified) {
    _isModified = isModified;

    if (_isModified) _checkExpiresTime = Long.MAX_VALUE / 2;
    else _checkExpiresTime = CurrentTime.currentTime() + _checkInterval;

    if (!isModified) _isModifiedLog = false;
  }
  /** Called when the server has a successful response */
  public void onSuccess() {
    if (_firstSuccessTime <= 0) {
      _firstSuccessTime = CurrentTime.currentTime();
    }

    // reset the connection fail recover time
    _dynamicRecoverTimeout.set(1000L);
    _firstFailTime = 0;
  }
  /** Called when the connection fails. */
  public void onFail() {
    _lastFailTime = CurrentTime.currentTime();

    if (_firstFailTime == 0) {
      _firstFailTime = _lastFailTime;
    }
    _firstSuccessTime = 0;

    toState(State.FAIL);

    long recoverTimeout = _dynamicRecoverTimeout.get();

    long nextRecoverTimeout = Math.min(recoverTimeout + 1000L, _recoverTimeout);

    _dynamicRecoverTimeout.compareAndSet(recoverTimeout, nextRecoverTimeout);
  }
  /** Returns true if the underlying dependencies have changed. */
  public boolean isModified(boolean isAsync) {
    long now = CurrentTime.currentTime();

    if (now < _checkExpiresTime) return _isModified;

    if (_isChecking.getAndSet(true)) return _isModified;

    _checkExpiresTime = now + _checkInterval;

    if (isAsync) {
      getWorker().wake();
    } else {
      try {
        checkImpl();
      } finally {
        _isChecking.set(false);
      }
    }

    return _isModified;
  }