001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.arcsde.pool;
018:
019: import java.net.URI;
020: import java.net.URISyntaxException;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.logging.Logger;
024:
025: /**
026: * Represents a set of ArcSDE database connection parameters. Instances of this
027: * class are used to validate ArcSDE connection params as in
028: * <code>DataSourceFactory.canProcess(java.util.Map)</code> and serves as keys
029: * for maintaining single <code>SdeConnectionPool</code>'s by each set of
030: * connection properties
031: *
032: * @author Gabriel Roldan
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/main/java/org/geotools/arcsde/pool/ArcSDEConnectionConfig.java $
034: * @version $Id: ArcSDEConnectionConfig.java 27863 2007-11-12 20:34:34Z desruisseaux $
035: */
036: public class ArcSDEConnectionConfig {
037: /**
038: * Shared package's logger
039: */
040: private static final Logger LOGGER = org.geotools.util.logging.Logging
041: .getLogger(ArcSDEConnectionConfig.class.getPackage()
042: .getName());
043:
044: /**
045: * message of the exception thrown if a mandatory parameter is not supplied
046: */
047: private static final String NULL_ARGUMENTS_MSG = "Illegal arguments. At least one of them was null. Check to pass "
048: + "correct values to dbtype, server, port, database, user and password parameters";
049:
050: /** DOCUMENT ME! */
051: private static final String ILLEGAL_ARGUMENT_MSG = " is not valid for parameter ";
052:
053: /** must equals to <code>"arcsde"</code> */
054: public static final String DBTYPE_PARAM = "dbtype";
055:
056: /** constant to pass "arcsde" as DBTYPE_PARAM */
057: public static final String DBTYPE_PARAM_VALUE = "arcsde";
058:
059: /** namespace URI assigned to datastore instance */
060: public static final String NAMESPACE_PARAM = "namespace";
061:
062: /** ArcSDE server parameter name */
063: public static final String SERVER_NAME_PARAM = "server";
064:
065: /** ArcSDE server port parameter name */
066: public static final String PORT_NUMBER_PARAM = "port";
067:
068: /** ArcSDE databse name parameter name */
069: public static final String INSTANCE_NAME_PARAM = "instance";
070:
071: /** ArcSDE database user name parameter name */
072: public static final String USER_NAME_PARAM = "user";
073:
074: /** ArcSDE database user password parameter name */
075: public static final String PASSWORD_PARAM = "password";
076:
077: /** DOCUMENT ME! */
078: public static final String MIN_CONNECTIONS_PARAM = "pool.minConnections";
079:
080: /** DOCUMENT ME! */
081: public static final String MAX_CONNECTIONS_PARAM = "pool.maxConnections";
082:
083: /** DOCUMENT ME! */
084: public static final String CONNECTION_TIMEOUT_PARAM = "pool.timeOut";
085:
086: /**
087: * parameter name who's value represents the feature class for wich an
088: * <code>SdeDataSource</code> will be created
089: *
090: * @task TODO: should this constant be moved to the SdeDataSource class?
091: * since SdeConnectionConfig thoes not validates the table param
092: */
093: protected static final String TABLE_NAME_PARAM = "table";
094:
095: /** namespace URI assigned to datastore */
096: String namespaceUri;
097:
098: /** name or IP of the ArcSDE server to connect to */
099: String serverName;
100:
101: /** port number where the ArcSDE instance listens for connections */
102: Integer portNumber;
103:
104: /** name of the ArcSDE database to connect to */
105: String databaseName;
106:
107: /** database user name to connect as */
108: String userName;
109:
110: /** database user password */
111: String userPassword;
112:
113: /** DOCUMENT ME! */
114: Integer minConnections = null;
115:
116: /** DOCUMENT ME! */
117: Integer maxConnections = null;
118:
119: /** DOCUMENT ME! */
120: Integer connTimeOut = null;
121:
122: /**
123: * DOCUMENT ME!
124: *
125: * @param params
126: *
127: * @throws NullPointerException
128: * if at least one mandatory parameter is
129: * @throws IllegalArgumentException
130: * if at least one mandatory parameter is present but has no a
131: * "valid" value.
132: */
133: public ArcSDEConnectionConfig(Map params)
134: throws NullPointerException, IllegalArgumentException {
135: init(params);
136: }
137:
138: /**
139: * DOCUMENT ME!
140: *
141: * @param dbType
142: * @param serverName
143: * DOCUMENT ME!
144: * @param portNumber
145: * DOCUMENT ME!
146: * @param databaseName
147: * DOCUMENT ME!
148: * @param userName
149: * DOCUMENT ME!
150: * @param userPassword
151: * DOCUMENT ME!
152: *
153: * @throws NullPointerException
154: * @throws IllegalArgumentException
155: */
156: public ArcSDEConnectionConfig(String dbType, String serverName,
157: String portNumber, String databaseName, String userName,
158: String userPassword) throws NullPointerException,
159: IllegalArgumentException {
160: Map params = new HashMap();
161: params.put(DBTYPE_PARAM, dbType);
162: params.put(SERVER_NAME_PARAM, serverName);
163: params.put(PORT_NUMBER_PARAM, portNumber);
164: params.put(INSTANCE_NAME_PARAM, databaseName);
165: params.put(USER_NAME_PARAM, userName);
166: params.put(PASSWORD_PARAM, userPassword);
167: init(params);
168: }
169:
170: /**
171: * DOCUMENT ME!
172: *
173: * @param params
174: * DOCUMENT ME!
175: *
176: * @throws NumberFormatException
177: * DOCUMENT ME!
178: * @throws IllegalArgumentException
179: * DOCUMENT ME!
180: */
181: private void init(Map params) throws NumberFormatException,
182: IllegalArgumentException {
183: String dbtype = (String) params.get(DBTYPE_PARAM);
184: String server = (String) params.get(SERVER_NAME_PARAM);
185: String port = String.valueOf(params.get(PORT_NUMBER_PARAM));
186: String instance = (String) params.get(INSTANCE_NAME_PARAM);
187: String user = (String) params.get(USER_NAME_PARAM);
188: String pwd = (String) params.get(PASSWORD_PARAM);
189: Integer _port = checkParams(dbtype, server, port, instance,
190: user, pwd);
191: this .serverName = server;
192: this .portNumber = _port;
193: this .databaseName = instance;
194: this .userName = user;
195: this .userPassword = pwd;
196: setUpOptionalParams(params);
197: }
198:
199: /**
200: * DOCUMENT ME!
201: *
202: * @param params
203: * DOCUMENT ME!
204: *
205: * @throws IllegalArgumentException
206: * DOCUMENT ME!
207: */
208: private void setUpOptionalParams(Map params)
209: throws IllegalArgumentException {
210: String exceptionMsg = null;
211: Object ns = params.get(NAMESPACE_PARAM);
212:
213: this .namespaceUri = ns == null ? null : String.valueOf(ns);
214:
215: this .minConnections = getInt(params.get(MIN_CONNECTIONS_PARAM),
216: ArcSDEConnectionPool.DEFAULT_CONNECTIONS);
217: this .maxConnections = getInt(params.get(MAX_CONNECTIONS_PARAM),
218: ArcSDEConnectionPool.DEFAULT_MAX_CONNECTIONS);
219: this .connTimeOut = getInt(params.get(CONNECTION_TIMEOUT_PARAM),
220: ArcSDEConnectionPool.DEFAULT_MAX_WAIT_TIME);
221:
222: if (this .minConnections.intValue() <= 0) {
223: exceptionMsg = MIN_CONNECTIONS_PARAM
224: + " must be a positive integer";
225: }
226:
227: if (this .maxConnections.intValue() <= 0) {
228: exceptionMsg = MAX_CONNECTIONS_PARAM
229: + " must be a positive integer";
230: }
231:
232: if (this .connTimeOut.intValue() <= 0) {
233: exceptionMsg = CONNECTION_TIMEOUT_PARAM
234: + " must be a positive integer";
235: }
236:
237: if (this .minConnections.intValue() > this .maxConnections
238: .intValue()) {
239: exceptionMsg = MIN_CONNECTIONS_PARAM
240: + " must be lower than " + MAX_CONNECTIONS_PARAM;
241: }
242:
243: if (exceptionMsg != null) {
244: throw new IllegalArgumentException(exceptionMsg);
245: }
246: }
247:
248: /**
249: * DOCUMENT ME!
250: *
251: * @param value
252: * DOCUMENT ME!
253: * @param defaultValue
254: * DOCUMENT ME!
255: *
256: * @return DOCUMENT ME!
257: */
258: private static final Integer getInt(Object value, int defaultValue) {
259: if (value == null) {
260: return new Integer(defaultValue);
261: }
262:
263: String sVal = String.valueOf(value);
264:
265: try {
266: return Integer.valueOf(sVal);
267: } catch (NumberFormatException ex) {
268: return new Integer(defaultValue);
269: }
270: }
271:
272: /**
273: * DOCUMENT ME!
274: *
275: * @param dbType
276: * DOCUMENT ME!
277: * @param serverName
278: * DOCUMENT ME!
279: * @param portNumber
280: * DOCUMENT ME!
281: * @param databaseName
282: * DOCUMENT ME!
283: * @param userName
284: * DOCUMENT ME!
285: * @param userPassword
286: * DOCUMENT ME!
287: *
288: * @return DOCUMENT ME!
289: *
290: * @throws IllegalArgumentException
291: * DOCUMENT ME!
292: * @throws NullPointerException
293: * DOCUMENT ME!
294: */
295: private Integer checkParams(String dbType, String serverName,
296: String portNumber, String databaseName, String userName,
297: String userPassword) throws IllegalArgumentException,
298: NullPointerException {
299: // check if dbtype is 'arcsde'
300: if (!(DBTYPE_PARAM_VALUE.equals(dbType))) {
301: throw new IllegalArgumentException(
302: "parameter dbtype must be " + DBTYPE_PARAM_VALUE);
303: }
304:
305: // check for nullity
306: if ((serverName == null) || (portNumber == null)
307: || (userName == null) || (userPassword == null)) {
308: throw new NullPointerException(NULL_ARGUMENTS_MSG);
309: }
310:
311: if (serverName.length() == 0) {
312: throwIllegal(SERVER_NAME_PARAM, serverName);
313: }
314:
315: if (databaseName == null || databaseName.length() == 0) {
316: LOGGER.warning("No database name specified");
317: databaseName = "";
318: // throwIllegal(INSTANCE_NAME_PARAM, databaseName);
319: }
320:
321: if (userName.length() == 0) {
322: throwIllegal(USER_NAME_PARAM, userName);
323: }
324:
325: if (userPassword.length() == 0) {
326: throwIllegal(PASSWORD_PARAM, userPassword);
327: }
328:
329: Integer port = null;
330:
331: try {
332: port = Integer.valueOf(portNumber);
333: } catch (NumberFormatException ex) {
334: throwIllegal(PORT_NUMBER_PARAM, portNumber);
335: }
336:
337: return port;
338: }
339:
340: /**
341: * DOCUMENT ME!
342: *
343: * @param paramName
344: * DOCUMENT ME!
345: * @param paramValue
346: * DOCUMENT ME!
347: *
348: * @throws IllegalArgumentException
349: * DOCUMENT ME!
350: */
351: private void throwIllegal(String paramName, String paramValue)
352: throws IllegalArgumentException {
353: throw new IllegalArgumentException("'" + paramValue + "'"
354: + ILLEGAL_ARGUMENT_MSG + paramName);
355: }
356:
357: public String getNamespaceUri() {
358: return namespaceUri;
359: }
360:
361: /**
362: * DOCUMENT ME!
363: *
364: * @return DOCUMENT ME!
365: */
366: public String getDatabaseName() {
367: return databaseName;
368: }
369:
370: /**
371: * DOCUMENT ME!
372: *
373: * @return DOCUMENT ME!
374: */
375: public Integer getPortNumber() {
376: return portNumber;
377: }
378:
379: /**
380: * DOCUMENT ME!
381: *
382: * @return DOCUMENT ME!
383: */
384: public String getServerName() {
385: return serverName;
386: }
387:
388: /**
389: * DOCUMENT ME!
390: *
391: * @return DOCUMENT ME!
392: */
393: public String getUserName() {
394: return userName;
395: }
396:
397: /**
398: * accessor method for retrieving the user password of the ArcSDE connection
399: * properties holded here
400: *
401: * @return the ArcSDE user password
402: */
403: public String getUserPassword() {
404: return userPassword;
405: }
406:
407: /**
408: * DOCUMENT ME!
409: *
410: * @return DOCUMENT ME!
411: */
412: public int hashCode() {
413: int hash = 37;
414: hash *= getServerName().hashCode();
415: hash *= getPortNumber().hashCode();
416: hash *= getUserName().hashCode();
417: return hash;
418: }
419:
420: /**
421: * checks for equality over another <code>ArcSDEConnectionConfig</code>, taking
422: * in count the values of database name, user name, and port number.
423: *
424: * @param o
425: * DOCUMENT ME!
426: *
427: * @return DOCUMENT ME!
428: */
429: public boolean equals(Object o) {
430: if (o == this ) {
431: return true;
432: }
433:
434: if (!(o instanceof ArcSDEConnectionConfig)) {
435: return false;
436: }
437:
438: ArcSDEConnectionConfig config = (ArcSDEConnectionConfig) o;
439:
440: return config.getServerName().equals(getServerName())
441: && config.getPortNumber().equals(getPortNumber())
442: && config.getUserName().equals(getUserName());
443: }
444:
445: /**
446: * DOCUMENT ME!
447: *
448: * @return DOCUMENT ME!
449: */
450: public Integer getConnTimeOut() {
451: return connTimeOut;
452: }
453:
454: /**
455: * DOCUMENT ME!
456: *
457: * @return DOCUMENT ME!
458: */
459: public Integer getMaxConnections() {
460: return maxConnections;
461: }
462:
463: /**
464: * DOCUMENT ME!
465: *
466: * @return DOCUMENT ME!
467: */
468: public Integer getMinConnections() {
469: return minConnections;
470: }
471:
472: /**
473: * DOCUMENT ME!
474: *
475: * @return DOCUMENT ME!
476: */
477: public String toString() {
478: StringBuffer sb = new StringBuffer(getClass().getName() + "[");
479: sb.append("dbtype=");
480: sb.append(ArcSDEConnectionConfig.DBTYPE_PARAM_VALUE);
481: sb.append(", server=");
482: sb.append(this .serverName);
483: sb.append(", port=");
484: sb.append(this .portNumber);
485: sb.append(", instance=");
486: sb.append(this .databaseName);
487: sb.append(", user=");
488: sb.append(this .userName);
489: //hidding password as the result of this method
490: //is probably going to end up in a stack trace
491: sb.append(", password=*****");
492: sb.append(", minConnections=");
493: sb.append(this .minConnections);
494: sb.append(", maxConnections=");
495: sb.append(this .maxConnections);
496: sb.append(", connTimeOut=");
497: sb.append(this .connTimeOut);
498: sb.append("]");
499:
500: return sb.toString();
501: }
502: }
|