1 4 package org.oddjob.framework; 5 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.util.Enumeration ; 9 import java.util.Properties ; 10 11 import org.apache.commons.beanutils.BeanUtilsBean; 12 import org.apache.commons.beanutils.ConvertUtilsBean; 13 import org.apache.commons.beanutils.Converter; 14 import org.apache.commons.beanutils.PropertyUtilsBean; 15 import org.oddjob.arooa.ArooaException; 16 17 public class BeanUtilsProvider { 18 19 public static BeanUtilsBean beanFor(String classDefFileName) { 20 Properties props = new Properties (); 21 try { 22 InputStream in = BeanUtilsProvider.class.getResourceAsStream(classDefFileName); 23 if (in == null) { 24 throw new ArooaException("No resource [" + classDefFileName + "]"); 25 } 26 props.load(in); 27 in.close(); 28 return beanFor(props); 29 } catch (IOException e) { 30 throw new RuntimeException (e); 31 } 32 } 33 34 public static BeanUtilsBean beanFor(Properties properties) { 35 BeanUtilsBean beanUtilsBean = new BeanUtilsBean( 36 new ConvertUtilsBean(), new PropertyUtilsBean()); 37 try { 38 for (Enumeration e = properties.keys() ; e.hasMoreElements() ;) { 39 String key = (String )e.nextElement(); 40 String val = (String )properties.get(key); 41 Class converterClass = Class.forName(key); 42 Converter converter = (Converter) converterClass.newInstance(); 43 Class converterFor = Class.forName(val); 44 beanUtilsBean.getConvertUtils().register(converter, converterFor); 45 } 46 return beanUtilsBean; 47 } catch (ClassNotFoundException e) { 48 throw new RuntimeException (e); 49 } catch (IllegalAccessException e) { 50 throw new RuntimeException (e); 51 } catch (InstantiationException e) { 52 throw new RuntimeException (e); 53 } 54 } 55 } 56 | Popular Tags |