All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.db.DirectDataSource Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
	Milyn - Copyright (C) 2006 - 2010

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License (version 2.1) as published by the Free Software
	Foundation.

	This library 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:
	http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.db;

import org.milyn.cdr.annotation.ConfigParam;
import org.milyn.delivery.annotation.Initialize;
import org.milyn.util.ClassUtil;
import org.milyn.event.report.annotation.VisitBeforeReport;
import org.milyn.event.report.annotation.VisitAfterReport;
import org.milyn.assertion.AssertArgument;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Direct DataSource.
 * 

* Configured with a specific JDBC driver plus username etc. * * @author [email protected] */ @VisitBeforeReport(summary = "Bind DirectDataSource ${resource.parameters.datasource} to ExecutionContext.", detailTemplate = "reporting/DirectDataSource_before.html") @VisitAfterReport(summary = "Cleaning up DirectDataSource ${resource.parameters.datasource}. Includes performing commit/rollback etc.", detailTemplate = "reporting/DirectDataSource_after.html") public class DirectDataSource extends AbstractDataSource { @ConfigParam(name = "datasource") private String name; @ConfigParam private boolean autoCommit; @ConfigParam private Class driver; @ConfigParam private String url; @ConfigParam private String username; @ConfigParam private String password; public String getName() { return name; } public DirectDataSource setName(String name) { AssertArgument.isNotNullAndNotEmpty(name, "name"); this.name = name; return this; } public DirectDataSource setAutoCommit(boolean autoCommit) { this.autoCommit = autoCommit; return this; } public DirectDataSource setDriver(Class driver) { AssertArgument.isNotNull(driver, "driver"); this.driver = driver; return this; } public DirectDataSource setUrl(String url) { AssertArgument.isNotNullAndNotEmpty(url, "url"); this.url = url; return this; } public DirectDataSource setUsername(String username) { AssertArgument.isNotNull(username, "username"); this.username = username; return this; } public DirectDataSource setPassword(String password) { AssertArgument.isNotNull(password, "password"); this.password = password; return this; } @Initialize public void registerDriver() throws SQLException { Driver driverInstance; AssertArgument.isNotNullAndNotEmpty(name, "name"); AssertArgument.isNotNull(driver, "driver"); AssertArgument.isNotNullAndNotEmpty(url, "url"); AssertArgument.isNotNull(username, "username"); AssertArgument.isNotNull(password, "password"); try { driverInstance = (Driver) driver.newInstance(); } catch (Exception e) { SQLException sqlE = new SQLException("Failed to register JDBC driver '" + driver + "'. Unable to create instance of driver class."); sqlE.initCause(e); throw sqlE; } DriverManager.registerDriver(driverInstance); } public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } public boolean isAutoCommit() { return autoCommit; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy