001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.common.jdbc.provider;
025    
026    import java.sql.Connection;
027    import java.sql.DriverManager;
028    
029    /**
030     * Default DatabaseMetadataProvider based on driver
031     * 
032     * @author <a href="mailto:litsenko_sergey@yahoo.com">Sergiy Litsenko</a>
033     */
034    public class DefaultDriverDatabaseMetadataProvider extends DefaultDatabaseMetadataProvider
035        implements DriverDatabaseMetadataProvider {
036        // ~ Instance fields ------------------------------------------------------------------
037    
038        private static final long serialVersionUID = -3616979905696406464L;
039        private String driverClassName;
040        private String databaseUrl;
041        private String userName;
042        private String password;
043    
044        // ~ Constructors ---------------------------------------------------------------------
045    
046        /**
047         * Default constructor
048         */
049        public DefaultDriverDatabaseMetadataProvider() {
050        }
051    
052        /**
053         * Constructor
054         * 
055         * @param name the DatabaseMetadataProvider logical name
056         */
057        public DefaultDriverDatabaseMetadataProvider( String name ) {
058            super(name);
059        }
060    
061        // ~ Methods --------------------------------------------------------------------------
062    
063        /**
064         * Opens new database connection based on suppied parameters
065         * 
066         * @return new database connection based on suppied parameters
067         * @throws Exception
068         */
069        @Override
070        protected Connection openConnection() throws Exception {
071            // log debug info
072            if (log.isDebugEnabled()) {
073                log.debug("Loading JDBC driver class: " + getDriverClassName());
074            }
075    
076            // trying to load database driver by name
077            Class.forName(getDriverClassName());
078    
079            // log debug info
080            if (log.isDebugEnabled()) {
081                log.debug("Opening database connection by using driver manager. The URL: " + getDatabaseUrl() + ". The user: "
082                          + getUserName());
083            }
084    
085            // opening connection by using Driver manager
086            return DriverManager.getConnection(getDatabaseUrl(), getUserName(), getPassword());
087        }
088    
089        /**
090         * Gets JDBC driver class name
091         * 
092         * @return the JDBC driver class name
093         */
094        public String getDriverClassName() {
095            // return
096            return driverClassName;
097        }
098    
099        /**
100         * Sets JDBC driver class name
101         * 
102         * @param driverClassName the JDBC driver class name
103         */
104        public void setDriverClassName( String driverClassName ) {
105            this.driverClassName = driverClassName;
106        }
107    
108        /**
109         * Gets database URL as string
110         * 
111         * @return database URL as string
112         */
113        public String getDatabaseUrl() {
114            // return
115            return databaseUrl;
116        }
117    
118        /**
119         * Sets the database URL as string
120         * 
121         * @param databaseUrl the database URL as string
122         */
123        public void setDatabaseUrl( String databaseUrl ) {
124            this.databaseUrl = databaseUrl;
125        }
126    
127        /**
128         * Gets the user name
129         * 
130         * @return the user name
131         */
132        public String getUserName() {
133            // return
134            return userName;
135        }
136    
137        /**
138         * Sets the user name
139         * 
140         * @param userName the user name
141         */
142        public void setUserName( String userName ) {
143            this.userName = userName;
144        }
145    
146        /**
147         * Get user's password
148         * 
149         * @return user's password
150         */
151        public String getPassword() {
152            // return
153            return password;
154        }
155    
156        /**
157         * Sets the user's password
158         * 
159         * @param password the user's password
160         */
161        public void setPassword( String password ) {
162            this.password = password;
163        }
164    }