Skip to content

Commit 66788cc

Browse files
committed
HBX-3203: Improve the implementation of class HibernateToolTask
Signed-off-by: Koen Aers <[email protected]>
1 parent e31e975 commit 66788cc

File tree

1 file changed

+105
-97
lines changed

1 file changed

+105
-97
lines changed

ant/src/main/java/org/hibernate/tool/ant/HibernateToolTask.java

Lines changed: 105 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@
4242
*/
4343
public class HibernateToolTask extends Task {
4444

45+
ConfigurationTask configurationTask;
46+
File destDir;
47+
List<ExporterTask> generators = new ArrayList<ExporterTask>();
48+
Path classPath;
49+
Path templatePath;
50+
Properties properties = new Properties();
51+
4552
public HibernateToolTask() {
4653
super();
4754
}
48-
ConfigurationTask configurationTask;
49-
private File destDir;
50-
private List<ExporterTask> generators = new ArrayList<ExporterTask>();
51-
private Path classPath;
52-
private Path templatePath;
53-
private Properties properties = new Properties();
54-
55+
5556
private void checkConfiguration() {
5657
if(configurationTask!=null) {
5758
throw new BuildException("Only a single configuration is allowed.");
@@ -61,209 +62,204 @@ private void checkConfiguration() {
6162
public ConfigurationTask createConfiguration() {
6263
checkConfiguration();
6364
configurationTask = new ConfigurationTask();
64-
return configurationTask;
65+
return configurationTask;
6566
}
66-
67+
6768

6869
public JDBCConfigurationTask createJDBCConfiguration() {
6970
checkConfiguration();
7071
configurationTask = new JDBCConfigurationTask();
71-
return (JDBCConfigurationTask) configurationTask;
72+
return (JDBCConfigurationTask) configurationTask;
7273
}
73-
74+
7475
public JPAConfigurationTask createJpaConfiguration() {
7576
checkConfiguration();
7677
configurationTask = new JPAConfigurationTask();
77-
return (JPAConfigurationTask) configurationTask;
78-
}
79-
78+
return (JPAConfigurationTask) configurationTask;
79+
}
80+
8081
public ExporterTask createHbm2DDL() {
8182
ExporterTask generator = new Hbm2DDLExporterTask(this);
8283
addGenerator( generator );
8384
return generator;
8485
}
85-
86+
8687
public ExporterTask createHbmTemplate() {
8788
ExporterTask generator = new GenericExporterTask(this);
8889
addGenerator( generator );
8990
return generator;
9091
}
91-
92+
9293
public ExporterTask createHbm2CfgXml() {
9394
ExporterTask generator = new Hbm2CfgXmlExporterTask(this);
9495
addGenerator( generator );
95-
96+
9697
return generator;
9798
}
9899

99-
protected boolean addGenerator(ExporterTask generator) {
100-
return generators.add(generator);
100+
protected void addGenerator(ExporterTask generator) {
101+
generators.add(generator);
101102
}
102-
103+
103104
public ExporterTask createHbm2Java() {
104105
ExporterTask generator = new Hbm2JavaExporterTask(this);
105106
addGenerator( generator );
106107
return generator;
107108
}
108-
109-
public ExporterTask createHbm2HbmXml() {
110-
ExporterTask generator= new Hbm2HbmXmlExporterTask(this);
111-
addGenerator( generator );
112-
return generator;
113-
}
114-
109+
110+
public ExporterTask createHbm2HbmXml() {
111+
ExporterTask generator= new Hbm2HbmXmlExporterTask(this);
112+
addGenerator( generator );
113+
return generator;
114+
}
115+
115116
public ExporterTask createHbm2Doc() {
116-
ExporterTask generator= new Hbm2DocExporterTask(this);
117-
addGenerator( generator );
118-
return generator;
119-
}
120-
117+
ExporterTask generator= new Hbm2DocExporterTask(this);
118+
addGenerator( generator );
119+
return generator;
120+
}
121+
121122
/*public ExporterTask createHbm2Jsf(){
122-
ExporterTask generator= new Hbm2JsfGeneratorTask(this);
123-
generators.add(generator);
124-
return generator;
123+
ExporterTask generator= new Hbm2JsfGeneratorTask(this);
124+
generators.add(generator);
125+
return generator;
125126
}*/
126-
127+
127128
public ExporterTask createHbm2DAO(){
128-
ExporterTask generator= new Hbm2DAOExporterTask(this);
129-
addGenerator( generator );
130-
return generator;
129+
ExporterTask generator= new Hbm2DAOExporterTask(this);
130+
addGenerator( generator );
131+
return generator;
131132
}
132-
133-
133+
134+
134135
public QueryExporterTask createQuery() {
135136
QueryExporterTask generator = new QueryExporterTask(this);
136137
generators.add(generator);
137138
return generator;
138139
}
139-
140+
140141
public HbmLintExporterTask createHbmLint() {
141142
HbmLintExporterTask generator = new HbmLintExporterTask(this);
142143
generators.add(generator);
143144
return generator;
144145
}
145-
146-
146+
147+
147148
/**
148-
* Set the classpath to be used when running the Java class
149-
*
150-
* @param s an Ant Path object containing the classpath.
151-
*/
152-
public void setClasspath(Path s) {
149+
* Set the classpath to be used when running the Java class
150+
*
151+
* @param s an Ant Path object containing the classpath.
152+
*/
153+
public void setClasspath(Path s) {
153154
classPath = s;
154-
}
155-
156-
157-
/**
158-
* Adds a path to the classpath.
159-
*
160-
* @return created classpath
161-
*/
162-
public Path createClasspath() {
155+
}
156+
157+
158+
/**
159+
* Adds a path to the classpath.
160+
*
161+
* @return created classpath
162+
*/
163+
public Path createClasspath() {
163164
classPath = new Path(getProject() );
164165
return classPath;
165-
}
166+
}
167+
166168

167-
168169
public void execute() {
169170
if(configurationTask==null) {
170171
throw new BuildException("No configuration specified. <" + getTaskName() + "> must have one of the following: <configuration>, <jpaconfiguration>, <annotationconfiguration> or <jdbcconfiguration>");
171172
}
172173
log("Executing Hibernate Tool with a " + configurationTask.getDescription() );
173174
validateParameters();
174175
Iterator<ExporterTask> iterator = generators.iterator();
175-
176+
176177
AntClassLoader loader = getProject().createClassLoader(classPath);
177-
178+
178179
ExporterTask generatorTask = null;
179180
int count = 1;
180181
try {
181182
ClassLoader classLoader = this.getClass().getClassLoader();
182183
loader.setParent(classLoader ); // if this is not set, classes from the taskdef cannot be found - which is crucial for e.g. annotations.
183184
loader.setThreadContextLoader();
184-
185-
while (iterator.hasNext() ) {
185+
186+
while (iterator.hasNext() ) {
186187
generatorTask = iterator.next();
187188
log(count++ + ". task: " + generatorTask.getName() );
188-
generatorTask.execute();
189+
generatorTask.execute();
189190
}
190-
} catch (RuntimeException re) {
191+
}
192+
catch (RuntimeException re) {
191193
reportException(re, count, generatorTask);
192-
}
194+
}
193195
finally {
194196
if (loader != null) {
195197
loader.resetThreadContextLoader();
196198
loader.cleanup();
197-
}
199+
}
198200
}
199201
}
200202

201203
private void reportException(Throwable re, int count, ExporterTask generatorTask) {
202-
log("An exception occurred while running exporter #" + count + ":" + generatorTask.getName(), Project.MSG_ERR);
204+
log("An exception occurred while running exporter #" + count + ":" + generatorTask, Project.MSG_ERR);
203205
log("To get the full stack trace run ant with -verbose", Project.MSG_ERR);
204-
206+
205207
log(re.toString(), Project.MSG_ERR);
206-
String ex = new String();
208+
StringBuilder ex = new StringBuilder();
207209
Throwable cause = re.getCause();
208210
while(cause!=null) {
209-
ex += cause.toString() + "\n";
211+
ex.append(cause.toString()).append("\n");
210212
if(cause==cause.getCause()) {
211213
break; // we reached the top.
212-
} else {
214+
}
215+
else {
213216
cause=cause.getCause();
214217
}
215218
}
216-
if(!StringUtil.isEmptyOrNull(ex)) {
217-
log(ex, Project.MSG_ERR);
219+
if(!StringUtil.isEmptyOrNull(ex.toString())) {
220+
log(ex.toString(), Project.MSG_ERR);
218221
}
219222

220223
String newbieMessage = ExceptionUtil.getProblemSolutionOrCause(re);
221224
if(newbieMessage!=null) {
222225
log(newbieMessage);
223-
}
224-
226+
}
227+
225228
if(re instanceof BuildException) {
226229
throw (BuildException)re;
227-
} else {
230+
}
231+
else {
228232
throw new BuildException(re, getLocation());
229233
}
230234
}
231235

232236
private void validateParameters() {
233237
if(generators.isEmpty()) {
234238
throw new BuildException("No exporters specified in <hibernatetool>. There has to be at least one specified. An exporter is e.g. <hbm2java> or <hbmtemplate>. See documentation for details.", getLocation());
235-
} else {
236-
Iterator<ExporterTask> iterator = generators.iterator();
237-
238-
while (iterator.hasNext() ) {
239-
ExporterTask generatorTask = iterator.next();
240-
generatorTask.validateParameters();
239+
}
240+
else {
241+
for (ExporterTask generatorTask : generators) {
242+
generatorTask.validateParameters();
241243
}
242244
}
243245
}
244246

245-
/**
246-
* @return
247-
*/
248247
public File getDestDir() {
249248
return destDir;
250249
}
251-
250+
252251
public void setDestDir(File file) {
253252
destDir = file;
254253
}
255254

256-
/**
257-
* @return
258-
*/
259255
public MetadataDescriptor getMetadataDescriptor() {
260256
return configurationTask.getMetadataDescriptor();
261257
}
262-
258+
263259
public void setTemplatePath(Path path) {
264260
templatePath = path;
265261
}
266-
262+
267263
public Path getTemplatePath() {
268264
if(templatePath==null) {
269265
templatePath = new Path(getProject()); // empty path
@@ -277,11 +273,11 @@ public Properties getProperties() {
277273
p.putAll(properties);
278274
return p;
279275
}
280-
276+
281277
public void addConfiguredPropertySet(PropertySet ps) {
282278
properties.putAll(ps.getProperties());
283279
}
284-
280+
285281
public void addConfiguredProperty(Environment.Variable property) {
286282
String key = property.getKey();
287283
String value = property.getValue();
@@ -295,6 +291,18 @@ public void addConfiguredProperty(Environment.Variable property) {
295291
return;
296292
}
297293
properties.put( key, value );
298-
}
299-
294+
}
295+
296+
@Override
297+
public Object clone() throws CloneNotSupportedException {
298+
HibernateToolTask htt = (HibernateToolTask) super.clone();
299+
htt.configurationTask = this.configurationTask;
300+
htt.destDir = this.destDir;
301+
htt.generators.addAll(this.generators);
302+
htt.classPath = this.classPath;
303+
htt.templatePath = this.templatePath;
304+
htt.properties.putAll(this.properties);
305+
return htt;
306+
}
307+
300308
}

0 commit comments

Comments
 (0)