Search in sources :

Example 1 with ConfigurableService

use of org.openhab.core.config.core.ConfigurableService in project openhab-core by openhab.

the class ConfigurableServiceResource method getServicesByFilter.

private List<ConfigurableServiceDTO> getServicesByFilter(String filter, Locale locale) {
    List<ConfigurableServiceDTO> services = new ArrayList<>();
    ServiceReference<?>[] serviceReferences = null;
    try {
        serviceReferences = bundleContext.getServiceReferences((String) null, filter);
    } catch (InvalidSyntaxException ex) {
        logger.error("Cannot get service references because the syntax of the filter '{}' is invalid.", filter);
    }
    if (serviceReferences != null) {
        for (ServiceReference<?> serviceReference : serviceReferences) {
            String id = getServiceId(serviceReference);
            ConfigurableService configurableService = ConfigurableServiceUtil.asConfigurableService((key) -> serviceReference.getProperty(key));
            String defaultLabel = configurableService.label();
            if (defaultLabel.isEmpty()) {
                // for multi context services the label can be changed and must be read
                // from config admin.
                defaultLabel = configurationService.getProperty(id, OpenHAB.SERVICE_CONTEXT);
            }
            String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferKey(configurableService.description_uri(), "label"));
            String label = i18nProvider.getText(serviceReference.getBundle(), key, defaultLabel, locale);
            String category = configurableService.category();
            String configDescriptionURI = configurableService.description_uri();
            if (configDescriptionURI.isEmpty()) {
                String factoryPid = (String) serviceReference.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID);
                configDescriptionURI = getConfigDescriptionByFactoryPid(factoryPid);
            }
            boolean multiple = configurableService.factory();
            services.add(new ConfigurableServiceDTO(id, label == null ? defaultLabel : label, category, configDescriptionURI, multiple));
        }
    }
    return services;
}
Also used : ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ConfigurableServiceDTO(org.openhab.core.io.rest.core.service.ConfigurableServiceDTO) ServiceReference(org.osgi.framework.ServiceReference) ConfigurableService(org.openhab.core.config.core.ConfigurableService)

Example 2 with ConfigurableService

use of org.openhab.core.config.core.ConfigurableService in project openhab-core by openhab.

the class ConfigurableServiceResource method getConfigDescriptionByFactoryPid.

@Nullable
private String getConfigDescriptionByFactoryPid(String factoryPid) {
    String configDescriptionURI = null;
    String filter = "(" + Constants.SERVICE_PID + "=" + factoryPid + ")";
    try {
        ServiceReference<?>[] refs = bundleContext.getServiceReferences((String) null, filter);
        if (refs != null && refs.length > 0) {
            ConfigurableService configurableService = ConfigurableServiceUtil.asConfigurableService((key) -> refs[0].getProperty(key));
            configDescriptionURI = configurableService.description_uri();
        }
    } catch (InvalidSyntaxException e) {
        logger.error("Cannot get service references because the syntax of the filter '{}' is invalid.", filter);
    }
    return configDescriptionURI;
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) ConfigurableService(org.openhab.core.config.core.ConfigurableService) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

ConfigurableService (org.openhab.core.config.core.ConfigurableService)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 ServiceReference (org.osgi.framework.ServiceReference)2 ArrayList (java.util.ArrayList)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 ConfigurableServiceDTO (org.openhab.core.io.rest.core.service.ConfigurableServiceDTO)1