abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r603474 - in /incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache: Cache.java CacheFactory.java CachedResponse.java CachedResponseBase.java InMemoryCache.java InMemoryCachedResponse.java LRUMap.java
Date Wed, 12 Dec 2007 06:00:04 GMT
Author: jmsnell
Date: Tue Dec 11 22:00:00 2007
New Revision: 603474

URL: http://svn.apache.org/viewvc?rev=603474&view=rev
Log:
These were inadvertently deleted with the last commit

Added:
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/Cache.java
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheFactory.java
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponse.java
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponseBase.java
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCachedResponse.java
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/Cache.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/Cache.java?rev=603474&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/Cache.java
(added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/Cache.java
Tue Dec 11 22:00:00 2007
@@ -0,0 +1,63 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.cache;
+
+import org.apache.abdera.protocol.client.ClientResponse;
+import org.apache.abdera.protocol.client.RequestOptions;
+
+public interface Cache 
+  extends Iterable<Object> {
+
+  public static enum Disposition {
+    STALE, FRESH, TRANSPARENT 
+  }
+  
+  /**
+   * Get the disposition of a specific item
+   */
+  Disposition disposition(
+    Object key, 
+    RequestOptions options);
+    
+  /**
+   * Get a specific item from the cache
+   */
+  CachedResponse get(
+    Object key);
+  
+  /**
+   * Clear all items from the cache
+   */
+  Cache clear();
+  
+  /**
+   * Remove a specific item from the cache
+   */
+  Cache remove(
+    Object key);
+  
+  /**
+   * Update the cached item
+   */
+  ClientResponse update(
+    Object key,
+    RequestOptions options, 
+    ClientResponse response,
+    ClientResponse cached_response);
+  
+}

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheFactory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheFactory.java?rev=603474&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheFactory.java
(added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheFactory.java
Tue Dec 11 22:00:00 2007
@@ -0,0 +1,26 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.cache;
+
+import org.apache.abdera.Abdera;
+
+public interface CacheFactory {
+
+  Cache getCache(Abdera abdera);
+  
+}

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponse.java?rev=603474&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponse.java
(added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponse.java
Tue Dec 11 22:00:00 2007
@@ -0,0 +1,43 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.cache;
+
+import org.apache.abdera.protocol.client.ClientResponse;
+
+public interface CachedResponse 
+  extends ClientResponse {
+
+  Object getKey();
+  
+  Cache getCache();
+  
+  long getCachedTime();
+  
+  long getInitialAge();
+  
+  long getResidentAge();
+
+  long getCurrentAge();
+  
+  long getFreshnessLifetime();
+
+  long getHowStale();
+
+  boolean isFresh();
+
+}

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponseBase.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponseBase.java?rev=603474&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponseBase.java
(added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponseBase.java
Tue Dec 11 22:00:00 2007
@@ -0,0 +1,108 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.cache;
+
+import java.util.Date;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.protocol.client.AbstractClientResponse;
+
+public abstract class CachedResponseBase 
+  extends AbstractClientResponse 
+  implements CachedResponse {
+
+  protected final Object key;
+  protected final Cache cache;
+  protected long initial_age;
+  
+  protected CachedResponseBase(
+    Abdera abdera, 
+    Object key, 
+    Cache cache) {
+      super(abdera);
+      this.key = key;
+      this.cache = cache;
+  }
+  
+  private long calcInitialAge() {
+    long age_value = getAge();
+    long now = (new Date()).getTime();
+    long cachedTime = getCachedTime();
+    long date_value = (cachedTime != -1) ? cachedTime : 0;
+    long apparent_age = Math.max(0,now - date_value);
+    long corrected_received_age = Math.max(apparent_age, age_value);
+    return corrected_received_age / 1000;
+  }
+  
+  public Object getKey() {
+    return key;
+  }
+
+  public Cache getCache() {
+    return cache;
+  }
+
+  public void release() {
+    if (cache != null) {
+      cache.remove(key); 
+    }
+  }
+  
+  public long getInitialAge() {
+    if (initial_age == -1) initial_age = calcInitialAge();
+    return initial_age;
+  }
+
+  public long getCachedTime() {
+    return getServerDate().getTime();
+  }
+  
+  public long getResidentAge() {
+    long now = (new Date()).getTime();
+    long init = getCachedTime();
+    return Math.max(0,(now - init))/1000;
+  }
+  
+  public long getCurrentAge() {
+    return getInitialAge() + getResidentAge();
+  }
+  
+  public long getFreshnessLifetime() {
+    long lifetime = getMaxAge();
+    if (lifetime == -1) {
+      Date expires_date = getExpires();
+      long expires = (expires_date != null) ? expires_date.getTime() : -1; 
+      long cachedTime = getCachedTime();
+      if (expires != -1) {
+        lifetime = (expires > cachedTime) ? (expires - cachedTime)/1000 : 0; 
+      } // else, expires is not set, return -1 for now. TODO: apply heuristics
+    }
+    return lifetime;
+  }
+  
+  public long getHowStale() {
+    return (!isFresh()) ? getCurrentAge() - getFreshnessLifetime() : 0;
+  }
+  
+  public boolean isFresh() {
+    long lifetime = getFreshnessLifetime();
+    long currentage = getCurrentAge();
+    return (lifetime != -1) ? lifetime > currentage : true;
+  }
+
+}

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java?rev=603474&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java
(added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java
Tue Dec 11 22:00:00 2007
@@ -0,0 +1,73 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.cache;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.protocol.client.ClientResponse;
+
+public abstract class InMemoryCache 
+  extends AbstractCache {
+
+  protected transient final Map<Object,CachedResponse> cache;
+
+  protected InMemoryCache(
+    Abdera abdera, 
+    Map<Object,CachedResponse> map) {
+      super(abdera);
+      this.cache = map;
+  }
+  
+  @Override
+  protected CachedResponse createCachedResponse(
+    ClientResponse response, 
+    Object key) 
+      throws IOException {
+    return new InMemoryCachedResponse(
+      abdera, this, key, response);
+  }
+
+  public Cache clear() {
+    cache.clear();
+    return this;
+  }
+
+  public CachedResponse get(
+    Object key) {   
+      return cache.get(key);
+  }
+
+  public Cache remove(
+    Object key) {
+      cache.remove(key);
+      return this;
+  }
+
+  protected void add(
+    Object key, 
+    CachedResponse response) {
+      cache.put(key, response);
+  }
+
+  public Iterator<Object> iterator() {
+    return cache.keySet().iterator();
+  }
+}

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCachedResponse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCachedResponse.java?rev=603474&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCachedResponse.java
(added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCachedResponse.java
Tue Dec 11 22:00:00 2007
@@ -0,0 +1,118 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.cache;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.protocol.client.ClientResponse;
+import org.apache.abdera.protocol.client.util.MethodHelper;
+import org.apache.abdera.protocol.util.CacheControlUtil;
+
+public class InMemoryCachedResponse 
+  extends CachedResponseBase
+  implements CachedResponse {
+
+  private final String method;
+  private final int status;
+  private final String status_text;
+  private final String uri;
+  private final Map<String,Object[]> headers;
+  private final byte[] buf;
+  
+  public InMemoryCachedResponse(
+    Abdera abdera,
+    Cache cache,
+    Object key,
+    ClientResponse response) 
+      throws IOException {
+    super(abdera, key,cache);
+    this.method = response.getMethod();
+    this.status = response.getStatus();
+    this.status_text = response.getStatusText();
+    this.uri = response.getUri();
+    this.headers = MethodHelper.getCacheableHeaders(response);
+    CacheControlUtil.parseCacheControl(this.getHeader("Cache-Control"), this);
+    buf = cacheStream(response.getInputStream());
+    response.setInputStream(getInputStream());
+  }
+
+  /**
+   * This is terribly inefficient, but it is an in-memory cache
+   * that is being used by parsers that incrementally consume 
+   * InputStreams at different rates.  There's really no other
+   * way to do it.
+   */
+  private byte[] cacheStream(InputStream in) throws IOException {
+    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
+    byte[] buf = new byte[1024];
+    int m = -1;
+    while((m = in.read(buf)) != -1) {
+      out.write(buf,0,m);
+    }
+    return out.toByteArray();
+  }
+  
+  public Map<String,Object[]> getHeaders() {
+    return headers;
+  }
+  
+  public String getMethod() {
+    return method;
+  }
+  
+  public String getHeader(String header) {
+    Object[] values = getHeaders().get(header);
+    return (values != null && values.length > 0) ? (String)values[0] : null;
+  }
+
+  public String[] getHeaderNames() {
+    return getHeaders().keySet().toArray(new String[getHeaders().size()]);
+  }
+
+  public Object[] getHeaders(String header) {
+    return getHeaders().get(header);
+  }
+
+  public int getStatus() {
+    return status;
+  }
+
+  public String getStatusText() {
+    return status_text;
+  }
+
+  public String getUri() {
+    return uri;
+  }
+
+  @Override
+  public InputStream getInputStream() throws IOException {
+    return new ByteArrayInputStream(this.buf);
+  }
+
+  @Override
+  public void setInputStream(InputStream in) {
+    throw new UnsupportedOperationException();
+  }
+
+}

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java?rev=603474&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java
(added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java
Tue Dec 11 22:00:00 2007
@@ -0,0 +1,39 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.cache;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public final class LRUMap<A,B> 
+  extends LinkedHashMap<A,B> {
+
+  private static final long serialVersionUID = -8243948270889739367L;
+  private final int size;
+  
+  public LRUMap(int initialSize, float loadFactor) {
+    super(initialSize, loadFactor, true);
+    this.size = initialSize;
+  }
+  
+  protected boolean removeEldestEntry(
+    Map.Entry<A,B> eldest) {
+    return size() > size;
+  }
+  
+}



Mime
View raw message