| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.springframework.batch.test; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import javax.sql.DataSource; |
| 23 | |
| 24 | import org.apache.commons.io.IOUtils; |
| 25 | import org.apache.commons.logging.Log; |
| 26 | import org.apache.commons.logging.LogFactory; |
| 27 | import org.springframework.beans.factory.BeanInitializationException; |
| 28 | import org.springframework.beans.factory.DisposableBean; |
| 29 | import org.springframework.beans.factory.InitializingBean; |
| 30 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 31 | import org.springframework.core.io.Resource; |
| 32 | import org.springframework.dao.DataAccessException; |
| 33 | import org.springframework.jdbc.core.JdbcTemplate; |
| 34 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 35 | import org.springframework.transaction.TransactionStatus; |
| 36 | import org.springframework.transaction.support.TransactionCallback; |
| 37 | import org.springframework.transaction.support.TransactionTemplate; |
| 38 | import org.springframework.util.Assert; |
| 39 | import org.springframework.util.ClassUtils; |
| 40 | import org.springframework.util.StringUtils; |
| 41 | |
| 42 | /** |
| 43 | * Wrapper for a {@link DataSource} that can run scripts on start up and shut |
| 44 | * down. Us as a bean definition <br/><br/> |
| 45 | * |
| 46 | * Run this class to initialize a database in a running server process. |
| 47 | * Make sure the server is running first by launching the "hsql-server" from the |
| 48 | * <code>hsql.server</code> project. Then you can right click in Eclipse and |
| 49 | * Run As -> Java Application. Do the same any time you want to wipe the |
| 50 | * database and start again. |
| 51 | * |
| 52 | * @author Dave Syer |
| 53 | * |
| 54 | */ |
| 55 | public class DataSourceInitializer implements InitializingBean, DisposableBean { |
| 56 | |
| 57 | private static final Log logger = LogFactory.getLog(DataSourceInitializer.class); |
| 58 | |
| 59 | private Resource[] initScripts; |
| 60 | |
| 61 | private Resource[] destroyScripts; |
| 62 | |
| 63 | private DataSource dataSource; |
| 64 | |
| 65 | private boolean ignoreFailedDrop = true; |
| 66 | |
| 67 | private boolean initialized = false; |
| 68 | |
| 69 | /** |
| 70 | * Main method as convenient entry point. |
| 71 | * |
| 72 | * @param args |
| 73 | */ |
| 74 | public static void main(String... args) { |
| 75 | new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(DataSourceInitializer.class, |
| 76 | DataSourceInitializer.class.getSimpleName() + "-context.xml")); |
| 77 | } |
| 78 | |
| 79 | public void destroy() { |
| 80 | if (destroyScripts==null) return; |
| 81 | for (int i = 0; i < destroyScripts.length; i++) { |
| 82 | Resource destroyScript = destroyScripts[i]; |
| 83 | try { |
| 84 | doExecuteScript(destroyScript); |
| 85 | } |
| 86 | catch (Exception e) { |
| 87 | if (logger.isDebugEnabled()) { |
| 88 | logger.warn("Could not execute destroy script [" + destroyScript + "]", e); |
| 89 | } |
| 90 | else { |
| 91 | logger.warn("Could not execute destroy script [" + destroyScript + "]"); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public void afterPropertiesSet() throws Exception { |
| 98 | Assert.notNull(dataSource); |
| 99 | initialize(); |
| 100 | } |
| 101 | |
| 102 | private void initialize() { |
| 103 | if (!initialized) { |
| 104 | destroy(); |
| 105 | if (initScripts != null) { |
| 106 | for (int i = 0; i < initScripts.length; i++) { |
| 107 | Resource initScript = initScripts[i]; |
| 108 | doExecuteScript(initScript); |
| 109 | } |
| 110 | } |
| 111 | initialized = true; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private void doExecuteScript(final Resource scriptResource) { |
| 116 | if (scriptResource == null || !scriptResource.exists()) |
| 117 | return; |
| 118 | TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource)); |
| 119 | transactionTemplate.execute(new TransactionCallback() { |
| 120 | |
| 121 | @SuppressWarnings("unchecked") |
| 122 | public Object doInTransaction(TransactionStatus status) { |
| 123 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); |
| 124 | String[] scripts; |
| 125 | try { |
| 126 | scripts = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource |
| 127 | .getInputStream())), ";"); |
| 128 | } |
| 129 | catch (IOException e) { |
| 130 | throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e); |
| 131 | } |
| 132 | for (int i = 0; i < scripts.length; i++) { |
| 133 | String script = scripts[i].trim(); |
| 134 | if (StringUtils.hasText(script)) { |
| 135 | try { |
| 136 | jdbcTemplate.execute(script); |
| 137 | } |
| 138 | catch (DataAccessException e) { |
| 139 | if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) { |
| 140 | logger.debug("DROP script failed (ignoring): " + script); |
| 141 | } |
| 142 | else { |
| 143 | throw e; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | }); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | private String stripComments(List<String> list) { |
| 156 | StringBuffer buffer = new StringBuffer(); |
| 157 | for (String line : list) { |
| 158 | if (!line.startsWith("//") && !line.startsWith("--")) { |
| 159 | buffer.append(line + "\n"); |
| 160 | } |
| 161 | } |
| 162 | return buffer.toString(); |
| 163 | } |
| 164 | |
| 165 | public void setInitScripts(Resource[] initScripts) { |
| 166 | this.initScripts = initScripts; |
| 167 | } |
| 168 | |
| 169 | public void setDestroyScripts(Resource[] destroyScripts) { |
| 170 | this.destroyScripts = destroyScripts; |
| 171 | } |
| 172 | |
| 173 | public void setDataSource(DataSource dataSource) { |
| 174 | this.dataSource = dataSource; |
| 175 | } |
| 176 | |
| 177 | public void setIgnoreFailedDrop(boolean ignoreFailedDrop) { |
| 178 | this.ignoreFailedDrop = ignoreFailedDrop; |
| 179 | } |
| 180 | |
| 181 | } |