1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db.darrt.ant;
22
23 import java.io.File;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.apache.log4j.Logger;
30 import org.apache.tools.ant.BuildException;
31 import org.enableit.db.ant.AbstractDBTask;
32 import org.enableit.db.beans.Provider;
33 import org.enableit.db.beans.ProviderExt;
34 import org.enableit.db.darrt.ReportListener;
35 import org.enableit.db.darrt.SchemaHandler;
36
37 /***
38 * Common ancestor for schema operations accessed as Ant Tasks.
39 */
40 public abstract class AbstractSchemaTask extends AbstractDBTask {
41 /***
42 * The Log4J <code>Logger</code> doing the logging.
43 */
44 private static Logger logger = Logger.getLogger(AbstractSchemaTask.class);
45
46 /***
47 * CVS info ABOUT this class and its current version
48 */
49 public static final String ABOUT
50 = "$Revision: 1.12 $";
51
52 /***
53 * The JDBC driver's class name.
54 */
55 protected String targetDriver;
56
57 /***
58 * The database URL to connect to.
59 */
60 protected String targetUrl;
61
62 /***
63 * Username to use when connecting to the database.
64 */
65 protected String targetUserid;
66
67 /***
68 * Schema name to use when connecting to the database.
69 */
70 protected String targetSchemaName;
71
72 /***
73 * Password to use when connecting to the database.
74 */
75 protected String targetPassword = "";
76 protected String refFile;
77 protected String targetFile;
78 protected ProviderExt refProvider;
79 protected ProviderExt targetProvider;
80
81 /***
82 * Internal list of the report formatters listening to this task.
83 */
84 private List formatters = new ArrayList();
85
86 /***
87 * Default Constructor.
88 */
89 public AbstractSchemaTask() {
90 super();
91 }
92
93 /***
94 * Sets the JDBC driver to use in making a connection
95 */
96 public void setTargetDriver(String driver) {
97 this.targetDriver = driver;
98 }
99
100 /***
101 * Sets the JDBC url to connect to.
102 */
103 public void setTargetUrl(String url) {
104 this.targetUrl = url;
105 }
106
107 /***
108 * Sets the JDBC password to use in making a connection
109 */
110 public void setTargetPassword(String password) {
111 this.targetPassword = password;
112 }
113
114 /***
115 * Sets the JDBC schema name to use in making a connection
116 */
117 public void setSchemaName(String schemaName) {
118 this.schemaName = schemaName;
119 }
120
121 /***
122 * Sets the JDBC userid to use in making a connection
123 */
124 public void setTargetUserid(String userid) {
125 this.targetUserid = userid;
126 }
127
128 /***
129 * Set the reference schema file.
130 */
131 public void setReferenceFile(String referenceFile) {
132 this.refFile = referenceFile;
133 }
134
135 /***
136 * Set the target schema file.
137 */
138 public void setTargetFile(String targetFile) {
139 this.targetFile = targetFile;
140 }
141
142 /***
143 * Return the reference schema database connection properties.
144 */
145 public Provider getReferenceProvider() {
146 if ((refProvider == null) && (driver != null) && (url != null)
147 && (userid != null) && (password != null)) {
148 refProvider = new ProviderExt();
149 refProvider.setConnectionProperties(driver, url, userid, password,
150 schemaName);
151 }
152
153 return refProvider;
154 }
155
156 /***
157 * Return the target schema database connection properties.
158 */
159 public Provider getTargetProvider() {
160 if ((targetProvider == null) && (targetDriver != null)
161 && (targetUrl != null) && (targetUserid != null)
162 && (targetPassword != null)) {
163 targetProvider = new ProviderExt();
164 targetProvider.setConnectionProperties(targetDriver, targetUrl,
165 targetUserid, targetPassword, targetSchemaName);
166 }
167
168 return targetProvider;
169 }
170
171 /***
172 * Add a new report formatter to output of this task.
173 *
174 */
175 public void addFormatter(FormatterElement rfe) {
176 formatters.add(rfe);
177 }
178
179 /***
180 * Allows access to configured formatters.
181 */
182 public List getFormatters() {
183 return formatters;
184 }
185
186 /***
187 * @return <code>List</code> of <code>ReportListener</code>.
188 */
189 public List getReportListeners()
190 throws BuildException {
191 List list = new ArrayList();
192
193 try {
194 for (Iterator i = getFormatters().iterator(); i.hasNext();) {
195 FormatterElement formatter = (FormatterElement) i.next();
196 ReportListener reporter = (ReportListener) this.getClass()
197 .getClassLoader()
198 .loadClass(formatter
199 .getClassname()).newInstance();
200 reporter.setProvider(getTargetProvider()) ;
201 reporter.setWriter(formatter.getOutputWriter());
202 list.add(reporter);
203 }
204 } catch (Exception e) {
205 e.printStackTrace();
206 logger.error(e.getClass().getName() + ":" + e.getMessage());
207 throw new BuildException(e.getMessage());
208 }
209
210 return list;
211 }
212
213 /***
214 * Ensure the combination of attributes specifying the reference
215 * schema is valid.
216 */
217 protected void checkInputs() {
218 checkRefInputs();
219 checkTargetInputs();
220 }
221
222 /***
223 * Ensure the combination of attributes specifying the reference
224 * schema is valid.
225 */
226 protected void checkRefInputs() {
227 // check have valid inputs
228 if ((refFile == null) && (getReferenceProvider() == null)) {
229 throw new BuildException("You must specify a reference schema");
230 }
231
232 if ((refFile != null) && (getReferenceProvider() != null)) {
233 throw new BuildException(
234 "You must specify just one of referenceFile and reference "
235 + "JDBC properties");
236 }
237 }
238
239 /***
240 * Ensure the combination of attributes specifying the reference
241 * schema is valid.
242 */
243 protected void checkTargetInputs() {
244 if ((targetFile == null) && (getTargetProvider() == null)) {
245 throw new BuildException("You must specify a target schema");
246 }
247
248 if ((targetFile != null) && (getTargetProvider() != null)) {
249 throw new BuildException(
250 "You must specify just one of targetFile and target "
251 + "JDBC properties");
252 }
253 }
254
255 /***
256 * @return SchemaHandler instance with reference and
257 * target schemas already set.
258 */
259 protected SchemaHandler getSchemaHandler()
260 throws BuildException {
261 SchemaHandler schemaHandler = new SchemaHandler();
262
263 try {
264 if (refFile != null) {
265 URL refSchema = new File(refFile).toURL();
266
267 schemaHandler.setRefSchemaUrl(refSchema);
268 log("\t... reference definition: " + refSchema.toExternalForm());
269 }
270
271 if (getReferenceProvider() != null) {
272 schemaHandler.setOnlineRefSchema(getReferenceProvider());
273 log("\t... reference definition: " + url);
274 }
275
276 log("and");
277
278 if (targetFile != null) {
279 URL targetSchema = new File(targetFile).toURL();
280
281 schemaHandler.setTargetSchemaUrl(targetSchema);
282 log("\t... target definition: " + targetSchema.toExternalForm());
283 }
284
285 if (getTargetProvider() != null) {
286 schemaHandler.setOnlineTargetSchema(getTargetProvider());
287 log("\t... target definition: " + targetUrl);
288 }
289 } catch (Exception e) {
290 logger.error(e.getClass().getName() + ":" + e.getMessage());
291 throw new BuildException(e.getMessage());
292 }
293
294 return schemaHandler;
295 }
296
297 /***
298 * The Ant-defined <code>execute</code> method must be implemented
299 * by subclasses.
300 */
301 public abstract void execute();
302 }
This page was automatically generated by Maven