use of org.springframework.core.convert.ConversionService in project camel by apache.
the class SpringTypeConverter method convertTo.
@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
// do not attempt to convert Camel types
if (type.getCanonicalName().startsWith("org.apache")) {
return null;
}
// do not attempt to convert List -> Map. Ognl expression may use this converter as a fallback expecting null
if (type.isAssignableFrom(Map.class) && (value.getClass().isArray() || value instanceof Collection)) {
return null;
}
TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf);
TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf);
for (ConversionService conversionService : conversionServices) {
if (conversionService.canConvert(sourceType, targetType)) {
try {
return (T) conversionService.convert(value, sourceType, targetType);
} catch (ConversionFailedException e) {
//
if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) {
return null;
} else {
throw new TypeConversionException(value, type, e);
}
}
}
}
return null;
}
use of org.springframework.core.convert.ConversionService in project cas by apereo.
the class CasConfigurationEmbeddedValueResolver method convertValueToDurationIfPossible.
private String convertValueToDurationIfPossible(final String value) {
try {
final ConversionService service = applicationContext.getEnvironment().getConversionService();
final Duration dur = service.convert(value, Duration.class);
if (dur != null) {
return String.valueOf(dur.toMillis());
}
} catch (final ConversionFailedException e) {
LOGGER.trace(e.getMessage());
}
return null;
}
use of org.springframework.core.convert.ConversionService in project grails-core by grails.
the class AbstractGrailsPluginManager method doRuntimeConfiguration.
/**
* Base implementation that simply goes through the list of plugins and calls doWithRuntimeConfiguration on each
* @param springConfig The RuntimeSpringConfiguration instance
*/
public void doRuntimeConfiguration(RuntimeSpringConfiguration springConfig) {
ApplicationContext context = springConfig.getUnrefreshedApplicationContext();
AutowireCapableBeanFactory autowireCapableBeanFactory = context.getAutowireCapableBeanFactory();
if (autowireCapableBeanFactory instanceof ConfigurableListableBeanFactory) {
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) autowireCapableBeanFactory;
ConversionService existingConversionService = beanFactory.getConversionService();
ConverterRegistry converterRegistry;
if (existingConversionService == null) {
GenericConversionService conversionService = new GenericConversionService();
converterRegistry = conversionService;
beanFactory.setConversionService(conversionService);
} else {
converterRegistry = (ConverterRegistry) existingConversionService;
}
converterRegistry.addConverter(new Converter<NavigableMap.NullSafeNavigator, Object>() {
@Override
public Object convert(NavigableMap.NullSafeNavigator source) {
return null;
}
});
}
checkInitialised();
for (GrailsPlugin plugin : pluginList) {
if (plugin.supportsCurrentScopeAndEnvironment() && plugin.isEnabled(context.getEnvironment().getActiveProfiles())) {
plugin.doWithRuntimeConfiguration(springConfig);
}
}
}
use of org.springframework.core.convert.ConversionService in project spring-framework by spring-projects.
the class InitBinderBindingContextTests method createBinderWithGlobalInitialization.
@Test
public void createBinderWithGlobalInitialization() throws Exception {
ConversionService conversionService = new DefaultFormattingConversionService();
bindingInitializer.setConversionService(conversionService);
ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
BindingContext context = createBindingContext("initBinder", WebDataBinder.class);
WebDataBinder dataBinder = context.createDataBinder(exchange, null, null);
assertSame(conversionService, dataBinder.getConversionService());
}
use of org.springframework.core.convert.ConversionService in project spring-framework by spring-projects.
the class EvalTag method createEvaluationContext.
private EvaluationContext createEvaluationContext(PageContext pageContext) {
StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
context.addPropertyAccessor(new MapAccessor());
context.addPropertyAccessor(new EnvironmentAccessor());
context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
ConversionService conversionService = getConversionService(pageContext);
if (conversionService != null) {
context.setTypeConverter(new StandardTypeConverter(conversionService));
}
return context;
}
Aggregations