KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > JdbcConfigParser


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc;
13
14 import com.versant.core.common.config.ConfigParser;
15 import com.versant.core.common.BindingSupportImpl;
16 import com.versant.core.common.Utils;
17 import com.versant.core.metadata.parser.JdoExtensionKeys;
18 import com.versant.core.metadata.parser.JdoExtension;
19 import com.versant.core.metadata.MDStatics;
20 import com.versant.core.metadata.MetaDataEnums;
21 import com.versant.core.jdbc.sql.SqlDriver;
22 import com.versant.core.jdbc.metadata.JdbcClass;
23 import com.versant.core.jdbc.metadata.JdbcMetaDataEnums;
24
25 import java.util.Properties JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.sql.Connection JavaDoc;
30
31 /**
32  * Parses JDBC specific info from Properties.
33  */

34 public class JdbcConfigParser {
35
36     private MetaDataEnums MDE = new MetaDataEnums();
37     private JdbcMetaDataEnums jdbcMDE = new JdbcMetaDataEnums();
38     public Map JavaDoc JDBC_CLASS_ID_ENUM = new HashMap JavaDoc();
39
40     public JdbcConfigParser() {
41         JDBC_CLASS_ID_ENUM.put(JdoExtension.HASH_VALUE,
42                 new Integer JavaDoc(JdbcConfig.DEFAULT_CLASS_ID_HASH));
43         JDBC_CLASS_ID_ENUM.put(JdoExtension.NO_VALUE,
44                 new Integer JavaDoc(JdbcConfig.DEFAULT_CLASS_ID_NO));
45         JDBC_CLASS_ID_ENUM.put(JdoExtension.NAME_VALUE,
46                 new Integer JavaDoc(JdbcConfig.DEFAULT_CLASS_ID_NAME));
47         JDBC_CLASS_ID_ENUM.put(JdoExtension.FULLNAME_VALUE,
48                 new Integer JavaDoc(JdbcConfig.DEFAULT_CLASS_ID_FULLNAME));
49     }
50
51     public JdbcConfig parse(Properties JavaDoc p) {
52         JdbcConfig jc = new JdbcConfig();
53         jc.name = "main";
54
55         /*The url can be null, if we are using a DataSource*/
56         jc.url = p.getProperty(ConfigParser.STD_CON_URL);
57         jc.db = p.getProperty(ConfigParser.STORE_DB);
58         if (jc.db != null) {
59             jc.db = jc.db.trim();
60         }
61         if (jc.db == null || jc.db.length() == 0) {
62             jc.db = SqlDriver.getNameFromURL(jc.url);
63             if (jc.db == null) {
64                 throw BindingSupportImpl.getInstance().runtime("Unable to guess " +
65                         "database type from URL '" + jc.url + "', " +
66                         "use the " + ConfigParser.STORE_DB +
67                         " property to set the database type");
68             }
69         }
70         jc.driver = p.getProperty(ConfigParser.STD_CON_DRIVER_NAME);
71         if (jc.driver != null && jc.driver.length() == 0) {
72             jc.driver = null;
73         }
74         if (jc.driver == null) {
75             jc.driver = SqlDriver.getDriverFromURL(jc.url);
76         }
77         if (jc.driver == null && Utils.isStringEmpty(p.getProperty(ConfigParser.STD_CON_FACTORY_NAME))) {
78             jc.driver = ConfigParser.getReq(p, ConfigParser.STD_CON_DRIVER_NAME);
79         }
80         jc.user = p.getProperty(ConfigParser.STD_CON_USER_NAME);
81         jc.password = p.getProperty(ConfigParser.STD_CON_PASSWORD);
82         jc.properties = p.getProperty(ConfigParser.STORE_PROPERTIES);
83         jc.conFactory = p.getProperty(ConfigParser.STD_CON_FACTORY_NAME);
84         // connection 2 stuff
85
jc.conFactory2 = p.getProperty(ConfigParser.STD_CON2_FACTORY_NAME);
86         jc.driver2 = p.getProperty(ConfigParser.CON2_DRIVER_NAME);
87         jc.properties2 = p.getProperty(ConfigParser.CON2_PROPERTIES);
88         jc.url2 = p.getProperty(ConfigParser.CON2_URL);
89         jc.user2 = p.getProperty(ConfigParser.CON2_USER_NAME);
90         jc.password2 = p.getProperty(ConfigParser.CON2_PASSWORD);
91
92         jc.maxActive = ConfigParser.getInt(p, ConfigParser.STORE_MAX_ACTIVE,
93                 ConfigParser.DEFAULT_STORE_MAX_ACTIVE);
94         jc.maxIdle = ConfigParser.getInt(p, ConfigParser.STORE_MAX_IDLE, ConfigParser.DEFAULT_STORE_MAX_IDLE);
95         jc.minIdle = ConfigParser.getInt(p, ConfigParser.STORE_MIN_IDLE, ConfigParser.DEFAULT_STORE_MIN_IDLE);
96         jc.reserved = ConfigParser.getInt(p, ConfigParser.STORE_RESERVED, ConfigParser.DEFAULT_STORE_RESERVED);
97         jc.conTimeout = ConfigParser.getInt(p, ConfigParser.STORE_CON_TIMEOUT, 120);
98         jc.testInterval = ConfigParser.getInt(p, ConfigParser.STORE_TEST_INTERVAL, 120);
99         jc.waitForConOnStartup = ConfigParser.getBoolean(p,
100                 ConfigParser.STORE_WAIT_FOR_CON_ON_STARTUP,
101                 ConfigParser.DEFAULT_STORE_WAIT_FOR_CON_ON_STARTUP);
102         jc.testOnAlloc = ConfigParser.getBoolean(p,
103                 ConfigParser.STORE_TEST_ON_ALLOC,
104                 ConfigParser.DEFAULT_STORE_TEST_ON_ALLOC);
105         jc.testOnRelease = ConfigParser.getBoolean(p,
106                 ConfigParser.STORE_TEST_ON_RELEASE,
107                 ConfigParser.DEFAULT_STORE_TEST_ON_RELEASE);
108         jc.testOnException = ConfigParser.getBoolean(p,
109                 ConfigParser.STORE_TEST_ON_EXCEPTION,
110                 ConfigParser.DEFAULT_STORE_TEST_ON_EXCEPTION);
111         jc.testWhenIdle = ConfigParser.getBoolean(p,
112                 ConfigParser.STORE_TEST_WHEN_IDLE,
113                 true);
114         jc.retryIntervalMs = ConfigParser.getInt(p,
115                 ConfigParser.STORE_RETRY_INTERVAL_MS,
116                 ConfigParser.DEFAULT_STORE_RETRY_INTERVAL_MS);
117         jc.retryCount = ConfigParser.getInt(p,
118                 ConfigParser.STORE_RETRY_COUNT,
119                 ConfigParser.DEFAULT_STORE_RETRY_COUNT);
120         jc.validateMappingOnStartup = ConfigParser.getBoolean(p,
121                 ConfigParser.STORE_VALIDATE_MAPPING_ON_STARTUP, false);
122         jc.validateSQL = ConfigParser.trim(p.getProperty(
123                 ConfigParser.STORE_VALIDATE_SQL));
124         jc.initSQL = ConfigParser.trim(p.getProperty(
125                 ConfigParser.STORE_INIT_SQL));
126         jc.maxConAge = ConfigParser.getInt(p, ConfigParser.STORE_MAX_CON_AGE,
127                 ConfigParser.DEFAULT_MAX_CON_AGE);
128
129         jc.blockWhenFull =
130                 ConfigParser.getBoolean(p, ConfigParser.STORE_BLOCK_WHEN_FULL, true);
131
132         jc.jdbcDisableStatementBatching =
133                 ConfigParser.getBoolean(p, ConfigParser.STORE_DISABLE_BATCHING, false);
134         jc.jdbcDisablePsCache =
135                 ConfigParser.getBoolean(p, ConfigParser.STORE_DISABLE_PS_CACHE, false);
136         jc.psCacheMax = ConfigParser.getInt(p, ConfigParser.STORE_PS_CACHE_MAX, 0);
137
138         String JavaDoc s = p.getProperty(ConfigParser.STORE_ISOLATION_LEVEL);
139         if (s == null) {
140             jc.isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
141         } else {
142             if (s.equals(ConfigParser.ISOLATION_LEVEL_READ_COMMITTED)) {
143                 jc.isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
144             } else if (s.equals(ConfigParser.ISOLATION_LEVEL_READ_UNCOMMITTED)) {
145                 jc.isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
146             } else if (s.equals(ConfigParser.ISOLATION_LEVEL_REPEATABLE_READ)) {
147                 jc.isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
148             } else if (s.equals(ConfigParser.ISOLATION_LEVEL_SERIALIZABLE)) {
149                 jc.isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
150             } else {
151                 throw BindingSupportImpl.getInstance().runtime("Invalid '" +
152                         ConfigParser.STORE_ISOLATION_LEVEL + "', expected " +
153                         ConfigParser.ISOLATION_LEVEL_READ_COMMITTED + ", " +
154                         ConfigParser.ISOLATION_LEVEL_REPEATABLE_READ + ", " +
155                         ConfigParser.ISOLATION_LEVEL_SERIALIZABLE + " or " +
156                         ConfigParser.ISOLATION_LEVEL_READ_UNCOMMITTED);
157             }
158         }
159
160         jc.jdbcNameGenerator = ConfigParser.getClassAndProps(p,
161                 ConfigParser.STORE_NAMEGEN,
162                 jc.jdbcNameGeneratorProps = new HashMap JavaDoc());
163         ConfigParser.getClassAndProps(p, ConfigParser.STORE_MIGRATION_CONTROLS,
164                 jc.jdbcMigrationControlProps = new HashMap JavaDoc());
165
166         String JavaDoc be = ConfigParser.STORE_EXT;
167         jc.jdbcOptimisticLocking = ConfigParser.getExtEnum(p, be,
168                 JdoExtensionKeys.JDBC_OPTIMISTIC_LOCKING,
169                 jdbcMDE.OPTIMISTIC_LOCKING_ENUM,
170                 JdbcClass.OPTIMISTIC_LOCKING_VERSION);
171         jc.readOnly = ConfigParser.getExtBoolean(p, be,
172                 JdoExtensionKeys.READ_ONLY, false);
173         jc.cacheStrategy = ConfigParser.getExtEnum(p, be,
174                 JdoExtensionKeys.CACHE_STRATEGY,
175                 MDE.CACHE_ENUM,
176                 MDStatics.CACHE_STRATEGY_YES);
177         jc.inheritance = ConfigParser.getExtEnum(p, be,
178                 JdoExtensionKeys.JDBC_INHERITANCE,
179                 jdbcMDE.INHERITANCE_ENUM,
180                 JdbcClass.INHERITANCE_FLAT);
181         s = p.getProperty(be + ConfigParser.JDBC_INHERITANCE_NO_CLASSID);
182         if ("true".equals(s)) {
183             jc.defaultClassId = JdbcConfig.DEFAULT_CLASS_ID_NO;
184         } else {
185             jc.defaultClassId = ConfigParser.getExtEnum(p, be,
186                     JdoExtensionKeys.JDBC_CLASS_ID,
187                     JDBC_CLASS_ID_ENUM,
188                     JdbcConfig.DEFAULT_CLASS_ID_HASH);
189         }
190         jc.jdbcDoNotCreateTable = ConfigParser.getExtBoolean(p, be,
191                 JdoExtensionKeys.JDBC_DO_NOT_CREATE_TABLE, false);
192         jc.oidsInDefaultFetchGroup = ConfigParser.getExtBoolean(p, be,
193                 JdoExtensionKeys.OIDS_IN_DEFAULT_FETCH_GROUP, true);
194
195         jc.managedOneToMany = ConfigParser.getBoolean(p,
196                 ConfigParser.STORE_MANAGED_ONE_TO_MANY,
197                 ConfigParser.DEFAULT_STORE_MANAGED_ONE_TO_MANY);
198         jc.managedManyToMany = ConfigParser.getBoolean(p,
199                 ConfigParser.STORE_MANAGED_MANY_TO_MANY,
200                 ConfigParser.DEFAULT_STORE_MANAGED_MANY_TO_MANY);
201
202         jc.jdbcKeyGenerator = ConfigParser.getClassAndProps(p, be + JdoExtension.
203                 toKeyString(JdoExtensionKeys.JDBC_KEY_GENERATOR),
204                 jc.jdbcKeyGeneratorProps = new HashMap JavaDoc());
205
206         jc.typeMappings = readTypeMappings(p);
207         jc.javaTypeMappings = readJavaTypeMappings(p);
208
209         return jc;
210     }
211
212     private ArrayList JavaDoc readTypeMappings(Properties JavaDoc p) {
213         int n = ConfigParser.MAX_STORE_TYPE_MAPPING_COUNT;
214         String JavaDoc s = null;
215         ArrayList JavaDoc a = new ArrayList JavaDoc(n);
216         for (int i = 0; i < n; i++) {
217             s = p.getProperty(ConfigParser.STORE_TYPE_MAPPING + i);
218             if (s != null) {
219                 a.add(ConfigParser.getReq(p, ConfigParser.STORE_TYPE_MAPPING + i));
220             }
221         }
222         return a;
223     }
224
225     private ArrayList JavaDoc readJavaTypeMappings(Properties JavaDoc p) {
226         int n = ConfigParser.MAX_STORE_JAVATYPE_MAPPING_COUNT;
227         String JavaDoc s = null;
228         ArrayList JavaDoc a = new ArrayList JavaDoc(n);
229         for (int i = 0; i < n; i++) {
230             s = p.getProperty(ConfigParser.STORE_JAVATYPE_MAPPING + i);
231             if (s != null) {
232                 a.add(ConfigParser.getReq(p, ConfigParser.STORE_JAVATYPE_MAPPING + i));
233             }
234         }
235         return a;
236     }
237
238 }
239
240
Popular Tags