001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.cache.eager;
019
020import java.sql.Connection;
021import java.sql.DatabaseMetaData;
022import java.sql.SQLException;
023import java.util.Map;
024import java.util.TreeMap;
025
026import net.sf.hajdbc.Database;
027import net.sf.hajdbc.DatabaseCluster;
028import net.sf.hajdbc.DatabaseProperties;
029import net.sf.hajdbc.cache.DatabaseMetaDataCache;
030import net.sf.hajdbc.dialect.Dialect;
031import net.sf.hajdbc.util.Resources;
032
033/**
034 * Per-database {@link DatabaseMetaDataCache} implementation that populates itself eagerly.
035 * @author Paul Ferraro
036 */
037public class EagerDatabaseMetaDataCache<Z, D extends Database<Z>> implements DatabaseMetaDataCache<Z, D>
038{
039        private final Map<D, DatabaseProperties> map = new TreeMap<D, DatabaseProperties>();
040        private final DatabaseCluster<Z, D> cluster;
041        
042        public EagerDatabaseMetaDataCache(DatabaseCluster<Z, D> cluster)
043        {
044                this.cluster = cluster;
045        }
046        
047        /**
048         * {@inheritDoc}
049         * @see net.sf.hajdbc.cache.DatabaseMetaDataCache#flush()
050         */
051        @Override
052        public void flush() throws SQLException
053        {
054                Map<D, DatabaseProperties> map = new TreeMap<D, DatabaseProperties>();
055                
056                for (D database: this.cluster.getBalancer())
057                {
058                        Connection connection = database.connect(database.getConnectionSource(), database.decodePassword(this.cluster.getDecoder()));
059                        
060                        try
061                        {
062                                map.put(database, this.createDatabaseProperties(connection));
063                        }
064                        finally
065                        {
066                                Resources.close(connection);
067                        }
068                }
069                
070                synchronized (this.map)
071                {
072                        this.map.clear();
073                        this.map.putAll(map);
074                }
075        }
076
077        /**
078         * {@inheritDoc}
079         * @see net.sf.hajdbc.cache.DatabaseMetaDataCache#getDatabaseProperties(net.sf.hajdbc.Database, java.sql.Connection)
080         */
081        @Override
082        public DatabaseProperties getDatabaseProperties(D database, Connection connection) throws SQLException
083        {
084                synchronized (this.map)
085                {
086                        DatabaseProperties properties = this.map.get(database);
087                        
088                        if (properties == null)
089                        {
090                                properties = this.createDatabaseProperties(connection);
091                                
092                                this.map.put(database, properties);
093                        }
094                        
095                        return properties;
096                }
097        }
098        
099        private DatabaseProperties createDatabaseProperties(Connection connection) throws SQLException
100        {
101                DatabaseMetaData metaData = connection.getMetaData();
102                Dialect dialect = this.cluster.getDialect();
103                return new EagerDatabaseProperties(metaData, dialect);
104        }
105}