Search in sources :

Example 6 with PackageAdmin

use of org.osgi.service.packageadmin.PackageAdmin in project aries by apache.

the class BundleContextInfoProvider method getRelationships.

@Override
public List<RelationshipInfo> getRelationships() {
    ArrayList<RelationshipInfo> r = new ArrayList<RelationshipInfo>();
    Bundle[] bundles = ctx.getBundles();
    PackageAdmin pa = (PackageAdmin) ctx.getService(ctx.getServiceReference(PackageAdmin.class.getName().toString()));
    if (bundles != null && bundles.length != 0) {
        for (Bundle b : bundles) {
            String bkey = getKeyForBundle(b);
            ComponentInfo ci = getComponentForId(bkey);
            //add all the packages..
            //we only add exports, as imports are implied in the reverse
            ExportedPackage[] eps = pa.getExportedPackages(b);
            if (eps != null && eps.length != 0) {
                for (ExportedPackage ep : eps) {
                    RelationshipInfoImpl ri = new RelationshipInfoImpl();
                    ri.setProvidedBy(ci);
                    ri.setType("Package");
                    ri.setName(ep.getName());
                    ri.setRelationshipAspects(new ArrayList<RelationshipAspect>());
                    ri.setConsumedBy(new ArrayList<ComponentInfo>());
                    //TODO: add versioning aspect.
                    Bundle[] imps = ep.getImportingBundles();
                    if (imps != null && imps.length != 0) {
                        for (Bundle imp : imps) {
                            ri.getConsumedBy().add(getComponentForId(getKeyForBundle(imp)));
                        }
                    }
                    r.add(ri);
                }
            }
            //add all the services.. 
            //we only add registered services, as ones in use are handled in the reverse
            ServiceReference[] srs = b.getRegisteredServices();
            if (srs != null && srs.length != 0) {
                for (ServiceReference sr : srs) {
                    RelationshipInfoImpl ri = getRIforSR(sr);
                    ri.setProvidedBy(ci);
                    r.add(ri);
                }
            }
        }
    }
    return r;
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) RelationshipInfo(org.apache.aries.samples.goat.api.RelationshipInfo) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) RelationshipAspect(org.apache.aries.samples.goat.api.RelationshipAspect) RelationshipInfoImpl(org.apache.aries.samples.goat.info.RelationshipInfoImpl) ComponentInfo(org.apache.aries.samples.goat.api.ComponentInfo)

Example 7 with PackageAdmin

use of org.osgi.service.packageadmin.PackageAdmin 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 8 with PackageAdmin

use of org.osgi.service.packageadmin.PackageAdmin 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 9 with PackageAdmin

use of org.osgi.service.packageadmin.PackageAdmin in project rt.equinox.framework by eclipse.

the class InstallTests method testInstall2NonSingletonBundles.

/**
 * Ensures two versions of a non-singleton bundle are accepted
 */
public void testInstall2NonSingletonBundles() throws BundleException, IOException {
    // $NON-NLS-1$
    Bundle installed1 = org.eclipse.core.tests.harness.BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle07");
    ServiceReference packageAdminSR = OSGiTestsActivator.getContext().getServiceReference(PackageAdmin.class.getName());
    PackageAdmin packageAdmin = (PackageAdmin) OSGiTestsActivator.getContext().getService(packageAdminSR);
    packageAdmin.resolveBundles(null);
    // $NON-NLS-1$
    Bundle installed2 = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle07b");
    packageAdmin.resolveBundles(null);
    OSGiTestsActivator.getContext().ungetService(packageAdminSR);
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("1.0", "bundle07", installed2.getSymbolicName());
        // $NON-NLS-1$
        assertEquals("1.1", Bundle.RESOLVED, installed2.getState());
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("1.2", "1.0.0.b", installed2.getHeaders().get(Constants.BUNDLE_VERSION));
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("1.3", "bundle07", installed1.getSymbolicName());
        // $NON-NLS-1$
        assertEquals("1.4", Bundle.RESOLVED, installed1.getState());
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("1.5", "1.0.0", installed1.getHeaders().get(Constants.BUNDLE_VERSION));
    } finally {
        installed1.uninstall();
        installed2.uninstall();
    }
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin)

Example 10 with PackageAdmin

use of org.osgi.service.packageadmin.PackageAdmin in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testImporterExporter01.

public void testImporterExporter01() throws BundleException {
    // $NON-NLS-1$
    Bundle importerExporter1 = installer.installBundle("exporter.importer1");
    installer.resolveBundles(new Bundle[] { importerExporter1 });
    PackageAdmin pa = installer.getPackageAdmin();
    // $NON-NLS-1$
    ExportedPackage[] origExportedPackages = pa.getExportedPackages("exporter.importer.test");
    // $NON-NLS-1$
    assertNotNull("No exporter.importer.test found", origExportedPackages);
    // $NON-NLS-1$
    assertEquals("Wrong number of exports", 1, origExportedPackages.length);
    Bundle exporter = origExportedPackages[0].getExportingBundle();
    // $NON-NLS-1$
    assertEquals("Wrong exporter", importerExporter1, exporter);
    // TODO need to get clarification from OSGi on what is returned by getImportingBundles when there is no importers
    Bundle[] origImporters = origExportedPackages[0].getImportingBundles();
    // $NON-NLS-1$
    assertTrue("Should have no importers", origImporters == null || origImporters.length == 0);
    // install another importer/exporter.  This bundle should wire to the original exporter
    // $NON-NLS-1$
    Bundle importerExporter2 = installer.installBundle("exporter.importer2");
    installer.resolveBundles(new Bundle[] { importerExporter2 });
    origImporters = origExportedPackages[0].getImportingBundles();
    // $NON-NLS-1$
    assertNotNull("No importers found", origImporters);
    // $NON-NLS-1$
    assertEquals("Wrong number of importers", 1, origImporters.length);
    // $NON-NLS-1$
    assertEquals("Wrong importer", importerExporter2, origImporters[0]);
    // $NON-NLS-1$
    ExportedPackage[] newExportedPackages = pa.getExportedPackages("exporter.importer.test");
    // $NON-NLS-1$
    assertNotNull("No exporter.importer.test found", newExportedPackages);
    // $NON-NLS-1$
    assertEquals("Wrong number of exports", 1, newExportedPackages.length);
    exporter = newExportedPackages[0].getExportingBundle();
    // $NON-NLS-1$
    assertEquals("Wrong exporter", importerExporter1, exporter);
    Bundle[] newImporters = newExportedPackages[0].getImportingBundles();
    // $NON-NLS-1$
    assertNotNull("No importers found", newImporters);
    // $NON-NLS-1$
    assertEquals("Wrong number of importers", 1, newImporters.length);
    // $NON-NLS-1$
    assertEquals("Wrong importer", importerExporter2, newImporters[0]);
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Aggregations

PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)38 Bundle (org.osgi.framework.Bundle)21 BundleContext (org.osgi.framework.BundleContext)14 ServiceReference (org.osgi.framework.ServiceReference)14 ExportedPackage (org.osgi.service.packageadmin.ExportedPackage)12 Test (org.junit.Test)11 RequiredBundle (org.osgi.service.packageadmin.RequiredBundle)6 StartLevel (org.osgi.service.startlevel.StartLevel)6 ArrayList (java.util.ArrayList)5 FrameworkUtils.getServicesInUseByBundle (org.apache.aries.jmx.util.FrameworkUtils.getServicesInUseByBundle)5 Version (org.osgi.framework.Version)4 File (java.io.File)3 IOException (java.io.IOException)3 Hashtable (java.util.Hashtable)3 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)3 Logger (org.apache.aries.jmx.Logger)3 Expectations (org.jmock.Expectations)3 BundleException (org.osgi.framework.BundleException)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 LinkedHashMap (java.util.LinkedHashMap)2