Search in sources :

Example 76 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testWeavingPersistence.

public void testWeavingPersistence() {
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in start()", e);
    }
    BundleContext systemContext = equinox.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext);
    Bundle test1 = null;
    try {
        test1 = systemContext.installBundle(installer.getBundleLocation("substitutes.a"));
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error installing bundle", e);
    }
    long testID1 = test1.getBundleId();
    final Bundle testFinal1 = test1;
    ServiceRegistration reg = systemContext.registerService(WeavingHook.class, new WeavingHook() {

        public void weave(WovenClass wovenClass) {
            if (!testFinal1.equals(wovenClass.getBundleWiring().getBundle()))
                return;
            if (!"substitutes.x.Ax".equals(wovenClass.getClassName()))
                return;
            List dynamicImports = wovenClass.getDynamicImports();
            dynamicImports.add("*");
        }
    }, null);
    try {
        testFinal1.loadClass("substitutes.x.Ax");
        testFinal1.loadClass("org.osgi.framework.hooks.bundle.FindHook");
    } catch (Throwable t) {
        fail("Unexpected testing bundle", t);
    } finally {
        reg.unregister();
    }
    // put the framework back to the RESOLVED state
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in start()", e);
    }
    systemContext = equinox.getBundleContext();
    test1 = systemContext.getBundle(testID1);
    Bundle test2 = null;
    try {
        test2 = systemContext.installBundle(installer.getBundleLocation("exporter.importer1"));
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error installing bundle", e);
    }
    long testID2 = test2.getBundleId();
    final Bundle testFinal2 = test2;
    reg = systemContext.registerService(WeavingHook.class, new WeavingHook() {

        public void weave(WovenClass wovenClass) {
            if (!testFinal2.equals(wovenClass.getBundleWiring().getBundle()))
                return;
            if (!"exporter.importer.test.Test1".equals(wovenClass.getClassName()))
                return;
            List dynamicImports = wovenClass.getDynamicImports();
            dynamicImports.add("*");
        }
    }, null);
    try {
        testFinal2.loadClass("exporter.importer.test.Test1");
        testFinal2.loadClass("org.osgi.framework.hooks.service.FindHook");
    } catch (Throwable t) {
        fail("Unexpected testing bundle", t);
    } finally {
        reg.unregister();
    }
    // put the framework back to the RESOLVED state
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in start()", e);
    }
    systemContext = equinox.getBundleContext();
    test1 = systemContext.getBundle(testID1);
    test2 = systemContext.getBundle(testID2);
    BundleRevision rev1 = test1.adapt(BundleRevision.class);
    BundleRevision rev2 = test2.adapt(BundleRevision.class);
    BundleWiring wiring1 = rev1.getWiring();
    BundleWiring wiring2 = rev2.getWiring();
    assertNotNull("wiring1 is null", wiring1);
    assertNotNull("wiring2 is null", wiring2);
    List packages1 = wiring1.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    List packages2 = wiring2.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    // could make this a more complete check, but with the bug the dynamic wires
    // are missing altogether because we fail to save the resolver state cache.
    assertEquals("Wrong number of wires for wiring1", 1, packages1.size());
    assertEquals("Wrong number of wires for wiring2", 1, packages2.size());
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Equinox(org.eclipse.osgi.launch.Equinox) BundleRevision(org.osgi.framework.wiring.BundleRevision) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) File(java.io.File) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 77 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project Lucee by lucee.

the class OSGiUtil method getRequiredPackages.

public static List<PackageQuery> getRequiredPackages(Bundle bundle) throws BundleException {
    List<PackageQuery> rtn = new ArrayList<PackageQuery>();
    BundleRevision br = bundle.adapt(BundleRevision.class);
    List<Requirement> requirements = br.getRequirements(null);
    Iterator<Requirement> it = requirements.iterator();
    Requirement r;
    Entry<String, String> e;
    String value;
    PackageQuery pd;
    while (it.hasNext()) {
        r = it.next();
        Iterator<Entry<String, String>> iit = r.getDirectives().entrySet().iterator();
        inner: while (iit.hasNext()) {
            e = iit.next();
            if (!"filter".equals(e.getKey()))
                continue;
            value = e.getValue();
            pd = toPackageQuery(value);
            if (pd != null)
                rtn.add(pd);
        }
    }
    return rtn;
}
Also used : Requirement(org.osgi.resource.Requirement) ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 78 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project Lucee by lucee.

the class OSGiUtil method getRequiredBundles.

public static List<BundleDefinition> getRequiredBundles(Bundle bundle) throws BundleException {
    List<BundleDefinition> rtn = new ArrayList<BundleDefinition>();
    BundleRevision br = bundle.adapt(BundleRevision.class);
    List<Requirement> requirements = br.getRequirements(null);
    Iterator<Requirement> it = requirements.iterator();
    Requirement r;
    Entry<String, String> e;
    String value, name;
    int index, start, end, op;
    BundleDefinition bd;
    while (it.hasNext()) {
        r = it.next();
        Iterator<Entry<String, String>> iit = r.getDirectives().entrySet().iterator();
        while (iit.hasNext()) {
            e = iit.next();
            if (!"filter".equals(e.getKey()))
                continue;
            value = e.getValue();
            // name
            index = value.indexOf("(osgi.wiring.bundle");
            if (index == -1)
                continue;
            start = value.indexOf('=', index);
            end = value.indexOf(')', index);
            if (start == -1 || end == -1 || end < start)
                continue;
            name = value.substring(start + 1, end).trim();
            rtn.add(bd = new BundleDefinition(name));
            // version
            op = -1;
            index = value.indexOf("(bundle-version");
            if (index == -1)
                continue;
            end = value.indexOf(')', index);
            start = value.indexOf("<=", index);
            if (start != -1 && start < end) {
                op = VersionDefinition.LTE;
                start += 2;
            } else {
                start = value.indexOf(">=", index);
                if (start != -1 && start < end) {
                    op = VersionDefinition.GTE;
                    start += 2;
                } else {
                    start = value.indexOf("=", index);
                    if (start != -1 && start < end) {
                        op = VersionDefinition.EQ;
                        start++;
                    }
                }
            }
            if (op == -1 || start == -1 || end == -1 || end < start)
                continue;
            bd.setVersion(op, value.substring(start, end).trim());
        }
    }
    return rtn;
// (&(osgi.wiring.bundle=slf4j.api)(bundle-version>=1.6.4))
}
Also used : Requirement(org.osgi.resource.Requirement) ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 79 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project rt.equinox.framework by eclipse.

the class EquinoxBundle method adapt0.

@SuppressWarnings("unchecked")
private <A> A adapt0(Class<A> adapterType) {
    if (AccessControlContext.class.equals(adapterType)) {
        Generation current = (Generation) module.getCurrentRevision().getRevisionInfo();
        ProtectionDomain domain = current.getDomain();
        return (A) (domain == null ? null : new AccessControlContext(new ProtectionDomain[] { domain }));
    }
    if (BundleContext.class.equals(adapterType)) {
        try {
            return (A) getBundleContext();
        } catch (SecurityException e) {
            return null;
        }
    }
    if (BundleRevision.class.equals(adapterType)) {
        if (module.getState().equals(State.UNINSTALLED)) {
            return null;
        }
        return (A) module.getCurrentRevision();
    }
    if (BundleRevisions.class.equals(adapterType)) {
        return (A) module.getRevisions();
    }
    if (BundleStartLevel.class.equals(adapterType)) {
        return (A) module;
    }
    if (BundleWiring.class.equals(adapterType)) {
        if (module.getState().equals(State.UNINSTALLED)) {
            return null;
        }
        ModuleRevision revision = module.getCurrentRevision();
        if (revision == null) {
            return null;
        }
        return (A) revision.getWiring();
    }
    if (BundleDTO.class.equals(adapterType)) {
        // Unfortunately we need to lock here to make sure the BSN and version
        // are consistent in case of updates
        readLock();
        try {
            return (A) DTOBuilder.newBundleDTO(this);
        } finally {
            readUnlock();
        }
    }
    if (BundleStartLevelDTO.class.equals(adapterType)) {
        if (module.getState().equals(State.UNINSTALLED)) {
            return null;
        }
        return (A) DTOBuilder.newBundleStartLevelDTO(this, module);
    }
    if (BundleRevisionDTO.class.equals(adapterType)) {
        if (module.getState().equals(State.UNINSTALLED)) {
            return null;
        }
        return (A) DTOBuilder.newBundleRevisionDTO(module.getCurrentRevision());
    }
    if (BundleRevisionDTO[].class.equals(adapterType)) {
        if (module.getState().equals(State.UNINSTALLED)) {
            return null;
        }
        // proper locking for us.
        return (A) DTOBuilder.newArrayBundleRevisionDTO(module.getRevisions());
    }
    if (BundleWiringDTO.class.equals(adapterType)) {
        if (module.getState().equals(State.UNINSTALLED)) {
            return null;
        }
        readLock();
        try {
            return (A) DTOBuilder.newBundleWiringDTO(module.getCurrentRevision());
        } finally {
            readUnlock();
        }
    }
    if (BundleWiringDTO[].class.equals(adapterType)) {
        if (module.getState().equals(State.UNINSTALLED)) {
            return null;
        }
        readLock();
        try {
            return (A) DTOBuilder.newArrayBundleWiringDTO(module.getRevisions());
        } finally {
            readUnlock();
        }
    }
    if (ServiceReferenceDTO[].class.equals(adapterType)) {
        if (module.getState().equals(State.UNINSTALLED)) {
            return null;
        }
        BundleContextImpl current = getBundleContextImpl();
        ServiceReference<?>[] references = (current == null) ? null : equinoxContainer.getServiceRegistry().getRegisteredServices(current);
        return (A) DTOBuilder.newArrayServiceReferenceDTO(references);
    }
    if (getBundleId() == 0) {
        if (Framework.class.equals(adapterType)) {
            return (A) this;
        }
        if (FrameworkStartLevel.class.equals(adapterType)) {
            return (A) module.getContainer().getFrameworkStartLevel();
        }
        if (FrameworkWiring.class.equals(adapterType)) {
            return (A) module.getContainer().getFrameworkWiring();
        }
        if (FrameworkDTO.class.equals(adapterType)) {
            BundleContextImpl current = getBundleContextImpl();
            Map<String, String> configuration = equinoxContainer.getConfiguration().getConfiguration();
            readLock();
            try {
                return (A) DTOBuilder.newFrameworkDTO(current, configuration);
            } finally {
                readUnlock();
            }
        }
        if (FrameworkStartLevelDTO.class.equals(adapterType)) {
            return (A) DTOBuilder.newFrameworkStartLevelDTO(module.getContainer().getFrameworkStartLevel());
        }
        if (FrameworkWiringDTO.class.equals(adapterType)) {
            readLock();
            try {
                Set<BundleWiring> allWirings = new HashSet<>();
                for (Module m : module.getContainer().getModules()) {
                    for (BundleRevision revision : m.getRevisions().getRevisions()) {
                        BundleWiring wiring = revision.getWiring();
                        if (wiring != null) {
                            allWirings.add(wiring);
                        }
                    }
                }
                for (ModuleRevision revision : module.getContainer().getRemovalPending()) {
                    BundleWiring wiring = revision.getWiring();
                    if (wiring != null) {
                        allWirings.add(wiring);
                    }
                }
                return (A) DTOBuilder.newFrameworkWiringDTO(allWirings);
            } finally {
                readUnlock();
            }
        }
    }
    // Equinox extras
    if (Module.class.equals(adapterType)) {
        return (A) module;
    }
    if (ProtectionDomain.class.equals(adapterType)) {
        Generation current = (Generation) module.getCurrentRevision().getRevisionInfo();
        return (A) current.getDomain();
    }
    return null;
}
Also used : ProtectionDomain(java.security.ProtectionDomain) BundleRevisionDTO(org.osgi.framework.wiring.dto.BundleRevisionDTO) BundleWiring(org.osgi.framework.wiring.BundleWiring) ServiceReference(org.osgi.framework.ServiceReference) ServiceReferenceDTO(org.osgi.framework.dto.ServiceReferenceDTO) Generation(org.eclipse.osgi.storage.BundleInfo.Generation) AccessControlContext(java.security.AccessControlContext) BundleWiringDTO(org.osgi.framework.wiring.dto.BundleWiringDTO) BundleRevision(org.osgi.framework.wiring.BundleRevision) SystemModule(org.eclipse.osgi.container.SystemModule) Module(org.eclipse.osgi.container.Module) ModuleRevision(org.eclipse.osgi.container.ModuleRevision) HashSet(java.util.HashSet)

Aggregations

BundleRevision (org.osgi.framework.wiring.BundleRevision)79 Bundle (org.osgi.framework.Bundle)42 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)18 BundleWiring (org.osgi.framework.wiring.BundleWiring)14 BundleCapability (org.osgi.framework.wiring.BundleCapability)13 Test (org.junit.Test)12 Map (java.util.Map)11 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)11 Resource (org.osgi.resource.Resource)11 HashSet (java.util.HashSet)9 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)9 BundleContext (org.osgi.framework.BundleContext)8 BundleException (org.osgi.framework.BundleException)8 List (java.util.List)7 BundleConstituent (org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent)7 BundleWire (org.osgi.framework.wiring.BundleWire)7 Requirement (org.osgi.resource.Requirement)7 File (java.io.File)6 Collection (java.util.Collection)6