Search in sources :

Example 26 with PackageAdmin

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

the class FrameworkMBeanHandler method open.

/**
     * @see org.apache.aries.jmx.MBeanHandler#open()
     */
public void open() {
    ServiceReference adminRef = context.getServiceReference(PackageAdmin.class.getCanonicalName());
    PackageAdmin packageAdmin = (PackageAdmin) context.getService(adminRef);
    ServiceReference startLevelRef = context.getServiceReference(StartLevel.class.getCanonicalName());
    StartLevel startLevel = (StartLevel) context.getService(startLevelRef);
    FrameworkMBean framework = new Framework(context, startLevel, packageAdmin);
    try {
        mbean = new StandardMBean(framework, FrameworkMBean.class);
    } catch (NotCompliantMBeanException e) {
        logger.log(LogService.LOG_ERROR, "Not compliant MBean", e);
    }
    agentContext.registerMBean(this);
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) StandardMBean(javax.management.StandardMBean) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) StartLevel(org.osgi.service.startlevel.StartLevel) FrameworkMBean(org.osgi.jmx.framework.FrameworkMBean) ServiceReference(org.osgi.framework.ServiceReference)

Example 27 with PackageAdmin

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

the class BundleStateMBeanHandler method open.

/**
     * @see org.apache.aries.jmx.MBeanHandler#open()
     */
public void open() {
    packageAdminRef = bundleContext.getServiceReference(PackageAdmin.class.getName());
    PackageAdmin packageAdmin = (PackageAdmin) bundleContext.getService(packageAdminRef);
    startLevelRef = bundleContext.getServiceReference(StartLevel.class.getName());
    StartLevel startLevel = (StartLevel) bundleContext.getService(startLevelRef);
    bundleStateMBean = new BundleState(bundleContext, packageAdmin, startLevel, stateConfig, logger);
    try {
        mbean = new RegistrableStandardEmitterMBean(bundleStateMBean, BundleStateMBean.class);
    } catch (NotCompliantMBeanException e) {
        logger.log(LogService.LOG_ERROR, "Failed to instantiate MBean for " + BundleStateMBean.class.getName(), e);
    }
    agentContext.registerMBean(this);
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RegistrableStandardEmitterMBean(org.apache.aries.jmx.util.shared.RegistrableStandardEmitterMBean) StartLevel(org.osgi.service.startlevel.StartLevel) BundleStateMBean(org.osgi.jmx.framework.BundleStateMBean)

Example 28 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 29 with PackageAdmin

use of org.osgi.service.packageadmin.PackageAdmin in project bnd by bndtools.

the class Launcher method activate.

public int activate() throws Exception {
    Policy.setPolicy(new AllPolicy());
    systemBundle = createFramework();
    if (systemBundle == null)
        return LauncherConstants.ERROR;
    active.set(true);
    doTimeoutHandler();
    doSecurity();
    // Initialize this framework so it becomes STARTING
    systemBundle.start();
    trace("system bundle started ok");
    BundleContext systemContext = systemBundle.getBundleContext();
    ServiceReference ref = systemContext.getServiceReference(PackageAdmin.class.getName());
    if (ref != null) {
        padmin = (PackageAdmin) systemContext.getService(ref);
    } else
        trace("could not get package admin");
    systemContext.addServiceListener(this, "(&(|(objectclass=" + Runnable.class.getName() + ")(objectclass=" + Callable.class.getName() + "))(main.thread=true))");
    int result = LauncherConstants.OK;
    // Start embedded activators
    trace("start embedded activators");
    if (parms.activators != null) {
        ClassLoader loader = getClass().getClassLoader();
        for (Object token : parms.activators) {
            try {
                Class<?> clazz = loader.loadClass((String) token);
                BundleActivator activator = (BundleActivator) clazz.getConstructor().newInstance();
                if (isImmediate(activator)) {
                    start(systemContext, result, activator);
                }
                embedded.add(activator);
                trace("adding activator %s", activator);
            } catch (Exception e) {
                throw new IllegalArgumentException("Embedded Bundle Activator incorrect: " + token, e);
            }
        }
    }
    update(System.currentTimeMillis() + 100);
    if (parms.trace) {
        report(out);
    }
    for (BundleActivator activator : embedded) if (!isImmediate(activator))
        result = start(systemContext, result, activator);
    return result;
}
Also used : BundleActivator(org.osgi.framework.BundleActivator) Callable(java.util.concurrent.Callable) BundleException(org.osgi.framework.BundleException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) BundleContext(org.osgi.framework.BundleContext)

Example 30 with PackageAdmin

use of org.osgi.service.packageadmin.PackageAdmin in project bnd by bndtools.

the class Activator method findHost.

/*
	 * Find the host for a fragment. We just iterate over all other bundles and
	 * ask for the fragments. We returns the first one found.
	 */
private Bundle findHost(Bundle bundle) {
    if (bundle == null)
        return null;
    PackageAdmin packageAdmin = (PackageAdmin) packageAdminTracker.getService();
    if (packageAdmin == null) {
        trace("Have a potential fragment but Package Admin not present to find the host");
        return bundle;
    }
    if ((packageAdmin.getBundleType(bundle) & PackageAdmin.BUNDLE_TYPE_FRAGMENT) == 0)
        return bundle;
    Bundle found = null;
    for (Bundle potentialFragmentHost : context.getBundles()) {
        if (potentialFragmentHost == bundle || potentialFragmentHost.getBundleId() == 0)
            continue;
        Bundle[] fragments = packageAdmin.getFragments(potentialFragmentHost);
        if (fragments == null)
            continue;
        for (Bundle fragment : fragments) {
            if (fragment == bundle) {
                if (found != null) {
                    trace("Have a test fragment but find multiple hosts. Fragment=%s, Previous=%s, Next=%s ", bundle, found, fragment);
                } else
                    found = potentialFragmentHost;
            }
        }
    }
    return found == null ? bundle : found;
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Bundle(org.osgi.framework.Bundle)

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