All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.globus.axis.transport.commons.CommonsHttpConnectionManager Maven / Gradle / Ivy

/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed 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.
 */
package org.globus.axis.transport.commons;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.params.HostParams;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CommonsHttpConnectionManager implements HttpConnectionManager {

    private static final IdleConnectionTimeoutThread IDLE_THREAD;

    static {
        IDLE_THREAD = new IdleConnectionTimeoutThread();
        IDLE_THREAD.setTimeoutInterval(1000 * 60 * 2);
        IDLE_THREAD.start();
    }

    private static Log logger =
        LogFactory.getLog(CommonsHttpConnectionManager.class);

    private String [] hostConfigurationParams;
    private HashMap hostPoolMap;
    private long idleTime = 1000 * 60 * 2; 
    private boolean staleChecking = true;
    
    private HttpConnectionManagerParams params = 
        new HttpConnectionManagerParams(); 

    public CommonsHttpConnectionManager(String [] hostConfigurationParams) {
        this.hostConfigurationParams = hostConfigurationParams;
        this.hostPoolMap = new HashMap();
        IDLE_THREAD.addConnectionManager(this);
    }
    
    public void setConnectionIdleTime(long time) {
        this.idleTime = time;
    }

    public long getConnectionIdleTime() {
        return this.idleTime;
    }

    public void setStaleCheckingEnabled(boolean staleChecking) {
        this.staleChecking = staleChecking;
    }
    
    public boolean isStaleCheckingEnabled() {
        return this.staleChecking;
    }
    
    public HttpConnection getConnection(HostConfiguration hostConfiguration) {
        return getConnectionWithTimeout(hostConfiguration, 0);
    }

    public HttpConnection getConnection(HostConfiguration hostConfiguration, 
                                        long timeout) {
        return getConnectionWithTimeout(hostConfiguration, timeout);
    }

    public HttpConnection getConnectionWithTimeout(
                   HostConfiguration hostConfiguration, long timeout) {
        ExtendedHostConfiguration extendedHostConfiguration = 
            new ExtendedHostConfiguration(hostConfiguration, 
                                          this.hostConfigurationParams);
        
        ConnectionPool pool = getConnectionPool(extendedHostConfiguration);
        ExtendedHttpConnection httpConnection = pool.getPooledConnection();
        if (httpConnection == null) {
            // not in the pool - create a new connection
            httpConnection = getNewConnection(extendedHostConfiguration);
            httpConnection.setFromPool(false);
        } else {
            httpConnection.setFromPool(true);
        }

        if (this.staleChecking) {
            // install our retry handler
            hostConfiguration.getParams().setParameter(
                              HttpMethodParams.RETRY_HANDLER,
                              httpConnection.getRetryHandler());
        }

        return httpConnection;
    }

    private ExtendedHttpConnection getNewConnection(
                           HostConfiguration hostConfiguration) {
        ExtendedHttpConnection httpConnection = 
            new ExtendedHttpConnection(hostConfiguration, this.staleChecking);
        httpConnection.setHttpConnectionManager(this);
        HttpConnectionParams connectionParams = httpConnection.getParams();
        connectionParams.setDefaults(this.params);
        
        if (this.hostConfigurationParams != null) {
            HostParams hostParams = hostConfiguration.getParams();
            for (int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy