Search in sources :

Example 16 with Service

use of org.codice.ddf.admin.core.api.Service 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)

Example 17 with Service

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

the class AdminConsoleServiceTest method testGetClaimsConfiguration.

@Test
public void testGetClaimsConfiguration() throws NotCompliantMBeanException {
    org.osgi.service.cm.ConfigurationAdmin testConfigAdmin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
    ConfigurationAdminImpl testConfigAdminExt = mock(ConfigurationAdminImpl.class);
    AdminConsoleService configAdmin = new AdminConsoleService(testConfigAdmin, testConfigAdminExt);
    List<Service> serviceList = new ArrayList<>();
    Service testService = new ServiceImpl();
    testService.put(TEST_KEY, TEST_VALUE);
    serviceList.add(testService);
    when(testConfigAdminExt.listServices(TEST_FILTER_1, TEST_FILTER_1)).thenReturn(serviceList);
    // check call before setting handler
    assertNotNull(configAdmin.getClaimsConfiguration(TEST_FILTER_1));
    GuestClaimsHandlerExt handlerExt = mock(GuestClaimsHandlerExt.class);
    when(handlerExt.getClaims()).thenReturn(new HashMap<>());
    when(handlerExt.getClaimsProfiles()).thenReturn(new HashMap<>());
    configAdmin.setGuestClaimsHandlerExt(handlerExt);
    assertNotNull(configAdmin.getClaimsConfiguration(TEST_FILTER_1));
    // check with bad filter
    assertNull(configAdmin.getClaimsConfiguration("bad_filter"));
}
Also used : ArrayList(java.util.ArrayList) Service(org.codice.ddf.admin.core.api.Service) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Test(org.junit.Test)

Example 18 with Service

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

the class AdminConsoleServiceTest method testGetService.

/**
 * Tests the {@link AdminConsoleService#getService(String)} method
 *
 * @throws Exception
 */
@Test
public void testGetService() throws Exception {
    org.osgi.service.cm.ConfigurationAdmin testConfigAdmin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
    ConfigurationAdminImpl testConfigAdminExt = mock(ConfigurationAdminImpl.class);
    AdminConsoleService configAdmin = new AdminConsoleService(testConfigAdmin, testConfigAdminExt) {

        @Override
        public boolean isPermittedToViewService(String servicePid) {
            return true;
        }
    };
    List<Service> serviceList = new ArrayList<>();
    Service testService = new ServiceImpl();
    testService.put(TEST_KEY, TEST_VALUE);
    serviceList.add(testService);
    when(testConfigAdminExt.listServices(TEST_FILTER_1, TEST_FILTER_1)).thenReturn(serviceList);
    assertEquals(testService.get(TEST_KEY), configAdmin.getService(TEST_FILTER_1).get(TEST_KEY));
    verify(testConfigAdminExt).listServices(TEST_FILTER_1, TEST_FILTER_1);
}
Also used : ArrayList(java.util.ArrayList) Service(org.codice.ddf.admin.core.api.Service) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

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