Search in sources :

Example 6 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ApplicationServiceBeanTest method testGetServices.

/**
 * Tests the {@link ApplicationServiceBean#getServices(String)} method
 *
 * @throws Exception
 */
@Test
public void testGetServices() throws Exception {
    ApplicationServiceBean serviceBean = newApplicationServiceBean();
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    List<Service> services = new ArrayList<>();
    Service testService1 = new ServiceImpl();
    List<Map<String, Object>> testService1Configs = new ArrayList<>();
    Map<String, Object> testConfig1 = new HashMap<>();
    testConfig1.put("bundle_location", TEST_LOCATION);
    testService1Configs.add(testConfig1);
    services.add(testService1);
    testService1.put("configurations", testService1Configs);
    BundleInfo testBundle1 = mock(BundleInfo.class);
    Set<BundleInfo> testBundles = new HashSet<>();
    testBundles.add(testBundle1);
    when(testApp.getBundles()).thenReturn(testBundles);
    when(testBundle1.getLocation()).thenReturn(TEST_LOCATION);
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    assertThat("Should find the given services.", serviceBean.getServices(TEST_APP_NAME).get(0), is(testService1));
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ServiceImpl(org.codice.ddf.admin.core.impl.ServiceImpl) ArrayList(java.util.ArrayList) SystemService(org.apache.karaf.system.SystemService) Service(org.codice.ddf.admin.core.api.Service) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BundleInfo(org.apache.karaf.features.BundleInfo) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ServiceOperation method readState.

private static Map<String, Object> readState(String pid, AdminConsoleServiceMBean adminConsoleServiceMBean) throws ConfiguratorException {
    try {
        Map<String, Object> configResults = adminConsoleServiceMBean.getProperties(pid);
        if (configResults.isEmpty()) {
            Optional<Service> defaultMetatypeValues = adminConsoleServiceMBean.listServices().stream().filter(service -> service.getId() != null && service.getId().equals(pid)).findFirst();
            List<MetatypeAttribute> metatypes = new ArrayList<>();
            if (defaultMetatypeValues.isPresent()) {
                metatypes = defaultMetatypeValues.get().getAttributeDefinitions();
            }
            return metatypes.stream().collect(Collectors.toMap(MetatypeAttribute::getId, MetatypeAttribute::getDefaultValue));
        } else {
            return configResults;
        }
    } catch (IOException e) {
        throw new ConfiguratorException(String.format("Unable to find configuration for pid, %s", pid), e);
    }
}
Also used : ConfigValidator.validateString(org.codice.ddf.admin.configurator.impl.ConfigValidator.validateString) ConfiguratorException(org.codice.ddf.admin.configurator.ConfiguratorException) Logger(org.slf4j.Logger) Service(org.codice.ddf.admin.core.api.Service) ServiceActions(org.codice.ddf.internal.admin.configurator.actions.ServiceActions) LoggerFactory(org.slf4j.LoggerFactory) Result(org.codice.ddf.admin.configurator.Result) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) MalformedObjectNameException(javax.management.MalformedObjectNameException) List(java.util.List) Operation(org.codice.ddf.admin.configurator.Operation) AdminConsoleServiceMBean(org.codice.ddf.admin.core.api.jmx.AdminConsoleServiceMBean) ConfigValidator.validateMap(org.codice.ddf.admin.configurator.impl.ConfigValidator.validateMap) MetatypeAttribute(org.codice.ddf.admin.core.api.MetatypeAttribute) Map(java.util.Map) OsgiUtils.getConfigAdminMBean(org.codice.ddf.admin.configurator.impl.OsgiUtils.getConfigAdminMBean) Optional(java.util.Optional) Collections(java.util.Collections) ArrayList(java.util.ArrayList) Service(org.codice.ddf.admin.core.api.Service) ConfigValidator.validateString(org.codice.ddf.admin.configurator.impl.ConfigValidator.validateString) IOException(java.io.IOException) MetatypeAttribute(org.codice.ddf.admin.core.api.MetatypeAttribute) ConfiguratorException(org.codice.ddf.admin.configurator.ConfiguratorException)

Example 8 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ConfigurationAdminImplTest method testListServicesIOException.

/**
 * Tests the {@link ConfigurationAdminImpl#listServices(String, String)} method for the case where
 * configurationAdmin.listConfigurations(....) throws an IOException
 *
 * @throws Exception
 */
@Test
public void testListServicesIOException() throws Exception {
    setUpListServices();
    setUpTestConfig();
    doThrow(new IOException()).when(testConfigAdmin).listConfigurations(anyString());
    List<Service> result = configurationAdminImpl.listServices(TEST_FACT_FILTER, TEST_FILTER);
    assertThat("Should recover gracefully but not add to the given data.", (String) result.get(0).get("name"), is(TEST_OCD));
    assertThat("Should only contain one map.", result.size(), is(1));
}
Also used : Service(org.codice.ddf.admin.core.api.Service) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ManagedService(org.osgi.service.cm.ManagedService) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class AdminConsoleService method getService.

@Override
public Service getService(String filter) {
    List<Service> services = configurationAdminImpl.listServices(filter, filter);
    Service service = null;
    if (!services.isEmpty()) {
        // just grab the first one, they should have specified a filter that returned just a single
        // result
        // if not, that is not our problem
        service = services.get(0);
    }
    return service;
}
Also used : Service(org.codice.ddf.admin.core.api.Service)

Example 10 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ConfigurationAdminImpl method addConfigurationData.

private void addConfigurationData(Service service, Configuration[] configs, Map<String, ObjectClassDefinition> objectClassDefinitions) {
    for (Configuration config : configs) {
        // ignore configuration object if it is invalid
        final String pid = config.getPid();
        if (!isAllowedPid(pid)) {
            continue;
        }
        ConfigurationDetails configData = new ConfigurationDetailsImpl();
        configData.setId(pid);
        String fpid = config.getFactoryPid();
        if (fpid != null) {
            configData.setFactoryPid(fpid);
        }
        // insert an entry for the PID
        try {
            ObjectClassDefinition ocd = objectClassDefinitions.get(config.getPid());
            if (ocd != null) {
                configData.setName(ocd.getName());
            } else {
                // no object class definition, use plain PID
                configData.setName(pid);
            }
        } catch (IllegalArgumentException t) {
            // Catch exception thrown by getObjectClassDefinition so other configurations
            // are displayed
            // no object class definition, use plain PID
            configData.setName(pid);
        }
        final Bundle bundle = getBoundBundle(config);
        if (null != bundle) {
            configData.setBundle(bundle.getBundleId());
            configData.setBundleName(getName(bundle));
            configData.setBundleLocation(bundle.getLocation());
        }
        ConfigurationProperties propertiesTable = new ConfigurationPropertiesImpl();
        Dictionary<String, Object> properties = config.getProperties();
        if (properties != null) {
            Enumeration<String> keys = properties.keys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                propertiesTable.put(key, properties.get(key));
            }
        }
        // If the configuration property is a password that has been set,
        // mask its value to "password" so that the real password value will be hidden.
        List<MetatypeAttribute> metatypeList = service.getAttributeDefinitions();
        metatypeList.stream().filter(metatype -> AttributeDefinition.PASSWORD == metatype.getType()).forEach(metatype -> {
            setPasswordMask(metatype, propertiesTable);
        });
        configData.setConfigurationProperties(propertiesTable);
        Map<String, Object> pluginDataMap = getConfigurationPluginData(configData.getId(), Collections.unmodifiableMap(configData));
        if (pluginDataMap != null && !pluginDataMap.isEmpty()) {
            configData.putAll(pluginDataMap);
        }
        List<ConfigurationDetails> configurationDetails;
        if (service.containsKey(Service.CONFIGURATIONS)) {
            configurationDetails = service.getConfigurations();
        } else if (service.containsKey(Service.DISABLED_CONFIGURATIONS)) {
            configurationDetails = service.getDisabledConfigurations();
        } else {
            configurationDetails = new ArrayList<>();
        }
        configurationDetails.add(configData);
        if (configData.getId().contains(ConfigurationDetails.DISABLED_SERVICE_IDENTIFIER)) {
            configData.setEnabled(false);
        } else {
            configData.setEnabled(true);
        }
        service.setConfigurations(configurationDetails);
    }
}
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) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ConfigurationProperties(org.codice.ddf.admin.core.api.ConfigurationProperties) MetatypeAttribute(org.codice.ddf.admin.core.api.MetatypeAttribute) ConfigurationDetails(org.codice.ddf.admin.core.api.ConfigurationDetails) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Aggregations

Service (org.codice.ddf.admin.core.api.Service)18 ArrayList (java.util.ArrayList)14 MetaTypeService (org.osgi.service.metatype.MetaTypeService)14 Test (org.junit.Test)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)8 IOException (java.io.IOException)6 List (java.util.List)6 ManagedService (org.osgi.service.cm.ManagedService)6 Collections (java.util.Collections)5 Hashtable (java.util.Hashtable)5 Collectors (java.util.stream.Collectors)5 Arrays (java.util.Arrays)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 SystemService (org.apache.karaf.system.SystemService)4 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)4 ConfigurationAdminPlugin (org.codice.ddf.ui.admin.api.plugin.ConfigurationAdminPlugin)4 BundleContext (org.osgi.framework.BundleContext)4 Filter (org.osgi.framework.Filter)4