1
1
/*
2
- * Copyright 2006-2007 the original author or authors.
2
+ * Copyright 2006-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
import java .nio .file .Paths ;
26
26
import java .util .Collections ;
27
27
import java .util .List ;
28
- import java .util .stream .Collectors ;
29
- import java .util .stream .Stream ;
30
28
31
29
import javax .sql .DataSource ;
32
30
40
38
import org .springframework .dao .DataAccessException ;
41
39
import org .springframework .jdbc .core .JdbcTemplate ;
42
40
import org .springframework .jdbc .datasource .DataSourceTransactionManager ;
43
- import org .springframework .transaction .TransactionStatus ;
44
41
import org .springframework .transaction .support .TransactionCallback ;
45
42
import org .springframework .transaction .support .TransactionTemplate ;
46
43
import org .springframework .util .Assert ;
49
46
50
47
/**
51
48
* 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>
53
50
*
54
51
* Run this class to initialize a database in a running server process.
55
52
* Make sure the server is running first by launching the "hsql-server" from the
58
55
* database and start again.
59
56
*
60
57
* @author Dave Syer
61
- *
58
+ * @author Drummond Dawson
59
+ * @author Mahmoud Ben Hassine
60
+ *
62
61
*/
63
62
public class DataSourceInitializer implements InitializingBean , DisposableBean {
64
63
@@ -85,11 +84,12 @@ public static void main(String... args) {
85
84
DataSourceInitializer .class .getSimpleName () + "-context.xml" ));
86
85
}
87
86
88
- @ Override
87
+ @ Override
89
88
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 ) {
93
93
try {
94
94
doExecuteScript (destroyScript );
95
95
}
@@ -104,61 +104,56 @@ public void destroy() {
104
104
}
105
105
}
106
106
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" );
110
110
initialize ();
111
111
}
112
112
113
113
private void initialize () {
114
- if (!initialized ) {
114
+ if (!this . initialized ) {
115
115
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 ) {
119
118
doExecuteScript (initScript );
120
119
}
121
120
}
122
- initialized = true ;
121
+ this . initialized = true ;
123
122
}
124
123
}
125
124
126
125
private void doExecuteScript (final Resource scriptResource ) {
127
- if (scriptResource == null || !scriptResource .exists ())
126
+ if (scriptResource == null || !scriptResource .exists ()) {
128
127
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 );
148
149
}
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 ;
156
152
}
157
153
}
158
154
}
159
- return null ;
160
155
}
161
-
156
+ return null ;
162
157
});
163
158
164
159
}
0 commit comments