1 27 28 package com.caucho.config; 29 30 import com.caucho.log.Log; 31 import com.caucho.util.L10N; 32 import com.caucho.xml.QName; 33 34 import java.util.Map ; 35 import java.util.logging.Logger ; 36 37 40 public class MapBuilder { 41 private final static L10N L = new L10N(MapBuilder.class); 42 private final static Logger log = Log.open(MapBuilder.class); 43 44 47 public static Object configure(Object bean, Map <String ,Object > map) 48 throws ConfigException 49 { 50 return configure(bean, map, true); 51 } 52 53 56 public static Object configureNoInit(Object bean, Map <String ,Object > map) 57 throws ConfigException 58 { 59 return configure(bean, map, false); 60 } 61 62 71 public static Object configure(Object bean, 72 Map <String ,Object > map, 73 boolean doInit) 74 throws ConfigException 75 { 76 NodeBuilder oldBuilder = NodeBuilder.getCurrentBuilder(); 77 78 try { 79 if (oldBuilder == null) 80 NodeBuilder.setCurrentBuilder(new NodeBuilder()); 81 82 TypeStrategy type = TypeStrategyFactory.getTypeStrategy(bean.getClass()); 83 84 return configure(type, bean, map, doInit); 85 } catch (ConfigException e) { 86 throw e; 87 } catch (RuntimeException e) { 88 throw e; 89 } catch (Throwable e) { 90 throw new ConfigException(e); 91 } finally { 92 NodeBuilder.setCurrentBuilder(oldBuilder); 93 } 94 } 95 96 private static Object configure(TypeStrategy typeStrategy, 97 Object bean, 98 Map <String ,Object > map, 99 boolean doInit) 100 throws Throwable 101 { 102 115 116 for (String key : map.keySet()) { 117 QName attrName = new QName(key); 118 119 AttributeStrategy attrStrategy 120 = typeStrategy.getAttributeStrategy(attrName); 121 122 if (attrStrategy != null) 123 attrStrategy.setAttribute(bean, attrName, map.get(key)); 124 } 125 126 if (doInit) { 127 typeStrategy.init(bean); 128 129 return typeStrategy.replaceObject(bean); 130 } 131 else 132 return bean; 133 } 134 } 135 | Popular Tags |