Search in sources :

Example 1 with ConfigurationClass

use of eu.bcvsolutions.idm.core.api.domain.ConfigurationClass in project CzechIdMng by bcvsolutions.

the class CzechIdMIcConvertUtil method convertIcConnectorConfiguration.

public static ConfigurationClass convertIcConnectorConfiguration(IcConnectorConfiguration configuration, Class<? extends ConfigurationClass> configurationClass) {
    if (configuration == null || configuration.getConfigurationProperties() == null || configuration.getConfigurationProperties().getProperties() == null) {
        return null;
    }
    List<IcConfigurationProperty> properties = configuration.getConfigurationProperties().getProperties();
    try {
        ConfigurationClass configurationClassInstance = configurationClass.newInstance();
        PropertyDescriptor[] descriptors;
        descriptors = Introspector.getBeanInfo(configurationClass).getPropertyDescriptors();
        Lists.newArrayList(descriptors).stream().forEach(descriptor -> {
            String propertyName = descriptor.getName();
            Method writeMethod = descriptor.getWriteMethod();
            IcConfigurationProperty propertyToConvert = properties.stream().filter(property -> propertyName.equals(property.getName())).findFirst().orElse(null);
            if (propertyToConvert == null) {
                return;
            }
            try {
                writeMethod.invoke(configurationClassInstance, propertyToConvert.getValue());
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                throw new IcException(e);
            }
        });
        return configurationClassInstance;
    } catch (IntrospectionException | InstantiationException | IllegalAccessException e) {
        throw new IcException(e);
    }
}
Also used : ConfigurationClass(eu.bcvsolutions.idm.core.api.domain.ConfigurationClass) PropertyDescriptor(java.beans.PropertyDescriptor) IntrospectionException(java.beans.IntrospectionException) IcException(eu.bcvsolutions.idm.ic.exception.IcException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) IcConfigurationProperty(eu.bcvsolutions.idm.ic.api.IcConfigurationProperty)

Example 2 with ConfigurationClass

use of eu.bcvsolutions.idm.core.api.domain.ConfigurationClass in project CzechIdMng by bcvsolutions.

the class CzechIdMIcConfigurationService method initDefaultConfiguration.

/**
 * Create instance of default connector configuration
 *
 * @param configurationClass
 * @return
 */
private IcConnectorConfiguration initDefaultConfiguration(Class<? extends ConfigurationClass> configurationClass) {
    try {
        ConfigurationClass configurationClassInstance = configurationClass.newInstance();
        List<IcConfigurationProperty> properties = new ArrayList<>();
        PropertyDescriptor[] descriptors = Introspector.getBeanInfo(configurationClass).getPropertyDescriptors();
        Lists.newArrayList(descriptors).stream().forEach(descriptor -> {
            Method readMethod = descriptor.getReadMethod();
            String propertyName = descriptor.getName();
            ConfigurationClassProperty property = readMethod.getAnnotation(ConfigurationClassProperty.class);
            if (property != null) {
                IcConfigurationPropertyImpl icProperty = (IcConfigurationPropertyImpl) CzechIdMIcConvertUtil.convertConfigurationProperty(property);
                icProperty.setName(propertyName);
                icProperty.setType(readMethod.getGenericReturnType().getTypeName());
                try {
                    icProperty.setValue(readMethod.invoke(configurationClassInstance));
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                    throw new CoreException("Cannot read value of connector configuration property!", e);
                }
                properties.add(icProperty);
            }
        });
        // Sort by order
        properties.sort(Comparator.comparing(IcConfigurationProperty::getOrder));
        IcConfigurationPropertiesImpl icProperties = new IcConfigurationPropertiesImpl();
        icProperties.setProperties(properties);
        IcConnectorConfigurationImpl configuration = new IcConnectorConfigurationImpl();
        configuration.setConnectorPoolingSupported(false);
        configuration.setConfigurationProperties(icProperties);
        return configuration;
    } catch (IntrospectionException | InstantiationException | IllegalAccessException e) {
        throw new CoreException("Cannot read connector configuration property!", e);
    }
}
Also used : ConfigurationClass(eu.bcvsolutions.idm.core.api.domain.ConfigurationClass) IcConnectorConfigurationImpl(eu.bcvsolutions.idm.ic.impl.IcConnectorConfigurationImpl) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) IcConfigurationPropertiesImpl(eu.bcvsolutions.idm.ic.impl.IcConfigurationPropertiesImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationClassProperty(eu.bcvsolutions.idm.core.api.domain.ConfigurationClassProperty) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) IcConfigurationProperty(eu.bcvsolutions.idm.ic.api.IcConfigurationProperty) IcConfigurationPropertyImpl(eu.bcvsolutions.idm.ic.impl.IcConfigurationPropertyImpl)

Example 3 with ConfigurationClass

use of eu.bcvsolutions.idm.core.api.domain.ConfigurationClass in project CzechIdMng by bcvsolutions.

the class DefaultFormService method convertConfigurationToFormDefinition.

/**
 * Create instance form definition from the given configuration class
 *
 * @param configurationClass
 * @return
 */
@Override
public IdmFormDefinitionDto convertConfigurationToFormDefinition(Class<? extends ConfigurationClass> configurationClass) {
    Assert.notNull(configurationClass, "Class with the configuration is required!");
    try {
        ConfigurationClass configurationClassInstance = configurationClass.newInstance();
        List<IdmFormAttributeDto> properties = new ArrayList<>();
        PropertyDescriptor[] descriptors = Introspector.getBeanInfo(configurationClass).getPropertyDescriptors();
        Lists.newArrayList(descriptors).stream().forEach(descriptor -> {
            Method readMethod = descriptor.getReadMethod();
            String propertyName = descriptor.getName();
            ConfigurationClassProperty property = readMethod.getAnnotation(ConfigurationClassProperty.class);
            if (property != null) {
                IdmFormAttributeDto formAttribute = this.convertConfigurationProperty(property);
                formAttribute.setCode(propertyName);
                // TODO: Better convertors  (move from IC and ACC module to the Core)!
                initPersistentType(readMethod, formAttribute);
                try {
                    formAttribute.setDefaultValue(this.convertDefaultValue(readMethod.invoke(configurationClassInstance), formAttribute.isMultiple()));
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                    throw new CoreException("Cannot read value of connector configuration property!", e);
                }
                properties.add(formAttribute);
            }
        });
        IdmFormDefinitionDto definition = new IdmFormDefinitionDto();
        definition.setFormAttributes(properties);
        return definition;
    } catch (IntrospectionException | InstantiationException | IllegalAccessException e) {
        throw new CoreException("Cannot read configuration property!", e);
    }
}
Also used : ConfigurationClass(eu.bcvsolutions.idm.core.api.domain.ConfigurationClass) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationClassProperty(eu.bcvsolutions.idm.core.api.domain.ConfigurationClassProperty) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)

Example 4 with ConfigurationClass

use of eu.bcvsolutions.idm.core.api.domain.ConfigurationClass in project CzechIdMng by bcvsolutions.

the class CzechIdMIcConfigurationService method getAvailableLocalConnectors.

/**
 * Return available local connectors for this IC implementation
 *
 * @return
 */
@SuppressWarnings("unchecked")
@Override
public Set<IcConnectorInfo> getAvailableLocalConnectors() {
    if (connectorInfos == null) {
        connectorInfos = new HashSet<>();
        connectorsConfigurations = new HashMap<>();
        connectorsClass = new HashMap<>();
        List<Class<?>> annotated = new ArrayList<>();
        // Find all class with annotation IcConnectorClass under specific
        // packages
        localConnectorsPackages.forEach(packageWithConnectors -> {
            Reflections reflections = new Reflections(packageWithConnectors);
            annotated.addAll(reflections.getTypesAnnotatedWith(IcConnectorClass.class));
        });
        LOG.info(MessageFormat.format("Found annotated classes with IcConnectorClass [{0}]", annotated));
        for (Class<?> clazz : annotated) {
            IcConnectorClass connectorAnnotation = clazz.getAnnotation(IcConnectorClass.class);
            if (!this.getFramework().equals(connectorAnnotation.framework())) {
                continue;
            }
            if (!IcConnector.class.isAssignableFrom(clazz)) {
                throw new IcException(MessageFormat.format("Cannot create instance of CzechIdM connector [{0}]! Connector class must be child of [{0}]!", IcConnector.class.getSimpleName()));
            }
            IcConnectorInfo info = CzechIdMIcConvertUtil.convertConnectorClass(connectorAnnotation, (Class<? extends IcConnector>) clazz);
            Class<? extends ConfigurationClass> configurationClass = connectorAnnotation.configurationClass();
            connectorInfos.add(info);
            IcConnectorConfiguration configuration = initDefaultConfiguration(configurationClass);
            // Put default configuration to cache
            connectorsConfigurations.put(info.getConnectorKey().getFullName(), configuration);
            // Put connector class to cache
            connectorsClass.put(info.getConnectorKey().getFullName(), ((Class<? extends IcConnector>) clazz));
        }
        LOG.info(MessageFormat.format("Found all local connector connectorInfos [{0}]", connectorInfos.toString()));
    }
    return connectorInfos;
}
Also used : IcConnectorConfiguration(eu.bcvsolutions.idm.ic.api.IcConnectorConfiguration) ArrayList(java.util.ArrayList) IcException(eu.bcvsolutions.idm.ic.exception.IcException) IcConnectorInfo(eu.bcvsolutions.idm.ic.api.IcConnectorInfo) IcConnectorClass(eu.bcvsolutions.idm.ic.api.annotation.IcConnectorClass) ConfigurationClass(eu.bcvsolutions.idm.core.api.domain.ConfigurationClass) IcConnectorClass(eu.bcvsolutions.idm.ic.api.annotation.IcConnectorClass) IcConnector(eu.bcvsolutions.idm.ic.api.IcConnector) Reflections(org.reflections.Reflections)

Aggregations

ConfigurationClass (eu.bcvsolutions.idm.core.api.domain.ConfigurationClass)4 IntrospectionException (java.beans.IntrospectionException)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 ConfigurationClassProperty (eu.bcvsolutions.idm.core.api.domain.ConfigurationClassProperty)2 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)2 IcConfigurationProperty (eu.bcvsolutions.idm.ic.api.IcConfigurationProperty)2 IcException (eu.bcvsolutions.idm.ic.exception.IcException)2 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)1 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)1 IcConnector (eu.bcvsolutions.idm.ic.api.IcConnector)1 IcConnectorConfiguration (eu.bcvsolutions.idm.ic.api.IcConnectorConfiguration)1 IcConnectorInfo (eu.bcvsolutions.idm.ic.api.IcConnectorInfo)1 IcConnectorClass (eu.bcvsolutions.idm.ic.api.annotation.IcConnectorClass)1 IcConfigurationPropertiesImpl (eu.bcvsolutions.idm.ic.impl.IcConfigurationPropertiesImpl)1 IcConfigurationPropertyImpl (eu.bcvsolutions.idm.ic.impl.IcConfigurationPropertyImpl)1 IcConnectorConfigurationImpl (eu.bcvsolutions.idm.ic.impl.IcConnectorConfigurationImpl)1 Reflections (org.reflections.Reflections)1