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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations