Re-using the Eclipse proxy preference settings in your Eclipse RCP application

The following demonstrates how to re-use the Eclipse proxy preferences in an Eclipse RCP application.

Create a project “” and select the “RCP application with a view” as template.

Add the preferences command to your application. See Eclipse Preferences Tutorial for details.

Add the plugin “” and “” as dependency to your plugin.

Run your plugin and open preferences.


Fantastic! You already have the preference dialog.

Now change your view to the following.



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.osgi.framework.FrameworkUtil;
import org.osgi.util.tracker.ServiceTracker;

public class View extends ViewPart {
	public static final String ID = "";
	private final ServiceTracker proxyTracker;

	public View() {
		proxyTracker = new ServiceTracker(FrameworkUtil.getBundle(
				this.getClass()).getBundleContext(), IProxyService.class
				.getName(), null);;

	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	public void createPartControl(Composite parent) {
		StyledText text = new StyledText(parent, SWT.NONE);

	 * Passing the focus request to the viewer's control.
	public void setFocus() {

	private String readWebpage() {
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer();

		try {
			URI uri = new URI("");
			IProxyService proxyService = getProxyService();
			IProxyData[] proxyDataForHost =;

			for (IProxyData data : proxyDataForHost) {
				if (data.getHost() != null) {
					System.setProperty("http.proxySet", "true");
					System.setProperty("http.proxyHost", data.getHost());
				if (data.getHost() != null) {
					System.setProperty("http.proxyPort", String.valueOf(data
			// Close the service and close the service tracker
			proxyService = null;

			URL url;

			url = uri.toURL();

			in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				// Process each line.
				sb.append(inputLine + "\n");

		} catch (MalformedURLException e) {
		} catch (IOException e) {
		} catch (URISyntaxException e) {
		} finally {
			if (in != null) {
				try {
				} catch (IOException e) {

		return sb.toString();


	public IProxyService getProxyService() {
		return (IProxyService) proxyTracker.getService();

	public void dispose() {


If you run your application behind a firewall and if you have maintained the proxy correctly then the View should display the HTML code.

Hope this helps!

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, With more than one million visitors per month his website is one of the central sources for Eclipse and Android programming information.
This entry was posted in Eclipse and tagged , , , . Bookmark the permalink.

5 Responses to Re-using the Eclipse proxy preference settings in your Eclipse RCP application

  1. George Pólya says:

    In getProxyService() you open a tracker without closing it. Everytime you call getProxyService(), this adds a new service listener to the bundle context without removing it.

    Also, as far as I know, the line
    if (data.getType() == IProxyData.HTTP_PROXY_TYPE) {
    is not necessary. returns only ProxyData that match the URI, i.e. in this case they all have the type HTTP_PROXY_TYPE. I usually just pick the first element of the array (if there is one), but I also have never seen more than one proxy data returned by select.
    And as a bonus, if this line was removed, the code would also work for URIs with other protocols like HTTPS.

  2. Lars Vogel says:

    @George: Thanks a lot for the feedback. I tried to adjust the code according to your suggestions. I now use

    / Close the service and close the service tracker

    and removed the check for IProxyData.HTTP_PROXY_TYPE.

    Thanks a lot.

  3. Philipp Kursawe says:

    Lars, I always like reading your site/blog! Good work!
    A little tip for the use of ServiceTracker. Just create it as a final class field. Then you can call getService() as often as you want and just close the tracker in the views dispose() method.
    Although if your bundle is stopped, the service tracker will be automatically unbound.

  4. Lars Vogel says:

    @Philipp: Sorry for taking so long to response. I adjusted my code example accoording your advice. Thank you very much for the tip.

  5. Wolfgang says:

    If you have taht .net plugins in your configuration the system properties are already set right as you can verify in the about dialogs configuration tab:


    Unless you do not use libraries that explicitly require proxy configuration do not modify those system properties. If you have to set system properties distinguish http and https protocolls.

Comments are closed.