Search in sources :

Example 51 with ConfigurationAdmin

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

the class CswSubscriptionEndpoint method getSubscriptionConfiguration.

private Configuration getSubscriptionConfiguration(String subscriptionUuid) {
    String methodName = "getSubscriptionConfiguration";
    LOGGER.trace("ENTERING: {}", methodName);
    String filterStr = getSubscriptionUuidFilter(subscriptionUuid);
    LOGGER.debug("filterStr = {}", filterStr);
    Configuration config = null;
    try {
        org.osgi.framework.Filter filter = getBundleContext().createFilter(filterStr);
        LOGGER.debug("filter.toString() = {}", filter.toString());
        ConfigurationAdmin configAdmin = getConfigAdmin();
        if (configAdmin != null) {
            Configuration[] configs = configAdmin.listConfigurations(filter.toString());
            if (configs == null) {
                LOGGER.debug("Did NOT find a configuration for filter {}", filterStr);
            } else if (configs.length != 1) {
                LOGGER.debug("Found multiple configurations for filter {}", filterStr);
            } else {
                LOGGER.debug("Found exactly one configuration for filter {}", filterStr);
                config = configs[0];
            }
        }
    } catch (InvalidSyntaxException e) {
        LOGGER.debug("Invalid syntax for filter used for searching configuration instances", e);
    } catch (IOException e) {
        LOGGER.debug("IOException trying to list configurations for filter {}", filterStr, e);
    }
    LOGGER.trace("EXITING: {}", methodName);
    return config;
}
Also used : Configuration(org.osgi.service.cm.Configuration) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 52 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project camel by apache.

the class CamelBlueprintHelper method setPersistentFileForConfigAdmin.

// pick up persistent file configuration
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void setPersistentFileForConfigAdmin(BundleContext bundleContext, String pid, String fileName, final Dictionary props, String symbolicName, Set<Long> bpEvents, boolean expectReload) throws IOException, InterruptedException {
    if (pid != null) {
        if (fileName == null) {
            throw new IllegalArgumentException("The persistent file should not be null");
        } else {
            File load = new File(fileName);
            LOG.debug("Loading properties from OSGi config admin file: {}", load);
            org.apache.felix.utils.properties.Properties cfg = new org.apache.felix.utils.properties.Properties(load);
            for (Object key : cfg.keySet()) {
                props.put(key, cfg.get(key));
            }
            ConfigurationAdmin configAdmin = CamelBlueprintHelper.getOsgiService(bundleContext, ConfigurationAdmin.class);
            if (configAdmin != null) {
                // ensure we update
                // we *have to* use "null" as 2nd arg to have correct bundle location for Configuration object
                final Configuration config = configAdmin.getConfiguration(pid, null);
                LOG.info("Updating ConfigAdmin {} by overriding properties {}", config, props);
                // be CREATED again
                if (expectReload) {
                    CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, bundleContext, symbolicName, BlueprintEvent.CREATED, new Runnable() {

                        @Override
                        public void run() {
                            try {
                                config.update(props);
                            } catch (IOException e) {
                                throw new RuntimeException(e.getMessage(), e);
                            }
                        }
                    });
                } else {
                    config.update(props);
                }
            }
        }
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 53 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project camel by apache.

the class KuraRouter method start.

// Lifecycle
@Override
public void start(BundleContext bundleContext) throws Exception {
    try {
        this.bundleContext = bundleContext;
        log.debug("Initializing bundle {}.", bundleContext.getBundle().getBundleId());
        camelContext = createCamelContext();
        camelContext.addRoutes(this);
        ConfigurationAdmin configurationAdmin = requiredService(ConfigurationAdmin.class);
        Configuration camelKuraConfig = configurationAdmin.getConfiguration(camelXmlRoutesPid());
        if (camelKuraConfig != null && camelKuraConfig.getProperties() != null) {
            Object routePropertyValue = camelKuraConfig.getProperties().get(camelXmlRoutesProperty());
            if (routePropertyValue != null) {
                InputStream routesXml = new ByteArrayInputStream(routePropertyValue.toString().getBytes());
                RoutesDefinition loadedRoutes = camelContext.loadRoutesDefinition(routesXml);
                camelContext.addRouteDefinitions(loadedRoutes.getRoutes());
            }
        }
        beforeStart(camelContext);
        log.debug("About to start Camel Kura router: {}", getClass().getName());
        camelContext.start();
        producerTemplate = camelContext.createProducerTemplate();
        consumerTemplate = camelContext.createConsumerTemplate();
        log.debug("Bundle {} started.", bundleContext.getBundle().getBundleId());
    } catch (Throwable e) {
        String errorMessage = "Problem when starting Kura module " + getClass().getName() + ":";
        log.warn(errorMessage, e);
        // Print error to the Kura console.
        System.err.println(errorMessage);
        e.printStackTrace();
        throw e;
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RoutesDefinition(org.apache.camel.model.RoutesDefinition) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 54 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project opennms by OpenNMS.

the class HeartbeatSinkPerfIT method setUp.

@Before
public void setUp() throws Exception {
    Hashtable<String, Object> kafkaConfig = new Hashtable<String, Object>();
    kafkaConfig.put("bootstrap.servers", kafkaServer.getKafkaConnectString());
    ConfigurationAdmin configAdmin = mock(ConfigurationAdmin.class, RETURNS_DEEP_STUBS);
    when(configAdmin.getConfiguration(KafkaSinkConstants.KAFKA_CONFIG_PID).getProperties()).thenReturn(kafkaConfig);
    messageDispatcherFactory.setConfigAdmin(configAdmin);
    messageDispatcherFactory.init();
    System.setProperty(String.format("%sbootstrap.servers", KafkaSinkConstants.KAFKA_CONFIG_SYS_PROP_PREFIX), kafkaServer.getKafkaConnectString());
    System.setProperty(String.format("%sauto.offset.reset", KafkaSinkConstants.KAFKA_CONFIG_SYS_PROP_PREFIX), "earliest");
    consumerManager.afterPropertiesSet();
}
Also used : Hashtable(java.util.Hashtable) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Before(org.junit.Before)

Example 55 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project opennms by OpenNMS.

the class HeartbeatSinkIT method setUp.

@Before
public void setUp() throws Exception {
    Hashtable<String, Object> kafkaConfig = new Hashtable<String, Object>();
    kafkaConfig.put("bootstrap.servers", kafkaServer.getKafkaConnectString());
    ConfigurationAdmin configAdmin = mock(ConfigurationAdmin.class, RETURNS_DEEP_STUBS);
    when(configAdmin.getConfiguration(KafkaSinkConstants.KAFKA_CONFIG_PID).getProperties()).thenReturn(kafkaConfig);
    remoteMessageDispatcherFactory.setConfigAdmin(configAdmin);
    remoteMessageDispatcherFactory.init();
    System.setProperty(String.format("%sbootstrap.servers", KafkaSinkConstants.KAFKA_CONFIG_SYS_PROP_PREFIX), kafkaServer.getKafkaConnectString());
    System.setProperty(String.format("%sauto.offset.reset", KafkaSinkConstants.KAFKA_CONFIG_SYS_PROP_PREFIX), "earliest");
    consumerManager.afterPropertiesSet();
}
Also used : Hashtable(java.util.Hashtable) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Before(org.junit.Before)

Aggregations

ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)108 Configuration (org.osgi.service.cm.Configuration)42 Subject (javax.security.auth.Subject)24 Test (org.junit.Test)19 Hashtable (java.util.Hashtable)14 BundleContext (org.osgi.framework.BundleContext)14 ServiceReference (org.osgi.framework.ServiceReference)13 IOException (java.io.IOException)9 Dictionary (java.util.Dictionary)8 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Method (java.lang.reflect.Method)6 Bundle (org.osgi.framework.Bundle)6 File (java.io.File)4 Properties (java.util.Properties)4 MBeanServer (javax.management.MBeanServer)4 ObjectName (javax.management.ObjectName)4 KarafMBeanServerGuard (org.apache.karaf.management.KarafMBeanServerGuard)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CompositeData (javax.management.openmbean.CompositeData)3