Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/selector/misc/BufferedProxySelector.java

raw symbols SCIP
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 * Implements a cache that can be used to warp it around an existing
18 * ProxySelector. You can specify a maximum cache size and a "time to live" for
19 * positive resolves.
20 *
21 * @author Markus Bernhardt, Copyright 2016
22 * @author Bernd Rosstauscher, Copyright 2009
23 ****************************************************************************/
24
25 public class BufferedProxySelector extends ProxySelector {
26
27 /*************************************************************************
28 * Define the available scopes of the cache key generation
29 ************************************************************************/
30
31 public enum CacheScope {
32
33 /*********************************************************************
34 * Cache keys are generated by <code>uri.getHost()</code>.
35 ********************************************************************/
36
37 CACHE_SCOPE_HOST,
38
39 /*********************************************************************
40 * Cache keys are generated by
41 * <code>uri.getHost() + &quot;:&quot; + uri.getPort()</code>.
42 ********************************************************************/
43
44 CACHE_SCOPE_HOST_PORT,
45
46 /*********************************************************************
47 * Cache keys are generated by <code>uri.toString()</code>.
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 * Constructor
79 *
80 * @param maxSize
81 * the max size for the cache.
82 * @param ttl
83 * the "time to live" for cache entries as amount in
84 * milliseconds.
85 * @param delegate
86 * the delegate to use.
87 * @param cacheScope
88 * the desired cache scope.
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 * connectFailed
102 *
103 * @see java.net.ProxySelector#connectFailed(java.net.URI,
104 * java.net.SocketAddress, java.io.IOException)
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 * select
114 *
115 * @see java.net.ProxySelector#select(java.net.URI)
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 * Purge cache to get some free space for a new entry.
153 ************************************************************************/
154
155 private void purgeCache() {
156
157 // Remove all expired entries and find the oldest.
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 // Remove oldest if no expired entries were found.
173 if (!removedOne && oldest != null) {
174 this.cache.remove(oldest.getKey());
175 }
176 }
177
178 }