Skip to content

Commit

Permalink
found hotspot; added ConverisonServiceFactoryBean
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Donald committed Nov 20, 2009
1 parent 4024b67 commit 692b1ef
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ public void registerCustomEditors(PropertyEditorRegistry registry) {
@Test
public void testCustomConverter() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
GenericConversionService conversionService = (GenericConversionService) ConversionServiceFactory.createDefault();
GenericConversionService conversionService = (GenericConversionService) ConversionServiceFactory.createDefaultConversionService();
conversionService.addConverter(new Converter<String, Float>() {
public Float convert(String source) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.support;

import java.util.Set;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.ConversionServiceFactory;

/**
* A factory for a ConversionService that installs default converters appropriate for most environments.
* Set the <code>converters</code> property to supplement or override the default converters.
* @author Keith Donald
* @since 3.0
* @see ConversionServiceFactory#createDefaultConversionService()
*/
public class ConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean {

private Set<Object> converters;

private ConversionService conversionService;

/**
* Configure the set of custom Converters that should be added.
*/
public void setConverters(Set<Object> converters) {
this.converters = converters;
}

// implementing InitializingBean

public void afterPropertiesSet() {
this.conversionService = createConversionService();
registerConverters(this.converters, (ConverterRegistry) this.conversionService);
}

// implementing FactoryBean

public ConversionService getObject() {
return this.conversionService;
}

public Class<? extends ConversionService> getObjectType() {
return ConversionService.class;
}

public boolean isSingleton() {
return true;
}

// subclassing hooks

/**
* Creates the ConversionService instance returned by this factory bean.
*/
protected ConversionService createConversionService() {
return ConversionServiceFactory.createDefaultConversionService();
}

// internal helpers

private void registerConverters(Set<Object> converters, ConverterRegistry registry) {
if (converters != null) {
for (Object converter : converters) {
if (converter instanceof Converter<?, ?>) {
registry.addConverter((Converter<?, ?>) converter);
} else if (converter instanceof ConverterFactory<?, ?>) {
registry.addConverterFactory((ConverterFactory<?, ?>) converter);
} else if (converter instanceof GenericConverter) {
registry.addGenericConverter((GenericConverter) converter);
} else {
throw new IllegalArgumentException("Each converter must implement one of the Converter, ConverterFactory, or GenericConverter interfaces");
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*/
public class FormattingConversionService implements FormatterRegistry, ConversionService {

private ConversionService conversionService = ConversionServiceFactory.createDefault();
private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();

// implementing FormattingRegistry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,30 @@
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer;
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
import org.springframework.format.number.NumberFormatter;
import org.springframework.util.ClassUtils;

/**
* A factory for a FormattingConversionService that installs default formatters for common types such as numbers and datetimes.
* Subclasses may override {@link #installFormatters(FormatterRegistry)} to register custom formatters.
* @author Keith Donald
* @since 3.0
*/
public class FormattingConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean {

private FormattingConversionService conversionService = new FormattingConversionService();

private static final boolean jodaTimePresent = ClassUtils.isPresent(
"org.joda.time.DateTime", FormattingConversionService.class.getClassLoader());

private FormattingConversionService conversionService;

// implementing InitializingBean

public void afterPropertiesSet() {
installNumberFormatting();
installJodaTimeFormattingIfPresent();
this.conversionService = new FormattingConversionService();
installFormatters(this.conversionService);
}

// implementing FactoryBean
Expand All @@ -45,25 +50,22 @@ public ConversionService getObject() {
return this.conversionService;
}

public Class<? extends ConversionService> getObjectType() {
public Class<ConversionService> getObjectType() {
return ConversionService.class;
}

public boolean isSingleton() {
return true;
}

// internal helpers

private void installNumberFormatting() {
this.conversionService.addFormatterForFieldType(Number.class, new NumberFormatter());
this.conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
}
// subclassing hooks

private void installJodaTimeFormattingIfPresent() {
if (ClassUtils.isPresent("org.joda.time.DateTime", FormattingConversionService.class.getClassLoader())) {
new JodaTimeFormattingConfigurer().installJodaTimeFormatting(this.conversionService);
}
protected void installFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(Number.class, new NumberFormatter());
registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
if (jodaTimePresent) {
new JodaTimeFormattingConfigurer().installJodaTimeFormatting(registry);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,14 @@
*/
public final class ConversionServiceFactory {

private static ConversionService DEFAULT_INSTANCE;

private ConversionServiceFactory() {

}

/**
* Get the shared default ConversionService.
*/
public static synchronized ConversionService getDefault() {
if (DEFAULT_INSTANCE == null) {
DEFAULT_INSTANCE = createDefault();
}
return DEFAULT_INSTANCE;
}

/**
* Create a new default ConversionService prototype that can be safely modified.
*/
public static ConversionService createDefault() {
public static ConversionService createDefaultConversionService() {
GenericConversionService conversionService = new GenericConversionService();
conversionService.addGenericConverter(new ArrayToArrayConverter(conversionService));
conversionService.addGenericConverter(new ArrayToCollectionConverter(conversionService));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,7 @@

public class DefaultConversionTests {

private ConversionService conversionService = ConversionServiceFactory.getDefault();

@Test
@SuppressWarnings("unchecked")
public void testUnmodifiableListConversion() {
List<String> stringList = new ArrayList<String>();
stringList.add("foo");
stringList.add("bar");

List<String> frozenList = Collections.unmodifiableList(stringList);

List<String> converted = conversionService.convert(frozenList, List.class);

// The converted list should contain all the elements in the original list
Assert.assertEquals(frozenList, converted);
// Looks like it was supposed to be a copy (but CollectionToCollectionConverter
// doesn't work that way right now). Commented out (DS).
// Assert.assertNotSame(frozenList, converted);
}
private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();

@Test
public void testStringToCharacter() {
Expand Down Expand Up @@ -740,4 +722,22 @@ public void genericConverterDelegatingBackToConversionServiceConverterNotFound()
}
}

@Test
@SuppressWarnings("unchecked")
public void testUnmodifiableListConversion() {
List<String> stringList = new ArrayList<String>();
stringList.add("foo");
stringList.add("bar");

List<String> frozenList = Collections.unmodifiableList(stringList);

List<String> converted = conversionService.convert(frozenList, List.class);

// The converted list should contain all the elements in the original list
Assert.assertEquals(frozenList, converted);
// TODO Looks like it was supposed to be a copy (but CollectionToCollectionConverter
// doesn't work that way right now). Commented out (DS).
// Assert.assertNotSame(frozenList, converted);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
*/
public class StandardTypeConverter implements TypeConverter {

private static ConversionService DEFAULT_INSTANCE;

private final ConversionService conversionService;

public StandardTypeConverter() {
this.conversionService = ConversionServiceFactory.getDefault();
this.conversionService = getDefaultConversionService();
}

public StandardTypeConverter(ConversionService conversionService) {
Expand All @@ -64,4 +66,12 @@ public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws E
}
}

private ConversionService getDefaultConversionService() {
synchronized(this) {
if (DEFAULT_INSTANCE == null) {
DEFAULT_INSTANCE = ConversionServiceFactory.createDefaultConversionService();
}
}
return DEFAULT_INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void testSetParameterizedList() throws Exception {
*/
private static class TypeConvertorUsingConversionService implements TypeConverter {

private final ConversionService service = ConversionServiceFactory.createDefault();
private final ConversionService service = ConversionServiceFactory.createDefaultConversionService();

public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
return this.service.canConvert(sourceType, targetType);
Expand Down

0 comments on commit 692b1ef

Please sign in to comment.