001: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: // Copyright (C) 2004 The jTDS Project
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: // Lesser General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this library; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.jdbc;
019:
020: import java.io.*;
021: import java.util.Properties;
022: import java.util.Map;
023: import java.util.HashMap;
024:
025: import net.sourceforge.jtds.ssl.Ssl;
026:
027: /**
028: * Container for default property constants.
029: * <p/>
030: * This class also provides static utility methods for
031: * {@link Properties} and <code>Settings</code> objects.
032: * <p/>
033: * To add new properties to the jTDS driver, do the following:
034: * <ol>
035: * <li>Add <code>prop.<em>foo</em></code> and <code>prop.desc.<em>foo</em></code>
036: * properties to <code>Messages.properties</code>.</li>
037: * <li>Add a <code>static final</code> default field to {@link DefaultProperties}.</li>
038: * <li>Update {@link #addDefaultProperties(java.util.Properties)} to set the default.</li>
039: * <li>Update <code>Driver.createChoicesMap()</code> and
040: * <code>DriverUnitTest.test_getPropertyInfo_Choices()</code> if the property
041: * has a specific set of inputs, e.g., "true" and "false", or "1" and "2".</li>
042: * <li>Update <code>Driver.createRequiredTrueMap()</code> and
043: * <code>DriverUnitTest.test_getPropertyInfo_Required()</code> if the property
044: * is required.</li>
045: * <li>Add a new test to <code>DefaultPropertiesTestLibrary</code> for the new
046: * property.</li>
047: * </ol>
048: *
049: * @author David D. Kilzer
050: * @version $Id: DefaultProperties.java,v 1.32 2007/08/22 18:22:04 bheineman Exp $
051: */
052: public final class DefaultProperties {
053:
054: /** Default <code>appName</code> property. */
055: public static final String APP_NAME = "jTDS";
056: /** Default <code>batchSize</code> property for SQL Server. */
057: public static final String BATCH_SIZE_SQLSERVER = "0";
058: /** Default <code>batchSize</code> property for Sybase. */
059: public static final String BATCH_SIZE_SYBASE = "1000";
060: /** Default <code>bindAddress</code> property. */
061: public static final String BIND_ADDRESS = "";
062: /** Default <code>bufferMaxMemory</code> property. */
063: public static final String BUFFER_MAX_MEMORY = "1024";
064: /** Default <code>bufferMinPackets</code> property. */
065: public static final String BUFFER_MIN_PACKETS = "8";
066: /** Default <code>cacheMetaData</code> property. */
067: public static final String CACHEMETA = "false";
068: /** Default <code>charset</code> property. */
069: public static final String CHARSET = "";
070: /** Default <code>databaseName</code> property. */
071: public static final String DATABASE_NAME = "";
072: /** Default <code>instance</code> property. */
073: public static final String INSTANCE = "";
074: /** Default <code>domain</code> property. */
075: public static final String DOMAIN = "";
076: /** Default <code>lastUpdateCount</code> property. */
077: public static final String LAST_UPDATE_COUNT = "true";
078: /** Default <code>lobBufferSize</code> property. */
079: public static final String LOB_BUFFER_SIZE = "32768";
080: /** Default <code>loginTimeout</code> property. */
081: public static final String LOGIN_TIMEOUT = "0";
082: /** Default <code>macAddress</code> property. */
083: public static final String MAC_ADDRESS = "000000000000";
084: /** Default <code>maxStatements</code> property. */
085: public static final String MAX_STATEMENTS = "500";
086: /** Default <code>namedPipe</code> property. */
087: public static final String NAMED_PIPE = "false";
088: /** Default <code>namedPipePath</code> property for SQL Server. */
089: public static final String NAMED_PIPE_PATH_SQLSERVER = "/sql/query";
090: /** Default <code>namedPipePath</code> property for Sybase. */
091: public static final String NAMED_PIPE_PATH_SYBASE = "/sybase/query";
092: /** Default <code>packetSize</code> property for TDS 4.2. */
093: public static final String PACKET_SIZE_42 = String
094: .valueOf(TdsCore.MIN_PKT_SIZE);
095: /** Default <code>packetSize</code> property for TDS 5.0. */
096: public static final String PACKET_SIZE_50 = "0";
097: /** Default <code>packetSize</code> property for TDS 7.0 and TDS 8.0. */
098: public static final String PACKET_SIZE_70_80 = "0"; // server sets packet size
099: /** Default <code>password</code> property. */
100: public static final String PASSWORD = "";
101: /** Default <code>portNumber</code> property for SQL Server. */
102: public static final String PORT_NUMBER_SQLSERVER = "1433";
103: /** Default <code>portNumber</code> property for Sybase. */
104: public static final String PORT_NUMBER_SYBASE = "7100";
105: /** Default <code>language</code> property. */
106: public static final String LANGUAGE = "";
107: /** Default <code>prepareSql</code> property for SQL Server. */
108: public static final String PREPARE_SQLSERVER = String
109: .valueOf(TdsCore.PREPARE);
110: /** Default <code>prepareSql</code> property for Sybase. */
111: public static final String PREPARE_SYBASE = String
112: .valueOf(TdsCore.TEMPORARY_STORED_PROCEDURES);
113: /** Default <code>progName</code> property. */
114: public static final String PROG_NAME = "jTDS";
115: /** Default <code>tcpNoDelay</code> property. */
116: public static final String TCP_NODELAY = "true";
117: /** Default <code>tmpDir</code> property. */
118: public static final String BUFFER_DIR = new File(System
119: .getProperty("java.io.tmpdir")).toString();
120: /** Default <code>sendStringParametersAsUnicode</code> property. */
121: public static final String USE_UNICODE = "true";
122: /** Default <code>useCursors</code> property. */
123: public static final String USECURSORS = "false";
124: /** Default <code>useJCIFS</code> property. */
125: public static final String USEJCIFS = "false";
126: /** Default <code>useLOBs</code> property. */
127: public static final String USELOBS = "true";
128: /** Default <code>user</code> property. */
129: public static final String USER = "";
130: /** Default <code>wsid</code> property. */
131: public static final String WSID = "";
132: /** Default <code>XaEmulation</code> property. */
133: public static final String XAEMULATION = "true";
134: /** Default <code>logfile</code> property. */
135: public static final String LOGFILE = "";
136: /** Default <code>sockeTimeout</code> property. */
137: public static final String SOCKET_TIMEOUT = "0";
138:
139: /** Default <code>serverType</code> property for SQL Server. */
140: public static final String SERVER_TYPE_SQLSERVER = "sqlserver";
141: /** Default <code>serverType</code> property for Sybase. */
142: public static final String SERVER_TYPE_SYBASE = "sybase";
143:
144: /** Default <code>tds</code> property for TDS 4.2. */
145: public static final String TDS_VERSION_42 = "4.2";
146: /** Default <code>tds</code> property for TDS 5.0. */
147: public static final String TDS_VERSION_50 = "5.0";
148: /** Default <code>tds</code> property for TDS 7.0. */
149: public static final String TDS_VERSION_70 = "7.0";
150: /** Default <code>tds</code> property for TDS 8.0. */
151: public static final String TDS_VERSION_80 = "8.0";
152:
153: /** Default <code>ssl</code> property. */
154: public static final String SSL = Ssl.SSL_OFF;
155:
156: /** Default TDS version based on server type */
157: private static final HashMap tdsDefaults;
158: /** Default port number based on server type */
159: private static final HashMap portNumberDefaults;
160: /** Default packet size based on TDS version */
161: private static final HashMap packetSizeDefaults;
162: /** Default max batch size based on server type */
163: private static final HashMap batchSizeDefaults;
164: /** Default prepare SQL mode based on server type */
165: private static final HashMap prepareSQLDefaults;
166:
167: static {
168: tdsDefaults = new HashMap(2);
169: tdsDefaults.put(String.valueOf(Driver.SQLSERVER),
170: TDS_VERSION_80);
171: tdsDefaults.put(String.valueOf(Driver.SYBASE), TDS_VERSION_50);
172:
173: portNumberDefaults = new HashMap(2);
174: portNumberDefaults.put(String.valueOf(Driver.SQLSERVER),
175: PORT_NUMBER_SQLSERVER);
176: portNumberDefaults.put(String.valueOf(Driver.SYBASE),
177: PORT_NUMBER_SYBASE);
178:
179: packetSizeDefaults = new HashMap(4);
180: packetSizeDefaults.put(TDS_VERSION_42, PACKET_SIZE_42);
181: packetSizeDefaults.put(TDS_VERSION_50, PACKET_SIZE_50);
182: packetSizeDefaults.put(TDS_VERSION_70, PACKET_SIZE_70_80);
183: packetSizeDefaults.put(TDS_VERSION_80, PACKET_SIZE_70_80);
184:
185: batchSizeDefaults = new HashMap(2);
186: batchSizeDefaults.put(String.valueOf(Driver.SQLSERVER),
187: BATCH_SIZE_SQLSERVER);
188: batchSizeDefaults.put(String.valueOf(Driver.SYBASE),
189: BATCH_SIZE_SYBASE);
190:
191: prepareSQLDefaults = new HashMap(2);
192: prepareSQLDefaults.put(String.valueOf(Driver.SQLSERVER),
193: PREPARE_SQLSERVER);
194: prepareSQLDefaults.put(String.valueOf(Driver.SYBASE),
195: PREPARE_SYBASE);
196: }
197:
198: /**
199: * Add default properties to the <code>props</code> properties object.
200: *
201: * @param props The properties object.
202: * @return The updated <code>props</code> object, or <code>null</code>
203: * if the <code>serverType</code> property is not set.
204: */
205: public static Properties addDefaultProperties(final Properties props) {
206: final String serverType = props.getProperty(Messages
207: .get(Driver.SERVERTYPE));
208:
209: if (serverType == null) {
210: return null;
211: }
212:
213: addDefaultPropertyIfNotSet(props, Driver.TDS,
214: Driver.SERVERTYPE, tdsDefaults);
215:
216: addDefaultPropertyIfNotSet(props, Driver.PORTNUMBER,
217: Driver.SERVERTYPE, portNumberDefaults);
218:
219: addDefaultPropertyIfNotSet(props, Driver.USER, USER);
220: addDefaultPropertyIfNotSet(props, Driver.PASSWORD, PASSWORD);
221:
222: addDefaultPropertyIfNotSet(props, Driver.DATABASENAME,
223: DATABASE_NAME);
224: addDefaultPropertyIfNotSet(props, Driver.INSTANCE, INSTANCE);
225: addDefaultPropertyIfNotSet(props, Driver.DOMAIN, DOMAIN);
226: addDefaultPropertyIfNotSet(props, Driver.APPNAME, APP_NAME);
227: addDefaultPropertyIfNotSet(props, Driver.PROGNAME, PROG_NAME);
228: addDefaultPropertyIfNotSet(props, Driver.WSID, WSID);
229: addDefaultPropertyIfNotSet(props, Driver.BATCHSIZE,
230: Driver.SERVERTYPE, batchSizeDefaults);
231: addDefaultPropertyIfNotSet(props, Driver.LASTUPDATECOUNT,
232: LAST_UPDATE_COUNT);
233: addDefaultPropertyIfNotSet(props, Driver.LOBBUFFER,
234: LOB_BUFFER_SIZE);
235: addDefaultPropertyIfNotSet(props, Driver.LOGINTIMEOUT,
236: LOGIN_TIMEOUT);
237: addDefaultPropertyIfNotSet(props, Driver.SOTIMEOUT,
238: SOCKET_TIMEOUT);
239: addDefaultPropertyIfNotSet(props, Driver.MACADDRESS,
240: MAC_ADDRESS);
241: addDefaultPropertyIfNotSet(props, Driver.MAXSTATEMENTS,
242: MAX_STATEMENTS);
243: addDefaultPropertyIfNotSet(props, Driver.NAMEDPIPE, NAMED_PIPE);
244: addDefaultPropertyIfNotSet(props, Driver.PACKETSIZE,
245: Driver.TDS, packetSizeDefaults);
246: addDefaultPropertyIfNotSet(props, Driver.CACHEMETA, CACHEMETA);
247: addDefaultPropertyIfNotSet(props, Driver.CHARSET, CHARSET);
248: addDefaultPropertyIfNotSet(props, Driver.LANGUAGE, LANGUAGE);
249: addDefaultPropertyIfNotSet(props, Driver.PREPARESQL,
250: Driver.SERVERTYPE, prepareSQLDefaults);
251: addDefaultPropertyIfNotSet(props,
252: Driver.SENDSTRINGPARAMETERSASUNICODE, USE_UNICODE);
253: addDefaultPropertyIfNotSet(props, Driver.TCPNODELAY,
254: TCP_NODELAY);
255: addDefaultPropertyIfNotSet(props, Driver.XAEMULATION,
256: XAEMULATION);
257: addDefaultPropertyIfNotSet(props, Driver.LOGFILE, LOGFILE);
258: addDefaultPropertyIfNotSet(props, Driver.SSL, SSL);
259: addDefaultPropertyIfNotSet(props, Driver.USECURSORS, USECURSORS);
260: addDefaultPropertyIfNotSet(props, Driver.BUFFERMAXMEMORY,
261: BUFFER_MAX_MEMORY);
262: addDefaultPropertyIfNotSet(props, Driver.BUFFERMINPACKETS,
263: BUFFER_MIN_PACKETS);
264: addDefaultPropertyIfNotSet(props, Driver.USELOBS, USELOBS);
265: addDefaultPropertyIfNotSet(props, Driver.BINDADDRESS,
266: BIND_ADDRESS);
267: addDefaultPropertyIfNotSet(props, Driver.USEJCIFS, USEJCIFS);
268: addDefaultPropertyIfNotSet(props, Driver.BUFFERDIR, BUFFER_DIR);
269:
270: return props;
271: }
272:
273: /**
274: * Sets a default property if the property is not already set.
275: *
276: * @param props The properties object.
277: * @param key The message key to set.
278: * @param defaultValue The default value to set.
279: */
280: private static void addDefaultPropertyIfNotSet(
281: final Properties props, final String key,
282: final String defaultValue) {
283: final String messageKey = Messages.get(key);
284:
285: if (props.getProperty(messageKey) == null) {
286: props.setProperty(messageKey, defaultValue);
287: }
288: }
289:
290: /**
291: * Sets a default property if the property is not already set, using
292: * the <code>defaultKey</code> and the <code>defaults</code> map to
293: * determine the correct value.
294: *
295: * @param props The properties object.
296: * @param key The message key to set.
297: * @param defaultKey The key whose value determines which default
298: * value to set from <code>defaults</code>.
299: * @param defaults The mapping of <code>defaultKey</code> values to
300: * the correct <code>key</code> value to set.
301: */
302: private static void addDefaultPropertyIfNotSet(
303: final Properties props, final String key,
304: final String defaultKey, final Map defaults) {
305: final String defaultKeyValue = props.getProperty(Messages
306: .get(defaultKey));
307:
308: if (defaultKeyValue == null) {
309: return;
310: }
311:
312: final String messageKey = Messages.get(key);
313:
314: if (props.getProperty(messageKey) == null) {
315: final Object defaultValue = defaults.get(defaultKeyValue);
316:
317: if (defaultValue != null) {
318: props.setProperty(messageKey, String
319: .valueOf(defaultValue));
320: }
321: }
322: }
323:
324: /**
325: * Returns the default path for the named pipe for a given serverType.
326: *
327: * @param serverType {@link Driver#SQLSERVER} or {@link Driver#SYBASE} or <code>0</code> (default)
328: * @return default named pipe path
329: * @throws IllegalArgumentException if an invalid serverType is given
330: */
331: public static String getNamedPipePath(int serverType) {
332: if (serverType == 0 || serverType == Driver.SQLSERVER) {
333: return NAMED_PIPE_PATH_SQLSERVER;
334: } else if (serverType == Driver.SYBASE) {
335: return NAMED_PIPE_PATH_SYBASE;
336: }
337: throw new IllegalArgumentException("Unknown serverType: "
338: + serverType);
339: }
340:
341: /**
342: * Converts an integer server type to its string representation.
343: *
344: * @param serverType the server type as an <code>int</code>
345: * @return the server type as a string if known, or <code>null</code> if unknown
346: */
347: public static String getServerType(int serverType) {
348: if (serverType == Driver.SQLSERVER) {
349: return SERVER_TYPE_SQLSERVER;
350: } else if (serverType == Driver.SYBASE) {
351: return SERVER_TYPE_SYBASE;
352: }
353:
354: return null;
355: }
356:
357: /**
358: * Converts a string server type to its integer representation.
359: *
360: * @param serverType the server type as a string
361: * @return the server type as an integer if known or <code>null</code> if
362: * unknown
363: */
364: public static Integer getServerType(String serverType) {
365: if (DefaultProperties.SERVER_TYPE_SQLSERVER.equals(serverType)) {
366: return new Integer(Driver.SQLSERVER);
367: } else if (DefaultProperties.SERVER_TYPE_SYBASE
368: .equals(serverType)) {
369: return new Integer(Driver.SYBASE);
370: }
371:
372: return null;
373: }
374:
375: /**
376: * Same as {@link #getServerType(int)}, only it returns the default server
377: * type (<code>"sqlserver"</code>) if <code>serverType</code> is 0.
378: *
379: * @param serverType integer server type or 0 for default
380: * @return the server type as a string if known or <code>"sqlserver"</code>
381: * if unknown
382: */
383: public static String getServerTypeWithDefault(int serverType) {
384: if (serverType == 0) {
385: return DefaultProperties.SERVER_TYPE_SQLSERVER;
386: } else if (serverType == Driver.SQLSERVER
387: || serverType == Driver.SYBASE) {
388: return getServerType(serverType);
389: } else {
390: throw new IllegalArgumentException(
391: "Only 0, 1 and 2 accepted for serverType");
392: }
393: }
394:
395: /**
396: * Converts a string TDS version to its integer representation.
397: *
398: * @param tdsVersion The TDS version as a string.
399: * @return The TDS version as an integer if known, or <code>null</code> if unknown.
400: */
401: public static Integer getTdsVersion(String tdsVersion) {
402: if (DefaultProperties.TDS_VERSION_42.equals(tdsVersion)) {
403: return new Integer(Driver.TDS42);
404: } else if (DefaultProperties.TDS_VERSION_50.equals(tdsVersion)) {
405: return new Integer(Driver.TDS50);
406: } else if (DefaultProperties.TDS_VERSION_70.equals(tdsVersion)) {
407: return new Integer(Driver.TDS70);
408: } else if (DefaultProperties.TDS_VERSION_80.equals(tdsVersion)) {
409: return new Integer(Driver.TDS80);
410: }
411:
412: return null;
413: }
414: }
|