use of org.osgi.framework.Bundle in project karaf by apache.
the class InfoAction method getOsgiFramework.
String getOsgiFramework() {
try {
Callable<String> call = new Callable<String>() {
@Override
public String call() throws Exception {
BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
Bundle sysBundle = context.getBundle(0);
return sysBundle.getSymbolicName() + "-" + sysBundle.getVersion();
}
};
return call.call();
} catch (Throwable t) {
// We're not in OSGi, just safely return null
return null;
}
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class GuardingFindHookTest method mockConfigAdminBundleContext.
@SuppressWarnings({ "rawtypes", "unchecked" })
private BundleContext mockConfigAdminBundleContext(Dictionary<String, Object>... configs) throws IOException, InvalidSyntaxException {
Configuration[] configurations = new Configuration[configs.length];
for (int i = 0; i < configs.length; i++) {
Configuration conf = EasyMock.createMock(Configuration.class);
EasyMock.expect(conf.getProperties()).andReturn(configs[i]).anyTimes();
EasyMock.expect(conf.getPid()).andReturn((String) configs[i].get(Constants.SERVICE_PID)).anyTimes();
EasyMock.replay(conf);
configurations[i] = conf;
}
ConfigurationAdmin ca = EasyMock.createMock(ConfigurationAdmin.class);
EasyMock.expect(ca.listConfigurations("(&(service.pid=org.apache.karaf.service.acl.*)(service.guard=*))")).andReturn(configurations).anyTimes();
EasyMock.replay(ca);
final ServiceReference caSR = EasyMock.createMock(ServiceReference.class);
EasyMock.replay(caSR);
Bundle b = EasyMock.createMock(Bundle.class);
EasyMock.expect(b.getBundleId()).andReturn(877342449L).anyTimes();
EasyMock.replay(b);
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
EasyMock.expect(bc.createFilter(EasyMock.isA(String.class))).andAnswer(() -> FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0])).anyTimes();
String cmFilter = "(&(objectClass=" + ConfigurationAdmin.class.getName() + ")" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
bc.addServiceListener(EasyMock.isA(ServiceListener.class), EasyMock.eq(cmFilter));
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(bc.getServiceReferences(EasyMock.anyObject(String.class), EasyMock.eq(cmFilter))).andReturn(new ServiceReference<?>[] { caSR }).anyTimes();
EasyMock.expect(bc.getService(caSR)).andReturn(ca).anyTimes();
EasyMock.replay(bc);
return bc;
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class GuardProxyCatalogTest method testGuardProxyCatalog.
@Test
public void testGuardProxyCatalog() throws Exception {
Bundle b = EasyMock.createMock(Bundle.class);
EasyMock.expect(b.getBundleId()).andReturn(9823L).anyTimes();
EasyMock.replay(b);
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
bc.addServiceListener(EasyMock.isA(ServiceListener.class));
EasyMock.expectLastCall().once();
String caFilter = "(&(objectClass=org.osgi.service.cm.ConfigurationAdmin)" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
EasyMock.expect(bc.createFilter(caFilter)).andReturn(FrameworkUtil.createFilter(caFilter)).anyTimes();
String pmFilter = "(&(objectClass=org.apache.aries.proxy.ProxyManager)" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
EasyMock.expect(bc.createFilter(pmFilter)).andReturn(FrameworkUtil.createFilter(pmFilter)).anyTimes();
EasyMock.replay(bc);
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
assertTrue("Service Tracker for ConfigAdmin should be opened", gpc.configAdminTracker.getTrackingCount() != -1);
assertTrue("Service Tracker for ProxyManager should be opened", gpc.proxyManagerTracker.getTrackingCount() != -1);
EasyMock.verify(bc);
// Add some more behaviour checks to the bundle context
EasyMock.reset(bc);
bc.removeServiceListener(EasyMock.isA(ServiceListener.class));
EasyMock.expectLastCall().once();
EasyMock.replay(bc);
gpc.close();
assertEquals("Service Tracker for ConfigAdmin should be closed", -1, gpc.configAdminTracker.getTrackingCount());
assertEquals("Service Tracker for ProxyManager should be closed", -1, gpc.proxyManagerTracker.getTrackingCount());
EasyMock.verify(bc);
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class WebContainerServiceImpl method stop.
public void stop(List<Long> bundleIds) throws Exception {
if (bundleIds != null && !bundleIds.isEmpty()) {
for (long bundleId : bundleIds) {
if (webEventHandler.getBundleEvents().containsKey(bundleId)) {
WebEvent webEvent = webEventHandler.getBundleEvents().get(bundleId);
Bundle bundle = webEvent.getBundle();
if (bundle != null) {
// deploy
warManager.stop(bundleId);
} else {
System.out.println("Bundle ID " + bundleId + " is invalid");
LOGGER.warn("Bundle ID {} is invalid", bundleId);
}
}
}
}
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class WebContainerServiceImpl method list.
public List<WebBundle> list() throws Exception {
Bundle[] bundles = bundleContext.getBundles();
Map<Long, WebEvent> bundleEvents = webEventHandler.getBundleEvents();
List<WebBundle> webBundles = new ArrayList<>();
if (bundles != null) {
for (Bundle bundle : bundles) {
// first check if the bundle is a web bundle
String contextPath = bundle.getHeaders().get("Web-ContextPath");
if (contextPath == null) {
// this one used by pax-web but is deprecated
contextPath = bundle.getHeaders().get("Webapp-Context");
}
if (contextPath == null) {
// the bundle is not a web bundle
continue;
}
WebBundle webBundle = new WebBundle();
contextPath = contextPath.trim();
// get the bundle name
String name = bundle.getHeaders().get(Constants.BUNDLE_NAME);
// if there is no name, then default to symbolic name
name = (name == null) ? bundle.getSymbolicName() : name;
// if there is no symbolic name, resort to location
name = (name == null) ? bundle.getLocation() : name;
// get the bundle version
String version = bundle.getHeaders().get(Constants.BUNDLE_VERSION);
name = ((version != null)) ? name + " (" + version + ")" : name;
long bundleId = bundle.getBundleId();
int level = bundle.adapt(BundleStartLevel.class).getStartLevel();
if (!contextPath.startsWith("/")) {
contextPath = "/" + contextPath;
}
webBundle.setBundleId(bundleId);
webBundle.setName(name);
webBundle.setContextPath(contextPath);
webBundle.setLevel(level);
webBundle.setState(getStateString(bundle));
webBundle.setWebState(state(bundle.getBundleId()));
webBundles.add(webBundle);
}
}
return webBundles;
}
Aggregations