Search in sources :

Example 1 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project sling by apache.

the class AdapterManagerTest method setUp.

@org.junit.Before
public void setUp() throws Exception {
    am = new AdapterManagerImpl();
    final PackageAdmin pa = this.context.mock(PackageAdmin.class);
    final ExportedPackage ep = this.context.mock(ExportedPackage.class);
    this.context.checking(new Expectations() {

        {
            allowing(pa).getExportedPackage(with(any(String.class)));
            will(returnValue(ep));
        }
    });
    PrivateAccessor.setField(am, "packageAdmin", pa);
}
Also used : Expectations(org.jmock.Expectations) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Example 2 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project sling by apache.

the class ClassLoadingTest method testLoading_SLING_6258.

@Test
public void testLoading_SLING_6258() throws Exception {
    final BundleContext bundleContext = this.context.mock(BundleContext.class);
    final PackageAdmin packageAdmin = this.context.mock(PackageAdmin.class);
    final ExportedPackage ep1 = this.context.mock(ExportedPackage.class, "ep1");
    final ExportedPackage ep2 = this.context.mock(ExportedPackage.class, "ep2");
    final ExportedPackage ep3 = this.context.mock(ExportedPackage.class, "ep3");
    final Bundle bundle1 = this.context.mock(Bundle.class, "bundle1");
    final Bundle bundle2 = this.context.mock(Bundle.class, "bundle2");
    final Bundle bundle3 = this.context.mock(Bundle.class, "bundle3");
    final ClassNotFoundException cnfe = new ClassNotFoundException();
    this.context.checking(new Expectations() {

        {
            allowing(bundleContext).createFilter(with(any(String.class)));
            will(returnValue(null));
            allowing(bundleContext).getServiceReferences(with(any(String.class)), with((String) null));
            will(returnValue(null));
            allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
            allowing(bundleContext).removeServiceListener(with(any(ServiceListener.class)));
            allowing(packageAdmin).getExportedPackages(with("org.apache.sling.test"));
            will(returnValue(new ExportedPackage[] { ep1, ep2 }));
            allowing(packageAdmin).getExportedPackages(with("org.apache.sling.test3"));
            will(returnValue(new ExportedPackage[] { ep3 }));
            allowing(ep1).getExportingBundle();
            will(returnValue(bundle1));
            allowing(ep1).isRemovalPending();
            will(returnValue(false));
            allowing(ep2).getExportingBundle();
            will(returnValue(bundle2));
            allowing(ep2).isRemovalPending();
            will(returnValue(false));
            allowing(ep3).getExportingBundle();
            will(returnValue(bundle3));
            allowing(ep3).isRemovalPending();
            will(returnValue(false));
            allowing(bundle1).getBundleId();
            will(returnValue(1L));
            allowing(bundle1).getState();
            will(returnValue(Bundle.ACTIVE));
            allowing(bundle1).getVersion();
            will(returnValue(new Version("1.0.0")));
            allowing(bundle1).getSymbolicName();
            will(returnValue("org.apache.sling.test1"));
            allowing(bundle2).getBundleId();
            will(returnValue(2L));
            allowing(bundle2).getState();
            will(returnValue(Bundle.ACTIVE));
            allowing(bundle2).getVersion();
            will(returnValue(new Version("2.0.0")));
            allowing(bundle2).getSymbolicName();
            will(returnValue("org.apache.sling.test2"));
            allowing(bundle3).getBundleId();
            will(returnValue(3L));
            allowing(bundle3).getState();
            will(returnValue(Bundle.ACTIVE));
            allowing(bundle3).getVersion();
            will(returnValue(new Version("1.2.3")));
            allowing(bundle3).getSymbolicName();
            will(returnValue("org.apache.sling.test3"));
            allowing(bundle1).loadClass("org.apache.sling.test.T1");
            will(throwException(cnfe));
            allowing(bundle1).getResource("org/apache/sling/test/T3.class");
            will(returnValue(new URL("jar:file:/ws/org.apache.sling.test1.jar!/org/apache/sling/test/T3.class")));
            allowing(bundle2).loadClass("org.apache.sling.test.T1");
            will(returnValue(ArrayList.class));
            allowing(bundle2).loadClass("org.apache.sling.test.T2");
            will(throwException(new ClassNotFoundException()));
            allowing(bundle2).getResource("org/apache/sling/test/T3.class");
            will(returnValue(null));
            allowing(bundle2).getResources("org/apache/sling/test/T3.class");
            will(returnValue(null));
            allowing(bundle3).getResource("org/apache/sling/test3/T4.class");
            will(returnValue(new URL("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class")));
            allowing(bundle3).getResources("org/apache/sling/test3/T4.class");
            will(returnValue(Collections.enumeration(new ArrayList<URL>() {

                {
                    add(new URL("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class"));
                }
            })));
        }
    });
    DynamicClassLoaderManagerImpl manager = new DynamicClassLoaderManagerImpl(bundleContext, packageAdmin, null, new DynamicClassLoaderManagerFactory(bundleContext, packageAdmin));
    final ClassLoader cl = manager.getDynamicClassLoader();
    Assert.assertEquals(ArrayList.class, cl.loadClass("org.apache.sling.test.T1"));
    try {
        cl.loadClass("org.apache.sling.test.T2");
    } catch (Exception e) {
        Assert.assertEquals(ClassNotFoundException.class, e.getClass());
    }
    Assert.assertNull(cl.getResource("org/apache/sling/test/T3.class"));
    Assert.assertFalse(cl.getResources("org/apache/sling/test/T3.class").hasMoreElements());
    Assert.assertEquals("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class", cl.getResource("org/apache/sling/test3/T4.class").toString());
    Enumeration<URL> resourceURLs = cl.getResources("org/apache/sling/test3/T4.class");
    int count = 0;
    URL lastURL = new URL("https://sling.apache.org");
    while (resourceURLs.hasMoreElements()) {
        count++;
        lastURL = resourceURLs.nextElement();
    }
    Assert.assertEquals(1, count);
    Assert.assertEquals("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class", lastURL.toString());
}
Also used : Expectations(org.jmock.Expectations) ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) URL(java.net.URL) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Version(org.osgi.framework.Version) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 3 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class Felix method getExportedPackages.

/**
 * Returns the exported packages associated with the specified
 * package name. This is used by the PackageAdmin service
 * implementation.
 *
 * @param pkgName The name of the exported package to find.
 * @return The exported package or null if no matching package was found.
 */
ExportedPackage[] getExportedPackages(String pkgName) {
    // First, get all exporters of the package.
    Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
    BundleRequirementImpl req = new BundleRequirementImpl(null, BundleRevision.PACKAGE_NAMESPACE, Collections.EMPTY_MAP, attrs);
    List<BundleCapability> exports = m_resolver.findProviders(req, false);
    // We only want resolved capabilities.
    for (Iterator<BundleCapability> it = exports.iterator(); it.hasNext(); ) {
        if (it.next().getRevision().getWiring() == null) {
            it.remove();
        }
    }
    if (exports != null) {
        List pkgs = new ArrayList();
        for (Iterator<BundleCapability> it = exports.iterator(); it.hasNext(); ) {
            // Get the bundle associated with the current exporting revision.
            Bundle bundle = it.next().getRevision().getBundle();
            // We need to find the version of the exported package, but this
            // is tricky since there may be multiple versions of the package
            // offered by a given bundle, since multiple revisions of the
            // bundle JAR file may exist if the bundle was updated without
            // refreshing the framework. In this case, each revision of the
            // bundle JAR file is represented as a revision ordered from
            // newest to oldest. We assume that the first revision found to
            // be exporting the package is the provider of the package,
            // which makes sense since it must have been resolved first.
            List<BundleRevision> originRevisions = bundle.adapt(BundleRevisions.class).getRevisions();
            for (int i = originRevisions.size() - 1; i >= 0; i--) {
                BundleRevision originBr = originRevisions.get(i);
                List<BundleRevision> revisions = Collections.singletonList(originBr);
                if ((originBr.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
                    // If it's a fragment, find the revisions of the attached
                    // bundle(s) and work with those instead. Note that fragments
                    // can be attached to multiple hosts...
                    revisions = new ArrayList<BundleRevision>();
                    for (BundleWire bw : originBr.getWiring().getRequiredWires(HostNamespace.HOST_NAMESPACE)) {
                        revisions.add(bw.getProviderWiring().getRevision());
                    }
                }
                for (BundleRevision br : revisions) {
                    List<BundleCapability> caps = (br.getWiring() == null) ? br.getDeclaredCapabilities(null) : br.getWiring().getCapabilities(null);
                    for (BundleCapability cap : caps) {
                        if (cap.getNamespace().equals(req.getNamespace()) && CapabilitySet.matches(cap, req.getFilter())) {
                            pkgs.add(new ExportedPackageImpl(this, (BundleImpl) br.getBundle(), br, cap));
                        }
                    }
                }
            }
        }
        return (pkgs.isEmpty()) ? null : (ExportedPackage[]) pkgs.toArray(new ExportedPackage[pkgs.size()]);
    }
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) BundleRevision(org.osgi.framework.wiring.BundleRevision) List(java.util.List) ArrayList(java.util.ArrayList) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRevisions(org.osgi.framework.wiring.BundleRevisions)

Example 4 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class MBeanRegistrator method registerPackageMBeans.

private void registerPackageMBeans() {
    try {
        ExportedPackage[] pkgs = ac.getPackageadmin().getExportedPackages((Bundle) null);
        if (pkgs != null) {
            for (int i = 0; i < pkgs.length; i++) {
                try {
                    server.registerMBean(new ManagedPackage(pkgs[i]), new ObjectName(ObjectNames.PACKAGE + InstrumentationSupport.getPackageName(pkgs[i])));
                    ac.debug("registed mbean for " + InstrumentationSupport.getPackageName(pkgs[i]));
                } catch (InstanceAlreadyExistsException iaee) {
                    ac.error("unexpected error:", iaee);
                } catch (MBeanRegistrationException mre) {
                    ac.error("unexpected error:", mre);
                } catch (NotCompliantMBeanException ncme) {
                    ac.error("unexpected error:", ncme);
                } catch (MalformedObjectNameException mone) {
                    ac.error("unexpected error:", mone);
                } catch (NullPointerException npe) {
                    ac.error("unexpected error:", npe);
                }
            }
        } else
            ac.debug("no packages found");
    } catch (ServiceNotAvailableException se) {
        ac.error("No package admin available", se);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) ObjectName(javax.management.ObjectName)

Example 5 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class InstrumentationSupport method getImportedPackages.

public static ExportedPackage[] getImportedPackages(Bundle bundle, AgentContext ac) throws ServiceNotAvailableException {
    Vector imported = new Vector();
    Bundle[] allBundles = ac.getBundleContext().getBundles();
    for (int i = 0; i < allBundles.length; i++) {
        Bundle b = allBundles[i];
        ExportedPackage[] eps = ac.getPackageadmin().getExportedPackages(b);
        if (eps == null)
            continue;
        exported: for (int j = 0; j < eps.length; j++) {
            ExportedPackage ep = eps[j];
            Bundle[] imp = ep.getImportingBundles();
            if (imp == null)
                continue;
            for (int k = 0; k < imp.length; k++) {
                Bundle b2 = imp[k];
                if (b2.getBundleId() == bundle.getBundleId()) {
                    imported.add(ep);
                    continue exported;
                }
            }
        }
    }
    if (imported.size() == 0)
        return null;
    else
        return (ExportedPackage[]) imported.toArray(new ExportedPackage[imported.size()]);
}
Also used : Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Vector(java.util.Vector)

Aggregations

ExportedPackage (org.osgi.service.packageadmin.ExportedPackage)54 Bundle (org.osgi.framework.Bundle)40 PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)21 ArrayList (java.util.ArrayList)19 RequiredBundle (org.osgi.service.packageadmin.RequiredBundle)14 Test (org.junit.Test)9 HashMap (java.util.HashMap)7 List (java.util.List)7 Version (org.osgi.framework.Version)6 LinkedHashSet (java.util.LinkedHashSet)4 TreeMap (java.util.TreeMap)4 Expectations (org.jmock.Expectations)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 File (java.io.File)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 BundleCapability (org.osgi.framework.wiring.BundleCapability)3