| 1 |
package com.github.markusbernhardt.proxy.selector.misc; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.net.Proxy; |
| 5 |
import java.net.ProxySelector; |
| 6 |
import java.net.SocketAddress; |
| 7 |
import java.net.URI; |
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Collections; |
| 10 |
import java.util.Iterator; |
| 11 |
import java.util.List; |
| 12 |
import java.util.Map.Entry; |
| 13 |
import java.util.Set; |
| 14 |
import java.util.concurrent.ConcurrentHashMap; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class BufferedProxySelector extends ProxySelector { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public enum CacheScope { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
CACHE_SCOPE_HOST, |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
CACHE_SCOPE_HOST_PORT, |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
CACHE_SCOPE_URL |
| 51 |
} |
| 52 |
|
| 53 |
private ProxySelector delegate; |
| 54 |
|
| 55 |
private ConcurrentHashMap<String, CacheEntry> cache; |
| 56 |
private int maxSize; |
| 57 |
private long ttl; |
| 58 |
private CacheScope cacheScope; |
| 59 |
|
| 60 |
private static class CacheEntry { |
| 61 |
List<Proxy> result; |
| 62 |
long expireAt; |
| 63 |
|
| 64 |
public CacheEntry(List<Proxy> r, long expireAt) { |
| 65 |
super(); |
| 66 |
this.result = new ArrayList<Proxy>(r.size()); |
| 67 |
this.result.addAll(r); |
| 68 |
this.result = Collections.unmodifiableList(this.result); |
| 69 |
this.expireAt = expireAt; |
| 70 |
} |
| 71 |
|
| 72 |
public boolean isExpired() { |
| 73 |
return System.nanoTime() >= this.expireAt; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public BufferedProxySelector(int maxSize, long ttl, ProxySelector delegate, CacheScope cacheScope) { |
| 92 |
super(); |
| 93 |
this.cache = new ConcurrentHashMap<String, CacheEntry>(); |
| 94 |
this.maxSize = maxSize; |
| 95 |
this.delegate = delegate; |
| 96 |
this.ttl = ttl; |
| 97 |
this.cacheScope = cacheScope; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@Override |
| 108 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
| 109 |
this.delegate.connectFailed(uri, sa, ioe); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
@Override |
| 119 |
public List<Proxy> select(URI uri) { |
| 120 |
String cacheKey; |
| 121 |
switch (cacheScope) { |
| 122 |
case CACHE_SCOPE_HOST: |
| 123 |
cacheKey = uri.getHost(); |
| 124 |
break; |
| 125 |
case CACHE_SCOPE_HOST_PORT: |
| 126 |
cacheKey = uri.getHost() + ":" + uri.getPort(); |
| 127 |
break; |
| 128 |
case CACHE_SCOPE_URL: |
| 129 |
cacheKey = uri.toString(); |
| 130 |
break; |
| 131 |
default: |
| 132 |
throw new RuntimeException("FixMe: Unhandled CacheScope enum constant."); |
| 133 |
} |
| 134 |
|
| 135 |
CacheEntry entry = this.cache.get(cacheKey); |
| 136 |
if (entry == null || entry.isExpired()) { |
| 137 |
List<Proxy> result = this.delegate.select(uri); |
| 138 |
entry = new CacheEntry(result, System.nanoTime() + this.ttl * 1000 * 1000); |
| 139 |
|
| 140 |
synchronized (this.cache) { |
| 141 |
if (this.cache.size() >= this.maxSize) { |
| 142 |
purgeCache(); |
| 143 |
} |
| 144 |
this.cache.put(cacheKey, entry); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return entry.result; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
private void purgeCache() { |
| 156 |
|
| 157 |
|
| 158 |
boolean removedOne = false; |
| 159 |
Entry<String, CacheEntry> oldest = null; |
| 160 |
|
| 161 |
Set<Entry<String, CacheEntry>> entries = this.cache.entrySet(); |
| 162 |
for (Iterator<Entry<String, CacheEntry>> it = entries.iterator(); it.hasNext();) { |
| 163 |
Entry<String, CacheEntry> entry = it.next(); |
| 164 |
if (entry.getValue().isExpired()) { |
| 165 |
it.remove(); |
| 166 |
removedOne = true; |
| 167 |
} else if (oldest == null || entry.getValue().expireAt < oldest.getValue().expireAt) { |
| 168 |
oldest = entry; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
if (!removedOne && oldest != null) { |
| 174 |
this.cache.remove(oldest.getKey()); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
} |