use of org.osgi.service.packageadmin.PackageAdmin in project aries by apache.
the class FrameworkUtilsTest method testGetBundleImportedPackages.
@Test
public void testGetBundleImportedPackages() throws Exception {
Bundle bundle = mock(Bundle.class);
BundleContext context = mock(BundleContext.class);
Bundle b1 = mock(Bundle.class);
Bundle b2 = mock(Bundle.class);
Bundle b3 = mock(Bundle.class);
when(context.getBundles()).thenReturn(new Bundle[] { bundle, b1, b2, b3 });
ExportedPackage ep1 = mock(ExportedPackage.class);
when(ep1.getImportingBundles()).thenReturn(new Bundle[] { bundle, b2, b3 });
when(ep1.getName()).thenReturn("org.apache.aries.jmx.b1");
when(ep1.getVersion()).thenReturn(Version.emptyVersion);
ExportedPackage ep2 = mock(ExportedPackage.class);
when(ep2.getImportingBundles()).thenReturn(new Bundle[] { bundle, b3 });
when(ep2.getName()).thenReturn("org.apache.aries.jmx.b2");
when(ep2.getVersion()).thenReturn(Version.parseVersion("2.0.1"));
PackageAdmin admin = mock(PackageAdmin.class);
when(admin.getExportedPackages(b1)).thenReturn(new ExportedPackage[] { ep1 });
when(admin.getExportedPackages(b2)).thenReturn(new ExportedPackage[] { ep2 });
when(admin.getExportedPackages(b3)).thenReturn(null);
//check first with DynamicImport
Dictionary<String, String> headers = new Hashtable<String, String>();
headers.put(Constants.DYNAMICIMPORT_PACKAGE, "*");
when(bundle.getHeaders()).thenReturn(headers);
assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0", "org.apache.aries.jmx.b2;2.0.1" }, getBundleImportedPackages(context, bundle, admin));
//check with ImportPackage statement
headers.remove(Constants.DYNAMICIMPORT_PACKAGE);
String importPackageStatement = "org.apache.aries.jmx.b1;version=0.0.0;resolution:=optional,org.apache.aries.jmx.b2;attribute:=value;version=\"[2.0, 3.0)\"";
headers.put(Constants.IMPORT_PACKAGE, importPackageStatement);
when(admin.getExportedPackages("org.apache.aries.jmx.b1")).thenReturn(new ExportedPackage[] { ep1 });
when(admin.getExportedPackages("org.apache.aries.jmx.b2")).thenReturn(new ExportedPackage[] { ep2 });
assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0", "org.apache.aries.jmx.b2;2.0.1" }, getBundleImportedPackages(context, bundle, admin));
}
use of org.osgi.service.packageadmin.PackageAdmin in project aries by apache.
the class FrameworkUtilsTest method testIsBundlePendingRemoval.
@Test
public void testIsBundlePendingRemoval() throws Exception {
Bundle bundle = mock(Bundle.class);
when(bundle.getSymbolicName()).thenReturn("org.apache.testb");
RequiredBundle reqBundle = mock(RequiredBundle.class);
when(reqBundle.getBundle()).thenReturn(bundle);
when(reqBundle.isRemovalPending()).thenReturn(true);
PackageAdmin admin = mock(PackageAdmin.class);
when(admin.getRequiredBundles("org.apache.testb")).thenReturn(new RequiredBundle[] { reqBundle });
assertTrue(isBundlePendingRemoval(bundle, admin));
}
use of org.osgi.service.packageadmin.PackageAdmin in project karaf by apache.
the class SecuredCommandConfigTransformer method refreshTheAffectedShellCommandBundle.
private void refreshTheAffectedShellCommandBundle(ConfigurationEvent event, Configuration config) {
if (!config.getPid().startsWith(PROXY_COMMAND_ACL_PID_PREFIX)) {
// not a command scope configuration file
return;
}
String filter = "";
String scopeName = config.getPid().substring(PROXY_COMMAND_ACL_PID_PREFIX.length());
if (scopeName.indexOf('.') >= 0) {
// scopes don't contains dots, not a command scope
return;
}
scopeName = scopeName.trim();
for (Entry<String, String> entry : loadScopeBundleMaps().entrySet()) {
if (entry.getKey().equals(scopeName)) {
filter = "(" + "osgi.blueprint.container.symbolicname" + "=" + entry.getValue() + ")";
break;
}
}
if (filter.length() == 0) {
return;
}
BundleContext bundleContext = event.getReference().getBundle().getBundleContext();
try {
ServiceReference<?>[] sr = bundleContext.getServiceReferences("org.osgi.service.blueprint.container.BlueprintContainer", filter);
if (sr == null) {
LOGGER.error("can't find the command bundle for scope " + scopeName);
return;
}
LOGGER.debug("the refreshed bundle is " + sr[0].getBundle().getSymbolicName());
ServiceReference ref = bundleContext.getServiceReference(PackageAdmin.class.getName());
if (ref == null) {
LOGGER.error("PackageAdmin service is unavailable.");
return;
}
try {
PackageAdmin pa = (PackageAdmin) bundleContext.getService(ref);
if (pa == null) {
LOGGER.error("PackageAdmin service is unavailable.");
return;
}
pa.refreshPackages(new Bundle[] { sr[0].getBundle() });
} finally {
bundleContext.ungetService(ref);
}
} catch (InvalidSyntaxException ex) {
LOGGER.error("Problem refresh the affected shell command bundle", ex);
}
}
use of org.osgi.service.packageadmin.PackageAdmin 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());
}
use of org.osgi.service.packageadmin.PackageAdmin in project sling by apache.
the class SlingFelixTest method test_Felix_getBundle_Class_from_PackageAdmin.
@SuppressWarnings("deprecation")
@Test
public void test_Felix_getBundle_Class_from_PackageAdmin() {
/*
* Tests whether the Felix.getBundle(Class) call from PackageAdmin
* is possible. This test should always succeed regardless of the
* SLING-2554 implementation because it uses regular Java call
* methodology.
*/
PackageAdmin pa = getService(framework, PackageAdmin.class);
assertNotNull(pa);
assertNull("Integer class provided by the VM not from a bundle", pa.getBundle(Integer.class));
assertEquals("BundleContext class must come from the framework", framework.getBundle(), pa.getBundle(BundleContext.class));
}
Aggregations