Search in sources :

Example 6 with Location

use of org.eclipse.osgi.service.datalocation.Location in project bndtools by bndtools.

the class BundleUtils method getBundleLocation.

public static IPath getBundleLocation(BundleContext context, String symbolicName, VersionRange range) {
    Location installLocation = Platform.getInstallLocation();
    Location configLocation = Platform.getConfigurationLocation();
    Bundle bundle = findBundle(context, symbolicName, range);
    if (bundle == null)
        return null;
    String location = bundle.getLocation();
    if (location.startsWith("file:")) {
        //$NON-NLS-1$
        location = location.substring(5);
    } else if (location.startsWith("reference:file:")) {
        //$NON-NLS-1$
        location = location.substring(15);
    }
    IPath bundlePath = new Path(location);
    if (bundlePath.isAbsolute())
        return bundlePath;
    // Try install location
    if (installLocation != null) {
        IPath installedBundlePath = new Path(installLocation.getURL().getFile()).append(bundlePath);
        if (installedBundlePath.toFile().exists())
            return installedBundlePath;
    }
    // Try config location
    if (configLocation != null) {
        IPath configuredBundlePath = new Path(configLocation.getURL().getFile()).append(bundlePath);
        if (configuredBundlePath.toFile().exists())
            return configuredBundlePath;
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) Bundle(org.osgi.framework.Bundle) Location(org.eclipse.osgi.service.datalocation.Location)

Example 7 with Location

use of org.eclipse.osgi.service.datalocation.Location in project knime-core by knime.

the class KNIMEApplication method writeWorkspaceVersion.

/**
 * Write the version of the metadata into a known file overwriting any
 * existing file contents. Writing the version file isn't really crucial, so
 * the function is silent about failure
 */
private static void writeWorkspaceVersion() {
    Location instanceLoc = Platform.getInstanceLocation();
    if (instanceLoc == null || instanceLoc.isReadOnly()) {
        return;
    }
    File versionFile = getVersionFile(instanceLoc.getURL(), true);
    if (versionFile == null) {
        return;
    }
    OutputStream output = null;
    try {
        String versionLine = WORKSPACE_VERSION_KEY + '=' + WORKSPACE_VERSION_VALUE;
        output = new FileOutputStream(versionFile);
        // $NON-NLS-1$
        output.write(versionLine.getBytes("UTF-8"));
    } catch (IOException e) {
    } finally {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException e) {
        // do nothing
        }
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) Location(org.eclipse.osgi.service.datalocation.Location)

Example 8 with Location

use of org.eclipse.osgi.service.datalocation.Location in project knime-core by knime.

the class KNIMEConstants method assignUniqueID.

private static void assignUniqueID() {
    Location configLocation = Platform.getConfigurationLocation();
    if (configLocation != null) {
        URL configURL = configLocation.getURL();
        if (configURL != null) {
            String path = configURL.getPath();
            if (Platform.OS_WIN32.equals(Platform.getOS()) && path.matches("^/[a-zA-Z]:/.*")) {
                // Windows path with drive letter => remove first slash
                path = path.substring(1);
            }
            Path uniqueId = Paths.get(path, "org.knime.core", "knime-id");
            if (!Files.exists(uniqueId)) {
                try {
                    Files.createDirectories(uniqueId.getParent());
                    knimeID = "01-" + createUniqeID();
                    try (OutputStream os = Files.newOutputStream(uniqueId)) {
                        os.write(knimeID.toString().getBytes("UTF-8"));
                    } catch (IOException ex) {
                        NodeLogger.getLogger(KNIMEConstants.class).error("Could not write KNIME id to '" + uniqueId.toAbsolutePath() + "': " + ex.getMessage(), ex);
                    }
                } catch (IOException ex) {
                    NodeLogger.getLogger(KNIMEConstants.class).error("Could not create configuration directory '" + uniqueId.getParent().toAbsolutePath() + "': " + ex.getMessage(), ex);
                }
            } else if (Files.isReadable(uniqueId)) {
                try (InputStream is = Files.newInputStream(uniqueId)) {
                    byte[] buf = new byte[256];
                    int read = is.read(buf);
                    knimeID = new String(buf, 0, read, Charset.forName("UTF-8"));
                } catch (IOException ex) {
                    NodeLogger.getLogger(KNIMEConstants.class).error("Could not read KNIME id from '" + uniqueId.toAbsolutePath() + "': " + ex.getMessage(), ex);
                }
            }
        }
    }
    if (knimeID == null) {
        knimeID = "00-" + createUniqeID();
    }
}
Also used : Path(java.nio.file.Path) KNIMEPath(org.knime.core.internal.KNIMEPath) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL) Location(org.eclipse.osgi.service.datalocation.Location)

Example 9 with Location

use of org.eclipse.osgi.service.datalocation.Location in project knime-core by knime.

the class EclipseUtil method checkSDK.

private static boolean checkSDK() {
    Location installLocation = Platform.getInstallLocation();
    if (installLocation == null) {
        return true;
    }
    Location configurationLocation = Platform.getConfigurationLocation();
    if (configurationLocation == null) {
        return true;
    }
    return configurationLocation.getURL().getPath().contains("/.metadata/.plugins/org.eclipse.pde.core/");
}
Also used : Location(org.eclipse.osgi.service.datalocation.Location)

Example 10 with Location

use of org.eclipse.osgi.service.datalocation.Location in project rt.equinox.framework by eclipse.

the class BasicLocationTests method testCreateLocation04.

public void testCreateLocation04() {
    Location configLocation = configLocationTracker.getService();
    File testLocationFile = OSGiTestsActivator.getContext().getDataFile("testLocations/testCreateLocation04");
    Location testLocation = configLocation.createLocation(null, null, true);
    try {
        testLocation.set(testLocationFile.toURL(), true);
        testLocation.release();
        fail("Should not be able to lock read-only location");
    } catch (Throwable t) {
    // expected
    }
}
Also used : Location(org.eclipse.osgi.service.datalocation.Location)

Aggregations

Location (org.eclipse.osgi.service.datalocation.Location)42 File (java.io.File)14 URL (java.net.URL)13 IOException (java.io.IOException)12 Equinox (org.eclipse.osgi.launch.Equinox)10 MalformedURLException (java.net.MalformedURLException)9 FileNotFoundException (java.io.FileNotFoundException)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 ServiceReference (org.osgi.framework.ServiceReference)4 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Bundle (org.osgi.framework.Bundle)3 BundleContext (org.osgi.framework.BundleContext)3 BundleException (org.osgi.framework.BundleException)3 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2