1
15 package com.amazonaws.http.conn;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.http.conn.ConnectionRequest;
20 import org.apache.http.conn.HttpClientConnectionManager;
21 import org.apache.http.pool.ConnPoolControl;
22
23 import java.lang.reflect.InvocationHandler;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.lang.reflect.Proxy;
27
28 public class ClientConnectionManagerFactory {
29 private static final Log log = LogFactory.getLog(ClientConnectionManagerFactory.class);
30
31
37 public static HttpClientConnectionManager wrap
38 (HttpClientConnectionManager orig) {
39 if (orig instanceof Wrapped)
40 throw new IllegalArgumentException();
41 final Class<?>[] interfaces;
42 if (orig instanceof ConnPoolControl) {
43 interfaces = new Class<?>[]{
44 HttpClientConnectionManager.class,
45 ConnPoolControl.class,
46 Wrapped.class
47 };
48 } else {
49 interfaces = new Class<?>[]{
50 HttpClientConnectionManager.class,
51 Wrapped.class
52 };
53 }
54 return (HttpClientConnectionManager) Proxy.newProxyInstance(
55
56 ClientConnectionManagerFactory.class.getClassLoader(),
57 interfaces,
58 new Handler(orig));
59 }
60
61
66 private static class Handler implements InvocationHandler {
67 private final HttpClientConnectionManager orig;
68
69 Handler(HttpClientConnectionManager real) {
70 this.orig = real;
71 }
72
73 @Override
74 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
75 try {
76 Object ret = method.invoke(orig, args);
77 return ret instanceof ConnectionRequest
78 ? ClientConnectionRequestFactory.wrap((ConnectionRequest) ret)
79 : ret
80 ;
81 } catch (InvocationTargetException e) {
82 log.debug("", e);
83 throw e.getCause();
84 }
85 }
86 }
87 }
88