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;
}
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);
}
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());
}
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();
}
}
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]);
}
Aggregations