KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > tools > ant > ExecuteScriptTask


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.tools.ant;
13
14 import org.apache.tools.ant.BuildException;
15
16 import java.io.*;
17 import java.sql.Connection JavaDoc;
18 import java.sql.Statement JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import com.versant.core.common.BindingSupportImpl;
24 import com.versant.core.jdbc.JdbcStorageManagerFactory;
25
26 /**
27  * This task will load a sql script file or/and execute a sql
28  * command in the body of the xml file.
29  */

30 public class ExecuteScriptTask extends JdoTaskBase {
31
32     private String JavaDoc scriptFile;
33     private String JavaDoc sqlCommand = "";
34
35     /**
36      * Set an inline SQL command to execute.
37      * NB: Properties are not expanded in this text.
38      */

39     public void addText(String JavaDoc sql) {
40         this.sqlCommand += sql ;
41     }
42
43     public void setDatastore(String JavaDoc datastore) {
44         // ignore - property kept for compatibility with old versions
45
}
46
47     public void setScriptFile(String JavaDoc scriptFile) {
48         this.scriptFile = scriptFile;
49     }
50
51     public void execute() throws BuildException {
52         super.execute();
53         File file = null;
54
55         if (isSqlCommandEmpty() && scriptFile == null) {
56             throw BindingSupportImpl.getInstance().illegalArgument("scriptFile property or sql statement must be set!");
57         }
58
59
60         if (scriptFile != null){
61             file = new File(scriptFile);
62             if (!file.exists()){
63                 throw BindingSupportImpl.getInstance().illegalArgument("File "+file.toString() +" does not exist");
64             }
65         }
66
67         try {
68             if (!isSqlCommandEmpty()) {
69                 runScript(sqlCommand);
70             }
71
72             if (file != null){
73                 runScript(getText(file));
74             }
75         } catch (Exception JavaDoc e) {
76             throw new BuildException(e);
77         }
78     }
79
80     public void runScript(String JavaDoc script) throws Exception JavaDoc {
81         Connection JavaDoc con = null;
82         JdbcStorageManagerFactory smf = (JdbcStorageManagerFactory)getSmf();
83         try {
84             con = smf.getConnectionSource().getConnection(false, true);
85             SQLScriptParser shredder = new SQLScriptParser();
86             ArrayList JavaDoc list = shredder.parse(script, true);
87             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
88                 SQLScriptParser.SQLScriptPart scriptPart = (SQLScriptParser.SQLScriptPart) iter.next();
89                 log("Executing: "+scriptPart.getSql());
90                 Statement JavaDoc stat = null;
91                 try {
92                     stat = con.createStatement();
93                     stat.execute(scriptPart.getSql());
94                 } finally {
95                     try {
96                         stat.close();
97                     } catch (SQLException JavaDoc e) {
98                         //hide
99
}
100                 }
101             }
102         } finally {
103             if (con != null) {
104                 try {
105                     smf.getConnectionSource().returnConnection(con);
106                 } catch (SQLException JavaDoc e) {
107                     // ignore
108
}
109             }
110         }
111     }
112
113     private String JavaDoc getText(File file) throws IOException {
114         int size = (int) file.length();
115         int chars_read = 0;
116         FileReader in = new FileReader(file);
117         char[] data = new char[size];
118         while (in.ready()) {
119             chars_read += in.read(data, chars_read, size - chars_read);
120         }
121         in.close();
122         return new String JavaDoc(data, 0, chars_read);
123     }
124
125     private boolean isSqlCommandEmpty(){
126         return sqlCommand.trim().equals("");
127     }
128 }
129
Popular Tags