${database.allClassCopyright}package ${glPackageBaseCommon}; #if ($database.isTargetContainerSpring()) import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; #end #if ($database.isTargetContainerSpring()) import org.seasar.dbflute.jdbc.DataSourceHandler; import org.seasar.dbflute.jdbc.NotClosingConnectionWrapper; #end import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; #if ($database.isTargetContainerSeasar()) import org.seasar.dbflute.cbean.ConditionBeanContext; import org.seasar.dbflute.s2dao.extension.TnSqlLogRegistry; #end import org.seasar.dbflute.util.DfSystemUtil; #if ($database.isTargetContainerSpring()) import org.springframework.jdbc.datasource.DataSourceUtils; #end /** * @author ${database.classAuthor} */ public class ${glDBFluteInitializer} { // =================================================================================== // Definition // ========== /** Log instance. */ private static final Log _log = LogFactory.getLog(${glDBFluteInitializer}.class); // =================================================================================== // Constructor // =========== #if ($database.isTargetContainerSeasar()) /** * Constructor. And initialize various components. */ public ${glDBFluteInitializer}() { _log.info("...Initializing DBFlute components!"); handleSqlLogRegistry(); loadCoolClasses(); ${glDBFluteConfig}.getInstance().lock(); } protected void handleSqlLogRegistry() { if (${glDBFluteConfig}.getInstance().isUseSqlLogRegistry()) { final StringBuilder sb = new StringBuilder(); sb.append("{SqlLog Information}").append(getLineSeparator()); sb.append(" [SqlLogRegistry]").append(getLineSeparator()); if (TnSqlLogRegistry.setupSqlLogRegistry()) { sb.append(" ...Setting up sqlLogRegistry(org.seasar.extension.jdbc)!").append(getLineSeparator()); sb.append(" Because the property 'useSqlLogRegistry' of the config of DBFlute is true."); } else { sb.append(" The sqlLogRegistry(org.seasar.extension.jdbc) is not supported at the version!"); } _log.info(sb); } else { final Object sqlLogRegistry = TnSqlLogRegistry.findContainerSqlLogRegistry(); if (sqlLogRegistry != null) { TnSqlLogRegistry.closeRegistration(); } } } protected void loadCoolClasses() { // for S2Container basically ConditionBeanContext.loadCoolClasses(); // Against the ClassLoader Headache! } #elseif ($database.isTargetContainerSpring()) /** * Constructor. And initialize various components. * @param dataSource The instance of data source. (NotNull) */ public ${glDBFluteInitializer}(DataSource dataSource) { _log.info("...Initializing DBFlute components!"); setupDataSourceHandler(dataSource); ${glDBFluteConfig}.getInstance().lock(); } /** * Set up the handler of data source to the configuration of DBFlute.
* If it uses commons-DBCP, it needs to arrange some for transaction. * * If you use a transaction library which has a data source which supports transaction, * It doesn't need these arrangement. (For example, the framework 'Atomikos')
* This method should be executed when application is initialized. * @param dataSource The instance of data source. (NotNull) */ protected void setupDataSourceHandler(DataSource dataSource) { if (dataSource == null) { String msg = "The argument 'dataSource' should not be null!"; throw new IllegalArgumentException(msg); } final ${glDBFluteConfig} config = ${glDBFluteConfig}.getInstance(); final DataSourceHandler dataSourceHandler = config.getDataSourceHandler(); if (dataSourceHandler != null) { return; } final String dataSourceFqcn = dataSource.getClass().getName(); if (dataSourceFqcn.startsWith("org.apache.commons.dbcp.")) { config.unlock(); config.setDataSourceHandler(new SpringDBCPDataSourceHandler()); } } protected static class SpringDBCPDataSourceHandler implements DataSourceHandler { public Connection getConnection(final DataSource ds) throws SQLException { final Connection conn = DataSourceUtils.getConnection(ds); if (DataSourceUtils.isConnectionTransactional(conn, ds)) { return new NotClosingConnectionWrapper(conn); } else { return conn; } } @Override public String toString() { return "SpringDBCPDataSourceHandler(for Spring and Commons-DBCP)"; } } #else /** * Constructor. And initialize various components. */ public ${glDBFluteInitializer}() { _log.info("...Initializing DBFlute components!"); ${glDBFluteConfig}.getInstance().lock(); } #end // =================================================================================== // General Helper // ============== protected String getLineSeparator() { return DfSystemUtil.getLineSeparator(); } }