]> gerrit.simantics Code Review - simantics/district.git/blobdiff - 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
diff --git a/org.simantics.district.maps/src/org/simantics/maps/eclipse/EclipseProxyUtil.java b/org.simantics.district.maps/src/org/simantics/maps/eclipse/EclipseProxyUtil.java
new file mode 100644 (file)
index 0000000..601a82f
--- /dev/null
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * 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:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.maps.eclipse;
+
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+
+import org.eclipse.core.net.proxy.IProxyData;
+import org.eclipse.core.net.proxy.IProxyService;
+import org.eclipse.core.runtime.Platform;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.simantics.maps.IProxyUtil;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class EclipseProxyUtil implements IProxyUtil {
+
+    /**
+     * @return IProxyService of the platform or <code>null</code> if the
+     *         service is not available.
+     */
+    public Proxy getProxyService(final String url) {
+        Bundle b = Platform.getBundle("org.eclipse.core.net");
+        if (b == null)
+            return null;
+
+        BundleContext bc = b.getBundleContext();
+        ServiceReference<IProxyService> ref = bc.getServiceReference(IProxyService.class);
+        if (ref == null)
+            return null;
+
+        IProxyService service = bc.getService(ref);
+        if (service != null && service.isProxiesEnabled()) {
+            IProxyData proxyData = service.getProxyData(IProxyData.HTTP_PROXY_TYPE);
+            if (proxyData != null && proxyData.getHost() != null) {
+                return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyData.getHost(), proxyData.getPort()));
+            }
+        }
+        return null;
+    }
+
+}