001: package org.esupportail.cas.server.handlers.database;
002:
003: import java.sql.Connection;
004: import java.sql.DriverManager;
005:
006: import org.dom4j.Element;
007: import org.esupportail.cas.server.util.RedundantHandler;
008: import org.esupportail.cas.server.util.Server;
009: import org.esupportail.cas.server.util.log.Log;
010:
011: /**
012: * This abstract class implements a database server class, inherited by
013: * BindDatabaseServer and SearchDatabaseServer.
014: *
015: * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
016: */
017: abstract class DatabaseServer extends Server {
018:
019: /**
020: * The name of the class used to access the database through JDBC.
021: */
022: private String jdbcDriver;
023:
024: /**
025: * The JDBC URL of the database.
026: */
027: private String jdbcUrl;
028:
029: /**
030: * Constructor.
031: *
032: * @param handlerDebug debugging mode of the handler
033: * @param handler the handler the server will be used by
034: * @param serverElement the XML element that declares the server
035: * @throws Exception Exception
036: */
037: protected DatabaseServer(final Boolean handlerDebug,
038: final RedundantHandler handler, final Element serverElement)
039: throws Exception {
040: super (handlerDebug, handler, serverElement);
041: traceBegin();
042:
043: jdbcDriver = getServerSubElementContent(serverElement,
044: "jdbc_driver", true/*needed*/);
045: trace("jdbc_driver = " + jdbcDriver);
046:
047: checkClass(jdbcDriver);
048:
049: jdbcUrl = getServerSubElementContent(serverElement, "jdbc_url",
050: true/*needed*/);
051: trace("jdbc_url = " + jdbcUrl);
052:
053: traceEnd();
054: }
055:
056: /**
057: * Connect to a database using specified username and password.
058: *
059: * @param bindUsername the username to use for the connection
060: * @param bindPassword the associated password
061: *
062: * @return a Connection object on success, null on error (in this case,
063: * the error code can be retrieved with the connectError() method).
064: */
065: protected final Connection connect(final String bindUsername,
066: final String bindPassword) {
067: traceBegin();
068: Connection connection;
069: try {
070: Class.forName(jdbcDriver);
071: trace("Connecting to the database (`" + jdbcUrl + "')...");
072: connection = DriverManager.getConnection(jdbcUrl,
073: bindUsername, bindPassword);
074: } catch (java.lang.ClassNotFoundException e) {
075: trace("Connection failed: " + e.toString());
076: Log.warn("Database provider (" + jdbcDriver
077: + ") is probably not installed");
078: setConnectError(CONNECT_NOAUTH);
079: traceEnd(null);
080: return null;
081: } catch (java.sql.SQLException e) {
082: if (e
083: .getMessage()
084: .equals(
085: "Server configuration denies access to data source")) {
086: trace(e.getMessage());
087: setConnectError(CONNECT_NOAUTH);
088: traceEnd(null);
089: return null;
090: } else {
091: trace("Connection failed: " + e.getMessage());
092: setConnectError(CONNECT_FAILURE);
093: traceEnd(null);
094: return null;
095: }
096: } catch (Exception e) {
097: trace("Connection failed (" + e.getClass().getName()
098: + "): " + e.getMessage());
099: setConnectError(CONNECT_FAILURE);
100: traceEnd(null);
101: return null;
102: }
103: setConnectError(CONNECT_SUCCESS);
104: traceEnd("ok");
105: return connection;
106: }
107:
108: }
|