Search in sources :

Example 21 with PackageAdmin

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));
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) FrameworkUtils.getServicesInUseByBundle(org.apache.aries.jmx.util.FrameworkUtils.getServicesInUseByBundle) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Hashtable(java.util.Hashtable) Matchers.anyString(org.mockito.Matchers.anyString) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 22 with PackageAdmin

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));
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) FrameworkUtils.getServicesInUseByBundle(org.apache.aries.jmx.util.FrameworkUtils.getServicesInUseByBundle) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) Test(org.junit.Test)

Example 23 with PackageAdmin

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);
    }
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 24 with PackageAdmin

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());
}
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 25 with PackageAdmin

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));
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)27 Bundle (org.osgi.framework.Bundle)17 BundleContext (org.osgi.framework.BundleContext)13 ServiceReference (org.osgi.framework.ServiceReference)12 Test (org.junit.Test)11 ExportedPackage (org.osgi.service.packageadmin.ExportedPackage)7 RequiredBundle (org.osgi.service.packageadmin.RequiredBundle)6 StartLevel (org.osgi.service.startlevel.StartLevel)6 FrameworkUtils.getServicesInUseByBundle (org.apache.aries.jmx.util.FrameworkUtils.getServicesInUseByBundle)5 ArrayList (java.util.ArrayList)4 Version (org.osgi.framework.Version)4 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 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 File (java.io.File)2 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2