summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/selector/misc/ProtocolDispatchSelector.java
blob: bb574524b38be1c0ae5dd8d6205c56915e18951b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.btr.proxy.selector.misc;

import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.btr.proxy.selector.direct.NoProxySelector;
import com.btr.proxy.selector.fixed.FixedProxySelector;
import com.btr.proxy.util.Logger;
import com.btr.proxy.util.Logger.LogLevel;

/*****************************************************************************
 * This is a facade for a list of ProxySelecor objects. You can register 
 * different ProxySelectors per Protocol.
 * 
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class ProtocolDispatchSelector extends ProxySelector {
	
	private Map<String, ProxySelector> selectors;
	private ProxySelector fallbackSelector;
	
	/*************************************************************************
	 * Constructor
	 ************************************************************************/
	
	public ProtocolDispatchSelector() {
		super();
		this.selectors = new ConcurrentHashMap<String, ProxySelector>();
		this.fallbackSelector = NoProxySelector.getInstance();
	}
	
	/*************************************************************************
	 * Sets a selector responsible for the given protocol.
	 * @param protocol the name of the protocol.
	 * @param selector the selector to use.
	 ************************************************************************/
	
	public void setSelector(String protocol, ProxySelector selector) {
		if (protocol == null) {
			throw new NullPointerException("Protocol must not be null.");
		}
		if (selector == null) {
			throw new NullPointerException("Selector must not be null.");
		}
		if (protocol.toLowerCase().startsWith("socks")) {
			protocol = "socket";
		}
		this.selectors.put(protocol, selector);
	}
	
	/*************************************************************************
	 * Removes the selector installed for the given protocol.
	 * @param protocol the protocol name.
	 * @return the old selector that is removed.
	 ************************************************************************/
	
	public ProxySelector removeSelector(String protocol) {
		return this.selectors.remove(protocol);
	}
	
	/*************************************************************************
	 * Gets the selector installed for the given protocol.
	 * @param protocol the protocol name.
	 * @return the selector for that protocol, null if none is currently set.
	 ************************************************************************/
	
	public ProxySelector getSelector(String protocol) {
		return this.selectors.get(protocol);
	}
	
	/*************************************************************************
	 * Sets the fallback selector that is always called when no matching 
	 * protocol selector was found..
	 * @param selector the selector to use.
	 ************************************************************************/
	
	public void setFallbackSelector(ProxySelector selector) {
		if (selector == null) {
			throw new NullPointerException("Selector must not be null.");
		}
		this.fallbackSelector = selector;
	}
	
	private ProxySelector selectorForUri(URI uri) {
		if (uri == null || uri.getScheme() == null)
			return this.fallbackSelector;
		String protocol = uri.getScheme();
		ProxySelector selector = this.selectors.get(protocol);
		if (selector == null) {
			selector = this.fallbackSelector;
		}
		return selector;
	}
	
	/*************************************************************************
	 * connectFailed
	 * @see java.net.ProxySelector#connectFailed(java.net.URI, java.net.SocketAddress, java.io.IOException)
	 ************************************************************************/

	@Override
	public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
		ProxySelector selector = selectorForUri(uri);
		selector.connectFailed(uri, sa, ioe);
	}

	/*************************************************************************
	 * select
	 * @see java.net.ProxySelector#select(java.net.URI)
	 ************************************************************************/

	@Override
	public List<Proxy> select(URI uri) {
		ProxySelector selector = selectorForUri(uri);
		List<Proxy> ret = selector.select(uri);
		Logger.log(getClass(), LogLevel.TRACE, "Selector {0} for {1} -> {2}", selector.getClass(), uri, ret);
		return ret;
	}

	public void setFallbackSocksSelector(FixedProxySelector... pslist) {
		if (!(this.fallbackSelector instanceof NoProxySelector))
			return;
		for (FixedProxySelector ps : pslist) {
			if (ps != null && ps.getType() == Proxy.Type.SOCKS) {
				this.fallbackSelector = ps;
				return;
			}
		}
	}

	public boolean isEmpty() {
		return this.selectors.isEmpty() && this.fallbackSelector instanceof NoProxySelector;
	}

}