Search in sources :

Example 26 with ManagedService

use of org.osgi.service.cm.ManagedService in project felix by apache.

the class ConfigurationDependencyImpl method start.

@Override
public void start() {
    BundleContext context = m_component.getBundleContext();
    if (context != null) {
        // If null, we are in a test environment
        Properties props = new Properties();
        props.put(Constants.SERVICE_PID, m_pid);
        ManagedService ms = this;
        if (m_metaType != null) {
            ms = m_metaType;
        }
        m_registration = context.registerService(ManagedService.class.getName(), ms, toR6Dictionary(props));
    }
    super.start();
}
Also used : ManagedService(org.osgi.service.cm.ManagedService) Properties(java.util.Properties) BundleContext(org.osgi.framework.BundleContext)

Example 27 with ManagedService

use of org.osgi.service.cm.ManagedService in project felix by apache.

the class ManagedServiceTracker method updateService.

private void updateService(ServiceReference<ManagedService> service, final TargetedPID configPid, Dictionary<String, ?> properties, long revision, ConfigurationMap<?> configs) {
    // Get the ManagedService and terminate here if already
    // unregistered from the framework concurrently
    final ManagedService srv = this.getRealService(service);
    if (srv == null) {
        return;
    }
    // already unregistered this service concurrently
    if (configs == null) {
        configs = this.getService(service);
        if (configs == null) {
            return;
        }
    }
    // Both the ManagedService to update and the Configuration-to-PID
    // are available, so the service can be updated with the
    // configuration (which may be null)
    boolean doUpdate = false;
    if (properties == null) {
        doUpdate = configs.removeConfiguration(configPid, null);
    } else if (properties == INITIAL_MARKER) {
        // initial call to ManagedService may supply null properties
        properties = null;
        revision = -1;
        doUpdate = true;
    } else if (revision < 0 || configs.shallTake(configPid, null, revision)) {
        // run the plugins and cause the update
        properties = getProperties(properties, service, configPid.toString(), null);
        doUpdate = true;
        revision = Math.abs(revision);
    } else {
        // new configuration is not a better match, don't update
        doUpdate = false;
    }
    if (doUpdate) {
        try {
            updated(service, srv, properties);
            configs.record(configPid, null, revision);
        } catch (Throwable t) {
            this.handleCallBackError(t, service, configPid);
        } finally {
            this.ungetRealService(service);
        }
    }
}
Also used : ManagedService(org.osgi.service.cm.ManagedService)

Example 28 with ManagedService

use of org.osgi.service.cm.ManagedService in project aries by apache.

the class IsolatedCfgAdminRuntimeTest method assertExpectedServices.

/**
 * Assert that the following services are present in the service registry:
 * <p/>
 * - ConfigurationAdmin
 * - ManagedService
 * - HelloWorld
 *
 * @param ctx the bundle context
 * @param pid the service pid used to register the underlying ManagedService
 * @throws Exception
 */
private void assertExpectedServices(RichBundleContext ctx, String pid) throws Exception {
    // assert the CfgAdmin service was registered
    Assert.assertNotNull("Missing the ConfigurationAdmin service", ctx.getService(ConfigurationAdmin.class));
    // assert we have the ManagedService exposed
    Assert.assertNotNull("Missing the Managed service", ctx.getService(ManagedService.class, "(" + Constants.SERVICE_PID + "=" + pid + ")"));
    // now just make sure we can see it through the context of our config admin bundle context (should be in the same scope)
    ServiceReference ref = ctx.getServiceReference(ConfigurationAdmin.class.getName());
    Assert.assertNotNull("Couldn't find the ManagedService using the ConfigAdmin bundle context", new RichBundleContext(ref.getBundle().getBundleContext()).getService(ManagedService.class, "(" + Constants.SERVICE_PID + "=" + pid + ")"));
    // make sure we have the helloworld service registered
    HelloWorld helloWorldBluePrint = IsolationTestUtils.findHelloWorldService(ctx);
    Assert.assertNotNull("Missing the HelloWorld service", helloWorldBluePrint);
}
Also used : ManagedService(org.osgi.service.cm.ManagedService) RichBundleContext(org.apache.aries.itest.RichBundleContext) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference) HelloWorld(org.apache.aries.isolated.sample.HelloWorld)

Example 29 with ManagedService

use of org.osgi.service.cm.ManagedService in project ddf by codice.

the class ConfigurationAdminImpl method listServices.

@Override
public List<Service> listServices(String serviceFactoryFilter, String serviceFilter) {
    List<Service> serviceList = null;
    List<Service> serviceFactoryList = null;
    Map<Long, MetaTypeInformation> metaTypeInformationByBundle = new HashMap<>();
    try {
        // Get ManagedService instances
        serviceList = getServices(ManagedService.class.getName(), serviceFilter, true);
        Map<String, ObjectClassDefinition> configPidToOcdMap = getPidObjectClasses(metaTypeInformationByBundle);
        // Get ManagedService Metatypes
        List<Metatype> metatypeList = addMetaTypeNamesToMap(configPidToOcdMap, serviceFilter, SERVICE_PID);
        // Get ManagedServiceFactory instances
        serviceFactoryList = getServices(ManagedServiceFactory.class.getName(), serviceFactoryFilter, true);
        // Get ManagedServiceFactory Metatypes
        metatypeList.addAll(addMetaTypeNamesToMap(getFactoryPidObjectClasses(metaTypeInformationByBundle), serviceFactoryFilter, SERVICE_FACTORYPID));
        for (Service service : serviceFactoryList) {
            service.setFactory(true);
            for (Metatype metatype : metatypeList) {
                if (metatype.getId() != null && metatype.getId().equals(service.getId())) {
                    service.putAll(metatype);
                }
            }
            Configuration[] configs = configurationAdmin.listConfigurations("(|(service.factoryPid=" + service.getId() + ")(service.factoryPid=" + service.getId() + "_disabled))");
            if (configs != null) {
                addConfigurationData(service, configs, configPidToOcdMap);
            }
        }
        for (Service service : serviceList) {
            service.setFactory(false);
            for (Metatype metatype : metatypeList) {
                if (metatype.getId() != null && metatype.getId().equals(service.getId())) {
                    service.putAll(metatype);
                }
            }
            Configuration[] configs = configurationAdmin.listConfigurations("(" + SERVICE_PID + "=" + service.getId() + ")");
            if (configs != null) {
                addConfigurationData(service, configs, configPidToOcdMap);
            }
        }
        serviceList.addAll(serviceFactoryList);
    } catch (IOException e) {
        LOGGER.warn("Unable to obtain list of Configuration objects from ConfigurationAdmin.", e);
    } catch (InvalidSyntaxException e) {
        LOGGER.info("Provided LDAP filter is incorrect: {}", LogSanitizer.sanitize(serviceFilter), e);
    }
    if (serviceList != null) {
        return serviceList.stream().filter(service -> isPermittedToViewService(service.getId())).collect(Collectors.toList());
    } else {
        return new ArrayList<>();
    }
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) Constants(org.osgi.framework.Constants) Enumeration(java.util.Enumeration) LoggerFactory(org.slf4j.LoggerFactory) ConfigurationStatus(org.codice.ddf.admin.core.api.ConfigurationStatus) LogSanitizer(org.codice.ddf.log.sanitizer.LogSanitizer) Locale(java.util.Locale) MetatypeAttribute(org.codice.ddf.admin.core.api.MetatypeAttribute) Map(java.util.Map) Bundle(org.osgi.framework.Bundle) ServiceReference(org.osgi.framework.ServiceReference) Service(org.codice.ddf.admin.core.api.Service) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Metatype(org.codice.ddf.admin.core.api.Metatype) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ConfigurationAdminPlugin(org.codice.ddf.ui.admin.api.plugin.ConfigurationAdminPlugin) BundleContext(org.osgi.framework.BundleContext) ConfigurationDetails(org.codice.ddf.admin.core.api.ConfigurationDetails) Permissions(ddf.security.permission.Permissions) List(java.util.List) Stream(java.util.stream.Stream) Entry(java.util.Map.Entry) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) SecurityUtils(org.apache.shiro.SecurityUtils) ConfigurationProperties(org.codice.ddf.admin.core.api.ConfigurationProperties) Dictionary(java.util.Dictionary) SERVICE_PID(org.osgi.framework.Constants.SERVICE_PID) HashMap(java.util.HashMap) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ArrayList(java.util.ArrayList) SERVICE_FACTORYPID(org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID) Configuration(org.osgi.service.cm.Configuration) Subject(org.apache.shiro.subject.Subject) Hashtable(java.util.Hashtable) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Logger(org.slf4j.Logger) IOException(java.io.IOException) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ManagedService(org.osgi.service.cm.ManagedService) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) Configuration(org.osgi.service.cm.Configuration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Service(org.codice.ddf.admin.core.api.Service) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ManagedService(org.osgi.service.cm.ManagedService) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) IOException(java.io.IOException) Metatype(org.codice.ddf.admin.core.api.Metatype) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Aggregations

ManagedService (org.osgi.service.cm.ManagedService)29 ServiceReference (org.osgi.framework.ServiceReference)23 Hashtable (java.util.Hashtable)22 ConfigurationException (org.osgi.service.cm.ConfigurationException)20 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)19 Test (org.junit.Test)19 Properties (java.util.Properties)10 Dictionary (java.util.Dictionary)7 BundleContext (org.osgi.framework.BundleContext)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 Configuration (org.osgi.service.cm.Configuration)3 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)3 Enumeration (java.util.Enumeration)2 Bundle (org.osgi.framework.Bundle)2 Sets (com.google.common.collect.Sets)1 BasicDBObject (com.mongodb.BasicDBObject)1 CommandResult (com.mongodb.CommandResult)1