From f0ec30fd9cc7fa19f9f9bf82d7d7449a65d90359 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 15 Oct 2006 17:50:08 +0000 Subject: Created StorageCollection. More tests added. --- .../amazon/carbonado/repo/jdbc/JDBCRepository.java | 51 +++++++++++----------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/repo/jdbc') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java index 6e7bf2f..3a6ec68 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java @@ -21,6 +21,8 @@ package com.amazon.carbonado.repo.jdbc; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.SQLException; + +import java.util.ArrayList; import java.util.Map; import java.util.IdentityHashMap; @@ -54,6 +56,8 @@ import com.amazon.carbonado.capability.StorableInfoCapability; import com.amazon.carbonado.info.StorableProperty; +import com.amazon.carbonado.spi.StorageCollection; + /** * Repository implementation backed by a JDBC accessible database. * JDBCRepository is not independent of the underlying database schema, and so @@ -142,7 +146,7 @@ public class JDBCRepository private final DataSource mDataSource; private final String mCatalog; private final String mSchema; - private final Map, JDBCStorage> mStorages; + private final StorageCollection mStorages; // Track all open connections so that they can be closed when this // repository is closed. @@ -195,7 +199,22 @@ public class JDBCRepository mDataSource = dataSource; mCatalog = catalog; mSchema = schema; - mStorages = new IdentityHashMap, JDBCStorage>(); + + mStorages = new StorageCollection() { + protected Storage createStorage(Class type) + throws RepositoryException + { + // Lock on mAllTxnMgrs to prevent databases from being opened during shutdown. + synchronized (mAllTxnMgrs) { + JDBCStorableInfo info = examineStorable(type); + if (!info.isSupported()) { + throw new UnsupportedTypeException(type); + } + return new JDBCStorage(JDBCRepository.this, info); + } + } + }; + mOpenConnections = new IdentityHashMap(); mCurrentTxnMgr = new ThreadLocal(); mAllTxnMgrs = new WeakIdentityMap(); @@ -267,22 +286,7 @@ public class JDBCRepository @SuppressWarnings("unchecked") public Storage storageFor(Class type) throws RepositoryException { - // Lock on mAllTxnMgrs to prevent databases from being opened during shutdown. - synchronized (mAllTxnMgrs) { - JDBCStorage storage = (JDBCStorage) mStorages.get(type); - if (storage == null) { - // Examine and throw exception early if there is a problem. - JDBCStorableInfo info = examineStorable(type); - - if (!info.isSupported()) { - throw new UnsupportedTypeException(type); - } - - storage = new JDBCStorage(this, info); - mStorages.put(type, storage); - } - return storage; - } + return mStorages.storageFor(type); } public Transaction enterTransaction() { @@ -343,14 +347,11 @@ public class JDBCRepository public String[] getUserStorableTypeNames() { // We don't register Storable types persistently, so just return what // we know right now. - synchronized (mAllTxnMgrs) { - String[] names = new String[mStorages.size()]; - int i = 0; - for (Class type : mStorages.keySet()) { - names[i++] = type.getName(); - } - return names; + ArrayList names = new ArrayList(); + for (Storage storage : mStorages.allStorage()) { + names.add(storage.getStorableType().getName()); } + return names.toArray(new String[names.size()]); } public boolean isSupported(Class type) { -- cgit v1.2.3