KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jca > cfg > ObjectConfig


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jca.cfg;
30
31 import com.caucho.config.Config;
32 import com.caucho.config.ConfigException;
33 import com.caucho.log.Log;
34 import com.caucho.util.L10N;
35
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Configuration for an object with config values.
42  */

43 public class ObjectConfig {
44   private static final L10N L = new L10N(ObjectConfig.class);
45   private static final Logger JavaDoc log = Log.open(ObjectConfig.class);
46
47   private Class JavaDoc _type;
48   private HashMap JavaDoc<String JavaDoc,ConfigPropertyConfig> _propertyMap =
49     new HashMap JavaDoc<String JavaDoc,ConfigPropertyConfig>();
50   
51   public ObjectConfig()
52   {
53   }
54
55   /**
56    * Sets the type.
57    */

58   public void setType(Class JavaDoc type)
59     throws ConfigException
60   {
61     _type = type;
62
63     Config.validate(type, Object JavaDoc.class);
64   }
65
66   /**
67    * Gets the config property type.
68    */

69   public Class JavaDoc getType()
70   {
71     return _type;
72   }
73
74   /**
75    * Adds a new config property.
76    */

77   public void addConfigProperty(ConfigPropertyConfig property)
78     throws ConfigException
79   {
80     if (_propertyMap.get(property.getName()) != null)
81       throw new ConfigException(L.l("'{0}' is a duplicate property name. Property names must be declared only once.",
82                     property.getName()));
83
84     _propertyMap.put(property.getName(), property);
85   }
86
87   /**
88    * Instantiates the object and sets the default properties.
89    */

90   public Object JavaDoc instantiate()
91     throws Exception JavaDoc
92   {
93     if (_type == null)
94       throw new ConfigException(L.l("The type must be set for an object configuration."));
95     
96     Object JavaDoc object = _type.newInstance();
97
98     Iterator JavaDoc<ConfigPropertyConfig> iter = _propertyMap.values().iterator();
99     while (iter.hasNext()) {
100       ConfigPropertyConfig prop = iter.next();
101
102       if (prop.getValue() != null) {
103     Config.setAttribute(object, prop.getName(), prop.getValue());
104       }
105     }
106
107     return object;
108   }
109 }
110
Popular Tags