Search in sources :

Example 61 with Bundle

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);
}
Also used : StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) Bundle(org.osgi.framework.Bundle) ServiceReference(org.osgi.framework.ServiceReference)

Example 62 with Bundle

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;
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference)

Example 63 with Bundle

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;
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ServiceReference(org.osgi.framework.ServiceReference)

Example 64 with Bundle

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();
}
Also used : Bundle(org.osgi.framework.Bundle)

Example 65 with Bundle

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());
    }
}
Also used : Bundle(org.osgi.framework.Bundle) TabularType(javax.management.openmbean.TabularType) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) ArrayList(java.util.ArrayList) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) TabularData(javax.management.openmbean.TabularData) ServiceReference(org.osgi.framework.ServiceReference) ArrayType(javax.management.openmbean.ArrayType) TabularDataSupport(javax.management.openmbean.TabularDataSupport) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) CompositeType(javax.management.openmbean.CompositeType)

Aggregations

Bundle (org.osgi.framework.Bundle)2490 Test (org.junit.Test)659 URL (java.net.URL)388 BundleContext (org.osgi.framework.BundleContext)311 File (java.io.File)298 ArrayList (java.util.ArrayList)292 IOException (java.io.IOException)278 BundleException (org.osgi.framework.BundleException)270 HashMap (java.util.HashMap)149 ServiceReference (org.osgi.framework.ServiceReference)145 Path (org.eclipse.core.runtime.Path)126 Hashtable (java.util.Hashtable)124 HashSet (java.util.HashSet)95 InputStream (java.io.InputStream)94 IStatus (org.eclipse.core.runtime.IStatus)86 Status (org.eclipse.core.runtime.Status)82 List (java.util.List)80 Map (java.util.Map)74 BundleWiring (org.osgi.framework.wiring.BundleWiring)74 Version (org.osgi.framework.Version)73