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: package org.josso.gateway.session.service.store.db;
022:
023: import java.sql.Connection;
024: import java.sql.SQLException;
025:
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028: import javax.sql.DataSource;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.josso.gateway.session.exceptions.SSOSessionException;
033:
034: /**
035: * A DB session store that uses a DataSource to obtain connections to the DB server.
036: * The only configuration property specific to this implementation is the datasource JNDI name.
037: * See DbSessionStore for more configuration details.
038: *
039: * @see DbSessionStore
040: *
041: * @author Jeff Gutierrez (code@gutierrez.ph)
042: *
043: */
044: public class DataSourceSessionStore extends DbSessionStore {
045: private static final Log logger = LogFactory
046: .getLog(DataSourceSessionStore.class);
047:
048: private String _dsJndiName;
049: private DataSource _datasource;
050:
051: /**
052: * @return A db Connection.
053: */
054: protected Connection getConnection() throws SQLException,
055: SSOSessionException {
056: final Connection conn = getDataSource().getConnection();
057: conn.setAutoCommit(false);
058:
059: return conn;
060: }
061:
062: // ------------------------------------------------------------------
063: // Configuration properties.
064: // ------------------------------------------------------------------
065:
066: /**
067: * Sets the JNDI name of the DS associated to this Store.
068: * @param dsJndiName the JNDI name of the datasource used by the store.
069: */
070: public void setDsJndiName(String dsJndiName) {
071: _dsJndiName = dsJndiName;
072: _datasource = null; // Clear the previous reference to the DS.
073: }
074:
075: public String getDsJndiName() {
076: return _dsJndiName;
077: }
078:
079: // --------------------------------------------------------------------------
080: // Proteced utils
081: // --------------------------------------------------------------------------
082:
083: /**
084: * Lazy load the datasource instace used by this store.
085: *
086: *
087: * @throws SSOSessionException
088: */
089: protected synchronized DataSource getDataSource()
090: throws SSOSessionException {
091: if (_datasource == null) {
092: try {
093: if (logger.isDebugEnabled())
094: logger.debug("[getDatasource() : ]" + _dsJndiName);
095:
096: InitialContext ic = new InitialContext();
097: _datasource = (DataSource) ic.lookup(_dsJndiName);
098:
099: } catch (NamingException ne) {
100: logger.error("Error during DB connection lookup", ne);
101: throw new SSOSessionException("Error During Lookup\n"
102: + ne.getMessage());
103: }
104:
105: }
106:
107: return _datasource;
108: }
109:
110: }
|