001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 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: */
021: package net.sf.hajdbc.cache;
022:
023: import java.sql.Connection;
024: import java.sql.DatabaseMetaData;
025: import java.sql.SQLException;
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030:
031: import net.sf.hajdbc.DatabaseProperties;
032: import net.sf.hajdbc.Dialect;
033: import net.sf.hajdbc.QualifiedName;
034: import net.sf.hajdbc.SequenceProperties;
035: import net.sf.hajdbc.TableProperties;
036:
037: /**
038: * @author Paul Ferraro
039: *
040: */
041: public class LazyDatabaseProperties implements DatabaseProperties {
042: private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
043:
044: private Map<String, TableProperties> tableMap;
045: private Map<String, SequenceProperties> sequenceMap;
046: private Boolean supportsSelectForUpdate;
047: private DatabaseMetaDataSupport support;
048: private List<String> defaultSchemaList;
049: private Dialect dialect;
050:
051: public LazyDatabaseProperties(Dialect dialect) throws SQLException {
052: this .support = new DatabaseMetaDataSupport(
053: getDatabaseMetaData(), dialect);
054: this .dialect = dialect;
055: }
056:
057: public static void setConnection(Connection connection) {
058: threadLocal.set(connection);
059: }
060:
061: public static DatabaseMetaData getDatabaseMetaData()
062: throws SQLException {
063: return threadLocal.get().getMetaData();
064: }
065:
066: /**
067: * @see net.sf.hajdbc.DatabaseProperties#getTables()
068: */
069: @Override
070: public synchronized Collection<TableProperties> getTables()
071: throws SQLException {
072: return this .getTableMap().values();
073: }
074:
075: private synchronized Map<String, TableProperties> getTableMap()
076: throws SQLException {
077: if (this .tableMap == null) {
078: this .tableMap = new HashMap<String, TableProperties>();
079:
080: for (QualifiedName table : this .support
081: .getTables(getDatabaseMetaData())) {
082: TableProperties properties = new LazyTableProperties(
083: this .support, table);
084:
085: this .tableMap.put(properties.getName(), properties);
086: }
087: }
088:
089: return this .tableMap;
090: }
091:
092: private synchronized Map<String, SequenceProperties> getSequenceMap()
093: throws SQLException {
094: if (this .sequenceMap == null) {
095: this .sequenceMap = new HashMap<String, SequenceProperties>();
096:
097: for (SequenceProperties sequence : this .support
098: .getSequences(getDatabaseMetaData())) {
099: this .sequenceMap.put(sequence.getName(), sequence);
100: }
101: }
102:
103: return this .sequenceMap;
104: }
105:
106: private synchronized List<String> getDefaultSchemaList()
107: throws SQLException {
108: if (this .defaultSchemaList == null) {
109: this .defaultSchemaList = this .dialect
110: .getDefaultSchemas(getDatabaseMetaData());
111: }
112:
113: return this .defaultSchemaList;
114: }
115:
116: /**
117: * @see net.sf.hajdbc.DatabaseProperties#findTable(java.lang.String)
118: */
119: @Override
120: public TableProperties findTable(String table) throws SQLException {
121: return this .support.find(this .getTableMap(), table, this
122: .getDefaultSchemaList());
123: }
124:
125: /**
126: * @see net.sf.hajdbc.DatabaseProperties#supportsSelectForUpdate()
127: */
128: @Override
129: public synchronized boolean supportsSelectForUpdate()
130: throws SQLException {
131: if (this .supportsSelectForUpdate == null) {
132: this .supportsSelectForUpdate = getDatabaseMetaData()
133: .supportsSelectForUpdate();
134: }
135:
136: return this .supportsSelectForUpdate;
137: }
138:
139: /**
140: * @see net.sf.hajdbc.DatabaseProperties#findSequence(java.lang.String)
141: */
142: @Override
143: public SequenceProperties findSequence(String sequence)
144: throws SQLException {
145: return this .support.find(this .getSequenceMap(), sequence, this
146: .getDefaultSchemaList());
147: }
148:
149: /**
150: * @see net.sf.hajdbc.DatabaseProperties#getSequences()
151: */
152: @Override
153: public Collection<SequenceProperties> getSequences()
154: throws SQLException {
155: return this.getSequenceMap().values();
156: }
157: }
|