Skip to content
Snippets Groups Projects
Commit 2fede76e authored by ribeaudc's avatar ribeaudc
Browse files

minor: - Checks whether info is enabled before "infoying" the log.

- Add final keywords when possible.

SVN: 4651
parent 1958c7e5
No related branches found
No related tags found
No related merge requests found
...@@ -62,7 +62,8 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -62,7 +62,8 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
/** /**
* Creates an instance for the specified data source and sequence mapper. * Creates an instance for the specified data source and sequence mapper.
*/ */
public H2MassUploader(DataSource dataSource, ISequenceNameMapper sequenceNameMapper) throws SQLException public H2MassUploader(final DataSource dataSource, final ISequenceNameMapper sequenceNameMapper)
throws SQLException
{ {
this.sequenceNameMapper = sequenceNameMapper; this.sequenceNameMapper = sequenceNameMapper;
setDataSource(dataSource); setDataSource(dataSource);
...@@ -76,7 +77,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -76,7 +77,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
final BitSet isBinaryColumn; final BitSet isBinaryColumn;
MassUploadRecord(File massUploadFile, String tableName, BitSet isBinaryColumn) MassUploadRecord(final File massUploadFile, final String tableName, final BitSet isBinaryColumn)
{ {
this.massUploadFile = massUploadFile; this.massUploadFile = massUploadFile;
this.tableName = tableName; this.tableName = tableName;
...@@ -84,7 +85,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -84,7 +85,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
} }
} }
public void performMassUpload(File[] massUploadFiles) public final void performMassUpload(final File[] massUploadFiles)
{ {
String task = "Get database metadata"; String task = "Get database metadata";
try try
...@@ -93,14 +94,15 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -93,14 +94,15 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
final DatabaseMetaData dbMetaData = getConnection().getMetaData(); final DatabaseMetaData dbMetaData = getConnection().getMetaData();
try try
{ {
for (File massUploadFile : massUploadFiles) for (final File massUploadFile : massUploadFiles)
{ {
final String[] splitName = StringUtils.split(massUploadFile.getName(), "="); final String[] splitName = StringUtils.split(massUploadFile.getName(), "=");
assert splitName.length == 2 : "Missing '=' in name of file '" + massUploadFile.getName() + "'."; assert splitName.length == 2 : "Missing '=' in name of file '" + massUploadFile.getName() + "'.";
final String tableNameWithExtension = splitName[1]; final String tableNameWithExtension = splitName[1];
boolean tsvFileType = TSV.isOfType(tableNameWithExtension); final boolean tsvFileType = TSV.isOfType(tableNameWithExtension);
assert tsvFileType : "Not a " + TSV.getFileType() + " file: " + massUploadFile.getName(); assert tsvFileType : "Not a " + TSV.getFileType() + " file: " + massUploadFile.getName();
final String tableName = tableNameWithExtension.substring(0, tableNameWithExtension.lastIndexOf('.')); final String tableName =
tableNameWithExtension.substring(0, tableNameWithExtension.lastIndexOf('.'));
final BitSet isBinaryColumn = findBinaryColumns(dbMetaData, tableName); final BitSet isBinaryColumn = findBinaryColumns(dbMetaData, tableName);
massUploadRecords.add(new MassUploadRecord(massUploadFile, tableName, isBinaryColumn)); massUploadRecords.add(new MassUploadRecord(massUploadFile, tableName, isBinaryColumn));
} }
...@@ -109,21 +111,21 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -109,21 +111,21 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
task = "Close connection"; task = "Close connection";
dbMetaData.getConnection().close(); dbMetaData.getConnection().close();
} }
for (MassUploadRecord record : massUploadRecords) for (final MassUploadRecord record : massUploadRecords)
{ {
performMassUpload(record); performMassUpload(record);
} }
for (MassUploadRecord record : massUploadRecords) for (final MassUploadRecord record : massUploadRecords)
{ {
fixSequence(record.tableName); fixSequence(record.tableName);
} }
} catch (SQLException ex) } catch (final SQLException ex)
{ {
throw new UncategorizedSQLException(task, "UNKNOWN", ex); throw new UncategorizedSQLException(task, "UNKNOWN", ex);
} }
} }
private void performMassUpload(final MassUploadRecord record) private final void performMassUpload(final MassUploadRecord record)
{ {
try try
{ {
...@@ -152,7 +154,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -152,7 +154,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
return numberOfRows; return numberOfRows;
} }
public void setValues(PreparedStatement ps, int rowNo) throws SQLException public void setValues(final PreparedStatement ps, final int rowNo) throws SQLException
{ {
for (int colNo = 0; colNo < numberOfColumns; ++colNo) for (int colNo = 0; colNo < numberOfColumns; ++colNo)
{ {
...@@ -160,7 +162,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -160,7 +162,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
} }
} }
private Object tryGetValue(int rowNo, int colNo) private Object tryGetValue(final int rowNo, final int colNo)
{ {
final String stringValueOrNull = rows.get(rowNo)[colNo]; final String stringValueOrNull = rows.get(rowNo)[colNo];
if (stringValueOrNull == null) if (stringValueOrNull == null)
...@@ -176,13 +178,14 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -176,13 +178,14 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
} }
} }
}); });
} catch (Exception ex) } catch (final Exception ex)
{ {
throw CheckedExceptionTunnel.wrapIfNecessary(ex); throw CheckedExceptionTunnel.wrapIfNecessary(ex);
} }
} }
private BitSet findBinaryColumns(final DatabaseMetaData dbMetaData, final String tableName) throws SQLException private final BitSet findBinaryColumns(final DatabaseMetaData dbMetaData, final String tableName)
throws SQLException
{ {
final BitSet binary = new BitSet(); final BitSet binary = new BitSet();
final ResultSet rs = dbMetaData.getColumns(null, null, tableName.toUpperCase(), "%"); final ResultSet rs = dbMetaData.getColumns(null, null, tableName.toUpperCase(), "%");
...@@ -197,7 +200,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -197,7 +200,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
return binary; return binary;
} }
private List<String[]> readTSVFile(File tsvFile) throws IOException private final List<String[]> readTSVFile(final File tsvFile) throws IOException
{ {
final List<String[]> result = new ArrayList<String[]>(); final List<String[]> result = new ArrayList<String[]>();
final BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.openInputStream(tsvFile))); final BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.openInputStream(tsvFile)));
...@@ -239,7 +242,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -239,7 +242,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
return result; return result;
} }
private void fixSequence(String tableName) private final void fixSequence(final String tableName)
{ {
final String sequenceName = sequenceNameMapper.getSequencerForTable(tableName); final String sequenceName = sequenceNameMapper.getSequencerForTable(tableName);
if (sequenceName == null) if (sequenceName == null)
...@@ -250,11 +253,14 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade ...@@ -250,11 +253,14 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade
{ {
final long maxId = getSimpleJdbcTemplate().queryForLong(String.format("select max(id) from %s", tableName)); final long maxId = getSimpleJdbcTemplate().queryForLong(String.format("select max(id) from %s", tableName));
final long newSequenceValue = maxId + 1; final long newSequenceValue = maxId + 1;
operationLog.info("Updating sequence " + sequenceName + " for table " + tableName + " to value " if (operationLog.isInfoEnabled())
+ newSequenceValue); {
operationLog.info("Updating sequence " + sequenceName + " for table " + tableName + " to value "
+ newSequenceValue);
}
getJdbcTemplate().execute( getJdbcTemplate().execute(
String.format("alter sequence %s restart with %d", sequenceName, newSequenceValue)); String.format("alter sequence %s restart with %d", sequenceName, newSequenceValue));
} catch (DataAccessException ex) } catch (final DataAccessException ex)
{ {
operationLog.error("Failed to set new value for sequence '" + sequenceName + "' of table '" + tableName operationLog.error("Failed to set new value for sequence '" + sequenceName + "' of table '" + tableName
+ "'.", ex); + "'.", ex);
......
...@@ -63,14 +63,15 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -63,14 +63,15 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
/** /**
* Creates an instance for the specified data source and sequence mapper. * Creates an instance for the specified data source and sequence mapper.
*/ */
public PostgreSQLMassUploader(DataSource dataSource, ISequenceNameMapper sequenceNameMapper) throws SQLException public PostgreSQLMassUploader(final DataSource dataSource, final ISequenceNameMapper sequenceNameMapper)
throws SQLException
{ {
this.dataSource = dataSource; this.dataSource = dataSource;
this.sequenceNameMapper = sequenceNameMapper; this.sequenceNameMapper = sequenceNameMapper;
setDataSource(dataSource); setDataSource(dataSource);
} }
private CopyManager getCopyManager() throws SQLException, NoSuchFieldException, IllegalAccessException private final CopyManager getCopyManager() throws SQLException, NoSuchFieldException, IllegalAccessException
{ {
if (copyManager == null) if (copyManager == null)
{ {
...@@ -79,28 +80,28 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -79,28 +80,28 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
return copyManager; return copyManager;
} }
public void performMassUpload(File[] massUploadFiles) public final void performMassUpload(final File[] massUploadFiles)
{ {
Set<String> tables = new LinkedHashSet<String>(); final Set<String> tables = new LinkedHashSet<String>();
for (File file : massUploadFiles) for (final File file : massUploadFiles)
{ {
performMassUpload(file, tables); performMassUpload(file, tables);
} }
for (String name : tables) for (final String name : tables)
{ {
fixSequence(name); fixSequence(name);
} }
} }
private void performMassUpload(File massUploadFile, Set<String> tables) private final void performMassUpload(final File massUploadFile, final Set<String> tables)
{ {
try try
{ {
final String[] splitName = StringUtils.split(massUploadFile.getName(), "="); final String[] splitName = StringUtils.split(massUploadFile.getName(), "=");
assert splitName.length == 2 : "Missing '=' in name of file '" + massUploadFile.getName() + "'."; assert splitName.length == 2 : "Missing '=' in name of file '" + massUploadFile.getName() + "'.";
final String tableNameWithExtension = splitName[1]; final String tableNameWithExtension = splitName[1];
boolean csvFileType = CSV.isOfType(tableNameWithExtension); final boolean csvFileType = CSV.isOfType(tableNameWithExtension);
boolean tsvFileType = TSV.isOfType(tableNameWithExtension); final boolean tsvFileType = TSV.isOfType(tableNameWithExtension);
assert tsvFileType || csvFileType : "Non of expected file types [" + TSV.getFileType() + ", " assert tsvFileType || csvFileType : "Non of expected file types [" + TSV.getFileType() + ", "
+ CSV.getFileType() + "]: " + massUploadFile.getName(); + CSV.getFileType() + "]: " + massUploadFile.getName();
final String tableName = tableNameWithExtension.substring(0, tableNameWithExtension.lastIndexOf('.')); final String tableName = tableNameWithExtension.substring(0, tableNameWithExtension.lastIndexOf('.'));
...@@ -123,13 +124,13 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -123,13 +124,13 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
{ {
IOUtils.closeQuietly(is); IOUtils.closeQuietly(is);
} }
} catch (Exception ex) } catch (final Exception ex)
{ {
throw CheckedExceptionTunnel.wrapIfNecessary(ex); throw CheckedExceptionTunnel.wrapIfNecessary(ex);
} }
} }
private void fixSequence(String tableName) private final void fixSequence(final String tableName)
{ {
final String sequenceName = sequenceNameMapper.getSequencerForTable(tableName); final String sequenceName = sequenceNameMapper.getSequencerForTable(tableName);
if (sequenceName == null) if (sequenceName == null)
...@@ -138,21 +139,28 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -138,21 +139,28 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
} }
try try
{ {
getSimpleJdbcTemplate().queryForLong( // The result returned by setval is just the value of its second argument.
String.format("select setval('%s', max(id)) from %s", sequenceName, tableName)); final long newSequenceValue =
} catch (DataAccessException ex) getSimpleJdbcTemplate().queryForLong(
String.format("select setval('%s', max(id)) from %s", sequenceName, tableName));
if (operationLog.isInfoEnabled())
{
operationLog.info("Updating sequence " + sequenceName + " for table " + tableName + " to value "
+ newSequenceValue);
}
} catch (final DataAccessException ex)
{ {
operationLog.error("Failed to set new value for sequence '" + sequenceName + "' of table '" + tableName operationLog.error("Failed to set new value for sequence '" + sequenceName + "' of table '" + tableName
+ "'.", ex); + "'.", ex);
} }
} }
private PGConnection getPGConnection() throws SQLException, NoSuchFieldException, IllegalAccessException private final PGConnection getPGConnection() throws SQLException, NoSuchFieldException, IllegalAccessException
{ {
return getPGConnection(dataSource.getConnection()); return getPGConnection(dataSource.getConnection());
} }
private PGConnection getPGConnection(Connection conn) throws SQLException, NoSuchFieldException, private final PGConnection getPGConnection(final Connection conn) throws SQLException, NoSuchFieldException,
IllegalAccessException IllegalAccessException
{ {
if (conn instanceof PGConnection) if (conn instanceof PGConnection)
...@@ -163,7 +171,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -163,7 +171,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
{ {
operationLog.debug("Found connection of type '" + conn.getClass().getCanonicalName() + "'."); operationLog.debug("Found connection of type '" + conn.getClass().getCanonicalName() + "'.");
} }
Field delegateField = getField(conn.getClass(), "_conn"); final Field delegateField = getField(conn.getClass(), "_conn");
if (delegateField == null) if (delegateField == null)
{ {
throw new RuntimeException("No PostgreSQL driver found - cannot perform mass upload."); throw new RuntimeException("No PostgreSQL driver found - cannot perform mass upload.");
...@@ -172,7 +180,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -172,7 +180,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
return getPGConnection((Connection) delegateField.get(conn)); return getPGConnection((Connection) delegateField.get(conn));
} }
private static Field getField(Class<?> clazz, String fieldName) private final static Field getField(final Class<?> clazz, final String fieldName)
{ {
assert fieldName != null; assert fieldName != null;
if (clazz == null) if (clazz == null)
...@@ -180,7 +188,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -180,7 +188,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
return null; return null;
} }
for (Field field : clazz.getDeclaredFields()) for (final Field field : clazz.getDeclaredFields())
{ {
if (fieldName.equals(field.getName())) if (fieldName.equals(field.getName()))
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment