/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
* Authors: S.Chassande-Barrioz.
*
*/
package org.objectweb.speedo.ant;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.DTDLocation;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.generation.AbstractEnhancer;
import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
* Ant task that can be used to execute the generation.
* <p>Parameters of the task:</p><ul>
* <li><code>localpropertiesfile</code>, indicates whether there is a persistdesc.properties</li>
* <li><code>confFile</code>, location of the corresponding properties files</li>
* <li><code>logPropFile</code>, location of the log system properties file</li>
* <li><code>projectname</code> is the project name (optional) (Jorm parameter)</li>
* <li><code>mappername</code> is the mapper name for which the code will be generated</li>
* <li><code>keepsrc</code>, indicates whether generated Java files must be kept (optionnal, the default value is true</li>
* <li><code>input</code>, location of the base directory of <code>.class</code> files</li>
* <li><code>output</code>, location of the base destination directory</li>
* <li><code>classpath</code> nested classpath element containing the speedo and jorm jars</li>
* <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>
* <li><code>jormpath</code> (similar to <code>jdopath</code>), location of <code>.pd</code> JORM XML Descriptors</li>
* <li><code>persistenceaware</code> (similar to <code>jdopath</code>), location of Persistence Aware Java Class</li>
* <li><code>keepclasses</code>, indicates whether original java classes must be stored in another directory (see below)</li>
* <li><code>storedir</code>, location for storing original classes</li>
* <li><code>failsonerror</code>, indicates whether the process should fail if an error occurs</li></ul>
* @author S.Chassande-Barrioz
*/
public abstract class AbstractEnhancerTask extends Task {
private Path classpath = null;
private Path jormclasspath = null;
protected Description persistdesc = null;
private Description jorm = null;
private Description awareFiles = null;
private SpeedoCompilerParameter scp = null;
private AbstractEnhancer sc = null;
protected File src = null;
private File output = null;
protected abstract AbstractEnhancer getEnhancer();
protected abstract void initDefaultDescPath();
protected abstract void addDtdLocations(ArrayList al);
public void init() throws BuildException {
super.init();
sc = getEnhancer();
scp = sc.getSpeedoCompilerParameter();
}
/**
* @deprecated
*/
public void setConfFile(File confFile) {
}
public void setLogPropFile(File logPropFile) {
scp.logPropFile = logPropFile.getAbsolutePath();
}
public void setProjectname(String projectName) {
scp.projectName = projectName;
}
public void setMappername(String mapperName) {
scp.mapperName = mapperName;
}
public void setSrc(File src) {
this.src = src;
}
public void setKeepsrc(boolean keepsrc) {
scp.keepsrc = keepsrc;
}
/**
* Create a DTD location record. This stores the location of a DTD. The
* DTD is identified by its public Id. The location may either be a file
* location or a resource location.
*
* @return the DTD location object to be configured by Ant
*/
public DTDLocation createDTD() {
DTDLocation dtdLocation = new DTDLocation();
scp.dtdLocations.add(dtdLocation);
return dtdLocation;
}
public void setOutput(File output) {
this.output = output;
scp.output = output.getAbsolutePath();
}
public void setInput(File intput) {
scp.input = intput.getAbsolutePath();
}
public void setClasspath(Path cp) {
if (classpath == null)
classpath = cp;
else
classpath.append(cp);
}
public Path getClasspath() {
return classpath;
}
public Path createClasspath() {
if (classpath == null)
classpath = new Path(getProject());
return classpath.createPath();
}
public void setClasspathRef(Reference r) {
createClasspath().setRefid(r);
}
public Description createDescpath() {
Description d = new Description();
this.persistdesc = d;
d.setProject(getProject());
return d;
}
public MatchingTask createJavac() {
if(scp.javac == null) {
scp.javac = new Javac();
scp.javac.setProject(getProject());
}
return scp.javac;
}
public Description createJormpath() {
Description d = new Description();
this.jorm = d;
d.setProject(getProject());
return d;
}
public void setJormClasspath(Path cp) {
if (jormclasspath == null)
jormclasspath = cp;
else
jormclasspath.append(cp);
}
public Path getJormClasspath() {
return jormclasspath;
}
public Path createJormClasspath() {
if (jormclasspath == null)
jormclasspath = new Path(getProject());
return jormclasspath.createPath();
}
public void setJormClasspathRef(Reference r) {
createJormClasspath().setRefid(r);
}
public Description createPersistenceaware() {
Description d = new Description();
this.awareFiles = d;
d.setProject(getProject());
return d;
}
public void setGenerateJormFile(boolean val) {
scp.generateNeededJormFile = val;
}
public boolean getGenerateJormFile() {
return scp.generateNeededJormFile;
}
/**
* Main method of the task executed by ant.
* Parses xml parameters, loads AntSpeedoExec with its own loader
*/
public void execute() throws BuildException {
if (getClass().getClassLoader().getResourceAsStream("jorm.properties") == null) {
throw new BuildException("Speedo ant task ERROR: Impossible to " +
"find the 'jorm.properties' file in the classpath, classloader="
+ getClass().getClassLoader());
}
if (scp.mapperName == null) {
scp.mapperName = "rdb";
}
scp.input = src.getAbsolutePath();
if (persistdesc == null) {
persistdesc = createDescpath();
initDefaultDescPath();
}
scp.xmlDir = persistdesc.dir.getAbsolutePath();
scp.xml = getSelectedFiles(persistdesc);
log("scp.jdo.size=" + scp.xml.size(), Project.MSG_DEBUG);
if (jorm == null) {
jorm = createJormpath();
jorm.setDir(src);
jorm.setIncludes("**/*.pd");
}
scp.jormDir = jorm.dir.getAbsolutePath();
scp.jorm = getSelectedFiles(jorm);
if (awareFiles == null) {
awareFiles = createDescpath();
awareFiles.setDir(output);
awareFiles.setIncludes("**/*.class");
}
scp.awareFilesDir = awareFiles.dir.getAbsolutePath();
scp.awareFiles = getSelectedFiles(awareFiles);
if (jormclasspath == null) {
jormclasspath = classpath;
}
scp.jormclasspath = Arrays.asList(jormclasspath.list());
scp.classpath = classpath;
log("scp.jormclasspath=" + scp.jormclasspath, Project.MSG_DEBUG);
if (scp.dtdLocations.size() == 0) {
DTDLocation dl = new DTDLocation();
dl.setPublicId("-//ObjectWeb Consortium//DTD JORM DEFINITIONS 2.0//EN");
dl.setLocation("jorm2.dtd");
scp.dtdLocations.add(dl);
addDtdLocations(scp.dtdLocations);
}
classpath = null;
jormclasspath = null;
persistdesc = null;
jorm = null;
awareFiles = null;
scp = null;
src = null;
output = null;
try {
log("SpeedoCompiler init", Project.MSG_DEBUG);
sc.init();
log("SpeedoCompiler process", Project.MSG_DEBUG);
sc.process();
log("SpeedoCompiler end", Project.MSG_DEBUG);
} catch (SpeedoException e) {
ExceptionHelper.getNested(e).printStackTrace();
throw new BuildException(ExceptionHelper.getNested(e));
} catch (Throwable e) {
e.printStackTrace();
throw new BuildException(e);
}
}
/**
* Extract selected files from description item.
* @param desc
* @return a collection of String corresponding to selected files
*/
private Collection getSelectedFiles(Description desc) {
Collection result = Arrays.asList(
desc.getDirectoryScanner().getIncludedFiles());
Collection toDeselct = Arrays.asList(
desc.getDirectoryScanner().getExcludedFiles());
log(toDeselct.toString(), Project.MSG_DEBUG);
result.removeAll(toDeselct);
return result;
}
/**
* Task used to parse nested <code>jdopath</code> or <code>jormpath</code> elements.
*/
public class Description extends MatchingTask {
private File dir = null;
public void setDir(File dir) {
this.dir = dir;
}
public DirectoryScanner getDirectoryScanner() {
return super.getDirectoryScanner(dir);
}
}
}