use of org.osgi.framework.wiring.BundleWiring in project karaf by apache.
the class Capabilities method doExecute.
@Override
protected Object doExecute(List<Bundle> bundles) throws Exception {
boolean separatorNeeded = false;
Pattern ns = Pattern.compile(namespace.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*"));
for (Bundle b : bundles) {
if (separatorNeeded) {
System.out.println("");
}
// Print out any matching generic capabilities.
BundleWiring wiring = b.adapt(BundleWiring.class);
if (wiring != null) {
String title = b + " provides:";
System.out.println(title);
System.out.println(ShellUtil.getUnderlineString(title));
// Print generic capabilities for matching namespaces.
boolean matches = printMatchingCapabilities(wiring, ns);
// of the generic model in OSGi.
if (matchNamespace(ns, NONSTANDARD_SERVICE_NAMESPACE)) {
matches |= printServiceCapabilities(b);
}
// then say so.
if (!matches) {
System.out.println(namespace + " " + EMPTY_MESSAGE);
}
} else {
System.out.println("Bundle " + b.getBundleId() + " is not resolved.");
}
separatorNeeded = true;
}
return null;
}
use of org.osgi.framework.wiring.BundleWiring in project karaf by apache.
the class Classes method executeOnBundle.
@Override
protected void executeOnBundle(Bundle bundle) throws Exception {
BundleWiring wiring = bundle.adapt(BundleWiring.class);
if (wiring != null) {
Collection<String> resources;
if (displayAllFiles) {
resources = wiring.listResources("/", null, BundleWiring.LISTRESOURCES_RECURSE);
} else {
resources = wiring.listResources("/", "*class", BundleWiring.LISTRESOURCES_RECURSE);
}
Collection<String> localResources;
if (displayAllFiles) {
localResources = wiring.listResources("/", null, BundleWiring.LISTRESOURCES_RECURSE | BundleWiring.LISTRESOURCES_LOCAL);
} else {
localResources = wiring.listResources("/", "/*.class", BundleWiring.LISTRESOURCES_RECURSE | BundleWiring.LISTRESOURCES_LOCAL);
}
for (String resource : resources) {
if (localResources.contains(resource)) {
System.out.println(SimpleAnsi.INTENSITY_BOLD + resource + SimpleAnsi.INTENSITY_NORMAL);
} else {
System.out.println(resource);
}
}
} else {
System.out.println("Bundle " + bundle.getBundleId() + " is not resolved.");
}
}
use of org.osgi.framework.wiring.BundleWiring in project karaf by apache.
the class FindClass method findResource.
protected void findResource() {
Bundle[] bundles = bundleContext.getBundles();
String filter = "*" + className + "*";
for (Bundle bundle : bundles) {
BundleWiring wiring = bundle.adapt(BundleWiring.class);
if (wiring != null) {
Collection<String> resources = wiring.listResources("/", filter, BundleWiring.LISTRESOURCES_RECURSE);
if (resources.size() > 0) {
String title = ShellUtil.getBundleName(bundle);
System.out.println("\n" + title);
}
for (String resource : resources) {
System.out.println(resource);
}
} else {
System.out.println("Bundle " + bundle.getBundleId() + " is not resolved.");
}
}
}
use of org.osgi.framework.wiring.BundleWiring in project karaf by apache.
the class CommandExtension method start.
public void start() throws Exception {
try {
String header = bundle.getHeaders().get(CommandExtender.KARAF_COMMANDS);
Clause[] clauses = Parser.parseHeader(header);
BundleWiring wiring = bundle.adapt(BundleWiring.class);
for (Clause clause : clauses) {
String name = clause.getName();
int options = BundleWiring.LISTRESOURCES_LOCAL;
name = name.replace('.', '/');
if (name.endsWith("*")) {
options |= BundleWiring.LISTRESOURCES_RECURSE;
name = name.substring(0, name.length() - 1);
}
if (!name.startsWith("/")) {
name = "/" + name;
}
if (name.endsWith("/")) {
name = name.substring(0, name.length() - 1);
}
Collection<String> classes = wiring.listResources(name, "*.class", options);
for (String className : classes) {
className = className.replace('/', '.').replace(".class", "");
try {
inspectClass(bundle.loadClass(className));
} catch (final ClassNotFoundException | NoClassDefFoundError ex) {
LOGGER.info("Inspection of class {} failed.", className, ex);
}
}
}
AggregateServiceTracker.State state = tracker.open();
if (!state.isSatisfied()) {
LOGGER.info("Command registration delayed for bundle {}/{}. Missing dependencies: {}", bundle.getSymbolicName(), bundle.getVersion(), state.getMissingServices());
} else {
updateState(state);
}
} finally {
started.countDown();
}
}
use of org.osgi.framework.wiring.BundleWiring in project karaf by apache.
the class GuardProxyCatalogTest method testCreateProxy.
@SuppressWarnings({ "unchecked", "rawtypes" })
public Dictionary<String, Object> testCreateProxy(BundleContext bc, Class intf, final Class proxyRegClass, Object testService) throws Exception {
// Create the object that is actually being tested here
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
long serviceID = 456L;
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { intf.getName() });
serviceProps.put(Constants.SERVICE_ID, serviceID);
serviceProps.put(".foo", 123L);
final Map<ServiceReference<?>, Object> serviceMap = new HashMap<>();
// The mock bundle context for the bundle providing the service is set up here
BundleContext providerBC = EasyMock.createMock(BundleContext.class);
// These are the expected service properties of the proxy registration. Note the proxy marker...
final Hashtable<String, Object> expectedProxyProps = new Hashtable<>(serviceProps);
expectedProxyProps.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
// This will check that the right proxy is being registered.
EasyMock.expect(providerBC.registerService(EasyMock.isA(String[].class), EasyMock.anyObject(), EasyMock.isA(Dictionary.class))).andAnswer((IAnswer) () -> {
if (!runningUnderCoverage) {
assertArrayEquals(new String[] { proxyRegClass.getName() }, (String[]) EasyMock.getCurrentArguments()[0]);
Object svc = EasyMock.getCurrentArguments()[1];
assertTrue(svc instanceof ServiceFactory);
}
Dictionary<String, Object> props = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2];
for (String key : expectedProxyProps.keySet()) {
assertEquals(expectedProxyProps.get(key), props.get(key));
}
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.unregister();
EasyMock.expectLastCall().once();
EasyMock.replay(reg);
serviceMap.put(sr, EasyMock.getCurrentArguments()[1]);
return reg;
}).once();
EasyMock.expect(providerBC.getService(EasyMock.isA(ServiceReference.class))).andAnswer(() -> serviceMap.get(EasyMock.getCurrentArguments()[0])).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
assertEquals("Precondition", 0, gpc.proxyMap.size());
assertEquals("Precondition", 0, gpc.createProxyQueue.size());
// Create the proxy for the service
gpc.proxyIfNotAlreadyProxied(sr);
assertEquals(1, gpc.proxyMap.size());
// The actual proxy creation is done asynchronously.
GuardProxyCatalog.ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
assertNull("The registration shouldn't have happened yet", holder.registration);
assertEquals(1, gpc.createProxyQueue.size());
// Mimic the thread that works the queue to create the proxy
GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
ProxyManager pm = getProxyManager();
runnable.run(pm);
// The runnable should have put the actual registration in the holder
ServiceReference<?> proxySR = holder.registration.getReference();
for (String key : expectedProxyProps.keySet()) {
assertEquals(expectedProxyProps.get(key), proxySR.getProperty(key));
}
// Check that the proxy registration was done on the original provider bundle's context
EasyMock.verify(providerBC);
// Test that the actual proxy invokes the original service...
Object proxyService = serviceMap.get(proxySR);
assertNotSame("The proxy should not be the same object as the original service", testService, proxyService);
// Attempt to proxy the service again, make sure that no re-proxying happens
assertEquals("Precondition", 1, gpc.proxyMap.size());
assertEquals("Precondition", 0, gpc.createProxyQueue.size());
gpc.proxyIfNotAlreadyProxied(sr);
assertEquals("No additional proxy should have been created", 1, gpc.proxyMap.size());
assertEquals("No additional work on the queue is expected", 0, gpc.createProxyQueue.size());
Dictionary<String, Object> proxyProps = getServiceReferenceProperties(proxySR);
gpc.close();
// checks that the unregister call was made
EasyMock.verify(holder.registration);
return proxyProps;
}
Aggregations