use of org.osgi.framework.Bundle in project karaf by apache.
the class ServicesIdCompleter method complete.
@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
StringsCompleter delegate = new StringsCompleter();
Bundle[] bundles = bundleContext.getBundles();
for (Bundle bundle : bundles) {
ServiceReference[] references = bundle.getRegisteredServices();
if (references != null) {
for (ServiceReference reference : references) {
if (reference.getProperty(Constants.SERVICE_ID) != null) {
delegate.getStrings().add(reference.getProperty(Constants.SERVICE_ID).toString());
}
}
}
}
return delegate.complete(session, commandLine, candidates);
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class ListServices method execute.
@Override
public Object execute() throws Exception {
if (onlyNames) {
listNames();
return null;
}
List<ServiceReference<?>> serviceRefs = new ArrayList<>();
Bundle[] bundles = bundleContext.getBundles();
for (Bundle bundle : bundles) {
ServiceReference<?>[] services = bundle.getRegisteredServices();
if (services != null) {
for (ServiceReference<?> ref : services) {
String[] objectClasses = (String[]) ref.getProperty(Constants.OBJECTCLASS);
if (objectClass == null || ObjectClassMatcher.matchesAtLeastOneName(objectClasses, objectClass)) {
serviceRefs.add(ref);
}
}
}
}
Collections.sort(serviceRefs, new ServiceClassComparator());
for (ServiceReference<?> serviceRef : serviceRefs) {
if (showAll || !isCommand((String[]) serviceRef.getProperty(Constants.OBJECTCLASS))) {
printServiceRef(serviceRef);
}
}
return null;
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class ListServices method getServiceNamesMap.
public static Map<String, Integer> getServiceNamesMap(BundleContext bundleContext) {
Map<String, Integer> serviceNames = new HashMap<>();
Bundle[] bundles = bundleContext.getBundles();
for (Bundle bundle : bundles) {
ServiceReference<?>[] services = bundle.getRegisteredServices();
if (services != null) {
for (ServiceReference<?> serviceReference : services) {
String[] names = (String[]) serviceReference.getProperty(Constants.OBJECTCLASS);
if (names != null) {
for (String name : names) {
serviceNames.merge(name, 1, (a, b) -> a + b);
}
}
}
}
}
return serviceNames;
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class ListServices method printServiceRef.
private void printServiceRef(ServiceReference<?> serviceRef) {
String[] objectClass = (String[]) serviceRef.getProperty(Constants.OBJECTCLASS);
String serviceClasses = ShellUtil.getValueString(objectClass);
System.out.println(serviceClasses);
System.out.println(ShellUtil.getUnderlineString(serviceClasses));
printProperties(serviceRef);
String bundleName = ShellUtil.getBundleName(serviceRef.getBundle());
System.out.println("Provided by : ");
System.out.println(" " + bundleName);
Bundle[] usingBundles = serviceRef.getUsingBundles();
if (usingBundles != null) {
System.out.println("Used by: ");
for (Bundle bundle : usingBundles) {
System.out.println(" " + ShellUtil.getBundleName(bundle));
}
}
System.out.println();
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class ServicesMBeanImpl method getServices.
public TabularData getServices(long bundleId, boolean inUse) throws MBeanException {
try {
CompositeType serviceType = new CompositeType("Service", "OSGi Service", new String[] { "Interfaces", "Properties" }, new String[] { "Interfaces class name of the service", "Properties of the service" }, new OpenType[] { new ArrayType(1, SimpleType.STRING), new ArrayType(1, SimpleType.STRING) });
TabularType tableType = new TabularType("Services", "Table of OSGi Services", serviceType, new String[] { "Interfaces", "Properties" });
TabularData table = new TabularDataSupport(tableType);
Bundle[] bundles;
if (bundleId >= 0) {
bundles = new Bundle[] { bundleContext.getBundle(bundleId) };
} else {
bundles = bundleContext.getBundles();
}
for (Bundle bundle : bundles) {
try {
ServiceReference[] serviceReferences;
if (inUse) {
serviceReferences = bundle.getServicesInUse();
} else {
serviceReferences = bundle.getRegisteredServices();
}
if (serviceReferences != null) {
for (ServiceReference reference : serviceReferences) {
String[] interfaces = (String[]) reference.getProperty("objectClass");
List<String> properties = new ArrayList<>();
for (String key : reference.getPropertyKeys()) {
properties.add(key + " = " + reference.getProperty(key));
}
CompositeData data = new CompositeDataSupport(serviceType, new String[] { "Interfaces", "Properties" }, new Object[] { interfaces, properties.toArray(new String[0]) });
table.put(data);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return table;
} catch (Exception e) {
throw new MBeanException(null, e.toString());
}
}
Aggregations