Search in sources :

Example 31 with ExportedPackage

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

the class ClassLoadingTest method testLoading.

/**
     * This method tests the dynamic class loading through the package admin.
     * The returned class changes from map to array list.
     */
@Test
public void testLoading() throws Exception {
    final Sequence sequence = this.context.sequence("load-sequence");
    final BundleContext bundleContext = this.context.mock(BundleContext.class);
    final PackageAdmin packageAdmin = this.context.mock(PackageAdmin.class);
    final ExportedPackage ep = this.context.mock(ExportedPackage.class);
    final Bundle bundle = this.context.mock(Bundle.class);
    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("org.apache.sling.test");
            will(returnValue(new ExportedPackage[] { ep }));
            allowing(ep).getExportingBundle();
            will(returnValue(bundle));
            allowing(ep).isRemovalPending();
            will(returnValue(false));
            allowing(bundle).getBundleId();
            will(returnValue(2L));
            allowing(bundle).getState();
            will(returnValue(Bundle.ACTIVE));
            allowing(bundle).getSymbolicName();
            will(returnValue("org.apache.sling.test"));
            allowing(bundle).getVersion();
            will(returnValue(new Version("1.0.0")));
            one(bundle).loadClass("org.apache.sling.test.A");
            inSequence(sequence);
            will(returnValue(java.util.Map.class));
            one(bundle).loadClass("org.apache.sling.test.A");
            inSequence(sequence);
            will(returnValue(java.util.Map.class));
            one(bundle).loadClass("org.apache.sling.test.A");
            inSequence(sequence);
            will(returnValue(java.util.ArrayList.class));
        }
    });
    DynamicClassLoaderManagerImpl manager = new DynamicClassLoaderManagerImpl(bundleContext, packageAdmin, null, new DynamicClassLoaderManagerFactory(bundleContext, packageAdmin));
    final ClassLoader cl = manager.getDynamicClassLoader();
    final Class<?> c1 = cl.loadClass("org.apache.sling.test.A");
    Assert.assertEquals("java.util.Map", c1.getName());
    final Class<?> c2 = cl.loadClass("org.apache.sling.test.A");
    Assert.assertEquals("java.util.Map", c2.getName());
    // as we cache the result, we still get the map!
    final Class<?> c3 = cl.loadClass("org.apache.sling.test.A");
    Assert.assertEquals("java.util.Map", c3.getName());
}
Also used : Expectations(org.jmock.Expectations) ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) Sequence(org.jmock.Sequence) 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 32 with ExportedPackage

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

the class ExportedPackageServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    final String packName = request.getParameter("package");
    final ExportedPackage p = packageAdmin.getExportedPackage(packName);
    if (p == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Package not found: " + packName);
    } else {
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("PACKAGE FOUND: ");
        response.getWriter().write(p.toString());
        response.getWriter().flush();
    }
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Example 33 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project geronimo-xbean by apache.

the class BundleClassFinder method scanImportPackages.

private void scanImportPackages(Collection<String> classes, Bundle host, Bundle fragment) {
    BundleDescription description = new BundleDescription(fragment.getHeaders());
    List<BundleDescription.ImportPackage> imports = description.getExternalImports();
    for (BundleDescription.ImportPackage packageImport : imports) {
        String packageName = packageImport.getName();
        if (discoveryFilter.packageDiscoveryRequired(packageName)) {
            ExportedPackage[] exports = packageAdmin.getExportedPackages(packageName);
            Bundle wiredBundle = isWired(host, exports);
            if (wiredBundle != null) {
                Set<String> allClasses = findAllClasses(wiredBundle, packageName);
                classes.addAll(allClasses);
            }
        }
    }
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) RequireBundle(org.apache.xbean.osgi.bundle.util.BundleDescription.RequireBundle) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle)

Example 34 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project geronimo-xbean by apache.

the class BundleUtils method getWiredBundles.

public static LinkedHashSet<Bundle> getWiredBundles(PackageAdmin packageAdmin, Bundle bundle) {
    BundleDescription description = new BundleDescription(bundle.getHeaders());
    // handle static wire via Import-Package
    List<BundleDescription.ImportPackage> imports = description.getExternalImports();
    LinkedHashSet<Bundle> wiredBundles = new LinkedHashSet<Bundle>();
    for (BundleDescription.ImportPackage packageImport : imports) {
        ExportedPackage[] exports = packageAdmin.getExportedPackages(packageImport.getName());
        Bundle wiredBundle = getWiredBundle(bundle, exports);
        if (wiredBundle != null) {
            wiredBundles.add(wiredBundle);
        }
    }
    // handle dynamic wire via DynamicImport-Package
    if (!description.getDynamicImportPackage().isEmpty()) {
        for (Bundle b : bundle.getBundleContext().getBundles()) {
            if (!wiredBundles.contains(b)) {
                ExportedPackage[] exports = packageAdmin.getExportedPackages(b);
                Bundle wiredBundle = getWiredBundle(bundle, exports);
                if (wiredBundle != null) {
                    wiredBundles.add(wiredBundle);
                }
            }
        }
    }
    return wiredBundles;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Example 35 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project geronimo-xbean by apache.

the class BundleAssignableClassFinder method initialize.

private void initialize() {
    BundleDescription description = new BundleDescription(bundle.getHeaders());
    List<BundleDescription.ImportPackage> imports = description.getExternalImports();
    for (BundleDescription.ImportPackage packageImport : imports) {
        String packageName = packageImport.getName();
        ExportedPackage[] exports = packageAdmin.getExportedPackages(packageName);
        Bundle wiredBundle = isWired(bundle, exports);
        if (wiredBundle != null) {
            wiredImportedPackageNames.add(packageName.replace('.', '/'));
            break;
        }
    }
}
Also used : BundleDescription(org.apache.xbean.osgi.bundle.util.BundleDescription) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle)

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