Search in sources :

Example 1 with ModuleRevisionBuilder

use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.

the class Storage method update.

public Generation update(Module module, URLConnection content) throws BundleException {
    if (osgiLocation.isReadOnly()) {
        // $NON-NLS-1$
        throw new BundleException("The framework storage area is read only.", BundleException.INVALID_OPERATION);
    }
    URL sourceURL = content.getURL();
    InputStream in;
    try {
        in = content.getInputStream();
    } catch (Throwable e) {
        // $NON-NLS-1$
        throw new BundleException("Error reading bundle content.", e);
    }
    boolean isReference = in instanceof ReferenceInputStream;
    File staged = stageContent(in, sourceURL);
    ModuleRevision current = module.getCurrentRevision();
    Generation currentGen = (Generation) current.getRevisionInfo();
    BundleInfo bundleInfo = currentGen.getBundleInfo();
    Generation newGen = bundleInfo.createGeneration();
    try {
        File contentFile = getContentFile(staged, isReference, bundleInfo.getBundleId(), newGen.getGenerationId());
        newGen.setContent(contentFile, isReference);
        // Check that we can open the bundle file
        newGen.getBundleFile().open();
        setStorageHooks(newGen);
        ModuleRevisionBuilder builder = getBuilder(newGen);
        moduleContainer.update(module, builder, newGen);
    } catch (Throwable t) {
        if (!isReference) {
            try {
                delete(staged);
            } catch (IOException e) {
            // tried our best
            }
        }
        newGen.delete();
        if (t instanceof SecurityException) {
            // if the cause is a bundle exception then throw that
            if (t.getCause() instanceof BundleException) {
                throw (BundleException) t.getCause();
            }
            throw (SecurityException) t;
        }
        if (t instanceof BundleException) {
            throw (BundleException) t;
        }
        // $NON-NLS-1$
        throw new BundleException("Error occurred installing a bundle.", t);
    } finally {
        bundleInfo.unlockGeneration(newGen);
    }
    return newGen;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ReferenceInputStream(org.eclipse.osgi.storage.url.reference.ReferenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ReferenceInputStream(org.eclipse.osgi.storage.url.reference.ReferenceInputStream) IOException(java.io.IOException) URL(java.net.URL) Generation(org.eclipse.osgi.storage.BundleInfo.Generation) ModuleRevisionBuilder(org.eclipse.osgi.container.ModuleRevisionBuilder) BundleException(org.osgi.framework.BundleException) NestedDirBundleFile(org.eclipse.osgi.storage.bundlefile.NestedDirBundleFile) File(java.io.File) ZipBundleFile(org.eclipse.osgi.storage.bundlefile.ZipBundleFile) DirBundleFile(org.eclipse.osgi.storage.bundlefile.DirBundleFile) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) ModuleRevision(org.eclipse.osgi.container.ModuleRevision)

Example 2 with ModuleRevisionBuilder

use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.

the class Storage method install.

public Generation install(Module origin, String bundleLocation, URLConnection content) throws BundleException {
    if (osgiLocation.isReadOnly()) {
        // $NON-NLS-1$
        throw new BundleException("The framework storage area is read only.", BundleException.INVALID_OPERATION);
    }
    URL sourceURL = content.getURL();
    InputStream in;
    try {
        in = content.getInputStream();
    } catch (Throwable e) {
        // $NON-NLS-1$
        throw new BundleException("Error reading bundle content.", e);
    }
    // Check if the bundle already exists at this location
    // before doing the staging and generation creation.
    // This is important since some installers seem to continually
    // re-install bundles using the same location each startup
    Module existingLocation = moduleContainer.getModule(bundleLocation);
    if (existingLocation != null) {
        // Another thread could win the location lock and install before this thread does.
        try {
            in.close();
        } catch (IOException e) {
        // ignore
        }
        if (origin != null) {
            // Check that the existing location is visible from the origin module
            Bundle bundle = origin.getBundle();
            BundleContext context = bundle == null ? null : bundle.getBundleContext();
            if (context != null && context.getBundle(existingLocation.getId()) == null) {
                Bundle b = existingLocation.getBundle();
                throw new BundleException(NLS.bind(Msg.ModuleContainer_NameCollisionWithLocation, new Object[] { b.getSymbolicName(), b.getVersion(), bundleLocation }), BundleException.REJECTED_BY_HOOK);
            }
        }
        return (Generation) existingLocation.getCurrentRevision().getRevisionInfo();
    }
    boolean isReference = in instanceof ReferenceInputStream;
    File staged = stageContent(in, sourceURL);
    Generation generation = null;
    try {
        Long nextID = moduleDatabase.getAndIncrementNextId();
        BundleInfo info = new BundleInfo(this, nextID, bundleLocation, 0);
        generation = info.createGeneration();
        File contentFile = getContentFile(staged, isReference, nextID, generation.getGenerationId());
        generation.setContent(contentFile, isReference);
        // Check that we can open the bundle file
        generation.getBundleFile().open();
        setStorageHooks(generation);
        ModuleRevisionBuilder builder = getBuilder(generation);
        builder.setId(nextID);
        Module m = moduleContainer.install(origin, bundleLocation, builder, generation);
        if (!nextID.equals(m.getId())) {
            // this revision is already installed. delete the generation
            generation.delete();
            return (Generation) m.getCurrentRevision().getRevisionInfo();
        }
        return generation;
    } catch (Throwable t) {
        if (!isReference) {
            try {
                delete(staged);
            } catch (IOException e) {
            // tried our best
            }
        }
        if (generation != null) {
            generation.delete();
            generation.getBundleInfo().delete();
        }
        if (t instanceof SecurityException) {
            // if the cause is a bundle exception then throw that
            if (t.getCause() instanceof BundleException) {
                throw (BundleException) t.getCause();
            }
            throw (SecurityException) t;
        }
        if (t instanceof BundleException) {
            throw (BundleException) t;
        }
        // $NON-NLS-1$
        throw new BundleException("Error occurred installing a bundle.", t);
    } finally {
        if (generation != null) {
            generation.getBundleInfo().unlockGeneration(generation);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ReferenceInputStream(org.eclipse.osgi.storage.url.reference.ReferenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) ReferenceInputStream(org.eclipse.osgi.storage.url.reference.ReferenceInputStream) URL(java.net.URL) Generation(org.eclipse.osgi.storage.BundleInfo.Generation) ModuleRevisionBuilder(org.eclipse.osgi.container.ModuleRevisionBuilder) BundleException(org.osgi.framework.BundleException) Module(org.eclipse.osgi.container.Module) NestedDirBundleFile(org.eclipse.osgi.storage.bundlefile.NestedDirBundleFile) File(java.io.File) ZipBundleFile(org.eclipse.osgi.storage.bundlefile.ZipBundleFile) DirBundleFile(org.eclipse.osgi.storage.bundlefile.DirBundleFile) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) BundleContext(org.osgi.framework.BundleContext)

Example 3 with ModuleRevisionBuilder

use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.

the class AbstractTest method installDummyModule.

protected Module installDummyModule(String manifestFile, String location, String alias, String extraExports, String extraCapabilities, ModuleContainer container) throws BundleException, IOException {
    Map<String, String> manifest = getManifest(manifestFile);
    ModuleRevisionBuilder builder = OSGiManifestBuilderFactory.createBuilder(manifest, alias, extraExports, extraCapabilities);
    Module system = container.getModule(0);
    return container.install(system, location, builder, null);
}
Also used : ModuleRevisionBuilder(org.eclipse.osgi.container.ModuleRevisionBuilder) Module(org.eclipse.osgi.container.Module)

Example 4 with ModuleRevisionBuilder

use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.

the class AbstractTest method installDummyModule.

protected Module installDummyModule(Map<String, String> manifest, long id, String location, ModuleContainer container) throws BundleException {
    ModuleRevisionBuilder builder = OSGiManifestBuilderFactory.createBuilder(manifest);
    if (id > 0) {
        builder.setId(id);
    }
    Module system = container.getModule(0);
    return container.install(system, location, builder, null);
}
Also used : ModuleRevisionBuilder(org.eclipse.osgi.container.ModuleRevisionBuilder) Module(org.eclipse.osgi.container.Module)

Example 5 with ModuleRevisionBuilder

use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.

the class TestModuleContainer method testBug483849.

@Test
public void testBug483849() throws BundleException, IOException {
    DummyContainerAdaptor adaptor = createDummyAdaptor();
    ModuleContainer container = adaptor.getContainer();
    // install and resolve host bundle
    Module host = installDummyModule("bug483849.host.MF", "host", container);
    ResolutionReport report = container.resolve(Arrays.asList(host), true);
    Assert.assertNull("Failed to resolve host.", report.getResolutionException());
    // install and dynamically attach a fragment that exports a package and resolve an importer
    Module frag = installDummyModule("bug483849.frag.MF", "frag", container);
    Module importer = installDummyModule("bug483849.importer.MF", "importer", container);
    report = container.resolve(Arrays.asList(frag, importer), true);
    Assert.assertNull("Failed to resolve test fragment and importer.", report.getResolutionException());
    // get the count of package exports
    ModuleWiring wiring = host.getCurrentRevision().getWiring();
    int originalPackageCnt = wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE).size();
    // update the host to generate a new revision
    Map<String, String> updateManifest = getManifest("bug483849.host.MF");
    ModuleRevisionBuilder updateBuilder = OSGiManifestBuilderFactory.createBuilder(updateManifest);
    container.update(host, updateBuilder, null);
    // refresh host which should force the importer to re-resolve to the new revision
    report = container.refresh(Collections.singleton(host));
    ModuleWiring importerWiring = importer.getCurrentRevision().getWiring();
    Assert.assertNotNull("No wiring for importer.", importerWiring);
    List<ModuleWire> importerPackageWires = importerWiring.getRequiredModuleWires(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of importer package Wires.", 1, importerPackageWires.size());
    Assert.assertEquals("Wrong provider wiring.", host.getCurrentRevision().getWiring(), importerPackageWires.iterator().next().getProviderWiring());
    Assert.assertEquals("Wrong provider revision.", host.getCurrentRevision(), importerPackageWires.iterator().next().getProviderWiring().getRevision());
    wiring = host.getCurrentRevision().getWiring();
    List<BundleCapability> packages = wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of host packages.", originalPackageCnt, packages.size());
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) ResolutionReport(org.eclipse.osgi.report.resolution.ResolutionReport) DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) ModuleWiring(org.eclipse.osgi.container.ModuleWiring) ModuleRevisionBuilder(org.eclipse.osgi.container.ModuleRevisionBuilder) Module(org.eclipse.osgi.container.Module) BundleCapability(org.osgi.framework.wiring.BundleCapability) Test(org.junit.Test)

Aggregations

ModuleRevisionBuilder (org.eclipse.osgi.container.ModuleRevisionBuilder)11 Module (org.eclipse.osgi.container.Module)7 BundleException (org.osgi.framework.BundleException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataInputStream (java.io.DataInputStream)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ModuleContainer (org.eclipse.osgi.container.ModuleContainer)3 Generation (org.eclipse.osgi.storage.BundleInfo.Generation)3 BundleFile (org.eclipse.osgi.storage.bundlefile.BundleFile)3 DirBundleFile (org.eclipse.osgi.storage.bundlefile.DirBundleFile)3 NestedDirBundleFile (org.eclipse.osgi.storage.bundlefile.NestedDirBundleFile)3 ZipBundleFile (org.eclipse.osgi.storage.bundlefile.ZipBundleFile)3 DummyContainerAdaptor (org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2