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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jca.cfg;
030:
031: import com.caucho.config.Config;
032: import com.caucho.config.ConfigException;
033: import com.caucho.log.Log;
034: import com.caucho.util.L10N;
035:
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import java.util.logging.Logger;
039:
040: /**
041: * Configuration for an object with config values.
042: */
043: public class ObjectConfig {
044: private static final L10N L = new L10N(ObjectConfig.class);
045: private static final Logger log = Log.open(ObjectConfig.class);
046:
047: private Class _type;
048: private HashMap<String, ConfigPropertyConfig> _propertyMap = new HashMap<String, ConfigPropertyConfig>();
049:
050: public ObjectConfig() {
051: }
052:
053: /**
054: * Sets the type.
055: */
056: public void setType(Class type) throws ConfigException {
057: _type = type;
058:
059: Config.validate(type, Object.class);
060: }
061:
062: /**
063: * Gets the config property type.
064: */
065: public Class getType() {
066: return _type;
067: }
068:
069: /**
070: * Adds a new config property.
071: */
072: public void addConfigProperty(ConfigPropertyConfig property)
073: throws ConfigException {
074: if (_propertyMap.get(property.getName()) != null)
075: throw new ConfigException(
076: L
077: .l(
078: "'{0}' is a duplicate property name. Property names must be declared only once.",
079: property.getName()));
080:
081: _propertyMap.put(property.getName(), property);
082: }
083:
084: /**
085: * Instantiates the object and sets the default properties.
086: */
087: public Object instantiate() throws Exception {
088: if (_type == null)
089: throw new ConfigException(
090: L
091: .l("The type must be set for an object configuration."));
092:
093: Object object = _type.newInstance();
094:
095: Iterator<ConfigPropertyConfig> iter = _propertyMap.values()
096: .iterator();
097: while (iter.hasNext()) {
098: ConfigPropertyConfig prop = iter.next();
099:
100: if (prop.getValue() != null) {
101: Config.setAttribute(object, prop.getName(), prop
102: .getValue());
103: }
104: }
105:
106: return object;
107: }
108: }
|