001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (c) 2004-2008 Paul Ferraro 004 * 005 * This library is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Lesser General Public License as published by the 007 * Free Software Foundation; either version 2.1 of the License, or (at your 008 * option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, but WITHOUT 011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 013 * for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this library; if not, write to the Free Software Foundation, 017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 * 019 * Contact: ferraro@users.sourceforge.net 020 */ 021package net.sf.hajdbc.cache; 022 023import java.sql.DatabaseMetaData; 024import java.sql.SQLException; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029import net.sf.hajdbc.Dialect; 030import net.sf.hajdbc.QualifiedName; 031import net.sf.hajdbc.SequenceProperties; 032import net.sf.hajdbc.TableProperties; 033import net.sf.hajdbc.util.ref.VolatileReference; 034 035/** 036 * @author Paul Ferraro 037 * 038 */ 039public abstract class AbstractLazyDatabaseProperties extends AbstractDatabaseProperties implements DatabaseMetaDataProvider 040{ 041 private final VolatileReference<Map<String, TableProperties>> tableMapRef = new VolatileReference<Map<String, TableProperties>>(); 042 private final VolatileReference<Map<String, SequenceProperties>> sequenceMapRef = new VolatileReference<Map<String, SequenceProperties>>(); 043 private final VolatileReference<List<String>> defaultSchemaListRef = new VolatileReference<List<String>>(); 044 045 protected AbstractLazyDatabaseProperties(DatabaseMetaData metaData, DatabaseMetaDataSupportFactory factory, Dialect dialect) throws SQLException 046 { 047 super(metaData, factory, dialect); 048 } 049 050 protected Map<String, TableProperties> getTableMap() throws SQLException 051 { 052 synchronized (this.tableMapRef) 053 { 054 Map<String, TableProperties> map = this.tableMapRef.get(); 055 056 if (map == null) 057 { 058 map = new HashMap<String, TableProperties>(); 059 060 for (QualifiedName table: this.support.getTables(this.getDatabaseMetaData())) 061 { 062 TableProperties properties = new LazyTableProperties(this, this.support, table); 063 064 map.put(properties.getName(), properties); 065 } 066 067 this.tableMapRef.set(map); 068 } 069 070 return map; 071 } 072 } 073 074 protected Map<String, SequenceProperties> getSequenceMap() throws SQLException 075 { 076 synchronized (this.sequenceMapRef) 077 { 078 Map<String, SequenceProperties> map = this.sequenceMapRef.get(); 079 080 if (map == null) 081 { 082 map = new HashMap<String, SequenceProperties>(); 083 084 for (SequenceProperties sequence: this.support.getSequences(this.getDatabaseMetaData())) 085 { 086 map.put(sequence.getName(), sequence); 087 } 088 089 this.sequenceMapRef.set(map); 090 } 091 092 return map; 093 } 094 } 095 096 protected List<String> getDefaultSchemaList() throws SQLException 097 { 098 synchronized (this.defaultSchemaListRef) 099 { 100 List<String> list = this.defaultSchemaListRef.get(); 101 102 if (list == null) 103 { 104 list = this.dialect.getDefaultSchemas(this.getDatabaseMetaData()); 105 106 this.defaultSchemaListRef.set(list); 107 } 108 109 return list; 110 } 111 } 112}