Search in sources :

Example 1 with Metatype

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

the class AdminConsoleService method updateForLocation.

/**
 * @see AdminConsoleServiceMBean#updateForLocation(java.lang.String, java.lang.String,
 *     java.util.Map)
 */
@Override
public void updateForLocation(final String pid, String location, Map<String, Object> configurationTable) throws IOException {
    if (pid == null || pid.length() < 1) {
        throw loggedException(ILLEGAL_PID_MESSAGE);
    }
    if (configurationTable == null) {
        throw loggedException(ILLEGAL_TABLE_MESSAGE);
    }
    final Configuration config = configurationAdmin.getConfiguration(pid, location);
    if (isPermittedToViewService(config.getPid())) {
        final Metatype metatype = configurationAdminImpl.findMetatypeForConfig(config);
        if (metatype == null) {
            throw loggedException("Could not find metatype for " + pid);
        }
        final List<Map.Entry<String, Object>> configEntries = new ArrayList<>();
        CollectionUtils.addAll(configEntries, configurationTable.entrySet().iterator());
        CollectionUtils.transform(configEntries, new CardinalityTransformer(metatype.getAttributeDefinitions(), pid));
        final Dictionary<String, Object> configProperties = config.getProperties();
        final Dictionary<String, Object> newConfigProperties = (configProperties != null) ? configProperties : new Hashtable<>();
        // "password", do not update the password.
        for (Map.Entry<String, Object> configEntry : configEntries) {
            final String configEntryKey = configEntry.getKey();
            Object configEntryValue = sanitizeUIConfiguration(pid, configEntryKey, configEntry.getValue());
            if (configEntryValue.equals("password")) {
                for (Map<String, Object> metatypeProperties : metatype.getAttributeDefinitions()) {
                    if (metatypeProperties.get("id").equals(configEntry.getKey()) && AttributeDefinition.PASSWORD == (Integer) metatypeProperties.get("type") && configProperties != null) {
                        configEntryValue = configProperties.get(configEntryKey);
                        break;
                    }
                }
            }
            newConfigProperties.put(configEntryKey, configEntryValue);
        }
        config.update(newConfigProperties);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ArrayList(java.util.ArrayList) Metatype(org.codice.ddf.admin.core.api.Metatype) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Metatype

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

the class ConfigurationAdminImpl method addMetaTypeNamesToMap.

private List<Metatype> addMetaTypeNamesToMap(final Map<String, ObjectClassDefinition> objectClassDefinitions, final String filterSpec, final String type) {
    Filter filter = null;
    if (filterSpec != null) {
        try {
            filter = getBundleContext().createFilter(filterSpec);
        } catch (InvalidSyntaxException ignore) {
        // don't care
        }
    }
    List<Metatype> metatypeList = new ArrayList<>();
    for (Entry<String, ObjectClassDefinition> ociEntry : objectClassDefinitions.entrySet()) {
        final String pid = ociEntry.getKey();
        final ObjectClassDefinition ocd = ociEntry.getValue();
        if (filter == null) {
            AttributeDefinition[] defs = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
            metatypeList.add(new MetatypeImpl(pid, ocd.getName(), createMetatypeMap(defs)));
        } else {
            final Dictionary<String, String> props = new Hashtable<>();
            props.put(type, pid);
            if (filter.match(props)) {
                AttributeDefinition[] defs = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
                metatypeList.add(new MetatypeImpl(pid, ocd.getName(), createMetatypeMap(defs)));
            }
        }
    }
    return metatypeList;
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) Metatype(org.codice.ddf.admin.core.api.Metatype) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 3 with Metatype

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

Example 4 with Metatype

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

ArrayList (java.util.ArrayList)4 Metatype (org.codice.ddf.admin.core.api.Metatype)4 HashMap (java.util.HashMap)3 Hashtable (java.util.Hashtable)3 Map (java.util.Map)3 Filter (org.osgi.framework.Filter)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 Configuration (org.osgi.service.cm.Configuration)3 Sets (com.google.common.collect.Sets)2 KeyValueCollectionPermission (ddf.security.permission.KeyValueCollectionPermission)2 Permissions (ddf.security.permission.Permissions)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Dictionary (java.util.Dictionary)2 Enumeration (java.util.Enumeration)2 List (java.util.List)2 Locale (java.util.Locale)2 Entry (java.util.Map.Entry)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2