Search in sources :

Example 21 with ServiceReference

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

Example 22 with ServiceReference

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;
}
Also used : ShellTable(org.apache.karaf.shell.support.table.ShellTable) DataSourceFactory(org.osgi.service.jdbc.DataSourceFactory) ServiceReference(org.osgi.framework.ServiceReference)

Example 23 with ServiceReference

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

Example 24 with ServiceReference

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

Example 25 with ServiceReference

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());
    }
}
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

ServiceReference (org.osgi.framework.ServiceReference)1687 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)229 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)182 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65