au.net.causal.maven.plugins.boxdb.db.MariaDbDatabase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of boxdb-maven-plugin Show documentation
Show all versions of boxdb-maven-plugin Show documentation
Maven plugin to start databases using Docker and VMs
package au.net.causal.maven.plugins.boxdb.db;
import au.net.causal.maven.plugins.boxdb.JavaRunner;
import au.net.causal.maven.plugins.boxdb.ScriptReaderRunner;
import org.apache.maven.plugin.MojoExecutionException;
import org.eclipse.aether.resolution.DependencyResolutionException;
import javax.sql.DataSource;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
public class MariaDbDatabase extends BaseMySqlDatabase
{
public MariaDbDatabase(BoxConfiguration boxConfiguration, ProjectConfiguration projectConfiguration, BoxContext context)
{
super(boxConfiguration, projectConfiguration, context);
}
@Override
public JdbcConnectionInfo jdbcConnectionInfo(DatabaseTarget target) throws BoxDatabaseException
{
String databaseName = getBoxConfiguration().getDatabaseName();
String uri = "jdbc:mariadb://" +
getContext().getDockerHostAddress() +
":" + getBoxConfiguration().getDatabasePort() +
"/" + databaseName;
return new JdbcConnectionInfo(uri,
target.user(getBoxConfiguration()),
target.password(getBoxConfiguration()),
getContext().getDockerHostAddress(),
getBoxConfiguration().getDatabasePort());
}
@Override
public JdbcDriverInfo jdbcDriverInfo()
throws BoxDatabaseException
{
//Drivers are backward/forward compatible so just use latest
return new JdbcDriverInfo(new RunnerDependency("org.mariadb.jdbc", "mariadb-java-client", "1.4.6"), "org.mariadb.jdbc.Driver");
}
@Override
public void configureNewDatabase()
throws IOException, SQLException, BoxDatabaseException
{
URL initScript = MariaDbDatabase.class.getResource("mariadb-create-database.sql");
ScriptReaderRunner scriptRunner = getContext().createScriptReaderRunner(this, getBoxConfiguration(), getProjectConfiguration());
ScriptReaderExecution execution = new ScriptReaderExecution();
execution.setFiltering(true);
execution.setScripts(Arrays.asList(initScript));
try
{
scriptRunner.execute(execution, DatabaseTarget.ADMIN, getProjectConfiguration().getScriptTimeout());
}
catch (MojoExecutionException e)
{
throw new BoxDatabaseException(e);
}
}
private DataSource createMariaDbDataSource(DatabaseTarget target)
throws BoxDatabaseException, IOException
{
try
{
JavaRunner javaRunner = getContext().createJavaRunner("org.mariadb.jdbc.MariaDbDataSource",
jdbcDriverInfo().getDependencies());
Class> clientDataSourceClass = javaRunner.makeClass();
DataSource dataSource = (DataSource) clientDataSourceClass.getConstructor().newInstance();
//Need to set some properties on the data source
JdbcConnectionInfo jdbcInfo = jdbcConnectionInfo(target);
configureDataSource(dataSource, "setUrl", String.class, jdbcInfo.getUri());
configureDataSource(dataSource, "setUser", String.class, jdbcInfo.getUser());
configureDataSource(dataSource, "setPassword", String.class, jdbcInfo.getPassword());
return dataSource;
}
catch (DependencyResolutionException e)
{
throw new BoxDatabaseException("Failed to resolve MariaDB dependency: " + e, e);
}
catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e)
{
throw new BoxDatabaseException("Failed to create MariaDB data source: " + e, e);
}
}
private void configureDataSource(DataSource dataSource, String methodName, Class> valueType, Object value)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
if (value != null)
dataSource.getClass().getMethod(methodName, valueType).invoke(dataSource, value);
}
@Override
public Connection createJdbcConnection(DatabaseTarget targetDatabase) throws SQLException, BoxDatabaseException, IOException
{
return createMariaDbDataSource(targetDatabase).getConnection();
}
}