001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.transport.nhttp;
020:
021: import org.apache.http.nio.NHttpClientConnection;
022: import org.apache.http.protocol.HttpExecutionContext;
023: import org.apache.http.HttpHost;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import java.util.*;
028: import java.io.IOException;
029:
030: public class ConnectionPool {
031:
032: private static final Log log = LogFactory
033: .getLog(ConnectionPool.class);
034:
035: /** A map of available connections for reuse. The key selects the host+port of the
036: * connection and the value contains a List of available connections to destination
037: */
038: private static Map connMap = Collections
039: .synchronizedMap(new HashMap());
040:
041: public static NHttpClientConnection getConnection(String host,
042: int port) {
043:
044: String key = host + ":" + Integer.toString(port);
045: List connections = (List) connMap.get(key);
046:
047: if (connections == null || connections.isEmpty()) {
048: if (log.isDebugEnabled()) {
049: log.debug("No connections available for reuse");
050: }
051: return null;
052:
053: } else {
054: NHttpClientConnection conn = null;
055:
056: synchronized (connections) {
057: while (!connections.isEmpty()) {
058: conn = (NHttpClientConnection) connections
059: .remove(0);
060:
061: if (conn.isOpen()) {
062: if (log.isDebugEnabled()) {
063: log
064: .debug("A connection to host : "
065: + host
066: + " on port : "
067: + port
068: + " is available in the pool, and will be reused");
069: }
070: return conn;
071: } else {
072: if (log.isDebugEnabled()) {
073: log.debug("closing stale connection");
074: }
075: try {
076: conn.close();
077: } catch (IOException ignore) {
078: }
079: }
080: }
081: }
082: return null;
083: }
084: }
085:
086: public static void release(NHttpClientConnection conn) {
087:
088: HttpHost host = (HttpHost) conn.getContext().getAttribute(
089: HttpExecutionContext.HTTP_TARGET_HOST);
090: String key = host.getHostName() + ":"
091: + Integer.toString(host.getPort());
092:
093: List connections = (List) connMap.get(key);
094: if (connections == null) {
095: synchronized (connMap) {
096: // use double locking to make sure
097: connections = (List) connMap.get(key);
098: if (connections == null) {
099: connections = Collections
100: .synchronizedList(new LinkedList());
101: connMap.put(key, connections);
102: }
103: }
104: }
105:
106: connections.add(conn);
107:
108: log.debug("Released a connection to host: "
109: + host.getHostName() + " on port : " + host.getPort()
110: + " to the connection pool of current size : "
111: + connections.size());
112: }
113: }
|