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(
038 final Boolean handlerDebug,
039 final RedundantHandler handler,
040 final Element serverElement) throws Exception {
041 super(handlerDebug, handler, serverElement);
042 traceBegin();
043
044 jdbcDriver = getServerSubElementContent(serverElement, "jdbc_driver", true/*needed*/);
045 trace("jdbc_driver = " + jdbcDriver);
046
047 checkClass(jdbcDriver);
048
049 jdbcUrl = getServerSubElementContent(serverElement, "jdbc_url", true/*needed*/);
050 trace("jdbc_url = " + jdbcUrl);
051
052 traceEnd();
053 }
054
055 /**
056 * Connect to a database using specified username and password.
057 *
058 * @param bindUsername the username to use for the connection
059 * @param bindPassword the associated password
060 *
061 * @return a Connection object on success, null on error (in this case,
062 * the error code can be retrieved with the connectError() method).
063 */
064 protected final Connection connect(final String bindUsername,
065 final String bindPassword) {
066 traceBegin();
067 Connection connection;
068 try {
069 Class.forName(jdbcDriver);
070 trace("Connecting to the database (`" + jdbcUrl + "')...");
071 connection = DriverManager.getConnection(jdbcUrl, bindUsername, bindPassword);
072 } catch (java.lang.ClassNotFoundException e) {
073 trace("Connection failed: " + e.toString());
074 Log.warn("Database provider (" + jdbcDriver + ") is probably not installed");
075 setConnectError(CONNECT_NOAUTH);
076 traceEnd(null);
077 return null;
078 } catch (java.sql.SQLException e) {
079 if (e.getMessage().equals("Server configuration denies access to data source")) {
080 trace(e.getMessage());
081 setConnectError(CONNECT_NOAUTH);
082 traceEnd(null);
083 return null;
084 } else {
085 trace("Connection failed: " + e.getMessage());
086 setConnectError(CONNECT_FAILURE);
087 traceEnd(null);
088 return null;
089 }
090 } catch (Exception e) {
091 trace("Connection failed (" + e.getClass().getName() + "): " + e.getMessage());
092 setConnectError(CONNECT_FAILURE);
093 traceEnd(null);
094 return null;
095 }
096 setConnectError(CONNECT_SUCCESS);
097 traceEnd("ok");
098 return connection;
099 }
100
101 }
102