summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/config/ConfigProxy.java
blob: a97368d69b078c44c64edf8e7aec3d2a631800e1 (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
package config;

import java.io.IOException;
import java.net.ProxySelector;
import java.text.MessageFormat;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.btr.proxy.search.ProxySearch;
import com.btr.proxy.search.wpad.WpadProxySearchStrategy;
import com.btr.proxy.util.ProxyException;
import com.btr.proxy.util.Logger.LogBackEnd;
import com.btr.proxy.util.Logger.LogLevel;

/**
 * Configures the proxy
 * 
 * @author Jonathan Bauer
 */

public class ConfigProxy {

	/**
	 * Logger for this class
	 */
	private final static Logger LOGGER = Logger.getLogger(ConfigProxy.class);

	/**
	 * Initialization method.
	 */
	public static void init() throws IOException {
		// first setup the logger of proxy_vole
		com.btr.proxy.util.Logger.setBackend(new LogBackEnd() {

			@SuppressWarnings("deprecation")
			public void log(Class<?> clazz, LogLevel loglevel, String msg,
					Object... params) {
				Priority priority;
				switch (loglevel) {
				case ERROR:
					priority = Level.ERROR;
					break;
				case WARNING:
					priority = Priority.WARN;
					break;
				case INFO:
					priority = Priority.INFO;
					break;
				default:
					priority = Priority.DEBUG;
				}
				Logger.getLogger(clazz).log(priority, MessageFormat.format(msg, params));
			}

			public boolean isLogginEnabled(LogLevel logLevel) {
				return true;
			}
		});

		// try to find local proxy settings
		ProxySearch proxySearch = ProxySearch.getDefaultProxySearch();
		ProxySelector myProxySelector = proxySearch.getProxySelector();

		if (myProxySelector == null) {
			// didn't work, try WPAD detection
			LOGGER.error("No suitable proxy settings found, trying WPAD...");
			WpadProxySearchStrategy ss = new WpadProxySearchStrategy();
			try {
				myProxySelector = ss.getProxySelector();
			} catch (ProxyException e) {
				LOGGER.error("WPAD proxy error, see trace: ", e);
			}
		}
		// final check to see if WPAD actually worked
		if (myProxySelector != null) {
			LOGGER.debug("Proxy initialised.");
			ProxySelector.setDefault(myProxySelector);
		} else {
			LOGGER.error("Could not find a suitable proxy!");
		}
	}
}