]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/eclipse/EclipseProxyUtil.java
Share some projects for Simantics District
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / eclipse / EclipseProxyUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.maps.eclipse;
13
14 import java.net.InetSocketAddress;
15 import java.net.Proxy;
16
17 import org.eclipse.core.net.proxy.IProxyData;
18 import org.eclipse.core.net.proxy.IProxyService;
19 import org.eclipse.core.runtime.Platform;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceReference;
23 import org.simantics.maps.IProxyUtil;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public class EclipseProxyUtil implements IProxyUtil {
29
30     /**
31      * @return IProxyService of the platform or <code>null</code> if the
32      *         service is not available.
33      */
34     public Proxy getProxyService(final String url) {
35         Bundle b = Platform.getBundle("org.eclipse.core.net");
36         if (b == null)
37             return null;
38
39         BundleContext bc = b.getBundleContext();
40         ServiceReference<IProxyService> ref = bc.getServiceReference(IProxyService.class);
41         if (ref == null)
42             return null;
43
44         IProxyService service = bc.getService(ref);
45         if (service != null && service.isProxiesEnabled()) {
46             IProxyData proxyData = service.getProxyData(IProxyData.HTTP_PROXY_TYPE);
47             if (proxyData != null && proxyData.getHost() != null) {
48                 return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyData.getHost(), proxyData.getPort()));
49             }
50         }
51         return null;
52     }
53
54 }