Search in sources :

Example 1 with CollisionHook

use of org.osgi.framework.hooks.bundle.CollisionHook in project felix by apache.

the class CollisionHookTest method testCollisionHookInstall.

public void testCollisionHookInstall() throws Exception {
    BundleImpl identicalBundle = mockBundleImpl(1L, "foo", "1.2.1.a");
    BundleImpl differentBundle = mockBundleImpl(2L, "bar", "1.2.1.a");
    BundleImpl originatingBundle = mockBundleImpl(4L, "xyz", "1.0.0");
    CollisionHook testCollisionHook = new CollisionHook() {

        @Override
        public void filterCollisions(int operationType, Bundle target, Collection<Bundle> collisionCandidates) {
            if ((target.getBundleId() == 4L) && (operationType == CollisionHook.INSTALLING)) {
                collisionCandidates.clear();
            }
        }
    };
    @SuppressWarnings("unchecked") ServiceReference<CollisionHook> chRef = Mockito.mock(ServiceReference.class);
    // Mock the framework
    StatefulResolver mockResolver = Mockito.mock(StatefulResolver.class);
    Felix felixMock = Mockito.mock(Felix.class);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(CollisionHook.class)).thenReturn(Collections.singleton(chRef));
    when(felixMock.getHookRegistry()).thenReturn(hReg);
    Mockito.when(felixMock.getResolver()).thenReturn(mockResolver);
    Mockito.when(felixMock.getBundles()).thenReturn(new Bundle[] { differentBundle, identicalBundle });
    Mockito.when(felixMock.getService(felixMock, chRef, false)).thenReturn(testCollisionHook);
    // Mock the archive of the bundle being installed
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
    headerMap.put(Constants.BUNDLE_VERSION, "1.2.1.a");
    headerMap.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    BundleArchiveRevision archiveRevision = Mockito.mock(BundleArchiveRevision.class);
    Mockito.when(archiveRevision.getManifestHeader()).thenReturn(headerMap);
    BundleArchive archive = Mockito.mock(BundleArchive.class);
    Mockito.when(archive.getCurrentRevision()).thenReturn(archiveRevision);
    Mockito.when(archive.getId()).thenReturn(3L);
    BundleImpl bi = new BundleImpl(felixMock, originatingBundle, archive);
    assertEquals(3L, bi.getBundleId());
    // Do the revise operation.
    try {
        bi.revise(null, null);
        fail("Should have thrown a BundleException because the installed bundle is not unique");
    } catch (BundleException be) {
        // good
        assertTrue(be.getMessage().contains("not unique"));
    }
}
Also used : BundleArchive(org.apache.felix.framework.cache.BundleArchive) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleArchiveRevision(org.apache.felix.framework.cache.BundleArchiveRevision) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) CollisionHook(org.osgi.framework.hooks.bundle.CollisionHook)

Example 2 with CollisionHook

use of org.osgi.framework.hooks.bundle.CollisionHook in project felix by apache.

the class CollisionHookTest method testCollisionNotEnabled.

public void testCollisionNotEnabled() throws Exception {
    BundleImpl identicalBundle = mockBundleImpl(1L, "foo", "1.2.1.a");
    BundleImpl differentBundle = mockBundleImpl(2L, "bar", "1.2.1.a");
    CollisionHook testCollisionHook = new CollisionHook() {

        @Override
        public void filterCollisions(int operationType, Bundle target, Collection<Bundle> collisionCandidates) {
            if ((target.getBundleId() == 3L) && (operationType == CollisionHook.INSTALLING)) {
                collisionCandidates.clear();
            }
        }
    };
    @SuppressWarnings("unchecked") ServiceReference<CollisionHook> chRef = Mockito.mock(ServiceReference.class);
    Map<String, Object> config = new HashMap<String, Object>();
    config.put(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_SINGLE);
    // Mock the framework
    StatefulResolver mockResolver = Mockito.mock(StatefulResolver.class);
    Felix felixMock = Mockito.mock(Felix.class);
    Mockito.when(felixMock.getConfig()).thenReturn(config);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(CollisionHook.class)).thenReturn(Collections.singleton(chRef));
    when(felixMock.getHookRegistry()).thenReturn(hReg);
    Mockito.when(felixMock.getResolver()).thenReturn(mockResolver);
    Mockito.when(felixMock.getBundles()).thenReturn(new Bundle[] { differentBundle, identicalBundle });
    Mockito.when(felixMock.getService(felixMock, chRef, false)).thenReturn(testCollisionHook);
    // Mock the archive of the bundle being installed
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
    headerMap.put(Constants.BUNDLE_VERSION, "1.2.1.a");
    headerMap.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    BundleArchiveRevision archiveRevision = Mockito.mock(BundleArchiveRevision.class);
    Mockito.when(archiveRevision.getManifestHeader()).thenReturn(headerMap);
    BundleArchive archive = Mockito.mock(BundleArchive.class);
    Mockito.when(archive.getCurrentRevision()).thenReturn(archiveRevision);
    Mockito.when(archive.getId()).thenReturn(3L);
    try {
        new BundleImpl(felixMock, null, archive);
        fail("Should have thrown a BundleException because the collision hook is not enabled");
    } catch (BundleException be) {
        // good
        assertTrue(be.getMessage().contains("not unique"));
    }
}
Also used : BundleArchive(org.apache.felix.framework.cache.BundleArchive) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleArchiveRevision(org.apache.felix.framework.cache.BundleArchiveRevision) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) CollisionHook(org.osgi.framework.hooks.bundle.CollisionHook)

Example 3 with CollisionHook

use of org.osgi.framework.hooks.bundle.CollisionHook in project felix by apache.

the class CollisionHookTest method testCollisionHookUpdate.

public void testCollisionHookUpdate() throws Exception {
    BundleImpl identicalBundle = mockBundleImpl(1L, "foo", "1.2.1.a");
    BundleImpl differentBundle = mockBundleImpl(2L, "foo", "1.2.1");
    CollisionHook testCollisionHook = new CollisionHook() {

        @Override
        public void filterCollisions(int operationType, Bundle target, Collection<Bundle> collisionCandidates) {
            if ((target.getBundleId() == 3L) && (operationType == CollisionHook.UPDATING)) {
                collisionCandidates.clear();
            }
        }
    };
    @SuppressWarnings("unchecked") ServiceReference<CollisionHook> chRef = Mockito.mock(ServiceReference.class);
    Map<String, Object> config = new HashMap<String, Object>();
    config.put(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_MANAGED);
    // Mock the framework
    StatefulResolver mockResolver = Mockito.mock(StatefulResolver.class);
    Felix felixMock = Mockito.mock(Felix.class);
    Mockito.when(felixMock.getConfig()).thenReturn(config);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(CollisionHook.class)).thenReturn(Collections.singleton(chRef));
    when(felixMock.getHookRegistry()).thenReturn(hReg);
    Mockito.when(felixMock.getResolver()).thenReturn(mockResolver);
    Mockito.when(felixMock.getBundles()).thenReturn(new Bundle[] { differentBundle, identicalBundle });
    Mockito.when(felixMock.getService(felixMock, chRef, false)).thenReturn(testCollisionHook);
    // Mock the archive of the bundle being installed
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "zar");
    headerMap.put(Constants.BUNDLE_VERSION, "1.2.1.a");
    headerMap.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    BundleArchiveRevision archiveRevision = Mockito.mock(BundleArchiveRevision.class);
    Mockito.when(archiveRevision.getManifestHeader()).thenReturn(headerMap);
    BundleArchive archive = Mockito.mock(BundleArchive.class);
    Mockito.when(archive.getCurrentRevision()).thenReturn(archiveRevision);
    Mockito.when(archive.getId()).thenReturn(3L);
    BundleImpl bi = new BundleImpl(felixMock, null, archive);
    assertEquals("zar", bi.getSymbolicName());
    // Do the revise operation, change the bsn to foo
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
    bi.revise(null, null);
    assertEquals("foo", bi.getSymbolicName());
}
Also used : BundleArchive(org.apache.felix.framework.cache.BundleArchive) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleArchiveRevision(org.apache.felix.framework.cache.BundleArchiveRevision) Collection(java.util.Collection) CollisionHook(org.osgi.framework.hooks.bundle.CollisionHook)

Example 4 with CollisionHook

use of org.osgi.framework.hooks.bundle.CollisionHook in project rt.equinox.framework by eclipse.

the class BundleInstallUpdateTests method testCollisionHook.

public void testCollisionHook() throws BundleException, MalformedURLException, IOException {
    Bundle test1 = installer.installBundle("test");
    installer.installBundle("test2");
    try {
        test1.update(new URL(installer.getBundleLocation("test2")).openStream());
        fail("Expected to fail to update to another bsn/version that causes collision");
    } catch (BundleException e) {
    // expected;
    }
    Bundle junk = null;
    try {
        junk = OSGiTestsActivator.getContext().installBundle("junk", new URL(installer.getBundleLocation("test2")).openStream());
        fail("Expected to fail to install duplication bsn/version that causes collision");
    } catch (BundleException e) {
    // expected;
    } finally {
        if (junk != null)
            junk.uninstall();
        junk = null;
    }
    CollisionHook hook = new CollisionHook() {

        public void filterCollisions(int operationType, Bundle target, Collection collisionCandidates) {
            collisionCandidates.clear();
        }
    };
    ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(CollisionHook.class, hook, null);
    try {
        try {
            test1.update(new URL(installer.getBundleLocation("test2")).openStream());
        } catch (BundleException e) {
            fail("Expected to succeed in updating to a duplicate bsn/version", e);
        }
        try {
            junk = OSGiTestsActivator.getContext().installBundle("junk", new URL(installer.getBundleLocation("test2")).openStream());
        } catch (BundleException e) {
            fail("Expected to succeed to install duplication bsn/version that causes collision", e);
        } finally {
            if (junk != null)
                junk.uninstall();
            junk = null;
        }
    } finally {
        reg.unregister();
    }
}
Also used : CollisionHook(org.osgi.framework.hooks.bundle.CollisionHook)

Example 5 with CollisionHook

use of org.osgi.framework.hooks.bundle.CollisionHook in project felix by apache.

the class BundleImpl method createRevision.

private BundleRevisionImpl createRevision(boolean isUpdate) throws Exception {
    // Get and parse the manifest from the most recent revision and
    // create an associated revision object for it.
    Map headerMap = Util.getMultiReleaseAwareManifestHeaders(getFramework()._getProperty("java.specification.version"), m_archive.getCurrentRevision());
    // Create the bundle revision instance.
    BundleRevisionImpl revision = new BundleRevisionImpl(this, Long.toString(getBundleId()) + "." + m_archive.getCurrentRevisionNumber().toString(), headerMap, m_archive.getCurrentRevision().getContent());
    // For R4 bundles, verify that the bundle symbolic name + version
    // is unique unless this check has been disabled.
    String allowMultiple = (String) getFramework().getConfig().get(Constants.FRAMEWORK_BSNVERSION);
    allowMultiple = (allowMultiple == null) ? Constants.FRAMEWORK_BSNVERSION_MANAGED : allowMultiple;
    if (revision.getManifestVersion().equals("2") && !allowMultiple.equals(Constants.FRAMEWORK_BSNVERSION_MULTIPLE)) {
        Version bundleVersion = revision.getVersion();
        bundleVersion = (bundleVersion == null) ? Version.emptyVersion : bundleVersion;
        String symName = revision.getSymbolicName();
        List<Bundle> collisionCanditates = new ArrayList<Bundle>();
        Bundle[] bundles = getFramework().getBundles();
        for (int i = 0; (bundles != null) && (i < bundles.length); i++) {
            long id = ((BundleImpl) bundles[i]).getBundleId();
            if (id != getBundleId()) {
                if (symName.equals(bundles[i].getSymbolicName()) && bundleVersion.equals(bundles[i].getVersion())) {
                    collisionCanditates.add(bundles[i]);
                }
            }
        }
        if (!collisionCanditates.isEmpty() && allowMultiple.equals(Constants.FRAMEWORK_BSNVERSION_MANAGED)) {
            Set<ServiceReference<CollisionHook>> hooks = getFramework().getHookRegistry().getHooks(CollisionHook.class);
            if (!hooks.isEmpty()) {
                Collection<Bundle> shrinkableCollisionCandidates = new ShrinkableCollection<Bundle>(collisionCanditates);
                for (ServiceReference<CollisionHook> hook : hooks) {
                    CollisionHook ch = getFramework().getService(getFramework(), hook, false);
                    if (ch != null) {
                        int operationType;
                        Bundle target;
                        if (isUpdate) {
                            operationType = CollisionHook.UPDATING;
                            target = this;
                        } else {
                            operationType = CollisionHook.INSTALLING;
                            target = m_installingBundle == null ? this : m_installingBundle;
                        }
                        Felix.m_secureAction.invokeBundleCollisionHook(ch, operationType, target, shrinkableCollisionCandidates);
                    }
                }
            }
        }
        if (!collisionCanditates.isEmpty()) {
            throw new BundleException("Bundle symbolic name and version are not unique: " + symName + ':' + bundleVersion, BundleException.DUPLICATE_BUNDLE_ERROR);
        }
    }
    return revision;
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) Version(org.osgi.framework.Version) ShrinkableCollection(org.apache.felix.framework.util.ShrinkableCollection) BundleException(org.osgi.framework.BundleException) CollisionHook(org.osgi.framework.hooks.bundle.CollisionHook) HashMap(java.util.HashMap) Map(java.util.Map) StringMap(org.apache.felix.framework.util.StringMap)

Aggregations

CollisionHook (org.osgi.framework.hooks.bundle.CollisionHook)5 HashMap (java.util.HashMap)4 Bundle (org.osgi.framework.Bundle)4 Collection (java.util.Collection)3 BundleArchive (org.apache.felix.framework.cache.BundleArchive)3 BundleArchiveRevision (org.apache.felix.framework.cache.BundleArchiveRevision)3 BundleException (org.osgi.framework.BundleException)3 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ShrinkableCollection (org.apache.felix.framework.util.ShrinkableCollection)1 StringMap (org.apache.felix.framework.util.StringMap)1 ServiceReference (org.osgi.framework.ServiceReference)1 Version (org.osgi.framework.Version)1