Search in sources :

Example 1 with FileBundleVersionInfo

use of org.apache.sling.commons.osgi.bundleversion.FileBundleVersionInfo in project sling by apache.

the class Loader method getLauncherJarFiles.

/**
     * Returns all files in the <code>launchpadHome</code> directory which may
     * be considered as launcher JAR files, sorted based on their bundle version
     * information, most recent last. These files all start with the
     * {@link SharedConstants#LAUNCHER_JAR_REL_PATH}. This list may be empty if
     * the launcher JAR file has not been installed yet.
     *
     * @return The list of candidate launcher JAR files, which may be empty.
     *         <code>null</code> is returned if an IO error occurs trying to
     *         list the files.
     */
private File[] getLauncherJarFiles() {
    // Get list of files with names starting with our prefix
    final File[] rawList = launchpadHome.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isFile() && pathname.getName().startsWith(SharedConstants.LAUNCHER_JAR_REL_PATH);
        }
    });
    // Keep only those which have valid Bundle headers, and
    // sort them according to the bundle version numbers
    final List<FileBundleVersionInfo> list = new ArrayList<FileBundleVersionInfo>();
    for (File f : rawList) {
        FileBundleVersionInfo fvi = null;
        try {
            fvi = new FileBundleVersionInfo(f);
        } catch (IOException ioe) {
            // Cannot read bundle info from jar file - should never happen??
            throw new IllegalStateException("Cannot read bundle information from loader file " + f.getAbsolutePath());
        }
        if (fvi.isBundle()) {
            list.add(fvi);
        }
    }
    Collections.sort(list);
    final File[] result = new File[list.size()];
    int i = 0;
    for (FileBundleVersionInfo fvi : list) {
        result[i++] = fvi.getSource();
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileFilter(java.io.FileFilter) JarFile(java.util.jar.JarFile) File(java.io.File) FileBundleVersionInfo(org.apache.sling.commons.osgi.bundleversion.FileBundleVersionInfo)

Example 2 with FileBundleVersionInfo

use of org.apache.sling.commons.osgi.bundleversion.FileBundleVersionInfo in project sling by apache.

the class Loader method removeOldLauncherJars.

/**
     * Removes old candidate launcher JAR files leaving the most recent one as
     * the launcher JAR file to use on next Sling startup.
     */
private void removeOldLauncherJars() {
    final File[] launcherJars = getLauncherJarFiles();
    if (launcherJars != null && launcherJars.length > 0) {
        // Remove all files except current one
        final File current = getLauncherJarFile();
        for (File f : launcherJars) {
            if (f.getAbsolutePath().equals(current.getAbsolutePath())) {
                continue;
            }
            String versionInfo = null;
            try {
                FileBundleVersionInfo vi = new FileBundleVersionInfo(f);
                versionInfo = getBundleInfo(vi);
            } catch (IOException ignored) {
            }
            info("Deleting obsolete launcher jar: " + f.getName() + ", " + versionInfo);
            f.delete();
        }
        // And ensure the current file has the standard launcher name
        if (!SharedConstants.LAUNCHER_JAR_REL_PATH.equals(current.getName())) {
            info("Renaming current launcher jar " + current.getName() + " to " + SharedConstants.LAUNCHER_JAR_REL_PATH);
            File launcherFileName = new File(current.getParentFile(), SharedConstants.LAUNCHER_JAR_REL_PATH);
            current.renameTo(launcherFileName);
        }
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) FileBundleVersionInfo(org.apache.sling.commons.osgi.bundleversion.FileBundleVersionInfo)

Example 3 with FileBundleVersionInfo

use of org.apache.sling.commons.osgi.bundleversion.FileBundleVersionInfo in project sling by apache.

the class Loader method installLauncherJar.

/**
     * Copies the contents of the launcher JAR as indicated by the URL to the
     * sling home directory. If the existing file is is a more recent bundle
     * version than the supplied launcher JAR file, it is is not replaced.
     *
     * @return <code>true</code> if the launcher JAR file has been installed or
     *         updated, <code>false</code> otherwise.
     * @throws IOException If an error occurrs transferring the contents
     */
public boolean installLauncherJar(URL launcherJar) throws IOException {
    info("Checking launcher JAR in folder " + launchpadHome);
    final File currentLauncherJarFile = getLauncherJarFile();
    // Copy the new launcher jar to a temporary file, and
    // extract bundle version info
    final URLConnection launcherJarConn = launcherJar.openConnection();
    launcherJarConn.setUseCaches(false);
    final File tmp = new File(launchpadHome, "Loader_tmp_" + System.currentTimeMillis() + SharedConstants.LAUNCHER_JAR_REL_PATH);
    spool(launcherJarConn.getInputStream(), tmp);
    final FileBundleVersionInfo newVi = new FileBundleVersionInfo(tmp);
    boolean installNewLauncher = true;
    try {
        if (!newVi.isBundle()) {
            throw new IOException("New launcher jar is not a bundle, cannot get version info:" + launcherJar);
        }
        // Compare versions to decide whether to use the existing or new launcher jar
        if (currentLauncherJarFile.exists()) {
            final FileBundleVersionInfo currentVi = new FileBundleVersionInfo(currentLauncherJarFile);
            if (!currentVi.isBundle()) {
                throw new IOException("Existing launcher jar is not a bundle, cannot get version info:" + currentLauncherJarFile.getAbsolutePath());
            }
            String info = null;
            if (currentVi.compareTo(newVi) == 0) {
                info = "up to date";
                installNewLauncher = false;
            } else if (currentVi.compareTo(newVi) > 0) {
                info = "more recent than ours";
                installNewLauncher = false;
            }
            if (info != null) {
                info("Existing launcher is " + info + ", using it: " + getBundleInfo(currentVi) + " (" + currentLauncherJarFile.getName() + ")");
            }
        }
        if (installNewLauncher) {
            final File f = new File(tmp.getParentFile(), SharedConstants.LAUNCHER_JAR_REL_PATH + "." + System.currentTimeMillis());
            if (!tmp.renameTo(f)) {
                throw new IOException("Failed to rename " + tmp.getName() + " to " + f.getName());
            }
            info("Installing new launcher: " + launcherJar + ", " + getBundleInfo(newVi) + " (" + f.getName() + ")");
        }
    } finally {
        if (tmp.exists()) {
            tmp.delete();
        }
    }
    return installNewLauncher;
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) FileBundleVersionInfo(org.apache.sling.commons.osgi.bundleversion.FileBundleVersionInfo)

Aggregations

File (java.io.File)3 IOException (java.io.IOException)3 JarFile (java.util.jar.JarFile)3 FileBundleVersionInfo (org.apache.sling.commons.osgi.bundleversion.FileBundleVersionInfo)3 FileFilter (java.io.FileFilter)1 JarURLConnection (java.net.JarURLConnection)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1