1 11 package org.eclipse.core.internal.net; 12 13 import java.net.Authenticator ; 14 import java.util.*; 15 16 import org.eclipse.core.net.proxy.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 19 import org.eclipse.core.runtime.preferences.InstanceScope; 20 import org.eclipse.core.runtime.preferences.IEclipsePreferences.*; 21 import org.eclipse.osgi.util.NLS; 22 import org.osgi.service.prefs.BackingStoreException; 23 import org.osgi.service.prefs.Preferences; 24 25 public class ProxyManager implements IProxyService, IPreferenceChangeListener { 26 27 private static final String PREF_HAS_MIGRATED = "org.eclipse.core.net.hasMigrated"; 29 32 private static String HTTP_PROXY_HOST = "org.eclipse.update.core.proxy.host"; private static String HTTP_PROXY_PORT = "org.eclipse.update.core.proxy.port"; private static String HTTP_PROXY_ENABLE = "org.eclipse.update.core.proxy.enable"; 36 private static final String PREF_NON_PROXIED_HOSTS = "nonProxiedHosts"; private static final String PREF_ENABLED = "proxiesEnabled"; 39 private static IProxyService proxyManager; 40 41 ListenerList listeners = new ListenerList(ListenerList.IDENTITY); 42 private String [] nonProxiedHosts; 43 private final ProxyType[] proxies = new ProxyType[] { 44 new ProxyType(IProxyData.HTTP_PROXY_TYPE), 45 new ProxyType(IProxyData.HTTPS_PROXY_TYPE), 46 new ProxyType(IProxyData.SOCKS_PROXY_TYPE) 47 }; 48 49 53 public synchronized static IProxyService getProxyManager() { 54 if (proxyManager == null) 55 proxyManager = new ProxyManager(); 56 return proxyManager; 57 } 58 59 62 public void addProxyChangeListener(IProxyChangeListener listener) { 63 listeners.add(listener); 64 } 65 66 69 public void removeProxyChangeListener(IProxyChangeListener listener) { 70 listeners.remove(listener); 71 } 72 73 private void fireChange(final IProxyChangeEvent event) { 74 Object [] l = listeners.getListeners(); 75 for (int i = 0; i < l.length; i++) { 76 final IProxyChangeListener listener = (IProxyChangeListener)l[i]; 77 SafeRunner.run(new ISafeRunnable() { 78 public void run() throws Exception { 79 listener.proxyInfoChanged(event); 80 } 81 public void handleException(Throwable exception) { 82 } 84 }); 85 } 86 } 87 88 91 public synchronized String [] getNonProxiedHosts() { 92 if (nonProxiedHosts == null) { 93 String prop = Activator.getInstance().getInstancePreferences().get(PREF_NON_PROXIED_HOSTS, "localhost|127.0.0.1"); nonProxiedHosts = ProxyType.convertPropertyStringToHosts(prop); 95 } 96 if (nonProxiedHosts.length == 0) 97 return nonProxiedHosts; 98 String [] result = new String [nonProxiedHosts.length]; 99 System.arraycopy(nonProxiedHosts, 0, result, 0, nonProxiedHosts.length ); 100 return result; 101 } 102 103 106 public void setNonProxiedHosts(String [] hosts) { 107 Assert.isNotNull(hosts); 108 for (int i = 0; i < hosts.length; i++) { 109 String host = hosts[i]; 110 Assert.isNotNull(host); 111 Assert.isTrue(host.length() > 0); 112 } 113 String [] oldHosts = nonProxiedHosts; 114 nonProxiedHosts = hosts; 115 Activator.getInstance().getInstancePreferences().put(PREF_NON_PROXIED_HOSTS, ProxyType.convertHostsToPropertyString(nonProxiedHosts)); 116 try { 117 Activator.getInstance().getInstancePreferences().flush(); 118 } catch (BackingStoreException e) { 119 Activator.logError( 120 "An error occurred while writing out the non-proxied hosts list", e); } 122 IProxyData[] data = getProxyData(); 123 IProxyChangeEvent event = new ProxyChangeEvent(IProxyChangeEvent.NONPROXIED_HOSTS_CHANGED, oldHosts, getNonProxiedHosts(), data, new IProxyData[0]); 124 fireChange(event); 125 } 126 127 128 public IProxyData[] getProxyData() { 129 IProxyData[] result = new IProxyData[proxies.length]; 130 for (int i = 0; i < proxies.length; i++) { 131 ProxyType type = proxies[i]; 132 result[i] = type.getProxyData(ProxyType.VERIFY_EQUAL); 133 } 134 return result; 135 } 136 137 public void setProxyData(IProxyData[] proxies) { 138 doSetProxyData(proxies); 139 } 140 141 private void doSetProxyData(IProxyData[] proxyDatas) { 142 IProxyData[] oldData = getProxyData(); 143 String [] hosts = getNonProxiedHosts(); 144 IProxyData[] changedProxies = internalSetProxyData(proxyDatas); 145 if (changedProxies.length > 0) { 146 IProxyChangeEvent event = new ProxyChangeEvent(IProxyChangeEvent.PROXY_SERVICE_ENABLEMENT_CHANGE, hosts, hosts, oldData, changedProxies); 147 fireChange(event); 148 } 149 } 150 151 private IProxyData[] internalSetProxyData(IProxyData[] proxyDatas) { 152 List result = new ArrayList(); 153 for (int i = 0; i < proxyDatas.length; i++) { 154 IProxyData proxyData = proxyDatas[i]; 155 ProxyType type = getType(proxyData); 156 if (type != null && type.setProxyData(proxyData, isProxiesEnabled())) { 157 result.add(proxyData); 158 } 159 } 160 return (IProxyData[]) result.toArray(new IProxyData[result.size()]); 161 } 162 163 private ProxyType getType(IProxyData proxyData) { 164 for (int i = 0; i < proxies.length; i++) { 165 ProxyType type = proxies[i]; 166 if (type.getName().equals(proxyData.getType())) { 167 return type; 168 } 169 } 170 return null; 171 } 172 173 176 public boolean isProxiesEnabled() { 177 return Activator.getInstance().getInstancePreferences().getBoolean(PREF_ENABLED, false); 178 } 179 180 183 public void setProxiesEnabled(boolean enabled) { 184 boolean current = isProxiesEnabled(); 185 if (current == enabled) 186 return; 187 Activator.getInstance().getInstancePreferences().putBoolean(PREF_ENABLED, enabled); 190 } 191 192 private void internalSetEnabled(boolean enabled) { 193 Properties sysProps = System.getProperties(); 194 sysProps.put("proxySet", enabled ? "true" : "false"); updateSystemProperties(); 196 try { 197 Activator.getInstance().getInstancePreferences().flush(); 198 } catch (BackingStoreException e) { 199 Activator.logError( 200 "An error occurred while writing out the enablement state", e); } 202 String [] hosts = getNonProxiedHosts(); 203 IProxyData[] data = getProxyData(); 204 IProxyChangeEvent event = new ProxyChangeEvent(IProxyChangeEvent.PROXY_DATA_CHANGED, hosts, hosts, data, data); 205 fireChange(event); 206 } 207 208 private void updateSystemProperties() { 209 for (int i = 0; i < proxies.length; i++) { 210 ProxyType type = proxies[i]; 211 type.updateSystemProperties(internalGetProxyData(type.getName(), ProxyType.DO_NOT_VERIFY), isProxiesEnabled()); 212 } 213 } 214 215 public void initialize() { 216 migrateUpdateHttpProxy(new InstanceScope().getNode(""), true); ((IEclipsePreferences)Activator.getInstance().getInstancePreferences()).addPreferenceChangeListener(this); 219 for (int i = 0; i < proxies.length; i++) { 221 ProxyType type = proxies[i]; 222 type.initialize(isProxiesEnabled()); 223 } 224 registerAuthenticator(); 225 } 226 227 public IProxyData getProxyData(String type) { 228 return internalGetProxyData(type, ProxyType.VERIFY_EQUAL); 229 } 230 231 private IProxyData internalGetProxyData(String type, int verifySystemProperties) { 232 for (int i = 0; i < proxies.length; i++) { 233 ProxyType pt = proxies[i]; 234 if (pt.getName().equals(type)) { 235 return pt.getProxyData(verifySystemProperties); 236 } 237 } 238 return null; 239 } 240 241 public IProxyData[] getProxyDataForHost(String host) { 242 if (isHostFiltered(host)) 243 return new IProxyData[0]; 244 IProxyData[] data = getProxyData(); 245 List result = new ArrayList(); 246 for (int i = 0; i < data.length; i++) { 247 IProxyData proxyData = data[i]; 248 if (proxyData.getHost() != null) 249 result.add(proxyData); 250 } 251 return (IProxyData[]) result.toArray(new IProxyData[result.size()]); 252 } 253 254 private boolean isHostFiltered(String host) { 255 String [] filters = getNonProxiedHosts(); 256 for (int i = 0; i < filters.length; i++) { 257 String filter = filters[i]; 258 if (matchesFilter(host, filter)) 259 return true; 260 } 261 return false; 262 } 263 264 private boolean matchesFilter(String host, String filter) { 265 StringMatcher matcher = new StringMatcher(filter, false, false); 266 return matcher.match(host); 267 } 268 269 272 public IProxyData getProxyDataForHost(String host, String type) { 273 IProxyData[] data = getProxyDataForHost(host); 274 for (int i = 0; i < data.length; i++) { 275 IProxyData proxyData = data[i]; 276 if (proxyData.getType().equals(type) && proxyData.getHost() != null) 277 return proxyData; 278 } 279 return null; 280 } 281 282 private void registerAuthenticator() { 283 Authenticator a = getPluggedInAuthenticator(); 284 if (a != null) { 285 Authenticator.setDefault(a); 286 } 287 } 288 289 private Authenticator getPluggedInAuthenticator() { 290 IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint(Activator.ID, Activator.PT_AUTHENTICATOR).getExtensions(); 291 if (extensions.length == 0) 292 return null; 293 IExtension extension = extensions[0]; 294 IConfigurationElement[] configs = extension.getConfigurationElements(); 295 if (configs.length == 0) { 296 Activator.log(IStatus.ERROR, NLS.bind("Authenticator {0} is missing required fields", (new Object [] {extension.getUniqueIdentifier()})), null); return null; 298 } 299 try { 300 IConfigurationElement config = configs[0]; 301 return (Authenticator ) config.createExecutableExtension("class"); } catch (CoreException ex) { 303 Activator.log(IStatus.ERROR, NLS.bind("Unable to instantiate authenticator {0}", (new Object [] {extension.getUniqueIdentifier()})), ex); return null; 305 } 306 } 307 308 void migrateUpdateHttpProxy(Preferences node, boolean isInitialize) { 309 Preferences netPrefs = node.node(Activator.ID); 310 if (!netPrefs.getBoolean(PREF_HAS_MIGRATED, false)) { 311 if (isInitialize) 313 netPrefs.putBoolean(PREF_HAS_MIGRATED, true); 314 Preferences updatePrefs = node.node("org.eclipse.update.core"); String httpProxyHost = getHostToMigrate(updatePrefs, isInitialize ); 316 int port = getPortToMigrate(updatePrefs, isInitialize ); 317 boolean httpProxyEnable = getEnablementToMigrate(updatePrefs, isInitialize ); 318 if (httpProxyHost != null) { 319 ProxyData proxyData = new ProxyData(IProxyData.HTTP_PROXY_TYPE, httpProxyHost, port, false); 320 ProxyType type = getType(proxyData); 321 type.updatePreferencesIfMissing(netPrefs, proxyData); 322 if (httpProxyEnable) { 323 netPrefs.putBoolean(ProxyManager.PREF_ENABLED, true); 324 } 325 } 326 } 327 } 328 329 private boolean getEnablementToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { 330 boolean httpProxyEnable = false; 331 if (checkSystemProperties && updatePrefs.get(HTTP_PROXY_ENABLE, null) == null) { 332 httpProxyEnable = Boolean.getBoolean("http.proxySet"); } else { 334 httpProxyEnable = updatePrefs.getBoolean(HTTP_PROXY_ENABLE, false); 335 updatePrefs.remove(HTTP_PROXY_ENABLE); 336 } 337 return httpProxyEnable; 338 } 339 340 private int getPortToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { 341 String httpProxyPort = updatePrefs.get(HTTP_PROXY_PORT, ""); if (checkSystemProperties && "".equals(httpProxyPort)) { httpProxyPort = System.getProperty("http.proxyPort", ""); } 345 updatePrefs.remove(HTTP_PROXY_PORT); 346 int port = -1; 347 if (httpProxyPort != null && !"".equals(httpProxyPort)) try { 349 port = Integer.parseInt(httpProxyPort); 350 } catch (NumberFormatException e) { 351 } 353 return port; 354 } 355 356 private String getHostToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { 357 String httpProxyHost = updatePrefs.get(HTTP_PROXY_HOST, ""); if (checkSystemProperties && "".equals(httpProxyHost)) { httpProxyHost = System.getProperty("http.proxyHost", ""); } 361 if ("".equals(httpProxyHost)) httpProxyHost = null; 363 updatePrefs.remove(HTTP_PROXY_HOST); 364 return httpProxyHost; 365 } 366 367 public void preferenceChange(PreferenceChangeEvent event) { 368 if (event.getKey().equals(PREF_ENABLED)) { 369 internalSetEnabled(Activator.getInstance().getInstancePreferences().getBoolean(PREF_ENABLED, false)); 370 } 371 } 372 373 } 374 | Popular Tags |