KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > tools > ant > EtlExecuteTask


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.tools.ant;
17
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.DirectoryScanner;
20 import org.apache.tools.ant.Project;
21 import org.apache.tools.ant.Task;
22 import org.apache.tools.ant.taskdefs.Java;
23 import org.apache.tools.ant.types.FileSet;
24 import scriptella.interactive.ConsoleProgressIndicator;
25 import scriptella.interactive.LoggingConfigurer;
26 import scriptella.tools.launcher.EtlLauncher;
27 import scriptella.util.CollectionUtils;
28
29 import java.io.File JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.logging.Handler JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.LogRecord JavaDoc;
36
37
38 /**
39  * Ant task for STL scripts execution.
40  *
41  * @author Fyodor Kupolov
42  * @version 1.0
43  */

44 public class EtlExecuteTask extends Task {
45     private List JavaDoc<FileSet> filesets = new ArrayList JavaDoc<FileSet>();
46     private final EtlLauncher launcher = new EtlLauncher();
47     private String JavaDoc maxmemory;
48     private boolean fork;
49     private boolean inheritAll=true;
50     private boolean debug;
51     private boolean quiet;
52
53     public boolean isFork() {
54         return fork;
55     }
56
57     public void setFork(final boolean fork) {
58         this.fork = fork;
59     }
60
61     /**
62      * @return Max amount of memory to allocate to the forked VM (ignored if fork is disabled)
63      */

64     public String JavaDoc getMaxmemory() {
65         return maxmemory;
66     }
67
68     public void setMaxmemory(final String JavaDoc maxmemory) {
69         this.maxmemory = maxmemory;
70     }
71
72     /**
73      * @return true if pass all ant properties to script executor. Default value is false.
74      */

75     public boolean isInheritAll() {
76         return inheritAll;
77     }
78
79     /**
80      * @param inheritAll true if pass all ant properties to script executor.
81      */

82     public void setInheritAll(final boolean inheritAll) {
83         this.inheritAll = inheritAll;
84     }
85
86     public boolean isDebug() {
87         return debug;
88     }
89
90     public void setDebug(boolean debug) {
91         this.debug = debug;
92     }
93
94     public boolean isQuiet() {
95         return quiet;
96     }
97
98     public void setQuiet(boolean quiet) {
99         this.quiet = quiet;
100     }
101
102     public void addFileset(final FileSet set) {
103         filesets.add(set);
104     }
105
106     public void setFile(final String JavaDoc fileName) throws FileNotFoundException JavaDoc {
107         FileSet f = new FileSet();
108         f.setFile(launcher.resolveFile(null, fileName));
109         filesets.add(f);
110     }
111
112     public void execute() throws BuildException {
113         List JavaDoc<File JavaDoc> files = new ArrayList JavaDoc<File JavaDoc>();
114
115         try {
116             if (filesets.isEmpty()) { //if no files - use file default name
117
files.add(launcher.resolveFile(getProject().getBaseDir(), null));
118             } else {
119                 for (FileSet fs : filesets) {
120                     DirectoryScanner ds = fs.getDirectoryScanner(getProject());
121                     File JavaDoc srcDir = fs.getDir(getProject());
122
123                     String JavaDoc srcFiles[] = ds.getIncludedFiles();
124
125                     for (String JavaDoc fName : srcFiles) {
126                         File JavaDoc file = launcher.resolveFile(srcDir, fName);
127                         files.add(file);
128                     }
129                 }
130             }
131         } catch (FileNotFoundException JavaDoc e) {
132             throw new BuildException(e.getMessage(), e);
133         }
134
135         if (fork) {
136             fork(files);
137         } else {
138             execute(files);
139         }
140     }
141
142     private void execute(final List JavaDoc<File JavaDoc> files) {
143         launcher.setProgressIndicator(new ConsoleProgressIndicator());
144
145         if (inheritAll) { //inherit ant properties - not supported in forked mode yet
146
launcher.setProperties(getProject().getProperties());
147         } else {
148             launcher.setProperties(CollectionUtils.asMap(System.getProperties()));
149
150         }
151
152         Handler JavaDoc h = new AntHandler();
153         h.setLevel(Level.INFO);
154         if (debug) {
155             h.setLevel(Level.FINE);
156         }
157         if (quiet) {
158             h.setLevel(Level.WARNING);
159         }
160         LoggingConfigurer.configure(h);
161         for (File JavaDoc file : files) {
162             try {
163                 launcher.execute(file);
164             } catch (Exception JavaDoc e) {
165                 throw new BuildException("Unable to execute file " + file +
166                         ": " + e.getMessage(), e);
167             }
168         }
169         LoggingConfigurer.remove(h);
170     }
171
172     /**
173      * TODO Implement fork correctly - use ant LoaderUtils to get scriptella.jar location
174      * TODO Output errors
175      */

176     private void fork(final List JavaDoc<File JavaDoc> files) {
177         Java j = new Java();
178         j.setFork(true);
179         j.setProject(getProject());
180 // j.setClasspath(getClasspath());
181

182         j.setClassname(EtlLauncher.class.getName());
183
184         StringBuilder JavaDoc line = new StringBuilder JavaDoc();
185
186         for (File JavaDoc file : files) {
187             line.append(file.getPath()).append(' ');
188         }
189         j.createArg().setLine(line.toString());
190
191         if (maxmemory != null) {
192             j.setMaxmemory(maxmemory);
193         }
194
195         int r = j.executeJava();
196         if (r != 0) {
197             throw new BuildException("Unable to execute files: " + files +
198                     ". See error log.");
199         }
200     }
201
202     class AntHandler extends Handler JavaDoc {
203         private StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
204
205         public synchronized void publish(LogRecord JavaDoc record) {
206             String JavaDoc msg = record.getMessage();
207             if (msg != null) {
208                 sb.append(msg);
209             }
210             Throwable JavaDoc thrown = record.getThrown();
211             if (thrown != null) {
212                 sb.append("\n").append(thrown.toString()).append('\n');
213             }
214             final int lev = convert(record.getLevel());
215             if (lev >= 0) {
216                 log(sb.toString(), lev);
217             }
218             sb.setLength(0);
219         }
220
221         /**
222          * Converts JUL level to appropriate Ant message priority
223          *
224          * @param level JUL level
225          * @return Ant message priority
226          */

227         private int convert(Level JavaDoc level) {
228             final int lev = level.intValue();
229             if (lev >= Level.SEVERE.intValue()) {
230                 return Project.MSG_ERR;
231             }
232             if (lev >= Level.WARNING.intValue()) {
233                 return Project.MSG_WARN;
234             }
235             if (lev >= Level.INFO.intValue()) {
236                 return Project.MSG_INFO;
237             }
238             if (debug) {
239                 return Project.MSG_INFO;
240             }
241             if (lev >= Level.FINE.intValue()) {
242                 return Project.MSG_VERBOSE;
243             }
244             if (lev > Level.OFF.intValue()) {
245                 return Project.MSG_DEBUG;
246             }
247             return -1;
248         }
249
250         public void flush() {
251         }
252
253         public void close() {
254         }
255     }
256
257 }
258
Popular Tags