Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Schindl2015-12-01 14:01:05 +0000
committerTom Schindl2015-12-01 14:01:05 +0000
commit0d12ddbcffa4de56a3c702a1d5bc163dd57518b4 (patch)
tree51f4533b9e091efec44ad5c684cec92883b670de
parentcb4534bdf199253672f43ac148924b1b49bc896b (diff)
downloadorg.eclipse.efxclipse-0d12ddbcffa4de56a3c702a1d5bc163dd57518b4.tar.gz
org.eclipse.efxclipse-0d12ddbcffa4de56a3c702a1d5bc163dd57518b4.tar.xz
org.eclipse.efxclipse-0d12ddbcffa4de56a3c702a1d5bc163dd57518b4.zip
Bug 483378 - Add support to convert eg OSGi-URLs to the local filesystem
-rw-r--r--bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java88
-rw-r--r--bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/URLResolver.java38
-rw-r--r--bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java39
-rwxr-xr-xbundles/runtime/org.eclipse.fx.osgi.util/.project5
-rw-r--r--bundles/runtime/org.eclipse.fx.osgi.util/.settings/ca.ecliptical.pde.ds.prefs5
-rwxr-xr-xbundles/runtime/org.eclipse.fx.osgi.util/META-INF/MANIFEST.MF6
-rw-r--r--bundles/runtime/org.eclipse.fx.osgi.util/OSGI-INF/services/org.eclipse.fx.osgi.util.internal.FileLocatorURLResolver.xml7
-rw-r--r--bundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/internal/FileLocatorURLResolver.java48
8 files changed, 233 insertions, 3 deletions
diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java
new file mode 100644
index 000000000..12e8cc9bf
--- /dev/null
+++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Copyright (c) 2015 BestSolution.at 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:
+ * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.fx.core;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.eclipse.fx.core.log.LoggerCreator;
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * Interface wrapping a path allowing to resource temporary resources
+ *
+ * @since 2.2.0
+ */
+public interface Resource {
+ /**
+ * @return the path
+ */
+ public @NonNull Path getPath();
+
+ /**
+ * Release the resource. The semantics what releaseing a resource means
+ * depends on the wrapped resource eg for a temp-file releaseing could mean
+ * it is deleted
+ *
+ * @return <code>true</code> if releasing was successfull
+ */
+ public boolean release();
+
+ /**
+ * Create a temporary resource
+ *
+ * @param path
+ * the path
+ * @return the resource
+ */
+ public static Resource createTempResource(@NonNull Path path) {
+ return new Resource() {
+
+ @Override
+ public boolean release() {
+ try {
+ return Files.deleteIfExists(path);
+ } catch (IOException e) {
+ LoggerCreator.createLogger(Util.class).error("Unable to delete path '" + path + "'", e); //$NON-NLS-1$//$NON-NLS-2$
+ }
+ return false;
+ }
+
+ @Override
+ public Path getPath() {
+ return path;
+ }
+ };
+ }
+
+ /**
+ * Create a resource who won't be deleted if released
+ *
+ * @param path
+ * the path
+ * @return the resource
+ */
+ public static Resource createResource(@NonNull Path path) {
+ return new Resource() {
+
+ @Override
+ public boolean release() {
+ return true;
+ }
+
+ @Override
+ public Path getPath() {
+ return path;
+ }
+ };
+ }
+} \ No newline at end of file
diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/URLResolver.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/URLResolver.java
new file mode 100644
index 000000000..1c8a26c9c
--- /dev/null
+++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/URLResolver.java
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * Copyright (c) 2015 BestSolution.at 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:
+ * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.fx.core;
+
+import java.net.URL;
+import java.nio.file.Path;
+import java.util.function.Predicate;
+
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * Resolve an URL to a local path
+ */
+public interface URLResolver extends Predicate<@NonNull URL> {
+ /**
+ * Check if the provided URL is supported
+ */
+ @Override
+ boolean test(@NonNull URL t);
+
+ /**
+ * Convert the url to a local path which eg can be fed into a JavaFX media
+ * player
+ *
+ * @param url
+ * the url
+ * @return the local path
+ */
+ public @NonNull Path resolveToLocalPath(URL url);
+}
diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java
index 10a226414..1f91cd6a7 100644
--- a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java
+++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java
@@ -13,13 +13,16 @@ package org.eclipse.fx.core;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
+import org.eclipse.fx.core.function.ExExecutor;
import org.eclipse.fx.core.internal.JavaDSServiceProcessor;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
@@ -40,7 +43,7 @@ public class Util {
* @since 2.2.0
*/
public static boolean isFX9() {
- return System.getProperty("javafx.version") != null && System.getProperty("javafx.version").startsWith("9"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ return System.getProperty("javafx.version") != null && System.getProperty("javafx.version").startsWith("9"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}
/**
@@ -48,7 +51,7 @@ public class Util {
* @since 2.2.0
*/
public static boolean isFX8() {
- return System.getProperty("javafx.version") != null && System.getProperty("javafx.version").startsWith("8"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ return System.getProperty("javafx.version") != null && System.getProperty("javafx.version").startsWith("8"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}
/**
@@ -301,4 +304,36 @@ public class Util {
System.err.println(data);
return data;
}
+
+ /**
+ * Convert an URL to a path on the local filesystem
+ *
+ * @param url
+ * the url
+ * @param copyIfNeeded
+ * <code>true</code> if the url can not be converted to a local
+ * the content is copied to the local filesystem
+ * @return the path
+ */
+ public static Optional<Resource> getLocalPath(@NonNull URL url, boolean copyIfNeeded) {
+ return lookupServiceList(URLResolver.class)
+ .stream()
+ .filter(r -> r.test(url)).findFirst()
+ .map(r -> Optional.of(Resource.createResource(r.resolveToLocalPath(url))))
+ .orElseGet(() -> copyIfNeeded ? ExExecutor.executeSupplier( () -> Util.copyToTempFile(url), "Unable to copy resource") : Optional.empty()); //$NON-NLS-1$
+ }
+
+ private static Resource copyToTempFile(@NonNull URL url) throws IOException {
+ Path path = Files.createTempFile("tmp", Paths.get(url.getPath()).getFileName().toString()); //$NON-NLS-1$
+
+ try (InputStream stream = url.openStream()) {
+ Files.copy(stream, path);
+ }
+
+ if( path == null ) {
+ return null;
+ }
+
+ return Resource.createTempResource(path);
+ }
}
diff --git a/bundles/runtime/org.eclipse.fx.osgi.util/.project b/bundles/runtime/org.eclipse.fx.osgi.util/.project
index a952ba2e2..61ee2f895 100755
--- a/bundles/runtime/org.eclipse.fx.osgi.util/.project
+++ b/bundles/runtime/org.eclipse.fx.osgi.util/.project
@@ -20,6 +20,11 @@
<arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ds.core.builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
diff --git a/bundles/runtime/org.eclipse.fx.osgi.util/.settings/ca.ecliptical.pde.ds.prefs b/bundles/runtime/org.eclipse.fx.osgi.util/.settings/ca.ecliptical.pde.ds.prefs
new file mode 100644
index 000000000..3c38f2952
--- /dev/null
+++ b/bundles/runtime/org.eclipse.fx.osgi.util/.settings/ca.ecliptical.pde.ds.prefs
@@ -0,0 +1,5 @@
+eclipse.preferences.version=1
+enabled=true
+path=OSGI-INF/services
+validationErrorLevel=error
+validationErrorLevel.missingImplicitUnbindMethod=error
diff --git a/bundles/runtime/org.eclipse.fx.osgi.util/META-INF/MANIFEST.MF b/bundles/runtime/org.eclipse.fx.osgi.util/META-INF/MANIFEST.MF
index ef17734dd..7e76cd747 100755
--- a/bundles/runtime/org.eclipse.fx.osgi.util/META-INF/MANIFEST.MF
+++ b/bundles/runtime/org.eclipse.fx.osgi.util/META-INF/MANIFEST.MF
@@ -5,12 +5,16 @@ Bundle-SymbolicName: org.eclipse.fx.osgi.util
Bundle-Version: 2.2.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: org.eclipse.equinox.app;version="1.1.0";resolution:=optional,
+ org.eclipse.fx.core;version="2.2.0",
org.eclipse.fx.core.databinding;version="2.2.0";resolution:=optional,
org.osgi.framework;version="1.6.0",
org.osgi.framework.wiring;version="1.0.0",
+ org.osgi.service.component.annotations;version="1.2.0";resolution:=optional,
org.osgi.service.event;version="1.3.0";resolution:=optional
Export-Package: org.eclipse.fx.osgi.util;version="2.2.0"
Bundle-Vendor: %Bundle-Vendor
Require-Bundle: org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional,
- org.eclipse.core.databinding.observable;bundle-version="1.4.1";resolution:=optional
+ org.eclipse.core.databinding.observable;bundle-version="1.4.1";resolution:=optional,
+ org.eclipse.equinox.common;bundle-version="3.7.0"
Bundle-ActivationPolicy: lazy
+Service-Component: OSGI-INF/services/org.eclipse.fx.osgi.util.internal.FileLocatorURLResolver.xml
diff --git a/bundles/runtime/org.eclipse.fx.osgi.util/OSGI-INF/services/org.eclipse.fx.osgi.util.internal.FileLocatorURLResolver.xml b/bundles/runtime/org.eclipse.fx.osgi.util/OSGI-INF/services/org.eclipse.fx.osgi.util.internal.FileLocatorURLResolver.xml
new file mode 100644
index 000000000..d9a9eca95
--- /dev/null
+++ b/bundles/runtime/org.eclipse.fx.osgi.util/OSGI-INF/services/org.eclipse.fx.osgi.util.internal.FileLocatorURLResolver.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.fx.osgi.util.internal.FileLocatorURLResolver">
+ <service>
+ <provide interface="org.eclipse.fx.core.URLResolver"/>
+ </service>
+ <implementation class="org.eclipse.fx.osgi.util.internal.FileLocatorURLResolver"/>
+</scr:component> \ No newline at end of file
diff --git a/bundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/internal/FileLocatorURLResolver.java b/bundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/internal/FileLocatorURLResolver.java
new file mode 100644
index 000000000..a927d8267
--- /dev/null
+++ b/bundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/internal/FileLocatorURLResolver.java
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright (c) 2015 BestSolution.at 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:
+ * Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.fx.osgi.util.internal;
+
+import java.io.IOException;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.fx.core.URLResolver;
+import org.eclipse.jdt.annotation.NonNull;
+import org.osgi.service.component.annotations.Component;
+
+/**
+ * Resolver for OSGi-FileLocator
+ */
+@Component
+public class FileLocatorURLResolver implements URLResolver {
+
+ @Override
+ public boolean test(@NonNull URL t) {
+ try {
+ return FileLocator.toFileURL(t) != null;
+ } catch (IOException e) {
+ return false;
+ }
+ }
+
+ @SuppressWarnings("null")
+ @Override
+ public @NonNull Path resolveToLocalPath(URL url) {
+ try {
+ return Paths.get(FileLocator.toFileURL(url).toURI());
+ } catch (Throwable e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}

Back to the top