Return-Path: Delivered-To: apmail-incubator-abdera-commits-archive@locus.apache.org Received: (qmail 83608 invoked from network); 8 Sep 2006 01:41:22 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 8 Sep 2006 01:41:22 -0000 Received: (qmail 99067 invoked by uid 500); 8 Sep 2006 01:41:21 -0000 Delivered-To: apmail-incubator-abdera-commits-archive@incubator.apache.org Received: (qmail 99043 invoked by uid 500); 8 Sep 2006 01:41:21 -0000 Mailing-List: contact abdera-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: abdera-dev@incubator.apache.org Delivered-To: mailing list abdera-commits@incubator.apache.org Received: (qmail 99034 invoked by uid 99); 8 Sep 2006 01:41:21 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Sep 2006 18:41:21 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Sep 2006 18:41:20 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id F05771A981A; Thu, 7 Sep 2006 18:40:59 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r441330 - in /incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client: ./ cache/ cache/lru/ util/ Date: Fri, 08 Sep 2006 01:40:59 -0000 To: abdera-commits@incubator.apache.org From: jmsnell@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20060908014059.F05771A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: jmsnell Date: Thu Sep 7 18:40:58 2006 New Revision: 441330 URL: http://svn.apache.org/viewvc?view=rev&rev=441330 Log: General implementation improvements to improve threadsafety, reuse common code and make things generally more robust Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsClient.java incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheBase.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/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/lru/LRUCache.java incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCacheFactory.java incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/BaseRequestEntity.java incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/DataSourceRequestEntity.java Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java Thu Sep 7 18:40:58 2006 @@ -34,17 +34,19 @@ extends AbstractResponse implements ClientResponse { + protected final Abdera abdera; + protected final Parser parser; + protected final Date now = new Date(); + protected InputStream in = null; - protected Date response_date = null; - protected Date now = new Date(); - protected Abdera abdera = null; - protected Parser parser = null; + protected Date response_date = null; + + protected AbstractClientResponse(Abdera abdera) { + this.abdera = abdera; + this.parser = abdera.getParser(); + } protected synchronized Parser getParser() { - if (parser == null) { - if (abdera == null) abdera = new Abdera(); - parser = abdera.getParser(); - } return parser; } Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java Thu Sep 7 18:40:58 2006 @@ -43,16 +43,16 @@ */ public abstract class Client { - protected Abdera abdera = null; - protected Cache cache = null; - protected CacheFactory cacheFactory = null; + protected final Abdera abdera; + protected final Cache cache; public Client() { - this.abdera = new Abdera(); + this(new Abdera()); } protected Client(Abdera abdera) { this.abdera = abdera; + this.cache = initCache(initCacheFactory()); } /** @@ -87,27 +87,23 @@ */ public abstract void usePreemptiveAuthentication(boolean val); - public abstract void init(String userAgent); - - private CacheFactory getCacheFactory() { - if (cacheFactory == null) { - cacheFactory = (CacheFactory)ServiceUtil.newInstance( + private CacheFactory initCacheFactory() { + CacheFactory cacheFactory = + (CacheFactory)ServiceUtil.newInstance( "org.apache.abdera.protocol.cache.CacheFactory", "org.apache.abdera.protocol.cache.lru.LRUCacheFactory", abdera); - } return cacheFactory; } public Cache getCache() { - if (cache == null) { - CacheFactory factory = getCacheFactory(); - if (factory != null) - cache = factory.getCache(); - if (cache == null) - cache = new LRUCache(); - } return cache; + } + + public Cache initCache(CacheFactory factory) { + Cache cache = null; + if (factory != null) cache = factory.getCache(abdera); + return (cache != null) ? cache : new LRUCache(abdera); } public ClientResponse head( Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsClient.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsClient.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsClient.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsClient.java Thu Sep 7 18:40:58 2006 @@ -38,37 +38,35 @@ public class CommonsClient extends Client { - private HttpClient client = null; + private static final String DEFAULT_USER_AGENT = + Version.APP_NAME + "/" + Version.VERSION; + + private final HttpClient client; public CommonsClient() { - this(Version.APP_NAME + "/" + Version.VERSION); + this(DEFAULT_USER_AGENT); } public CommonsClient(Abdera abdera) { - this(Version.APP_NAME + "/" + Version.VERSION, abdera); + this(DEFAULT_USER_AGENT, abdera); } public CommonsClient(String userAgent) { - init(userAgent); + this(userAgent, new Abdera()); } public CommonsClient(String userAgent,Abdera abdera) { super(abdera); - init(userAgent); - } - - public void usePreemptiveAuthentication(boolean val) { - client.getParams().setAuthenticationPreemptive(val); - } - - @Override - public void init(String userAgent) { client = new HttpClient(); client.getParams().setParameter( HttpClientParams.USER_AGENT, userAgent); client.getParams().setBooleanParameter( - HttpClientParams.USE_EXPECT_CONTINUE, true); + HttpClientParams.USE_EXPECT_CONTINUE, true); + } + + public void usePreemptiveAuthentication(boolean val) { + client.getParams().setAuthenticationPreemptive(val); } private boolean useCache( @@ -127,7 +125,7 @@ MethodHelper.createMethod( method, uri, entity, options); client.executeMethod(httpMethod); - ClientResponse response = new CommonsResponse(httpMethod); + ClientResponse response = new CommonsResponse(abdera,httpMethod); return (options.getUseLocalCache()) ? response = cache.update(options, response, cached_response) : response; Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java Thu Sep 7 18:40:58 2006 @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; +import org.apache.abdera.Abdera; import org.apache.abdera.protocol.util.ContentEncodingUtil; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpMethod; @@ -36,9 +37,10 @@ extends AbstractClientResponse implements ClientResponse { - private HttpMethod method = null; + private final HttpMethod method; - protected CommonsResponse(HttpMethod method) { + protected CommonsResponse(Abdera abdera, HttpMethod method) { + super(abdera); if (method.isRequestSent()) this.method = method; else throw new IllegalStateException(); Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java Thu Sep 7 18:40:58 2006 @@ -39,39 +39,45 @@ private boolean noLocalCache = false; private boolean revalidateAuth = false; - private Map> headers = null; + private final Map> headers; - public RequestOptions() {} + public RequestOptions() { + headers = new HashMap>(); + } public RequestOptions(Date ifModifiedSince) { + this(); setIfModifiedSince(ifModifiedSince); } public RequestOptions(String ifNoneMatch) { + this(); setIfNoneMatch(ifNoneMatch); } public RequestOptions(String... ifNoneMatch) { + this(); setIfNoneMatch(ifNoneMatch); } public RequestOptions(Date ifModifiedSince, String ifNoneMatch) { + this(); setIfModifiedSince(ifModifiedSince); setIfNoneMatch(ifNoneMatch); } public RequestOptions(Date ifModifiedSince, String... ifNoneMatch) { + this(); setIfModifiedSince(ifModifiedSince); setIfNoneMatch(ifNoneMatch); } public RequestOptions(boolean no_cache) { + this(); setNoCache(no_cache); } private Map> getHeaders() { - if (headers == null) - headers = new HashMap>(); return headers; } Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheBase.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheBase.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheBase.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheBase.java Thu Sep 7 18:40:58 2006 @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.List; +import org.apache.abdera.Abdera; import org.apache.abdera.protocol.client.RequestOptions; import org.apache.abdera.protocol.client.ClientResponse; import org.apache.abdera.protocol.util.CacheControlUtil; @@ -27,6 +28,12 @@ public abstract class CacheBase implements Cache { + protected final Abdera abdera; + + protected CacheBase(Abdera abdera) { + this.abdera = abdera; + } + public CachedResponse get(String uri, RequestOptions options) { return get(getCacheKey(uri,options)); } Modified: 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?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheFactory.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CacheFactory.java Thu Sep 7 18:40:58 2006 @@ -17,8 +17,10 @@ */ package org.apache.abdera.protocol.client.cache; +import org.apache.abdera.Abdera; + public interface CacheFactory { - Cache getCache(); + Cache getCache(Abdera abdera); } Modified: 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?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponseBase.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/CachedResponseBase.java Thu Sep 7 18:40:58 2006 @@ -19,18 +19,19 @@ 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 CacheKey key = null; - protected Cache cache = null; + protected final CacheKey key; + protected final Cache cache; protected long initial_age = -1; - protected CachedResponseBase(CacheKey key, Cache cache) { - super(); + protected CachedResponseBase(Abdera abdera, CacheKey key, Cache cache) { + super(abdera); this.key = key; this.cache = cache; } @@ -46,7 +47,6 @@ public void release() { if (cache != null) { cache.remove(key); - this.cache= null; } } Modified: 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?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java Thu Sep 7 18:40:58 2006 @@ -21,14 +21,19 @@ import java.util.Collections; import java.util.Map; +import org.apache.abdera.Abdera; import org.apache.abdera.protocol.client.RequestOptions; import org.apache.abdera.protocol.client.ClientResponse; public abstract class InMemoryCache extends CacheBase { - protected transient Map cache = null; + protected transient Map cache; + protected InMemoryCache(Abdera abdera) { + super(abdera); + } + protected void setMap(Map map) { cache = Collections.synchronizedMap(map); } @@ -38,7 +43,7 @@ ClientResponse response, CacheKey key) throws IOException { - return new InMemoryCachedResponse(this, key, response); + return new InMemoryCachedResponse(abdera, this, key, response); } public void clear() { Modified: 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?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCachedResponse.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCachedResponse.java Thu Sep 7 18:40:58 2006 @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; +import org.apache.abdera.Abdera; import org.apache.abdera.protocol.client.ClientException; import org.apache.abdera.protocol.client.ClientResponse; import org.apache.abdera.protocol.client.util.MethodHelper; @@ -45,11 +46,12 @@ private byte[] buf = null; public InMemoryCachedResponse( + Abdera abdera, Cache cache, CacheKey key, ClientResponse response) throws IOException { - super(key,cache); + super(abdera, key,cache); this.method = response.getMethod(); this.status = response.getStatus(); this.status_text = response.getStatusText(); Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java Thu Sep 7 18:40:58 2006 @@ -20,6 +20,7 @@ import java.util.LinkedHashMap; import java.util.Map; +import org.apache.abdera.Abdera; import org.apache.abdera.protocol.client.cache.Cache; import org.apache.abdera.protocol.client.cache.CacheKey; import org.apache.abdera.protocol.client.cache.CachedResponse; @@ -32,11 +33,12 @@ private final static int DEFAULT_SIZE = 10; - public LRUCache() { - this(DEFAULT_SIZE); + public LRUCache(Abdera abdera) { + this(abdera,DEFAULT_SIZE); } - public LRUCache(final int size) { + public LRUCache(Abdera abdera, final int size) { + super(abdera); setMap( new LinkedHashMap(size,0.75f,true) { @Override Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCacheFactory.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCacheFactory.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCacheFactory.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCacheFactory.java Thu Sep 7 18:40:58 2006 @@ -17,13 +17,14 @@ */ package org.apache.abdera.protocol.client.cache.lru; +import org.apache.abdera.Abdera; import org.apache.abdera.protocol.client.cache.Cache; import org.apache.abdera.protocol.client.cache.CacheFactory; public class LRUCacheFactory implements CacheFactory { - public Cache getCache() { - return new LRUCache(); + public Cache getCache(Abdera abdera) { + return new LRUCache(abdera); } } Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/BaseRequestEntity.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/BaseRequestEntity.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/BaseRequestEntity.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/BaseRequestEntity.java Thu Sep 7 18:40:58 2006 @@ -22,11 +22,7 @@ import java.io.OutputStream; import org.apache.abdera.model.Base; -import org.apache.abdera.model.Document; -import org.apache.abdera.model.Element; -import org.apache.abdera.model.Entry; -import org.apache.abdera.model.Feed; -import org.apache.abdera.model.Service; +import org.apache.abdera.util.MimeTypeHelper; import org.apache.commons.httpclient.methods.RequestEntity; /** @@ -35,7 +31,7 @@ public class BaseRequestEntity implements RequestEntity { - private Base base = null; + private final Base base; private byte[] buf = null; private boolean use_chunked = true; @@ -85,35 +81,7 @@ } public String getContentType() { - String type = null; - if (base instanceof Document) { - Document doc = (Document) base; - if (doc.getContentType() != null) { - type = doc.getContentType().toString(); - } else { - if (doc.getRoot() instanceof Feed || - doc.getRoot() instanceof Entry) { - type = "application/atom+xml"; - } else if (doc.getRoot() instanceof Service) { - type = "application/atomserv+xml"; - } else { - type = "application/xml"; - } - } - } else if (base instanceof Feed || base instanceof Entry) { - Document doc = ((Element)base).getDocument(); - if (doc != null && doc.getContentType() != null) - type = doc.getContentType().toString(); - if (type == null) - type = "application/atom+xml"; - } else if (base instanceof Service) { - Document doc = ((Element)base).getDocument(); - if (doc != null) - type = doc.getContentType().toString(); - if (type == null) - type = "application/atomserv+xml"; - } - return (type != null) ? type : "application/xml"; + return MimeTypeHelper.getMimeType(base); } } Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/DataSourceRequestEntity.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/DataSourceRequestEntity.java?view=diff&rev=441330&r1=441329&r2=441330 ============================================================================== --- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/DataSourceRequestEntity.java (original) +++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/DataSourceRequestEntity.java Thu Sep 7 18:40:58 2006 @@ -29,7 +29,7 @@ public class DataSourceRequestEntity implements RequestEntity { - private DataSource dataSource = null; + private final DataSource dataSource; public DataSourceRequestEntity(DataHandler dataHandler) { this(dataHandler.getDataSource());