ok, here goes:
Code:
public class StaticInitializerBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
private Map classes;
private BeanWrapperImpl bri;
public StaticInitializerBeanFactoryPostProcessor() {
bri = new BeanWrapperImpl();
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
for (Iterator classIterator = classes.keySet().iterator(); classIterator.hasNext(); ) {
String className = (String)classIterator.next();
//System.out.println("Class " + className + ":");
Map vars = (Map)classes.get(className);
Class c = null;
try {
c = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new StaticInitializerBeansException("Class not found for " + className, e);
}
Method[] methods = c.getMethods();
for (Iterator fieldIterator = vars.keySet().iterator(); fieldIterator.hasNext(); ) {
String fieldName = (String)fieldIterator.next();
Object value = vars.get(fieldName);
Method method = findStaticSetter(methods, fieldName);
if (method == null) {
throw new StaticInitializerBeansException("No static setter method found for class " +
className + ", field " + fieldName);
}
//System.out.println("\tFound method " + method.getName() + " for field " + fieldName + ", value " + value);
Object newValue = bri.doTypeConversionIfNecessary(value, getPropertyType(method));
try {
method.invoke(null, new Object[] {newValue});
} catch (Exception e) {
throw new StaticInitializerBeansException("Invocation of method " + method.getName() +
" on class " + className + " with value " + value + " failed.", e);
}
}
}
}
private Class getPropertyType(Method setter) {
Class params[] = setter.getParameterTypes();
if (params.length != 1) {
throw new StaticInitializerBeansException("bad write method arg count: " + setter);
}
return params[0];
}
/**
* Look for a static setter method for field named fieldName in Method[].
* Return null if none found.
* @param methods
* @param fieldName
* @return
*/
private Method findStaticSetter(Method[] methods, String fieldName) {
String methodName = setterName(fieldName);
for (int i=0; i<methods.length; i++) {
if (methods[i].getName().equals(methodName) &&
Modifier.isStatic(methods[i].getModifiers())) {
return methods[i];
}
}
return null;
}
/**
* return the standard setter name for field fieldName
* @param fieldName
* @return
*/
private String setterName(String fieldName) {
String nameToUse = null;
if (fieldName.length() == 1) {
if (Character.isLowerCase(fieldName.charAt(0))) {
nameToUse = fieldName.toUpperCase();
} else {
nameToUse = fieldName;
}
} else {
if (Character.isLowerCase(fieldName.charAt(0)) && Character.isLowerCase(fieldName.charAt(1))) {
nameToUse = fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
} else {
nameToUse = fieldName;
}
}
return "set" + nameToUse;
}
public void setClasses(Map classes) {
this.classes = classes;
}
}
class StaticInitializerBeansException extends BeansException {
StaticInitializerBeansException(String msg) {
super(msg);
}
StaticInitializerBeansException(String msg, Throwable e) {
super(msg, e);
}
}
To use it, the xml is like this, where the key of the top level map is the class you want to configure, and the second level map are the static fields you want to assign values to:
Code:
<bean class="org.stl.utils.StaticInitializerBeanFactoryPostProcessor">
<property name="classes">
<map>
<entry key="org.stl.images.WebImages">
<map>
<entry key="LARGEMAXWIDTH" value="200"/>
<entry key="LARGEMAXHEIGHT" value="500"/>
<entry key="SMALLMAXWIDTH" value="100"/>
<entry key="SMALLMAXHEIGHT" value="250"/>
<entry key="SPSTANDARDMAXHEIGHT" value="10000"/><!-- unlimited -->
<entry key="SPSTANDARDMAXWIDTH" value="148"/>
<entry key="SPMEDIUMMAXHEIGHT" value="10000"/><!-- unlimited -->
<entry key="SPMEDIUMMAXWIDTH" value="75"/>
<entry key="SPTHUMBMAXHEIGHT" value="52"/>
<entry key="SPTHUMBMAXWIDTH" value="10000"/><!-- unlimited -->
</map>
</entry>
</map>
</property>
</bean>