package com.lonepulse.zombielink.core;

/*
 * #%L
 * ZombieLink
 * %%
 * Copyright (C) 2013 Lonepulse
 * %%
 * 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.
 * #L%
 */

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.protocol.HttpContext;

/**
 * <p>A concrete implementation of {@link HttpClient} which provides network 
 * interfacing over a thread-safe, <b>asynchronous</b> HTTP client.</p>
 * 
 * @version 2.2.0
 * <br><br>
 * @author <a href="mailto:lahiru@lonepulse.com">Lahiru Sahan Jayasinghe</a>
 */
public enum MultiThreadedHttpClient implements HttpClientContract {

	
	/**
	 * <p>Offers an {@link HttpClient} instantiated with a thread-safe 
	 * {@link PoolingClientConnectionManager}.
	 * 
	 * @since 2.1.0
	 */
	INSTANCE;
	
	
	/**
	 * <p>The multi-threaded implementation of {@link HttpClient} which is used to 
	 * execute requests in parallel.</p>
	 * <br><br>
	 * @since 1.1.1
	 */
	private transient HttpClient httpClient;
	
	
	/**
	 * <p>Default constructor overridden to provide an implementation of 
	 * {@link HttpClient} which can handle <i>multi-threaded request execution</i>.</p> 
	 * 
	 * <p>This implementation uses a {@link PoolingClientConnectionManager} with a 
	 * {@link SchemeRegistry} which has default port registrations of <b>HTTP</b> 
	 * and <b>HTTPS</b>.</p> 
	 * <br><br>
	 * @since 1.1.1
	 */
	private MultiThreadedHttpClient() {
		
		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
		schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
		
		PoolingClientConnectionManager pccm = new PoolingClientConnectionManager(schemeRegistry);
		pccm.setMaxTotal(128); //Max. number of client connections pooled
		
		this.httpClient = new DefaultHttpClient(pccm);
	}

	/**
	 * {@inheritDoc} 
	 */
	@Override
	public <T extends HttpRequestBase> HttpResponse executeRequest(T httpRequestBase) 
	throws ClientProtocolException, IOException {

		return this.httpClient.execute(httpRequestBase);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public <T extends HttpRequestBase> HttpResponse executeRequest(T httpRequestBase, HttpContext httpContext)
	throws ClientProtocolException, IOException {
	
		return this.httpClient.execute(httpRequestBase, httpContext);
	}
}

@@ -1921,16 +1921,22 @@
 private 
+final 
 transien
@@ -2748,11 +2748,11 @@
 tal(
-128
+256
 ); /
@@ -2799,55 +2799,355 @@
 d%0A%09%09
-%0A%09%09this.httpClient = new DefaultHttpClient(pccm
+pccm.setDefaultMaxPerRoute(24); //Max connections per route%0A%09%09%0A%09%09this.httpClient = new DefaultHttpClient(pccm);%0A%09%09%0A%09%09Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() %7B%0A%09%09%09%0A%09%09%09@Override%0A%09%09%09public void run() %7B //HttpClient considered to be %22out of scope%22 only on VM exit%0A%09%09%09%0A%09%09%09%09httpClient.getConnectionManager().shutdown(); %0A%09%09%09%7D%0A%09%09%7D)
 );%0A%09

Update max connections and connection manager cleanup