Script.java

biz/hammurapi/ant/Script.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 085 Do not declare runtime exceptions in the throws clause. 2 150:9
Java Inspector 089 Method documentation is too short. It is only 1 words. Should be at least 3 words. 2 57:9
Java Inspector 089 Parameter connectionEntry is not documented 2 57:9
Java Inspector 089 Parameter delimiter is not documented 2 66:9
Java Inspector 089 Parameter text is not documented 2 75:9
Java Inspector 089 Parameter file is not documented 2 88:9
Java Inspector 089 Undocumented parameter resource 2 103:9
Java Inspector 089 Javadoc contains tag for non-existent parameter file 2 103:9
Java Inspector 089 Parameter url is not documented 2 118:9
Java Inspector 089 Undocumented method 2 128:9
Java Inspector 089 Undocumented method 2 150:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 48:31
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 49:34
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 90:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 93:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 105:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 108:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 120:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 123:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 144:58
Java Inspector 040 Parameter name connectionEntry clashes with field name in Script 3 57:45

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.ant;
24
25import java.io.File;
26import java.io.FileReader;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.InputStreamReader;
30import java.io.StringReader;
31import java.net.URL;
32import java.sql.SQLException;
33
34import org.apache.tools.ant.BuildException;
35import org.apache.tools.ant.Task;
36
37import biz.hammurapi.sql.SQLProcessor;
38
39/**
40 * Executes SQL script
41 *
42 * @ant.task name="Script" category="Common"
43 * @author Pavel Vlasov
44 *
45 * @version $Revision: 1.1 $
46 */
47public class Script extends Task {
48 private String script="";
49 private String delimiter=";";
50 private ConnectionEntry connectionEntry;
51
52 /**
53 * Connection
54 * @ant.not-required
55 * @param connectionEntry
56 */
57 public void addConfiguredConnection(ConnectionEntry connectionEntry) {
58 this.connectionEntry=connectionEntry;
59 }
60
61 /**
62 * Statements delimiter (single character). Defaults to ';'
63 * @ant.not-required
64 * @param delimiter
65 */
66 public void setDelimiter(String delimiter) {
67 this.delimiter=delimiter;
68 }
69
70 /**
71 * Script text. Required if file is not set.
72 * @ant.not-required
73 * @param text
74 */
75 public void addText(String text) {
76 script+=text;
77 }
78
79 private File file;
80 private URL url;
81 private String resource;
82
83 /**
84 * Script file. One and only one of 'file', 'url', or 'resource' is required.
85 * @ant.required
86 * @param file
87 */
88 public void setFile(File file) {
89 if (url!=null) {
90 throw new BuildException("File and URL are mutually exclusive. URL already set");
91 }
92 if (resource!=null) {
93 throw new BuildException("File and resource are mutually exclusive. Resource already set");
94 }
95 this.file = file;
96 }
97
98 /**
99 * Script classloader resource. One and only one of 'file', 'url', or 'resource' is required.
100 * @ant.required
101 * @param file
102 */
103 public void setResource(String resource) {
104 if (url!=null) {
105 throw new BuildException("Resource and URL are mutually exclusive. URL already set");
106 }
107 if (file!=null) {
108 throw new BuildException("File and resource are mutually exclusive. File already set");
109 }
110 this.resource = resource;
111 }
112
113 /**
114 * Script URL. One and only one of 'file', 'url', or 'resource' is required.
115 * @ant.required
116 * @param url
117 */
118 public void setUrl(URL url) {
119 if (file!=null) {
120 throw new BuildException("File and URL are mutually exclusive. File already set");
121 }
122 if (resource!=null) {
123 throw new BuildException("Resource and URL are mutually exclusive. Resource already set");
124 }
125 this.url = url;
126 }
127
128 public void execute(SQLProcessor processor) throws IOException, SQLException {
129 if (script.trim().length()>0) {
130 processor.executeScript(new StringReader(script), delimiter.charAt(0));
131 }
132
133 if (file!=null) {
134 processor.executeScript(new FileReader(file), delimiter.charAt(0));
135 }
136
137 if (url!=null) {
138 processor.executeScript(new InputStreamReader(url.openStream()), delimiter.charAt(0));
139 }
140
141 if (resource!=null) {
142 InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(resource);
143 if (resourceAsStream==null) {
144 throw new BuildException("Resource not found: "+resource);
145 }
146 processor.executeScript(new InputStreamReader(resourceAsStream), delimiter.charAt(0));
147 }
148 }
149
150 public void execute() throws BuildException {
151 try {
152 execute(new SQLProcessor(connectionEntry.getDataSource(),null));
153 } catch (IOException e) {
154 throw new BuildException(e);
155 } catch (SQLException e) {
156 throw new BuildException(e);
157 }
158 }
159}
160