Source Code Cross Referenced for ConfigurationDynaBean.java in  » Library » Apache-commons-configuration-1.4-src » org » apache » commons » configuration » beanutils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons configuration 1.4 src » org.apache.commons.configuration.beanutils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.configuration.beanutils;
019:
020:        import java.util.Iterator;
021:        import java.util.List;
022:
023:        import org.apache.commons.beanutils.DynaBean;
024:        import org.apache.commons.beanutils.DynaClass;
025:        import org.apache.commons.configuration.Configuration;
026:        import org.apache.commons.configuration.ConfigurationMap;
027:        import org.apache.commons.configuration.ConversionException;
028:        import org.apache.commons.configuration.SubsetConfiguration;
029:        import org.apache.commons.lang.BooleanUtils;
030:        import org.apache.commons.logging.Log;
031:        import org.apache.commons.logging.LogFactory;
032:
033:        /**
034:         * The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
035:         * configurations properties from a wrapped configuration-collection
036:         * {@link org.apache.commons.configuration.Configuration} instance. It also
037:         * implements a {@link java.util.Map} interface so that it can be used in
038:         * JSP 2.0 Expression Language expressions.
039:         *
040:         * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped properties
041:         * to the appropriate <code>Configuration</code> subset using the
042:         * {@link org.apache.commons.configuration.Configuration#subset}
043:         * method. Similarly, indexed properties reference lists of configuration
044:         * properties using the
045:         * {@link org.apache.commons.configuration.Configuration#getList(String)}
046:         * method. Setting an indexed property always throws an exception.</p>
047:         *
048:         * <p>Note: Some of the methods expect that a dot (&quot;.&quot;) is used as
049:         * property delimitor for the wrapped configuration. This is true for most of
050:         * the default configurations. Hierarchical configurations, for which a specific
051:         * expression engine is set, may cause problems.</p>
052:         *
053:         * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
054:         * @version $Revision: 492216 $, $Date: 2007-01-03 17:51:24 +0100 (Mi, 03 Jan 2007) $
055:         * @since 1.0-rc1
056:         */
057:        public class ConfigurationDynaBean extends ConfigurationMap implements 
058:                DynaBean {
059:            /** Constant for the property delimiter.*/
060:            private static final String PROPERTY_DELIMITER = ".";
061:
062:            /** The logger.*/
063:            private static Log log = LogFactory
064:                    .getLog(ConfigurationDynaBean.class);
065:
066:            /**
067:             * Creates a new instance of <code>ConfigurationDynaBean</code> and sets
068:             * the configuration this bean is associated with.
069:             * @param configuration the configuration
070:             */
071:            public ConfigurationDynaBean(Configuration configuration) {
072:                super (configuration);
073:                if (log.isTraceEnabled()) {
074:                    log.trace("ConfigurationDynaBean(" + configuration + ")");
075:                }
076:            }
077:
078:            public void set(String name, Object value) {
079:                if (log.isTraceEnabled()) {
080:                    log.trace("set(" + name + "," + value + ")");
081:                }
082:
083:                if (value == null) {
084:                    throw new NullPointerException(
085:                            "Error trying to set property to null.");
086:                }
087:
088:                if (value instanceof  List) {
089:                    List list = (List) value;
090:                    Iterator iterator = list.iterator();
091:                    while (iterator.hasNext()) {
092:                        getConfiguration().addProperty(name, iterator.next());
093:                    }
094:                } else if (value instanceof  int[]) {
095:                    int[] array = (int[]) value;
096:                    for (int i = 0; i < array.length; i++) {
097:                        getConfiguration().addProperty(name,
098:                                new Integer(array[i]));
099:                    }
100:                } else if (value instanceof  boolean[]) {
101:                    boolean[] array = (boolean[]) value;
102:                    for (int i = 0; i < array.length; i++) {
103:                        getConfiguration().addProperty(name,
104:                                BooleanUtils.toBooleanObject(array[i]));
105:                    }
106:                } else if (value instanceof  char[]) {
107:                    char[] array = (char[]) value;
108:                    for (int i = 0; i < array.length; i++) {
109:                        getConfiguration().addProperty(name,
110:                                new Character(array[i]));
111:                    }
112:                } else if (value instanceof  byte[]) {
113:                    byte[] array = (byte[]) value;
114:                    for (int i = 0; i < array.length; i++) {
115:                        getConfiguration()
116:                                .addProperty(name, new Byte(array[i]));
117:                    }
118:                } else if (value instanceof  short[]) {
119:                    short[] array = (short[]) value;
120:                    for (int i = 0; i < array.length; i++) {
121:                        getConfiguration().addProperty(name,
122:                                new Short(array[i]));
123:                    }
124:                } else if (value instanceof  long[]) {
125:                    long[] array = (long[]) value;
126:                    for (int i = 0; i < array.length; i++) {
127:                        getConfiguration()
128:                                .addProperty(name, new Long(array[i]));
129:                    }
130:                } else if (value instanceof  float[]) {
131:                    float[] array = (float[]) value;
132:                    for (int i = 0; i < array.length; i++) {
133:                        getConfiguration().addProperty(name,
134:                                new Float(array[i]));
135:                    }
136:                } else if (value instanceof  double[]) {
137:                    double[] array = (double[]) value;
138:                    for (int i = 0; i < array.length; i++) {
139:                        getConfiguration().addProperty(name,
140:                                new Double(array[i]));
141:                    }
142:                } else if (value instanceof  Object[]) {
143:                    Object[] array = (Object[]) value;
144:                    for (int i = 0; i < array.length; i++) {
145:                        getConfiguration().addProperty(name, array[i]);
146:                    }
147:                } else {
148:                    getConfiguration().setProperty(name, value);
149:                }
150:            }
151:
152:            public Object get(String name) {
153:                if (log.isTraceEnabled()) {
154:                    log.trace("get(" + name + ")");
155:                }
156:
157:                // get configuration property
158:                Object result = getConfiguration().getProperty(name);
159:                if (result == null) {
160:                    // otherwise attempt to create bean from configuration subset
161:                    Configuration subset = new SubsetConfiguration(
162:                            getConfiguration(), name, PROPERTY_DELIMITER);
163:                    if (!subset.isEmpty()) {
164:                        result = new ConfigurationDynaBean(subset);
165:                    }
166:                }
167:
168:                if (log.isDebugEnabled()) {
169:                    log.debug(name + "=[" + result + "]");
170:                }
171:
172:                if (result == null) {
173:                    throw new IllegalArgumentException("Property '" + name
174:                            + "' does not exist.");
175:                }
176:                return result;
177:            }
178:
179:            public boolean contains(String name, String key) {
180:                Configuration subset = getConfiguration().subset(name);
181:                if (subset == null) {
182:                    throw new IllegalArgumentException("Mapped property '"
183:                            + name + "' does not exist.");
184:                }
185:
186:                return subset.containsKey(key);
187:            }
188:
189:            public Object get(String name, int index) {
190:                try {
191:                    List list = getConfiguration().getList(name);
192:                    if (list.isEmpty()) {
193:                        throw new IllegalArgumentException("Indexed property '"
194:                                + name + "' does not exist.");
195:                    }
196:
197:                    return list.get(index);
198:                } catch (ConversionException e) {
199:                    throw new IllegalArgumentException("Property '" + name
200:                            + "' is not indexed.");
201:                }
202:            }
203:
204:            public Object get(String name, String key) {
205:                Configuration subset = getConfiguration().subset(name);
206:                if (subset == null) {
207:                    throw new IllegalArgumentException("Mapped property '"
208:                            + name + "' does not exist.");
209:                }
210:
211:                return subset.getProperty(key);
212:            }
213:
214:            public DynaClass getDynaClass() {
215:                return new ConfigurationDynaClass(getConfiguration());
216:            }
217:
218:            public void remove(String name, String key) {
219:                Configuration subset = new SubsetConfiguration(
220:                        getConfiguration(), name, PROPERTY_DELIMITER);
221:                subset.setProperty(key, null);
222:            }
223:
224:            public void set(String name, int index, Object value) {
225:                try {
226:                    Object property = getConfiguration().getProperty(name);
227:
228:                    if (property == null) {
229:                        throw new IllegalArgumentException("Property '" + name
230:                                + "' does not exist.");
231:                    } else if (property instanceof  List) {
232:                        List list = (List) property;
233:                        list.set(index, value);
234:                        getConfiguration().setProperty(name, list);
235:                    } else if (property.getClass().isArray()) {
236:                        Object[] array = (Object[]) property;
237:                        array[index] = value;
238:                    } else if (index == 0) {
239:                        getConfiguration().setProperty(name, value);
240:                    } else {
241:                        throw new IllegalArgumentException("Property '" + name
242:                                + "' is not indexed.");
243:                    }
244:                } catch (ConversionException e) {
245:                    throw new IllegalArgumentException("Property '" + name
246:                            + "' is not indexed.");
247:                }
248:            }
249:
250:            public void set(String name, String key, Object value) {
251:                getConfiguration().setProperty(name + "." + key, value);
252:            }
253:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.