1 28 29 package com.caucho.config; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 import com.caucho.xml.QName; 34 35 import org.w3c.dom.Node ; 36 37 import java.lang.reflect.Method ; 38 import java.lang.reflect.Modifier ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 45 public class SetterAttributeStrategy extends AttributeStrategy { 46 private static final Logger log = Log.open(SetterAttributeStrategy.class); 47 private static final L10N L = new L10N(SetterAttributeStrategy.class); 48 49 private final Method _setter; 50 private final TypeStrategy _typeStrategy; 51 52 public SetterAttributeStrategy(Method setter) 53 throws Exception 54 { 55 if (! Modifier.isPublic(setter.getModifiers())) 56 throw new IllegalStateException (L.l("'{0}' is not public.", setter.toString())); 57 58 Class []param = setter.getParameterTypes(); 59 _setter = setter; 60 _typeStrategy = TypeStrategyFactory.getTypeStrategy(param[0]); 61 } 62 63 71 public void configure(NodeBuilder builder, 72 Object bean, 73 QName name, 74 Node node) 75 throws Exception 76 { 77 Object value = _typeStrategy.configure(builder, node, bean); 78 79 setAttribute(bean, name, value); 80 } 81 82 90 public void setAttribute(Object bean, QName name, Object value) 91 throws Exception 92 { 93 try { 94 _setter.invoke(bean, value); 95 } catch (IllegalArgumentException e) { 96 log.log(Level.FINE, e.toString(), e); 97 98 throw new ConfigException(L.l("Can't assign {0} ({1}) to a {2}.", 99 value, value.getClass(), _setter.getParameterTypes()[0])); 100 } 101 } 102 } 103 | Popular Tags |