Skip to content
Snippets Groups Projects
Commit 4d3e212d authored by brinn's avatar brinn
Browse files

change: make DatabaseConfigutationContext the single point of database...

change: make DatabaseConfigutationContext the single point of database configuration (prepare for Spring transaction support)

SVN: 540
parent a4421290
No related branches found
No related tags found
No related merge requests found
......@@ -20,7 +20,6 @@ import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
......@@ -80,8 +79,6 @@ public class DBMigrationEngine
private final DataSource metaDataSource;
private final DataSource dataSource;
private final File scriptFolder;
private final String owner;
......@@ -92,13 +89,15 @@ public class DBMigrationEngine
private final boolean shouldCreateFromScratch;
public DBMigrationEngine(DatabaseConfigurationContext context, String basicDatabaseName, String owner)
private final DataSource dataSource;
public DBMigrationEngine(DatabaseConfigurationContext context)
{
this.owner = owner;
this.owner = context.getOwner();
shouldCreateFromScratch = context.isCreateFromScratch();
metaDataSource = createMasterDataSource(context);
databaseName = basicDatabaseName + "_" + context.getDatabaseKind();
dataSource = createDataSource(context, databaseName, owner);
databaseName = context.getDatabaseName();
dataSource = context.getDataSource();
scriptFolder = new File(context.getScriptFolder());
folderOfDataScripts = context.getFolderOfDataScripts();
}
......@@ -111,14 +110,6 @@ public class DBMigrationEngine
return databaseName;
}
/**
* Returns the data source for the database.
*/
public final DataSource getDataSource()
{
return dataSource;
}
public void migrateTo(String version)
{
if (shouldCreateFromScratch)
......@@ -322,21 +313,6 @@ public class DBMigrationEngine
return dataSource;
}
/**
* Creates a <code>DataSource</code> from given <code>context</code>, <code>user</code> and
* <code>databaseName</code>.
*/
private final static DataSource createDataSource(DatabaseConfigurationContext context, String databaseName, String user)
{
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(context.getDriver());
String url = MessageFormat.format(context.getUrlTemplate(), databaseName);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword("");
return dataSource;
}
/**
* Checks whether given <code>DataAccessException</code> is caused by a "database does not exist" exception.
* <p>
......
......@@ -16,36 +16,100 @@
package ch.systemsx.cisd.dbmigration;
import org.springframework.jdbc.support.lob.LobHandler;
import java.text.MessageFormat;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.support.lob.LobHandler;
/**
* Configuration context for database operations.
*
*
* @author Franz-Josef Elmer
*/
public class DatabaseConfigurationContext
{
private String driver;
private LobHandler lobHandler;
private String adminURL;
private String adminUser;
private String adminPassword;
private String urlTemplate;
private String scriptFolder;
private String folderOfDataScripts;
private String databaseKind;
private boolean createFromScratch;
private DataSource dataSource;
private String owner;
private String basicDatabaseName;
private String databaseName;
public DatabaseConfigurationContext()
{
owner = System.getProperty("user.name");
}
/**
* Creates a <code>DataSource</code> for this context.
*/
private final DataSource createDataSource()
{
final BasicDataSource myDataSource = new BasicDataSource();
myDataSource.setDriverClassName(getDriver());
final String url = MessageFormat.format(getUrlTemplate(), getDatabaseName());
myDataSource.setUrl(url);
myDataSource.setUsername(owner);
myDataSource.setPassword("");
return myDataSource;
}
public final void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
}
/**
* Returns the {@link DataSource} of this data configuration.
*/
public final DataSource getDataSource()
{
if (dataSource == null)
{
dataSource = createDataSource();
}
return dataSource;
}
/**
* Returns the user name of the owner of the database.
*/
public final String getOwner()
{
return owner;
}
/**
* Sets the user name of the onwer of the database.
*/
public final void setOwner(String owner)
{
this.owner = owner;
}
/**
* Returns user name of the administrator.
*
......@@ -66,6 +130,34 @@ public class DatabaseConfigurationContext
this.adminUser = adminUser;
}
/**
* Returns the basic name of the database. The kind of database will be added to this to create the full database
* name.
*/
public String getBasicDatabaseName()
{
return basicDatabaseName;
}
/**
* Sets the basic name of the database. The kind of database will be added to this to create the full database name.
*
* @param basicDatabaseName The basic name of the database. Must not be <code>null</code>.
*/
public void setBasicDatabaseName(String basicDatabaseName)
{
this.basicDatabaseName = basicDatabaseName;
}
public String getDatabaseName()
{
if (databaseName == null)
{
databaseName = getBasicDatabaseName() + "_" + getDatabaseKind();
}
return databaseName;
}
/**
* Returns password of the administrator.
*
......@@ -147,8 +239,8 @@ public class DatabaseConfigurationContext
}
/**
* Returns the template to created the URL of the database to be created/migrated. It should
* contain <code>{0}</code> as a placeholder for the name of the database.
* Returns the template to created the URL of the database to be created/migrated. It should contain
* <code>{0}</code> as a placeholder for the name of the database.
*
* @return <code>null</code> when undefined.
*/
......@@ -187,7 +279,7 @@ public class DatabaseConfigurationContext
}
/**
* Returns database kind.
* Returns database kind.
*
* @return <code>null</code> when undefined.
*/
......@@ -197,8 +289,8 @@ public class DatabaseConfigurationContext
}
/**
* Sets database kind. This will be append to the name of the database. It allows to have different
* database instances in parallel (for developing, testing, etc.).
* Sets database kind. This will be append to the name of the database. It allows to have different database
* instances in parallel (for developing, testing, etc.).
*
* @param databaseKind New value. Can be <code>null</code>.
*/
......@@ -228,9 +320,8 @@ public class DatabaseConfigurationContext
}
/**
* Returns the folder which contains all Data SQL scripts. As a default value {@link #getScriptFolder()}
* will be returned if not definied by a non-<code>null</code> value in
* {@link #setFolderOfDataScripts(String)}.
* Returns the folder which contains all Data SQL scripts. As a default value {@link #getScriptFolder()} will be
* returned if not definied by a non-<code>null</code> value in {@link #setFolderOfDataScripts(String)}.
*
* @return <code>null</code> when {@link #getScriptFolder()} returns <code>null</code>.
*/
......
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