001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc;
012:
013: import com.versant.core.common.config.ConfigParser;
014: import com.versant.core.common.BindingSupportImpl;
015: import com.versant.core.common.Utils;
016: import com.versant.core.metadata.parser.JdoExtensionKeys;
017: import com.versant.core.metadata.parser.JdoExtension;
018: import com.versant.core.metadata.MDStatics;
019: import com.versant.core.metadata.MetaDataEnums;
020: import com.versant.core.jdbc.sql.SqlDriver;
021: import com.versant.core.jdbc.metadata.JdbcClass;
022: import com.versant.core.jdbc.metadata.JdbcMetaDataEnums;
023:
024: import java.util.Properties;
025: import java.util.HashMap;
026: import java.util.ArrayList;
027: import java.util.Map;
028: import java.sql.Connection;
029:
030: /**
031: * Parses JDBC specific info from Properties.
032: */
033: public class JdbcConfigParser {
034:
035: private MetaDataEnums MDE = new MetaDataEnums();
036: private JdbcMetaDataEnums jdbcMDE = new JdbcMetaDataEnums();
037: public Map JDBC_CLASS_ID_ENUM = new HashMap();
038:
039: public JdbcConfigParser() {
040: JDBC_CLASS_ID_ENUM.put(JdoExtension.HASH_VALUE, new Integer(
041: JdbcConfig.DEFAULT_CLASS_ID_HASH));
042: JDBC_CLASS_ID_ENUM.put(JdoExtension.NO_VALUE, new Integer(
043: JdbcConfig.DEFAULT_CLASS_ID_NO));
044: JDBC_CLASS_ID_ENUM.put(JdoExtension.NAME_VALUE, new Integer(
045: JdbcConfig.DEFAULT_CLASS_ID_NAME));
046: JDBC_CLASS_ID_ENUM.put(JdoExtension.FULLNAME_VALUE,
047: new Integer(JdbcConfig.DEFAULT_CLASS_ID_FULLNAME));
048: }
049:
050: public JdbcConfig parse(Properties p) {
051: JdbcConfig jc = new JdbcConfig();
052: jc.name = "main";
053:
054: /*The url can be null, if we are using a DataSource*/
055: jc.url = p.getProperty(ConfigParser.STD_CON_URL);
056: jc.db = p.getProperty(ConfigParser.STORE_DB);
057: if (jc.db != null) {
058: jc.db = jc.db.trim();
059: }
060: if (jc.db == null || jc.db.length() == 0) {
061: jc.db = SqlDriver.getNameFromURL(jc.url);
062: if (jc.db == null) {
063: throw BindingSupportImpl.getInstance().runtime(
064: "Unable to guess " + "database type from URL '"
065: + jc.url + "', " + "use the "
066: + ConfigParser.STORE_DB
067: + " property to set the database type");
068: }
069: }
070: jc.driver = p.getProperty(ConfigParser.STD_CON_DRIVER_NAME);
071: if (jc.driver != null && jc.driver.length() == 0) {
072: jc.driver = null;
073: }
074: if (jc.driver == null) {
075: jc.driver = SqlDriver.getDriverFromURL(jc.url);
076: }
077: if (jc.driver == null
078: && Utils
079: .isStringEmpty(p
080: .getProperty(ConfigParser.STD_CON_FACTORY_NAME))) {
081: jc.driver = ConfigParser.getReq(p,
082: ConfigParser.STD_CON_DRIVER_NAME);
083: }
084: jc.user = p.getProperty(ConfigParser.STD_CON_USER_NAME);
085: jc.password = p.getProperty(ConfigParser.STD_CON_PASSWORD);
086: jc.properties = p.getProperty(ConfigParser.STORE_PROPERTIES);
087: jc.conFactory = p
088: .getProperty(ConfigParser.STD_CON_FACTORY_NAME);
089: // connection 2 stuff
090: jc.conFactory2 = p
091: .getProperty(ConfigParser.STD_CON2_FACTORY_NAME);
092: jc.driver2 = p.getProperty(ConfigParser.CON2_DRIVER_NAME);
093: jc.properties2 = p.getProperty(ConfigParser.CON2_PROPERTIES);
094: jc.url2 = p.getProperty(ConfigParser.CON2_URL);
095: jc.user2 = p.getProperty(ConfigParser.CON2_USER_NAME);
096: jc.password2 = p.getProperty(ConfigParser.CON2_PASSWORD);
097:
098: jc.maxActive = ConfigParser.getInt(p,
099: ConfigParser.STORE_MAX_ACTIVE,
100: ConfigParser.DEFAULT_STORE_MAX_ACTIVE);
101: jc.maxIdle = ConfigParser.getInt(p,
102: ConfigParser.STORE_MAX_IDLE,
103: ConfigParser.DEFAULT_STORE_MAX_IDLE);
104: jc.minIdle = ConfigParser.getInt(p,
105: ConfigParser.STORE_MIN_IDLE,
106: ConfigParser.DEFAULT_STORE_MIN_IDLE);
107: jc.reserved = ConfigParser.getInt(p,
108: ConfigParser.STORE_RESERVED,
109: ConfigParser.DEFAULT_STORE_RESERVED);
110: jc.conTimeout = ConfigParser.getInt(p,
111: ConfigParser.STORE_CON_TIMEOUT, 120);
112: jc.testInterval = ConfigParser.getInt(p,
113: ConfigParser.STORE_TEST_INTERVAL, 120);
114: jc.waitForConOnStartup = ConfigParser.getBoolean(p,
115: ConfigParser.STORE_WAIT_FOR_CON_ON_STARTUP,
116: ConfigParser.DEFAULT_STORE_WAIT_FOR_CON_ON_STARTUP);
117: jc.testOnAlloc = ConfigParser.getBoolean(p,
118: ConfigParser.STORE_TEST_ON_ALLOC,
119: ConfigParser.DEFAULT_STORE_TEST_ON_ALLOC);
120: jc.testOnRelease = ConfigParser.getBoolean(p,
121: ConfigParser.STORE_TEST_ON_RELEASE,
122: ConfigParser.DEFAULT_STORE_TEST_ON_RELEASE);
123: jc.testOnException = ConfigParser.getBoolean(p,
124: ConfigParser.STORE_TEST_ON_EXCEPTION,
125: ConfigParser.DEFAULT_STORE_TEST_ON_EXCEPTION);
126: jc.testWhenIdle = ConfigParser.getBoolean(p,
127: ConfigParser.STORE_TEST_WHEN_IDLE, true);
128: jc.retryIntervalMs = ConfigParser.getInt(p,
129: ConfigParser.STORE_RETRY_INTERVAL_MS,
130: ConfigParser.DEFAULT_STORE_RETRY_INTERVAL_MS);
131: jc.retryCount = ConfigParser.getInt(p,
132: ConfigParser.STORE_RETRY_COUNT,
133: ConfigParser.DEFAULT_STORE_RETRY_COUNT);
134: jc.validateMappingOnStartup = ConfigParser.getBoolean(p,
135: ConfigParser.STORE_VALIDATE_MAPPING_ON_STARTUP, false);
136: jc.validateSQL = ConfigParser.trim(p
137: .getProperty(ConfigParser.STORE_VALIDATE_SQL));
138: jc.initSQL = ConfigParser.trim(p
139: .getProperty(ConfigParser.STORE_INIT_SQL));
140: jc.maxConAge = ConfigParser.getInt(p,
141: ConfigParser.STORE_MAX_CON_AGE,
142: ConfigParser.DEFAULT_MAX_CON_AGE);
143:
144: jc.blockWhenFull = ConfigParser.getBoolean(p,
145: ConfigParser.STORE_BLOCK_WHEN_FULL, true);
146:
147: jc.jdbcDisableStatementBatching = ConfigParser.getBoolean(p,
148: ConfigParser.STORE_DISABLE_BATCHING, false);
149: jc.jdbcDisablePsCache = ConfigParser.getBoolean(p,
150: ConfigParser.STORE_DISABLE_PS_CACHE, false);
151: jc.psCacheMax = ConfigParser.getInt(p,
152: ConfigParser.STORE_PS_CACHE_MAX, 0);
153:
154: String s = p.getProperty(ConfigParser.STORE_ISOLATION_LEVEL);
155: if (s == null) {
156: jc.isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
157: } else {
158: if (s.equals(ConfigParser.ISOLATION_LEVEL_READ_COMMITTED)) {
159: jc.isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
160: } else if (s
161: .equals(ConfigParser.ISOLATION_LEVEL_READ_UNCOMMITTED)) {
162: jc.isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
163: } else if (s
164: .equals(ConfigParser.ISOLATION_LEVEL_REPEATABLE_READ)) {
165: jc.isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
166: } else if (s
167: .equals(ConfigParser.ISOLATION_LEVEL_SERIALIZABLE)) {
168: jc.isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
169: } else {
170: throw BindingSupportImpl
171: .getInstance()
172: .runtime(
173: "Invalid '"
174: + ConfigParser.STORE_ISOLATION_LEVEL
175: + "', expected "
176: + ConfigParser.ISOLATION_LEVEL_READ_COMMITTED
177: + ", "
178: + ConfigParser.ISOLATION_LEVEL_REPEATABLE_READ
179: + ", "
180: + ConfigParser.ISOLATION_LEVEL_SERIALIZABLE
181: + " or "
182: + ConfigParser.ISOLATION_LEVEL_READ_UNCOMMITTED);
183: }
184: }
185:
186: jc.jdbcNameGenerator = ConfigParser.getClassAndProps(p,
187: ConfigParser.STORE_NAMEGEN,
188: jc.jdbcNameGeneratorProps = new HashMap());
189: ConfigParser.getClassAndProps(p,
190: ConfigParser.STORE_MIGRATION_CONTROLS,
191: jc.jdbcMigrationControlProps = new HashMap());
192:
193: String be = ConfigParser.STORE_EXT;
194: jc.jdbcOptimisticLocking = ConfigParser.getExtEnum(p, be,
195: JdoExtensionKeys.JDBC_OPTIMISTIC_LOCKING,
196: jdbcMDE.OPTIMISTIC_LOCKING_ENUM,
197: JdbcClass.OPTIMISTIC_LOCKING_VERSION);
198: jc.readOnly = ConfigParser.getExtBoolean(p, be,
199: JdoExtensionKeys.READ_ONLY, false);
200: jc.cacheStrategy = ConfigParser.getExtEnum(p, be,
201: JdoExtensionKeys.CACHE_STRATEGY, MDE.CACHE_ENUM,
202: MDStatics.CACHE_STRATEGY_YES);
203: jc.inheritance = ConfigParser.getExtEnum(p, be,
204: JdoExtensionKeys.JDBC_INHERITANCE,
205: jdbcMDE.INHERITANCE_ENUM, JdbcClass.INHERITANCE_FLAT);
206: s = p
207: .getProperty(be
208: + ConfigParser.JDBC_INHERITANCE_NO_CLASSID);
209: if ("true".equals(s)) {
210: jc.defaultClassId = JdbcConfig.DEFAULT_CLASS_ID_NO;
211: } else {
212: jc.defaultClassId = ConfigParser.getExtEnum(p, be,
213: JdoExtensionKeys.JDBC_CLASS_ID, JDBC_CLASS_ID_ENUM,
214: JdbcConfig.DEFAULT_CLASS_ID_HASH);
215: }
216: jc.jdbcDoNotCreateTable = ConfigParser.getExtBoolean(p, be,
217: JdoExtensionKeys.JDBC_DO_NOT_CREATE_TABLE, false);
218: jc.oidsInDefaultFetchGroup = ConfigParser.getExtBoolean(p, be,
219: JdoExtensionKeys.OIDS_IN_DEFAULT_FETCH_GROUP, true);
220:
221: jc.managedOneToMany = ConfigParser.getBoolean(p,
222: ConfigParser.STORE_MANAGED_ONE_TO_MANY,
223: ConfigParser.DEFAULT_STORE_MANAGED_ONE_TO_MANY);
224: jc.managedManyToMany = ConfigParser.getBoolean(p,
225: ConfigParser.STORE_MANAGED_MANY_TO_MANY,
226: ConfigParser.DEFAULT_STORE_MANAGED_MANY_TO_MANY);
227:
228: jc.jdbcKeyGenerator = ConfigParser
229: .getClassAndProps(
230: p,
231: be
232: + JdoExtension
233: .toKeyString(JdoExtensionKeys.JDBC_KEY_GENERATOR),
234: jc.jdbcKeyGeneratorProps = new HashMap());
235:
236: jc.typeMappings = readTypeMappings(p);
237: jc.javaTypeMappings = readJavaTypeMappings(p);
238:
239: return jc;
240: }
241:
242: private ArrayList readTypeMappings(Properties p) {
243: int n = ConfigParser.MAX_STORE_TYPE_MAPPING_COUNT;
244: String s = null;
245: ArrayList a = new ArrayList(n);
246: for (int i = 0; i < n; i++) {
247: s = p.getProperty(ConfigParser.STORE_TYPE_MAPPING + i);
248: if (s != null) {
249: a.add(ConfigParser.getReq(p,
250: ConfigParser.STORE_TYPE_MAPPING + i));
251: }
252: }
253: return a;
254: }
255:
256: private ArrayList readJavaTypeMappings(Properties p) {
257: int n = ConfigParser.MAX_STORE_JAVATYPE_MAPPING_COUNT;
258: String s = null;
259: ArrayList a = new ArrayList(n);
260: for (int i = 0; i < n; i++) {
261: s = p.getProperty(ConfigParser.STORE_JAVATYPE_MAPPING + i);
262: if (s != null) {
263: a.add(ConfigParser.getReq(p,
264: ConfigParser.STORE_JAVATYPE_MAPPING + i));
265: }
266: }
267: return a;
268: }
269:
270: }
|