001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.ant;
027:
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.DirectoryScanner;
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.Task;
032: import org.apache.tools.ant.taskdefs.Javac;
033: import org.apache.tools.ant.taskdefs.MatchingTask;
034: import org.apache.tools.ant.types.DTDLocation;
035: import org.apache.tools.ant.types.Path;
036: import org.apache.tools.ant.types.Reference;
037: import org.objectweb.speedo.api.ExceptionHelper;
038: import org.objectweb.speedo.api.SpeedoException;
039: import org.objectweb.speedo.generation.AbstractEnhancer;
040: import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
041:
042: import java.io.File;
043: import java.util.ArrayList;
044: import java.util.Arrays;
045: import java.util.Collection;
046:
047: /**
048: * Ant task that can be used to execute the generation.
049: * <p>Parameters of the task:</p><ul>
050: * <li><code>localpropertiesfile</code>, indicates whether there is a persistdesc.properties</li>
051: * <li><code>confFile</code>, location of the corresponding properties files</li>
052: * <li><code>logPropFile</code>, location of the log system properties file</li>
053: * <li><code>projectname</code> is the project name (optional) (Jorm parameter)</li>
054: * <li><code>mappername</code> is the mapper name for which the code will be generated</li>
055: * <li><code>keepsrc</code>, indicates whether generated Java files must be kept (optionnal, the default value is true</li>
056: * <li><code>input</code>, location of the base directory of <code>.class</code> files</li>
057: * <li><code>output</code>, location of the base destination directory</li>
058: * <li><code>classpath</code> nested classpath element containing the speedo and jorm jars</li>
059: * <li><code>jdopath</code> nested element containing a parameter <code>dir</code> and <code>include elements</code> , location of <code>.jdo</code> JDO XML Descriptors</li>
060: * <li><code>jormpath</code> (similar to <code>jdopath</code>), location of <code>.pd</code> JORM XML Descriptors</li>
061: * <li><code>persistenceaware</code> (similar to <code>jdopath</code>), location of Persistence Aware Java Class</li>
062: * <li><code>keepclasses</code>, indicates whether original java classes must be stored in another directory (see below)</li>
063: * <li><code>storedir</code>, location for storing original classes</li>
064: * <li><code>failsonerror</code>, indicates whether the process should fail if an error occurs</li></ul>
065: * @author S.Chassande-Barrioz
066: */
067: public abstract class AbstractEnhancerTask extends Task {
068:
069: private Path classpath = null;
070: private Path jormclasspath = null;
071: protected Description persistdesc = null;
072: private Description jorm = null;
073: private Description awareFiles = null;
074:
075: private SpeedoCompilerParameter scp = null;
076: private AbstractEnhancer sc = null;
077: protected File src = null;
078: private File output = null;
079:
080: protected abstract AbstractEnhancer getEnhancer();
081:
082: protected abstract void initDefaultDescPath();
083:
084: protected abstract void addDtdLocations(ArrayList al);
085:
086: public void init() throws BuildException {
087: super .init();
088: sc = getEnhancer();
089: scp = sc.getSpeedoCompilerParameter();
090: }
091:
092: /**
093: * @deprecated
094: */
095: public void setConfFile(File confFile) {
096: }
097:
098: public void setLogPropFile(File logPropFile) {
099: scp.logPropFile = logPropFile.getAbsolutePath();
100: }
101:
102: public void setProjectname(String projectName) {
103: scp.projectName = projectName;
104: }
105:
106: public void setMappername(String mapperName) {
107: scp.mapperName = mapperName;
108: }
109:
110: public void setSrc(File src) {
111: this .src = src;
112: }
113:
114: public void setKeepsrc(boolean keepsrc) {
115: scp.keepsrc = keepsrc;
116: }
117:
118: /**
119: * Create a DTD location record. This stores the location of a DTD. The
120: * DTD is identified by its public Id. The location may either be a file
121: * location or a resource location.
122: *
123: * @return the DTD location object to be configured by Ant
124: */
125: public DTDLocation createDTD() {
126: DTDLocation dtdLocation = new DTDLocation();
127: scp.dtdLocations.add(dtdLocation);
128: return dtdLocation;
129: }
130:
131: public void setOutput(File output) {
132: this .output = output;
133: scp.output = output.getAbsolutePath();
134: }
135:
136: public void setInput(File intput) {
137: scp.input = intput.getAbsolutePath();
138: }
139:
140: public void setClasspath(Path cp) {
141: if (classpath == null)
142: classpath = cp;
143: else
144: classpath.append(cp);
145: }
146:
147: public Path getClasspath() {
148: return classpath;
149: }
150:
151: public Path createClasspath() {
152: if (classpath == null)
153: classpath = new Path(getProject());
154: return classpath.createPath();
155: }
156:
157: public void setClasspathRef(Reference r) {
158: createClasspath().setRefid(r);
159: }
160:
161: public Description createDescpath() {
162: Description d = new Description();
163: this .persistdesc = d;
164: d.setProject(getProject());
165: return d;
166: }
167:
168: public MatchingTask createJavac() {
169: if (scp.javac == null) {
170: scp.javac = new Javac();
171: scp.javac.setProject(getProject());
172: }
173: return scp.javac;
174: }
175:
176: public Description createJormpath() {
177: Description d = new Description();
178: this .jorm = d;
179: d.setProject(getProject());
180: return d;
181: }
182:
183: public void setJormClasspath(Path cp) {
184: if (jormclasspath == null)
185: jormclasspath = cp;
186: else
187: jormclasspath.append(cp);
188: }
189:
190: public Path getJormClasspath() {
191: return jormclasspath;
192: }
193:
194: public Path createJormClasspath() {
195: if (jormclasspath == null)
196: jormclasspath = new Path(getProject());
197: return jormclasspath.createPath();
198: }
199:
200: public void setJormClasspathRef(Reference r) {
201: createJormClasspath().setRefid(r);
202: }
203:
204: public Description createPersistenceaware() {
205: Description d = new Description();
206: this .awareFiles = d;
207: d.setProject(getProject());
208: return d;
209: }
210:
211: public void setGenerateJormFile(boolean val) {
212: scp.generateNeededJormFile = val;
213: }
214:
215: public boolean getGenerateJormFile() {
216: return scp.generateNeededJormFile;
217: }
218:
219: /**
220: * Main method of the task executed by ant.
221: * Parses xml parameters, loads AntSpeedoExec with its own loader
222: */
223: public void execute() throws BuildException {
224: if (getClass().getClassLoader().getResourceAsStream(
225: "jorm.properties") == null) {
226: throw new BuildException(
227: "Speedo ant task ERROR: Impossible to "
228: + "find the 'jorm.properties' file in the classpath, classloader="
229: + getClass().getClassLoader());
230: }
231: if (scp.mapperName == null) {
232: scp.mapperName = "rdb";
233: }
234: scp.input = src.getAbsolutePath();
235: if (persistdesc == null) {
236: persistdesc = createDescpath();
237: initDefaultDescPath();
238: }
239: scp.xmlDir = persistdesc.dir.getAbsolutePath();
240: scp.xml = getSelectedFiles(persistdesc);
241: log("scp.jdo.size=" + scp.xml.size(), Project.MSG_DEBUG);
242:
243: if (jorm == null) {
244: jorm = createJormpath();
245: jorm.setDir(src);
246: jorm.setIncludes("**/*.pd");
247: }
248: scp.jormDir = jorm.dir.getAbsolutePath();
249: scp.jorm = getSelectedFiles(jorm);
250:
251: if (awareFiles == null) {
252: awareFiles = createDescpath();
253: awareFiles.setDir(output);
254: awareFiles.setIncludes("**/*.class");
255: }
256: scp.awareFilesDir = awareFiles.dir.getAbsolutePath();
257: scp.awareFiles = getSelectedFiles(awareFiles);
258:
259: if (jormclasspath == null) {
260: jormclasspath = classpath;
261: }
262: scp.jormclasspath = Arrays.asList(jormclasspath.list());
263: scp.classpath = classpath;
264:
265: log("scp.jormclasspath=" + scp.jormclasspath, Project.MSG_DEBUG);
266:
267: if (scp.dtdLocations.size() == 0) {
268: DTDLocation dl = new DTDLocation();
269: dl
270: .setPublicId("-//ObjectWeb Consortium//DTD JORM DEFINITIONS 2.0//EN");
271: dl.setLocation("jorm2.dtd");
272: scp.dtdLocations.add(dl);
273: addDtdLocations(scp.dtdLocations);
274: }
275: classpath = null;
276: jormclasspath = null;
277: persistdesc = null;
278: jorm = null;
279: awareFiles = null;
280: scp = null;
281: src = null;
282: output = null;
283: try {
284: log("SpeedoCompiler init", Project.MSG_DEBUG);
285: sc.init();
286: log("SpeedoCompiler process", Project.MSG_DEBUG);
287: sc.process();
288: log("SpeedoCompiler end", Project.MSG_DEBUG);
289: } catch (SpeedoException e) {
290: ExceptionHelper.getNested(e).printStackTrace();
291: throw new BuildException(ExceptionHelper.getNested(e));
292: } catch (Throwable e) {
293: e.printStackTrace();
294: throw new BuildException(e);
295: }
296: }
297:
298: /**
299: * Extract selected files from description item.
300: * @param desc
301: * @return a collection of String corresponding to selected files
302: */
303: private Collection getSelectedFiles(Description desc) {
304: Collection result = Arrays.asList(desc.getDirectoryScanner()
305: .getIncludedFiles());
306: Collection toDeselct = Arrays.asList(desc.getDirectoryScanner()
307: .getExcludedFiles());
308: log(toDeselct.toString(), Project.MSG_DEBUG);
309: result.removeAll(toDeselct);
310: return result;
311: }
312:
313: /**
314: * Task used to parse nested <code>jdopath</code> or <code>jormpath</code> elements.
315: */
316: public class Description extends MatchingTask {
317: private File dir = null;
318:
319: public void setDir(File dir) {
320: this .dir = dir;
321: }
322:
323: public DirectoryScanner getDirectoryScanner() {
324: return super.getDirectoryScanner(dir);
325: }
326: }
327: }
|