11/*
2- * Copyright 2006-2007 the original author or authors.
2+ * Copyright 2006-2018 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2525import java .nio .file .Paths ;
2626import java .util .Collections ;
2727import java .util .List ;
28- import java .util .stream .Collectors ;
29- import java .util .stream .Stream ;
3028
3129import javax .sql .DataSource ;
3230
4038import org .springframework .dao .DataAccessException ;
4139import org .springframework .jdbc .core .JdbcTemplate ;
4240import org .springframework .jdbc .datasource .DataSourceTransactionManager ;
43- import org .springframework .transaction .TransactionStatus ;
4441import org .springframework .transaction .support .TransactionCallback ;
4542import org .springframework .transaction .support .TransactionTemplate ;
4643import org .springframework .util .Assert ;
4946
5047/**
5148 * Wrapper for a {@link DataSource} that can run scripts on start up and shut
52- * down. Us as a bean definition <br> <br>
49+ * down. Use as a bean definition <br>
5350 *
5451 * Run this class to initialize a database in a running server process.
5552 * Make sure the server is running first by launching the "hsql-server" from the
5855 * database and start again.
5956 *
6057 * @author Dave Syer
61- *
58+ * @author Drummond Dawson
59+ * @author Mahmoud Ben Hassine
60+ *
6261 */
6362public class DataSourceInitializer implements InitializingBean , DisposableBean {
6463
@@ -85,11 +84,12 @@ public static void main(String... args) {
8584 DataSourceInitializer .class .getSimpleName () + "-context.xml" ));
8685 }
8786
88- @ Override
87+ @ Override
8988 public void destroy () {
90- if (destroyScripts ==null ) return ;
91- for (int i = 0 ; i < destroyScripts .length ; i ++) {
92- Resource destroyScript = destroyScripts [i ];
89+ if (this .destroyScripts == null ) {
90+ return ;
91+ }
92+ for (Resource destroyScript : this .destroyScripts ) {
9393 try {
9494 doExecuteScript (destroyScript );
9595 }
@@ -104,61 +104,56 @@ public void destroy() {
104104 }
105105 }
106106
107- @ Override
108- public void afterPropertiesSet () throws Exception {
109- Assert .notNull (dataSource , "A DataSource is required" );
107+ @ Override
108+ public void afterPropertiesSet () {
109+ Assert .notNull (this . dataSource , "A DataSource is required" );
110110 initialize ();
111111 }
112112
113113 private void initialize () {
114- if (!initialized ) {
114+ if (!this . initialized ) {
115115 destroy ();
116- if (initScripts != null ) {
117- for (int i = 0 ; i < initScripts .length ; i ++) {
118- Resource initScript = initScripts [i ];
116+ if (this .initScripts != null ) {
117+ for (Resource initScript : this .initScripts ) {
119118 doExecuteScript (initScript );
120119 }
121120 }
122- initialized = true ;
121+ this . initialized = true ;
123122 }
124123 }
125124
126125 private void doExecuteScript (final Resource scriptResource ) {
127- if (scriptResource == null || !scriptResource .exists ())
126+ if (scriptResource == null || !scriptResource .exists ()) {
128127 return ;
129- TransactionTemplate transactionTemplate = new TransactionTemplate (new DataSourceTransactionManager (dataSource ));
130- transactionTemplate .execute (new TransactionCallback <Void >() {
131-
132- @ Override
133- public Void doInTransaction (TransactionStatus status ) {
134- JdbcTemplate jdbcTemplate = new JdbcTemplate (dataSource );
135- String [] scripts ;
136- try {
137- scripts = StringUtils
138- .delimitedListToStringArray (stripComments (getScriptLines (scriptResource )), ";" );
139- }
140- catch (IOException e ) {
141- throw new BeanInitializationException ("Cannot load script from [" + scriptResource + "]" , e );
142- }
143- for (int i = 0 ; i < scripts .length ; i ++) {
144- String script = scripts [i ].trim ();
145- if (StringUtils .hasText (script )) {
146- try {
147- jdbcTemplate .execute (script );
128+ }
129+ TransactionTemplate transactionTemplate = new TransactionTemplate (new DataSourceTransactionManager (this .dataSource ));
130+ transactionTemplate .execute ((TransactionCallback <Void >) status -> {
131+ JdbcTemplate jdbcTemplate = new JdbcTemplate (this .dataSource );
132+ String [] scripts ;
133+ try {
134+ scripts = StringUtils
135+ .delimitedListToStringArray (stripComments (getScriptLines (scriptResource )), ";" );
136+ }
137+ catch (IOException e ) {
138+ throw new BeanInitializationException ("Cannot load script from [" + scriptResource + "]" , e );
139+ }
140+ for (String script : scripts ) {
141+ String trimmedScript = script .trim ();
142+ if (StringUtils .hasText (trimmedScript )) {
143+ try {
144+ jdbcTemplate .execute (trimmedScript );
145+ }
146+ catch (DataAccessException e ) {
147+ if (this .ignoreFailedDrop && trimmedScript .toLowerCase ().startsWith ("drop" )) {
148+ logger .debug ("DROP script failed (ignoring): " + trimmedScript );
148149 }
149- catch (DataAccessException e ) {
150- if (ignoreFailedDrop && script .toLowerCase ().startsWith ("drop" )) {
151- logger .debug ("DROP script failed (ignoring): " + script );
152- }
153- else {
154- throw e ;
155- }
150+ else {
151+ throw e ;
156152 }
157153 }
158154 }
159- return null ;
160155 }
161-
156+ return null ;
162157 });
163158
164159 }
0 commit comments