Source Code Cross Referenced for PropertyUtils.java in  » Testing » unitils » org » unitils » util » 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 » Testing » unitils » org.unitils.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006-2007,  Unitils.org
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.unitils.util;
017:
018:        import org.unitils.core.UnitilsException;
019:        import static org.unitils.util.ReflectionUtils.createInstanceOfType;
020:
021:        import java.util.ArrayList;
022:        import java.util.List;
023:        import java.util.Properties;
024:
025:        /**
026:         * Utilities for working with property files.
027:         *
028:         * @author Tim Ducheyne
029:         * @author Filip Neven
030:         */
031:        public class PropertyUtils {
032:
033:            /**
034:             * Gets the string value for the property with the given name. If no such property is found or
035:             * the value is empty, an exception will be raised.
036:             *
037:             * @param propertyName The name, not null
038:             * @param properties   The properties, not null
039:             * @return The trimmed string value, not null
040:             */
041:            public static String getString(String propertyName,
042:                    Properties properties) {
043:                String value = getProperty(propertyName, properties);
044:                if (value == null || "".equals(value.trim())) {
045:                    throw new UnitilsException("No value found for property "
046:                            + propertyName);
047:                }
048:                return value.trim();
049:            }
050:
051:            /**
052:             * Gets the string value for the property with the given name. If no such property is found or
053:             * the value is empty, the given default value is returned.
054:             *
055:             * @param propertyName The name, not null
056:             * @param defaultValue The default value
057:             * @param properties   The properties, not null
058:             * @return The trimmed string value, not null
059:             */
060:            public static String getString(String propertyName,
061:                    String defaultValue, Properties properties) {
062:                String value = getProperty(propertyName, properties);
063:                if (value == null || "".equals(value.trim())) {
064:                    return defaultValue;
065:                }
066:                return value.trim();
067:            }
068:
069:            /**
070:             * Gets the list of comma separated string values for the property with the given name. If no such property is found or
071:             * the value is empty, an empty list is returned. Empty elements (",,") will not be added. A space (", ,") is not
072:             * empty, a "" will be added.
073:             *
074:             * @param propertyName The name, not null
075:             * @param properties   The properties, not null
076:             * @return The trimmed string list, empty if none found
077:             */
078:            public static List<String> getStringList(String propertyName,
079:                    Properties properties) {
080:                return getStringList(propertyName, properties, false);
081:
082:            }
083:
084:            /**
085:             * Gets the list of comma separated string values for the property with the given name. If no such property is found or
086:             * the value is empty, an empty list is returned if not required, else an exception is raised. Empty elements (",,")
087:             * will not be added. A space (", ,") is not empty, a "" will be added.
088:             *
089:             * @param propertyName The name, not null
090:             * @param properties   The properties, not null
091:             * @param required     If true an exception will be raised when the property is not found or empty
092:             * @return The trimmed string list, empty or exception if none found
093:             */
094:            public static List<String> getStringList(String propertyName,
095:                    Properties properties, boolean required) {
096:                String values = getProperty(propertyName, properties);
097:                if (values == null || "".equals(values.trim())) {
098:                    if (required) {
099:                        throw new UnitilsException(
100:                                "No value found for property " + propertyName);
101:                    }
102:                    return new ArrayList<String>(0);
103:                }
104:                String[] splitValues = values.split(",");
105:                List<String> result = new ArrayList<String>(splitValues.length);
106:                for (String value : splitValues) {
107:                    result.add(value.trim());
108:                }
109:
110:                if (required && result.isEmpty()) {
111:                    throw new UnitilsException("No value found for property "
112:                            + propertyName);
113:                }
114:                return result;
115:            }
116:
117:            /**
118:             * Gets the boolean value for the property with the given name. If no such property is found or
119:             * the value is empty, an exception will be raised.
120:             *
121:             * @param propertyName The name, not null
122:             * @param properties   The properties, not null
123:             * @return The boolean value, not null
124:             */
125:            public static boolean getBoolean(String propertyName,
126:                    Properties properties) {
127:                String value = getProperty(propertyName, properties);
128:                if (value == null || "".equals(value.trim())) {
129:                    throw new UnitilsException("No value found for property "
130:                            + propertyName);
131:                }
132:                return Boolean.valueOf(value.trim());
133:            }
134:
135:            /**
136:             * Gets the boolean value for the property with the given name. If no such property is found or
137:             * the value is empty, the given default value is returned.
138:             *
139:             * @param propertyName The name, not null
140:             * @param defaultValue The default value
141:             * @param properties   The properties, not null
142:             * @return The boolean value, not null
143:             */
144:            public static boolean getBoolean(String propertyName,
145:                    boolean defaultValue, Properties properties) {
146:                String value = getProperty(propertyName, properties);
147:                if (value == null || "".equals(value.trim())) {
148:                    return defaultValue;
149:                }
150:                return Boolean.valueOf(value.trim());
151:            }
152:
153:            /**
154:             * Gets the long value for the property with the given name. If no such property is found, the value is empty
155:             * or cannot be converted to a long, an exception will be raised.
156:             *
157:             * @param propertyName The name, not null
158:             * @param properties   The properties, not null
159:             * @return The long value, not null
160:             */
161:            public static long getLong(String propertyName,
162:                    Properties properties) {
163:                String value = getProperty(propertyName, properties);
164:                if (value == null || "".equals(value.trim())) {
165:                    throw new UnitilsException("No value found for property "
166:                            + propertyName);
167:                }
168:                try {
169:                    return Long.valueOf(value.trim());
170:
171:                } catch (NumberFormatException e) {
172:                    throw new UnitilsException("Value " + value
173:                            + " for property " + propertyName
174:                            + " is not a number.");
175:                }
176:            }
177:
178:            /**
179:             * Gets the long value for the property with the given name. If no such property is found or
180:             * the value is empty, the given default value is returned. If the value cannot be converted to a long,
181:             * an exception will be raised.
182:             *
183:             * @param propertyName The name, not null
184:             * @param defaultValue The default value
185:             * @param properties   The properties, not null
186:             * @return The string value, not null
187:             */
188:            public static long getLong(String propertyName, long defaultValue,
189:                    Properties properties) {
190:                String value = getProperty(propertyName, properties);
191:                if (value == null || "".equals(value.trim())) {
192:                    return defaultValue;
193:                }
194:                try {
195:                    return Long.valueOf(value.trim());
196:
197:                } catch (NumberFormatException e) {
198:                    throw new UnitilsException("Value " + value
199:                            + " for property " + propertyName
200:                            + " is not a number.");
201:                }
202:            }
203:
204:            /**
205:             * Checks whether the property with the given name exists in the System or in the given properties.
206:             *
207:             * @param propertyName The property name, not null
208:             * @param properties   The properties if not found in System, not null
209:             * @return True if the property exitsts
210:             */
211:            public static boolean containsProperty(String propertyName,
212:                    Properties properties) {
213:                return getProperty(propertyName, properties) != null;
214:            }
215:
216:            /**
217:             * Gets an instance of the type specified by the property with the given name. If no such property is found, the
218:             * value is empty or the instance cannot be created, an exception will be raised.
219:             *
220:             * @param propertyName The name, not null
221:             * @param properties   The properties, not null
222:             * @return The instance value, not null
223:             */
224:            @SuppressWarnings({"unchecked"})
225:            public static <T> T getInstance(String propertyName,
226:                    Properties properties) {
227:                String className = getString(propertyName, properties);
228:                return (T) createInstanceOfType(className, false);
229:            }
230:
231:            /**
232:             * Gets an instance of the type specified by the property with the given name. If no such property is found, the
233:             * value is empty, the given default value is returned. If the instance cannot be created an exception will be raised.
234:             *
235:             * @param propertyName The name, not null
236:             * @param defaultValue The default value
237:             * @param properties   The properties, not null
238:             * @return The instance value, not null
239:             */
240:            @SuppressWarnings({"unchecked"})
241:            public static <T> T getInstance(String propertyName,
242:                    T defaultValue, Properties properties) {
243:                String className = getString(propertyName, null, properties);
244:                if (className == null) {
245:                    return defaultValue;
246:                }
247:                return (T) createInstanceOfType(className, false);
248:            }
249:
250:            /**
251:             * Gets a property from the System or from the given properties.
252:             *
253:             * @param propertyName The name of the property, not null
254:             * @param properties   The properties if not found in System, not null
255:             * @return The property value, null if not found
256:             */
257:            private static String getProperty(String propertyName,
258:                    Properties properties) {
259:                String value = System.getProperty(propertyName);
260:                if (value == null) {
261:                    value = properties.getProperty(propertyName);
262:                }
263:                return value;
264:            }
265:
266:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.