Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/selector/misc/ProtocolDispatchSelector.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.List;
9 import java.util.Map;
10 import java.util.concurrent.ConcurrentHashMap;
11
12 import com.github.markusbernhardt.proxy.selector.direct.NoProxySelector;
13
14 /*****************************************************************************
15 * This is a facade for a list of ProxySelecor objects. You can register
16 * different ProxySelectors per Protocol.
17 *
18 * @author Markus Bernhardt, Copyright 2016
19 * @author Bernd Rosstauscher, Copyright 2009
20 ****************************************************************************/
21
22 public class ProtocolDispatchSelector extends ProxySelector {
23
24 private Map<String, ProxySelector> selectors;
25 private ProxySelector fallbackSelector;
26
27 /*************************************************************************
28 * Constructor
29 ************************************************************************/
30
31 public ProtocolDispatchSelector() {
32 super();
33 this.selectors = new ConcurrentHashMap<String, ProxySelector>();
34 this.fallbackSelector = NoProxySelector.getInstance();
35 }
36
37 /*************************************************************************
38 * Sets a selector responsible for the given protocol.
39 *
40 * @param protocol
41 * the name of the protocol.
42 * @param selector
43 * the selector to use.
44 ************************************************************************/
45
46 public void setSelector(String protocol, ProxySelector selector) {
47 if (protocol == null) {
48 throw new NullPointerException("Protocol must not be null.");
49 }
50 if (selector == null) {
51 throw new NullPointerException("Selector must not be null.");
52 }
53 this.selectors.put(protocol, selector);
54 }
55
56 /*************************************************************************
57 * Removes the selector installed for the given protocol.
58 *
59 * @param protocol
60 * the protocol name.
61 * @return the old selector that is removed.
62 ************************************************************************/
63
64 public ProxySelector removeSelector(String protocol) {
65 return this.selectors.remove(protocol);
66 }
67
68 /*************************************************************************
69 * Gets the selector installed for the given protocol.
70 *
71 * @param protocol
72 * the protocol name.
73 * @return the selector for that protocol, null if none is currently set.
74 ************************************************************************/
75
76 public ProxySelector getSelector(String protocol) {
77 return this.selectors.get(protocol);
78 }
79
80 /*************************************************************************
81 * Sets the fallback selector that is always called when no matching
82 * protocol selector was found..
83 *
84 * @param selector
85 * the selector to use.
86 ************************************************************************/
87
88 public void setFallbackSelector(ProxySelector selector) {
89 if (selector == null) {
90 throw new NullPointerException("Selector must not be null.");
91 }
92 this.fallbackSelector = selector;
93 }
94
95 /*************************************************************************
96 * connectFailed
97 *
98 * @see java.net.ProxySelector#connectFailed(java.net.URI,
99 * java.net.SocketAddress, java.io.IOException)
100 ************************************************************************/
101
102 @Override
103 public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
104 ProxySelector selector = this.fallbackSelector;
105 String protocol = uri.getScheme();
106 if (protocol != null && this.selectors.get(protocol) != null) {
107 selector = this.selectors.get(protocol);
108 }
109 selector.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 ProxySelector selector = this.fallbackSelector;
121 String protocol = uri.getScheme();
122 if (protocol != null && this.selectors.get(protocol) != null) {
123 selector = this.selectors.get(protocol);
124 }
125 return selector.select(uri);
126 }
127
128 /*************************************************************************
129 * Gets the size of the selector map.
130 *
131 * @return the size of the selector map.
132 ************************************************************************/
133
134 public int size() {
135 return this.selectors.size();
136 }
137
138 }