ConcurrentHashMap has a high memory overhead (which has bitten me in projects before, where CHM was used for in many places convenience but wasn't strictly needed). Unless there's a compelling need to have a non-blocking threadsafe map, a synchronized hashmap should work well.
Sam On Mon, Feb 22, 2010 at 2:36 PM, sebb <[email protected]> wrote: > On 22/02/2010, [email protected] <[email protected]> wrote: >> Author: olegk >> Date: Mon Feb 22 18:45:55 2010 >> New Revision: 915013 >> >> URL: http://svn.apache.org/viewvc?rev=915013&view=rev >> Log: >> HTTPCLIENT-915: mechanism to attatch user define attributes to connections >> >> Modified: >> >> httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java >> >> httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java >> >> Modified: >> httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java >> URL: >> http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java?rev=915013&r1=915012&r2=915013&view=diff >> ============================================================================== >> --- >> httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java >> (original) >> +++ >> httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java >> Mon Feb 22 18:45:55 2010 >> @@ -43,6 +43,7 @@ >> import org.apache.http.conn.OperatedClientConnection; >> import org.apache.http.conn.ManagedClientConnection; >> import org.apache.http.conn.ClientConnectionManager; >> +import org.apache.http.protocol.HttpContext; >> >> /** >> * Abstract adapter from {...@link OperatedClientConnection operated} to >> @@ -65,7 +66,8 @@ >> * >> * @since 4.0 >> */ >> -public abstract class AbstractClientConnAdapter implements >> ManagedClientConnection { >> +public abstract class AbstractClientConnAdapter >> + implements ManagedClientConnection, >> HttpContext { >> >> /** >> * The connection manager, if any. >> @@ -322,4 +324,32 @@ >> } >> } >> >> + public synchronized Object getAttribute(final String id) { >> + OperatedClientConnection conn = getWrappedConnection(); >> + assertValid(conn); >> + if (conn instanceof HttpContext) { >> + return ((HttpContext) conn).getAttribute(id); >> + } else { >> + return null; >> + } >> + } >> + >> + public synchronized Object removeAttribute(final String id) { >> + OperatedClientConnection conn = getWrappedConnection(); >> + assertValid(conn); >> + if (conn instanceof HttpContext) { >> + return ((HttpContext) conn).removeAttribute(id); >> + } else { >> + return null; >> + } >> + } >> + >> + public synchronized void setAttribute(final String id, final Object >> obj) { >> + OperatedClientConnection conn = getWrappedConnection(); >> + assertValid(conn); >> + if (conn instanceof HttpContext) { >> + ((HttpContext) conn).setAttribute(id, obj); >> + } >> + } >> + >> } >> >> Modified: >> httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java >> URL: >> http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java?rev=915013&r1=915012&r2=915013&view=diff >> ============================================================================== >> --- >> httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java >> (original) >> +++ >> httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java >> Mon Feb 22 18:45:55 2010 >> @@ -29,6 +29,8 @@ >> >> import java.io.IOException; >> import java.net.Socket; >> +import java.util.HashMap; >> +import java.util.Map; >> >> import org.apache.http.annotation.NotThreadSafe; >> >> @@ -41,6 +43,7 @@ >> import org.apache.http.HttpResponse; >> import org.apache.http.HttpResponseFactory; >> import org.apache.http.params.HttpParams; >> +import org.apache.http.protocol.HttpContext; >> import org.apache.http.impl.SocketHttpClientConnection; >> import org.apache.http.io.HttpMessageParser; >> import org.apache.http.io.SessionInputBuffer; >> @@ -65,7 +68,7 @@ >> */ >> @NotThreadSafe // connSecure, targetHost >> public class DefaultClientConnection extends SocketHttpClientConnection >> - implements OperatedClientConnection { >> + implements OperatedClientConnection, HttpContext { >> >> private final Log log = LogFactory.getLog(getClass()); >> private final Log headerLog = >> LogFactory.getLog("org.apache.http.headers"); >> @@ -83,8 +86,12 @@ >> /** True if this connection was shutdown. */ >> private volatile boolean shutdown; >> >> + /** connection specific attributes */ >> + private final Map<String, Object> attributes; >> + >> public DefaultClientConnection() { >> super(); >> + this.attributes = new HashMap<String, Object>(); > > Maybe we could use ConcurrentHashMap here? > In which case, perhaps the synchronisation could be removed from some > of the new methods. > >> } >> >> public final HttpHost getTargetHost() { >> @@ -253,4 +260,16 @@ >> } >> } >> >> + public Object getAttribute(final String id) { >> + return this.attributes.get(id); >> + } >> + >> + public Object removeAttribute(final String id) { >> + return this.attributes.remove(id); >> + } >> + >> + public void setAttribute(final String id, final Object obj) { >> + this.attributes.put(id, obj); >> + } >> + >> } >> >> >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
