KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > DBCPDataSourceProperties


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.conf;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.apache.cayenne.ConfigurationException;
27 import org.apache.cayenne.util.ResourceLocator;
28 import org.apache.commons.pool.impl.GenericObjectPool;
29
30 /**
31  * A helper class providing access to DBCP properties.
32  *
33  * @author Andrus Adamchik
34  * @since 2.0
35  */

36 class DBCPDataSourceProperties {
37
38     private static final String JavaDoc PROPERTY_PREFIX = "cayenne.dbcp.";
39
40     private Properties JavaDoc properties;
41
42     /**
43      * Loads properties from the specified location.
44      */

45     static Properties JavaDoc loadProperties(ResourceLocator resourceLocator, String JavaDoc location)
46             throws IOException JavaDoc {
47
48         InputStream JavaDoc in = resourceLocator.findResourceStream(location);
49
50         // try appending ".properties" extension..
51
if (in == null && !location.endsWith(".properties")) {
52             in = resourceLocator.findResourceStream(location + ".properties");
53         }
54
55         if (in == null) {
56             throw new ConfigurationException("DBCP properties file not found: "
57                     + location);
58         }
59
60         Properties JavaDoc properties = new Properties JavaDoc();
61         try {
62             properties.load(in);
63         }
64         finally {
65             try {
66                 in.close();
67             }
68             catch (IOException JavaDoc e) {
69             }
70         }
71
72         return properties;
73     }
74
75     DBCPDataSourceProperties(ResourceLocator resourceLocator, String JavaDoc location)
76             throws IOException JavaDoc {
77         this(loadProperties(resourceLocator, location));
78     }
79
80     DBCPDataSourceProperties(Properties JavaDoc properties) {
81         this.properties = properties;
82     }
83
84     Properties JavaDoc getProperties() {
85         return properties;
86     }
87
88     String JavaDoc getString(String JavaDoc property, boolean required) {
89         String JavaDoc value = properties.getProperty(PROPERTY_PREFIX + property);
90
91         if (required && value == null) {
92             throw new ConfigurationException("No value for required property: "
93                     + PROPERTY_PREFIX
94                     + property);
95         }
96
97         return value;
98     }
99
100     String JavaDoc getString(String JavaDoc property) {
101         return getString(property, false);
102     }
103
104     boolean getBoolean(String JavaDoc property, boolean defaultValue) {
105         String JavaDoc value = getString(property);
106         return (value != null)
107                 ? "true".equalsIgnoreCase(getString(property))
108                 : defaultValue;
109     }
110
111     int getInt(String JavaDoc property, int defaultValue) {
112         String JavaDoc value = getString(property);
113
114         try {
115             return (value != null) ? Integer.parseInt(value) : defaultValue;
116         }
117         catch (NumberFormatException JavaDoc nfex) {
118             return defaultValue;
119         }
120     }
121
122     long getLong(String JavaDoc property, long defaultValue) {
123         String JavaDoc value = getString(property);
124         try {
125             return (value != null) ? Long.parseLong(value) : defaultValue;
126         }
127         catch (NumberFormatException JavaDoc nfex) {
128             return defaultValue;
129         }
130     }
131
132     byte getByte(String JavaDoc property, byte defaultValue) {
133         String JavaDoc value = getString(property);
134         try {
135             return (value != null) ? Byte.parseByte(value) : defaultValue;
136         }
137         catch (NumberFormatException JavaDoc nfex) {
138             return defaultValue;
139         }
140     }
141
142     byte getWhenExhaustedAction(String JavaDoc property, byte defaultValue)
143             throws ConfigurationException {
144
145         String JavaDoc value = getString(property);
146
147         if (value == null) {
148             return defaultValue;
149         }
150
151         // try byte...
152
try {
153             return Byte.parseByte(value);
154         }
155         catch (NumberFormatException JavaDoc nfex) {
156             // try symbolic
157
try {
158                 return GenericObjectPool.class.getField(value).getByte(null);
159             }
160             catch (Throwable JavaDoc th) {
161                 throw new ConfigurationException("Invalid 'whenExhaustedAction': "
162                         + value);
163             }
164         }
165     }
166
167     int getTransactionIsolation(String JavaDoc property, int defaultValue) {
168
169         String JavaDoc value = getString(property);
170
171         if (value == null) {
172             return defaultValue;
173         }
174
175         // try int...
176
try {
177             return Integer.parseInt(value);
178         }
179         catch (NumberFormatException JavaDoc nfex) {
180             // try symbolic
181
try {
182                 return Connection JavaDoc.class.getField(value).getInt(null);
183             }
184             catch (Throwable JavaDoc th) {
185                 throw new ConfigurationException(
186                         "Invalid 'defaultTransactionIsolation': " + value);
187             }
188         }
189     }
190
191 }
192
Popular Tags