Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
AnalyzerBeans Copyright (C) 2014 Neopost - Customer Information Management This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU Lesser General Public License, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this distribution; if not, write to: Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
 
 package org.eobjects.analyzer.storage;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
Abstract class for storage providers that use an SQL database as a backend to store values.
 
 public abstract class SqlDatabaseStorageProvider implements StorageProvider {
 
 	private static final Logger logger = LoggerFactory.getLogger(SqlDatabaseStorageProvider.class);
 
 	private final AtomicInteger _nextTableId = new AtomicInteger(1);
 	private final Connection _connection;
 
 	public SqlDatabaseStorageProvider(String driverClassNameString connectionUrl) {
 		this(driverClassNameconnectionUrlnullnull);
 	}
 
 	public SqlDatabaseStorageProvider(String driverClassNameString connectionUrlString usernameString password) {
 		.info("Creating new storage provider, driver={}, url={}"driverClassNameconnectionUrl);
 		try {
 			Class.forName(driverClassName);
 		} catch (ClassNotFoundException e) {
 			throw new IllegalStateException("Could not initialize the Hsqldb driver"e);
 		}
 
 		try {
 			if (username != null) {
 				 = DriverManager.getConnection(connectionUrlusernamepassword);
 			} else {
 				 = DriverManager.getConnection(connectionUrl);
 			}
 
 			// optimize
 		} catch (SQLException e) {
 			throw new IllegalStateException("Could not open connection to database: " + connectionUrle);
 		}
 	}
 
 	protected Connection getConnection() {
 		return ;
 	}
 
 	protected void finalize() {
 		try {
 		} catch (SQLException e) {
 			// nothing to do
 		}
 	}

Subclasses can override this method to control table name generation

Returns:
the name of the next table to create
 
 	protected String getNextTableName() {
 		return "ab_" + .getAndIncrement();
 	}
 
 	public <E> List<E> createList(Class<E> valueTypethrows IllegalStateException {
 		String tableName = getNextTableName();
 		String valueTypeName = SqlDatabaseUtils.getSqlType(valueType);
 		.info("Creating table {} for List"tableName);
 		return new SqlDatabaseList<E>(tableNamevalueTypeName);
	}
	public <E> Set<E> createSet(Class<E> valueTypethrows IllegalStateException {
		String tableName = getNextTableName();
		String valueTypeName = SqlDatabaseUtils.getSqlType(valueType);
		.info("Creating table {} for Set"tableName);
		return new SqlDatabaseSet<E>(tableNamevalueTypeName);
	}
	public <K, V> Map<K, V> createMap(Class<K> keyTypeClass<V> valueTypethrows IllegalStateException {
		String tableName = getNextTableName();
		String keyTypeName = SqlDatabaseUtils.getSqlType(keyType);
		String valueTypeName = SqlDatabaseUtils.getSqlType(valueType);
		.info("Creating table {} for Map"tableName);
		return new SqlDatabaseMap<K, V>(tableNamekeyTypeNamevalueTypeName);
	}
		String tableName = getNextTableName();
		.info("Creating table {} for RowAnnotationFactory"tableName);
		return factory;
	}
New to GrepCode? Check out our FAQ X