use of org.osgi.framework.ServiceReference 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.ServiceReference in project karaf by apache.
the class DSFactoriesCommand method execute.
@Override
public Object execute() throws Exception {
ShellTable table = new ShellTable();
table.column("Name");
table.column("Class");
table.column("Version");
Collection<ServiceReference<DataSourceFactory>> refs = context.getServiceReferences(DataSourceFactory.class, null);
for (ServiceReference<DataSourceFactory> ref : refs) {
String driverName = (String) ref.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_NAME);
String driverClass = (String) ref.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS);
String driverVersion = (String) ref.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION);
table.addRow().addContent(driverName, driverClass, driverVersion);
}
table.print(System.out);
return null;
}
use of org.osgi.framework.ServiceReference 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.ServiceReference 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.ServiceReference 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