001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.contrib.table.model.sql;
016    
017    import java.sql.Connection;
018    import java.sql.DriverManager;
019    import java.sql.SQLException;
020    
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    
024    /**
025     * @author mindbridge
026     */
027    public class SimpleSqlConnectionSource implements ISqlConnectionSource
028    {
029    
030        private static final Log LOG = LogFactory
031                .getLog(SimpleSqlConnectionSource.class);
032    
033        private String m_strUrl;
034    
035        private String m_strUser;
036    
037        private String m_strPwd;
038    
039        public SimpleSqlConnectionSource(String strUrl)
040        {
041            this(strUrl, null, null);
042        }
043    
044        public SimpleSqlConnectionSource(String strUrl, String strUser,
045                String strPwd)
046        {
047            m_strUrl = strUrl;
048            m_strUser = strUser;
049            m_strPwd = strPwd;
050        }
051    
052        /**
053         * @see org.apache.tapestry.contrib.table.model.sql.ISqlConnectionSource#obtainConnection()
054         */
055        public Connection obtainConnection()
056            throws SQLException
057        {
058            if (m_strUser == null) return DriverManager.getConnection(m_strUrl);
059    
060            return DriverManager.getConnection(m_strUrl, m_strUser, m_strPwd);
061        }
062    
063        /**
064         * @see org.apache.tapestry.contrib.table.model.sql.ISqlConnectionSource#returnConnection(Connection)
065         */
066        public void returnConnection(Connection objConnection)
067        {
068            try
069            {
070                objConnection.close();
071            }
072            catch (SQLException e)
073            {
074                LOG.warn("Could not close connection", e);
075            }
076        }
077    
078    }