Search in sources :

Example 1 with ArtifactUrlTransformer

use of org.apache.felix.fileinstall.ArtifactUrlTransformer in project felix by apache.

the class DirectoryWatcher method install.

/**
 * Install an artifact and return the bundle object.
 * It uses {@link Artifact#getPath()} as location
 * of the new bundle. Before installing a file,
 * it sees if the file has been identified as a bad file in
 * earlier run. If yes, then it compares to see if the file has changed
 * since then. It installs the file if the file has changed.
 * If the file has not been identified as a bad file in earlier run,
 * then it always installs it.
 *
 * @param artifact the artifact to be installed
 * @return Bundle object that was installed
 */
private Bundle install(Artifact artifact) {
    File path = artifact.getPath();
    Bundle bundle = null;
    AtomicBoolean modified = new AtomicBoolean();
    try {
        // If the listener is an installer, ask for an update
        if (artifact.getListener() instanceof ArtifactInstaller) {
            ((ArtifactInstaller) artifact.getListener()).install(path);
        } else // if the listener is an url transformer
        if (artifact.getListener() instanceof ArtifactUrlTransformer) {
            Artifact badArtifact = installationFailures.get(path);
            if (badArtifact != null && badArtifact.getChecksum() == artifact.getChecksum()) {
                // Don't attempt to install it; nothing has changed.
                return null;
            }
            URL transformed = artifact.getTransformedUrl();
            String location = transformed.toString();
            BufferedInputStream in = new BufferedInputStream(transformed.openStream());
            bundle = installOrUpdateBundle(location, in, artifact.getChecksum(), modified);
            artifact.setBundleId(bundle.getBundleId());
        } else // if the listener is an artifact transformer
        if (artifact.getListener() instanceof ArtifactTransformer) {
            Artifact badArtifact = installationFailures.get(path);
            if (badArtifact != null && badArtifact.getChecksum() == artifact.getChecksum()) {
                // Don't attempt to install it; nothing has changed.
                return null;
            }
            File transformed = artifact.getTransformed();
            String location = path.toURI().normalize().toString();
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(transformed != null ? transformed : path));
            bundle = installOrUpdateBundle(location, in, artifact.getChecksum(), modified);
            artifact.setBundleId(bundle.getBundleId());
        }
        installationFailures.remove(path);
        setArtifact(path, artifact);
    } catch (Exception e) {
        log(Logger.LOG_ERROR, "Failed to install artifact: " + path, e);
        // Add it our bad jars list, so that we don't
        // attempt to install it again and again until the underlying
        // jar has been modified.
        installationFailures.put(path, artifact);
    }
    return modified.get() ? bundle : null;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArtifactInstaller(org.apache.felix.fileinstall.ArtifactInstaller) BufferedInputStream(java.io.BufferedInputStream) Bundle(org.osgi.framework.Bundle) ArtifactTransformer(org.apache.felix.fileinstall.ArtifactTransformer) ArtifactUrlTransformer(org.apache.felix.fileinstall.ArtifactUrlTransformer) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) BundleException(org.osgi.framework.BundleException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 2 with ArtifactUrlTransformer

use of org.apache.felix.fileinstall.ArtifactUrlTransformer in project felix by apache.

the class DirectoryWatcher method update.

private Bundle update(Artifact artifact) {
    Bundle bundle = null;
    try {
        File path = artifact.getPath();
        // If the listener is an installer, ask for an update
        if (artifact.getListener() instanceof ArtifactInstaller) {
            ((ArtifactInstaller) artifact.getListener()).update(path);
        } else // if the listener is an url transformer
        if (artifact.getListener() instanceof ArtifactUrlTransformer) {
            URL transformed = artifact.getTransformedUrl();
            bundle = context.getBundle(artifact.getBundleId());
            if (bundle == null) {
                log(Logger.LOG_WARNING, "Failed to update bundle: " + path + " with ID " + artifact.getBundleId() + ". The bundle has been uninstalled", null);
                return null;
            }
            Util.log(context, Logger.LOG_INFO, "Updating bundle " + bundle.getSymbolicName() + " / " + bundle.getVersion(), null);
            stopTransient(bundle);
            Util.storeChecksum(bundle, artifact.getChecksum(), context);
            InputStream in = (transformed != null) ? transformed.openStream() : new FileInputStream(path);
            try {
                bundle.update(in);
            } finally {
                in.close();
            }
        } else // else we need to ask for an update on the bundle
        if (artifact.getListener() instanceof ArtifactTransformer) {
            File transformed = artifact.getTransformed();
            bundle = context.getBundle(artifact.getBundleId());
            if (bundle == null) {
                log(Logger.LOG_WARNING, "Failed to update bundle: " + path + " with ID " + artifact.getBundleId() + ". The bundle has been uninstalled", null);
                return null;
            }
            Util.log(context, Logger.LOG_INFO, "Updating bundle " + bundle.getSymbolicName() + " / " + bundle.getVersion(), null);
            stopTransient(bundle);
            Util.storeChecksum(bundle, artifact.getChecksum(), context);
            InputStream in = new FileInputStream(transformed != null ? transformed : path);
            try {
                bundle.update(in);
            } finally {
                in.close();
            }
        }
    } catch (Throwable t) {
        log(Logger.LOG_WARNING, "Failed to update artifact " + artifact.getPath(), t);
    }
    return bundle;
}
Also used : ArtifactInstaller(org.apache.felix.fileinstall.ArtifactInstaller) Bundle(org.osgi.framework.Bundle) BufferedInputStream(java.io.BufferedInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArtifactTransformer(org.apache.felix.fileinstall.ArtifactTransformer) ArtifactUrlTransformer(org.apache.felix.fileinstall.ArtifactUrlTransformer) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 URL (java.net.URL)2 ArtifactInstaller (org.apache.felix.fileinstall.ArtifactInstaller)2 ArtifactTransformer (org.apache.felix.fileinstall.ArtifactTransformer)2 ArtifactUrlTransformer (org.apache.felix.fileinstall.ArtifactUrlTransformer)2 Bundle (org.osgi.framework.Bundle)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JarInputStream (java.util.jar.JarInputStream)1 BundleException (org.osgi.framework.BundleException)1