I've written a basic DynamicTypeConverterFactory which discovers
TypeConverters automatically. This might be a good feature for Stripes?
(with some robustness added to the implementation)
/**
* DynamicTypeConverterFactory discovers TypeConverters using ResolverUtil
*/
public class DynamicTypeConverterFactory extends
DefaultTypeConverterFactory {
private Map<Class, TypeConverter> converterMap = new HashMap<Class,
TypeConverter>();
public DynamicTypeConverterFactory(final String... packages) throws
Exception {
initTypeConverters(packages);
}
private void initTypeConverters(final String... packages) throws
Exception {
// Find all TypeConverters
final ResolverUtil<TypeConverter> r = new
ResolverUtil<TypeConverter>();
r.findImplementations(TypeConverter.class, packages);
final Set<Class<? extends TypeConverter>> converters =
r.getClasses();
for (final Class converter : converters) {
final Type[] ifces = converter.getGenericInterfaces();
// Find the TypeConverter interface
for (final Type ifce : ifces) {
if (ifce instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType) ifce;
if (pt.getRawType().equals(TypeConverter.class)) {
//Get the type it converts for
final Type[] actualArgs =
pt.getActualTypeArguments();
if (actualArgs.length == 1) {
final Type t = actualArgs[0];
if (t instanceof Class) {
converterMap.put((Class) t,
(TypeConverter) converter.newInstance());
}
}
}
}
}
}
}
public TypeConverter getTypeConverter(final Class forType, final
Locale locale) throws Exception {
final TypeConverter converter = converterMap.get(forType);
if (null != converter) {
return converter;
}
return super.getTypeConverter(forType, locale);
}
}
The DynamicTypeConverter will find all TypeConverters and determine the
Type they are intended to convert for.
This doesn't work for converters that are converters for annotated
classes, like XmlTypeConverter, EntityTypeConverter since they both
convert Object types and there would need to be a tidy solution to that
problem since a lot of people are using TypeConverters like this now.
public class EntityTypeConverter implements TypeConverter<Object>
public class XmlTypeConverter implements TypeConverter<Object>
People could either implement a new TypeConverter<Object> which does the
switch manually (defeats the point of the dynamic type converter factory
though) or my personal solution will probably be to create a new
interface AnnotatedTypeConverter that declares the annotation it
converts for.
public interface AnnotatedTypeConverter<T> {
public Object convert(final String input, final Class forType, final
Collection<ValidationError> errors);
}
public class XmlTypeConverter implements
AnnotatedTypeConverter<XmlType.class>;
public class EntityTypeConverter implements
AnnotatedTypeConverter<Entity.class>;
And then discover and configure these in basically the same way I have
for normal TypeConverters.
But I suspect noone has as much trouble with the TypeConverter
mechanisms as I do so the extra interface will probably just obfuscate
things for most people.
thanks,
Gary
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development