001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.config.attribute;
031:
032: import java.lang.reflect.*;
033:
034: import com.caucho.config.*;
035: import com.caucho.config.type.*;
036: import com.caucho.util.L10N;
037: import com.caucho.xml.QName;
038:
039: public class SetterAttribute extends Attribute {
040: private final L10N L = new L10N(SetterAttribute.class);
041:
042: private final Method _setter;
043: private final ConfigType _type;
044:
045: public SetterAttribute(Method setter, ConfigType type) {
046: _setter = setter;
047: _setter.setAccessible(true);
048: _type = type;
049: }
050:
051: /**
052: * Returns the config type of the attribute value.
053: */
054: public ConfigType getConfigType() {
055: return _type;
056: }
057:
058: /**
059: * Sets the value of the attribute
060: */
061: @Override
062: public void setText(Object bean, QName name, String value)
063: throws ConfigException {
064: try {
065: _setter.invoke(bean, _type.valueOf(value));
066: } catch (Exception e) {
067: throw ConfigException.create(_setter, e);
068: }
069: }
070:
071: /**
072: * Sets the value of the attribute
073: */
074: @Override
075: public void setValue(Object bean, QName name, Object value)
076: throws ConfigException {
077: try {
078: _setter.invoke(bean, value);
079: } catch (IllegalArgumentException e) {
080: throw ConfigException.create(_setter, L.l(
081: "'{0}' is an illegal value.", value), e);
082: } catch (Exception e) {
083: throw ConfigException.create(_setter, e);
084: }
085: }
086:
087: /**
088: * Creates the child bean.
089: */
090: @Override
091: public Object create(Object parent) throws ConfigException {
092: try {
093: return _type.create(parent);
094: } catch (Exception e) {
095: throw ConfigException.create(_setter, e);
096: }
097: }
098:
099: public String toString() {
100: return getClass().getSimpleName() + "[" + _setter + "]";
101: }
102: }
|