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