Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f3f34e7de51b55e5fd76b7f574d04345cc302348 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.net;

import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.runtime.CoreException;

/**
 * This class adapts ProxyManager to add additional layer of providers on its
 * top.
 */
public class ProxySelector {

	private static final String DIRECT_PROVIDER = "Direct"; //$NON-NLS-1$
	private static final String ECLIPSE_PROVIDER = "Manual"; //$NON-NLS-1$
	private static final String NATIVE_PROVIDER = "Native"; //$NON-NLS-1$

	public static String[] getProviders() {
		return new String[] { DIRECT_PROVIDER, ECLIPSE_PROVIDER,
				NATIVE_PROVIDER };
	}

	public static String getDefaultProvider() {
		IProxyService service = ProxyManager.getProxyManager();
		if (!service.isProxiesEnabled()) {
			return DIRECT_PROVIDER;
		} else if (service.isProxiesEnabled()
				&& !service.isSystemProxiesEnabled()) {
			return ECLIPSE_PROVIDER;
		}
		return NATIVE_PROVIDER;
	}

	public static void setDefaultProvider(String provider) {
		IProxyService service = ProxyManager.getProxyManager();
		if (provider.equals(DIRECT_PROVIDER)) {
			service.setProxiesEnabled(false);
			service.setProxiesEnabled(false);
		} else if (provider.equals(ECLIPSE_PROVIDER)) {
			service.setProxiesEnabled(true);
			service.setSystemProxiesEnabled(false);
		} else if (provider.equals(NATIVE_PROVIDER)) {
			service.setProxiesEnabled(true);
			service.setSystemProxiesEnabled(true);
		} else {
			throw new IllegalArgumentException("Provider not supported"); //$NON-NLS-1$
		}
	}

	public static ProxyData[] getProxyData(String provider) {
		ProxyManager manager = (ProxyManager) ProxyManager.getProxyManager();
		if (provider.equals(DIRECT_PROVIDER)) {
			return new ProxyData[0];
		} else if (provider.equals(ECLIPSE_PROVIDER)) {
			return castArray(manager.getProxyData());
		} else if (provider.equals(NATIVE_PROVIDER)) {
			return castArray(manager.getNativeProxyData());
		}
		throw new IllegalArgumentException("Provider not supported"); //$NON-NLS-1$
	}

	private static ProxyData[] castArray(IProxyData data[]) {
		ProxyData[] ret = new ProxyData[data.length];
		System.arraycopy(data, 0, ret, 0, data.length);
		return ret;
	}

	public static void setProxyData(String provider, ProxyData proxies[]) {
		if (provider.equals(ECLIPSE_PROVIDER)) {
			IProxyService service = ProxyManager.getProxyManager();
			try {
				service.setProxyData(proxies);
			} catch (CoreException e) {
				// Should never occur since ProxyManager does not
				// declare CoreException to be thrown
				throw new RuntimeException(e);
			}
		} else {
			throw new IllegalArgumentException(
					"Provider does not support setting proxy data"); //$NON-NLS-1$ 
		}
	}

	public static boolean canSetProxyData(String provider) {
		if (provider.equals(ECLIPSE_PROVIDER)) {
			return true;
		}
		return false;
	}

	public static String[] getBypassHosts(String provider) {
		ProxyManager manager = (ProxyManager) ProxyManager.getProxyManager();
		if (provider.equals(DIRECT_PROVIDER)) {
			return new String[0];
		} else if (provider.equals(ECLIPSE_PROVIDER)) {
			return manager.getNonProxiedHosts();
		} else if (provider.equals(NATIVE_PROVIDER)) {
			return manager.getNativeNonProxiedHosts();
		}
		throw new IllegalArgumentException("Provider not supported"); //$NON-NLS-1$ 
	}

	public static void setBypassHosts(String provider, String hosts[]) {
		if (provider.equals(ECLIPSE_PROVIDER)) {
			IProxyService service = ProxyManager.getProxyManager();
			try {
				service.setNonProxiedHosts(hosts);
			} catch (CoreException e) {
				// Should never occur since ProxyManager does not
				// declare CoreException to be thrown
				throw new RuntimeException(e);
			}
		} else {
			throw new IllegalArgumentException(
					"Provider does not support setting bypass hosts"); //$NON-NLS-1$ 
		}
	}

	public static boolean canSetBypassHosts(String provider) {
		if (provider.equals(ECLIPSE_PROVIDER)) {
			return true;
		}
		return false;
	}

}

Back to the top