コード例 #1
0
ファイル: HttpPath.java プロジェクト: dlitz/resin
  protected CacheEntry getCache() {
    if (_cacheEntry == null) {
      synchronized (_cache) {
        _cacheEntry = _cache.get(getPath());
        if (_cacheEntry == null) {
          _cacheEntry = new CacheEntry();
          _cache.put(getPath(), _cacheEntry);
        }
      }
    }

    long now;

    now = Alarm.getCurrentTime();

    synchronized (_cacheEntry) {
      try {
        if (_cacheEntry.expires > now) return _cacheEntry;

        HttpStreamWrapper stream = (HttpStreamWrapper) openReadImpl();
        stream.setHead(true);
        stream.setSocketTimeout(120000);

        String status = (String) stream.getAttribute("status");
        if (status.equals("200")) {
          String lastModified = (String) stream.getAttribute("last-modified");

          _cacheEntry.lastModified = 0;
          if (lastModified != null) {
            QDate date = QDate.getGlobalDate();
            synchronized (date) {
              _cacheEntry.lastModified = date.parseDate(lastModified);
            }
          }

          String length = (String) stream.getAttribute("content-length");
          _cacheEntry.length = 0;
          if (length != null) {
            _cacheEntry.length = Integer.parseInt(length);
          }
        } else _cacheEntry.lastModified = -1;

        _cacheEntry.expires = now + 5000;

        stream.close();
        return _cacheEntry;
      } catch (Exception e) {
        _cacheEntry.lastModified = -1;
        _cacheEntry.expires = now + 5000;

        return _cacheEntry;
      }
    }
  }