001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software 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: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021:
022: package org.josso.gateway.session.service.store.db;
023:
024: import java.sql.Connection;
025: import java.sql.Driver;
026: import java.sql.SQLException;
027: import java.util.Properties;
028:
029: import org.josso.gateway.session.exceptions.SSOSessionException;
030:
031: /**
032: * This DB Session store obtains DB connections directly from the JDBC Driver.
033: *
034: * @author Jeff Gutierrez (code@gutierrez.ph)
035: */
036: public class JdbcSessionStore extends DbSessionStore {
037:
038: /**
039: * The connection username to use when trying to connect to the database.
040: */
041: private String _connectionName = null;
042:
043: /**
044: * The connection URL to use when trying to connect to the database.
045: */
046: private String _connectionPassword = null;
047:
048: /**
049: * The connection URL to use when trying to connect to the database.
050: */
051: private String _connectionURL = null;
052:
053: /**
054: * Instance of the JDBC Driver class we use as a connection factory.
055: */
056: private Driver _driver = null;
057:
058: /**
059: * The JDBC driver to use.
060: */
061: private String _driverName = null;
062:
063: // --------------------------------
064: // JdbcSessionStore specific
065: // --------------------------------
066:
067: public String getConnectionName() {
068: return _connectionName;
069: }
070:
071: public void setConnectionName(String connectionName) {
072: _connectionName = connectionName;
073: }
074:
075: public String getConnectionPassword() {
076: return _connectionPassword;
077: }
078:
079: public void setConnectionPassword(String connectionPassword) {
080: _connectionPassword = connectionPassword;
081: }
082:
083: public String getConnectionURL() {
084: return _connectionURL;
085:
086: }
087:
088: public void setConnectionURL(String connectionURL) {
089: _connectionURL = connectionURL;
090: }
091:
092: public String getDriverName() {
093: return _driverName;
094: }
095:
096: public void setDriverName(String driverName) {
097: _driverName = driverName;
098: _driver = null; // Clear old driver reference, if any
099: }
100:
101: // -----------------------------
102: // DbSessionStore-implementation
103: // -----------------------------
104:
105: public Connection getConnection() throws SQLException,
106: SSOSessionException {
107: Connection retval = null;
108:
109: try {
110: // Instantiate our database driver if necessary
111: if (_driver == null) {
112: Class clazz = Class.forName(_driverName);
113: _driver = (Driver) clazz.newInstance();
114: }
115:
116: // Open a new connection
117: final Properties props = new Properties();
118: if (_connectionName != null)
119: props.put("user", _connectionName);
120:
121: if (_connectionPassword != null)
122: props.put("password", _connectionPassword);
123:
124: retval = _driver.connect(_connectionURL, props);
125: retval.setAutoCommit(false);
126: } catch (InstantiationException e) {
127: throw new SSOSessionException(e);
128: } catch (IllegalAccessException e) {
129: throw new SSOSessionException(e);
130: } catch (ClassNotFoundException e) {
131: throw new SSOSessionException(e);
132: }
133:
134: return retval;
135: }
136: }
|