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 org.jboss.dna.common.util.Logger;
027    import org.jboss.dna.common.jdbc.JdbcMetadataI18n;
028    
029    import java.sql.Connection;
030    import java.sql.DatabaseMetaData;
031    import java.util.Properties;
032    
033    /**
034     * Default DatabaseMetadataProvider
035     * 
036     * @author <a href="mailto:litsenko_sergey@yahoo.com">Sergiy Litsenko</a>
037     */
038    public abstract class DefaultDatabaseMetadataProvider implements DatabaseMetadataProvider {
039        // ~ Instance fields ------------------------------------------------------------------
040        private static final long serialVersionUID = -4164910060171439260L;
041        private String name;
042        private Properties properties;
043        private String emptyStringNotation;
044        private String nullStringNotation;
045    
046        /**
047         * Logging for this instance
048         */
049        protected Logger log = Logger.getLogger(getClass());
050    
051        /**
052         * Database metadata
053         */
054        protected DatabaseMetaData databaseMetaData;
055    
056        /**
057         * Datbase connection
058         */
059        protected Connection connection;
060    
061        // ~ Constructors ---------------------------------------------------------------------
062    
063        /**
064         * Default Constructor
065         */
066        public DefaultDatabaseMetadataProvider() {
067        }
068    
069        /**
070         * Constructor
071         * 
072         * @param name the DatabaseMetadataProvider logical name
073         */
074        public DefaultDatabaseMetadataProvider( String name ) {
075            this();
076            setName(name);
077        }
078    
079        // ~ Methods --------------------------------------------------------------------------
080    
081        /**
082         * Opens new database connection based on supplied parameters
083         * 
084         * @return new database connection based on supplied parameters
085         * @throws Exception 
086         */
087        protected abstract Connection openConnection() throws Exception;
088    
089        /**
090         * Releases database resources
091         * 
092         * @param silently if true never generates Exception; otherwise mage rethrow RunTimeException
093         */
094        public void release( boolean silently ) {
095            // releases databaseMetaData
096            if (databaseMetaData != null) {
097                databaseMetaData = null;
098            }
099    
100            // releases connection
101            if (connection != null) {
102                try {
103                    // close connection
104                    connection.close();
105    
106                    log.debug (JdbcMetadataI18n.databaseConnectionHasBeenReleased.text(getName()));
107                } catch (Exception ex) {
108                    log.error(JdbcMetadataI18n.errorClosingDatabaseConnection, ex);
109    
110                    if (!silently) {
111                        throw new RuntimeException(
112                             JdbcMetadataI18n.errorClosingDatabaseConnection.text(getName()), ex);
113                    }
114                }
115            }
116        }
117    
118        /**
119         * Returns database metadata
120         * 
121         * @return database metadata
122         * @throws Exception 
123         */
124        public DatabaseMetaData getDatabaseMetaData() throws Exception {
125            // lazy load of database metadata
126            if (databaseMetaData == null) {
127                // log debug info
128                if (log.isDebugEnabled()) {
129                    log.debug(String.format("Getting Database metadata for a provider %1$s", getName()));
130                }
131    
132                // obtains metadata from connection
133                databaseMetaData = getConnection().getMetaData();
134                // log debug info
135                if (log.isDebugEnabled()) {
136                    log.debug(String.format("Database metadata received for a provider %1$s", getName()));
137                }
138            }
139    
140            // return
141            return databaseMetaData;
142        }
143    
144        /**
145         * Returns database connection
146         * 
147         * @return database connection
148         * @throws Exception 
149         */
150        public Connection getConnection() throws Exception {
151            // lazy open of connection
152            if (connection == null) {
153                // opens new connectection
154                connection = openConnection();
155    
156                // log debug info
157                log.info(JdbcMetadataI18n.databaseConnectionHasBeenEstablished, getName());
158            }
159    
160            // return
161            return connection;
162        }
163    
164        /**
165         * Returns DatabaseMetadataProvider logical name
166         * 
167         * @return the DatabaseMetadataProvider logical name
168         */
169        public String getName() {
170            // return
171            return name;
172        }
173    
174        /**
175         * Sets the DatabaseMetadataProvider logical name
176         * 
177         * @param name the DatabaseMetadataProvider logical name
178         */
179        public void setName( String name ) {
180            this.name = name;
181        }
182    
183        /**
184         * Get provider's notation for empty string
185         * 
186         * @return provider's notation for empty string
187         */
188        public String getEmptyStringNotation() {
189            return emptyStringNotation;
190        }
191    
192        /**
193         * Set provider's notation for empty string
194         * 
195         * @param emptyStringNotation the provider's notation for empty string
196         */
197        public void setEmptyStringNotation( String emptyStringNotation ) {
198            this.emptyStringNotation = emptyStringNotation;
199        }
200    
201        /**
202         * Get provider's notation for NULL string
203         * 
204         * @return provider's notation for NULL string
205         */
206        public String getNullStringNotation() {
207            return nullStringNotation;
208        }
209    
210        /**
211         * Set provider's notation for NULL string
212         * 
213         * @param nullStringNotation the provider's notation for NULL string
214         */
215        public void setNullStringNotation( String nullStringNotation ) {
216            this.nullStringNotation = nullStringNotation;
217        }
218    
219        /**
220         * Returns provider properties
221         * 
222         * @return provider properties
223         */
224        public Properties getProperties() {
225            return properties;
226        }
227    
228        /**
229         * Sets the provider properties
230         * 
231         * @param properties the provider properties
232         */
233        public void setProperties( Properties properties ) {
234            this.properties = properties;
235        }
236    }