1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.client.webdav;
17
18 import org.apache.commons.httpclient.Credentials;
19 import org.apache.commons.httpclient.HostConfiguration;
20 import org.apache.commons.httpclient.HttpClient;
21 import org.apache.commons.httpclient.HttpState;
22 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
23 import org.apache.commons.httpclient.UsernamePasswordCredentials;
24 import org.apache.commons.httpclient.protocol.Protocol;
25 import org.apache.commons.pool.KeyedObjectPool;
26 import org.apache.commons.pool.KeyedPoolableObjectFactory;
27 import org.apache.commons.pool.impl.StackKeyedObjectPool;
28
29 /**
30 * Pool for storing HttpClient instances.
31 */
32 public class HttpClientPool {
33
34 public static KeyedObjectPool getPool() {
35 return pool;
36 }
37
38
39
40 private static KeyedObjectPool pool = new StackKeyedObjectPool(new HttpClientFactory());
41
42 private static class HttpClientFactory implements KeyedPoolableObjectFactory {
43
44 /**
45 * Is called whenever a new instance is needed.
46 * Creates and initializes a HttpClient
47 */
48 public Object makeObject(Object key) {
49 if (key instanceof WebdavConfig) {
50 WebdavConfig config = ((WebdavConfig) key);
51
52 MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
53
54 HostConfiguration hostConfig = new HostConfiguration();
55 Protocol protocol;
56 if (config.getProtocol() == null || config.getProtocol().equals("")) {
57 protocol = Protocol.getProtocol("http");
58 } else {
59 protocol = Protocol.getProtocol(config.getProtocol());
60 }
61 hostConfig.setHost(config.getHost(), config.getPort(), protocol);
62
63 HttpState state = new HttpState();
64 Credentials credentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
65 state.setCredentials(null, config.getHost(), credentials);
66 state.setAuthenticationPreemptive(true);
67
68 HttpClient client = new HttpClient();
69 client.setHttpConnectionManager(connectionManager);
70 client.setHostConfiguration(hostConfig);
71 client.setState(state);
72
73
74
75 return client;
76 } else {
77 throw new IllegalArgumentException("makeObject: Key must be of type WebdavConfig, but is of type "
78 + key.getClass().getName());
79 }
80 }
81
82 /**
83 * Is invoked on every instance before it is returned from the pool.
84 */
85 public void activateObject(Object key, Object obj) {
86
87 }
88
89 /**
90 * Is invoked on every instance when it is being "dropped" from the pool
91 * (whether due to the response from validateObject, or for reasons specific
92 * to the pool implementation.)
93 */
94 public void destroyObject(Object key, Object obj) {
95 obj = null;
96 }
97
98 /**
99 * Ensures that the instance is safe to be returned by the pool.
100 * Returns false if this instance should be destroyed.
101 */
102 public boolean validateObject(Object key, Object obj) {
103
104 return true;
105 }
106
107 /**
108 * Is invoked on every instance when it is returned to the pool.
109 */
110 public void passivateObject(Object arg0, Object arg1) {
111
112 }
113
114 }
115 }