001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.util;
020:
021: import java.io.File;
022:
023: import org.apache.log4j.xml.DOMConfigurator;
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.DirectoryScanner;
026: import org.apache.tools.ant.Project;
027: import org.apache.tools.ant.taskdefs.MatchingTask;
028:
029: import de.schlund.pfixxml.config.GlobalConfigurator;
030: import de.schlund.pfixxml.resources.FileResource;
031: import de.schlund.pfixxml.resources.ResourceUtil;
032: import de.schlund.pfixxml.targets.TargetGenerator;
033:
034: /**
035: * @author adam
036: *
037: * To change the template for this generated type comment go to
038: * Window - Preferences - Java - Code Generation - Code and Comments
039: */
040: public class TargetGeneratorTask extends MatchingTask {
041:
042: /** holds directory to perform generation in */
043: private File dir;
044: /** holds the file containing the log4j configuration */
045: private File log4jconfig;
046:
047: public void execute() throws BuildException {
048:
049: TargetGenerator gen = null;
050:
051: // code has been taken over from de/schlund/pfixxml/targets/TargetGenerator#main(String[])
052:
053: File log4jconfigfile = getLog4jconfig();
054: if (log4jconfigfile == null) {
055: throw new BuildException("Need the log4jconfig attribute.");
056: }
057: DOMConfigurator.configure(log4jconfig.getPath());
058:
059: try {
060: GlobalConfigurator.setDocroot(getDir().getPath());
061: } catch (IllegalStateException e) {
062: // Ignore exception as there is no problem
063: // if the docroot has already been configured
064: }
065:
066: DirectoryScanner scanner = getDirectoryScanner(getDir());
067: scanner.scan();
068: String[] confignames = scanner.getIncludedFiles(); // **/depend.xml relative to getDir()
069:
070: if (confignames.length > 0) {
071: try {
072: for (int i = 0; i < confignames.length; i++) {
073: FileResource confile = ResourceUtil
074: .getFileResourceFromDocroot(confignames[i]);
075: if (confile.exists() && confile.canRead()
076: && confile.isFile()) {
077: try {
078: gen = createTargetGenerator(confile);
079: gen
080: .setIsGetModTimeMaybeUpdateSkipped(false);
081: System.out.println("---------- Doing "
082: + confignames[i] + "...");
083: gen.generateAll();
084: System.out.println("---------- ...done ["
085: + confignames[i] + "]");
086:
087: TargetGenerator.resetFactories();
088: } catch (Exception e) {
089: throw new BuildException(confile + ": "
090: + e.getMessage(), e);
091: }
092: } else {
093: throw new BuildException(
094: "Couldn't read configfile '"
095: + confignames[i] + "'");
096: }
097: }
098: } finally {
099: log(TargetGenerator.getReportAsString(),
100: Project.MSG_INFO);
101: }
102: } else {
103: log("Need configfile to work on", Project.MSG_WARN);
104: }
105:
106: }
107:
108: protected TargetGenerator createTargetGenerator(
109: FileResource confilepath) {
110: try {
111: return new TargetGenerator(confilepath);
112: } catch (Exception e) {
113: throw new BuildException(e.getMessage(), e);
114: }
115: }
116:
117: public File getDir() {
118: return dir;
119: }
120:
121: public void setDir(File dir) {
122: this .dir = dir;
123: }
124:
125: public File getLog4jconfig() {
126: return log4jconfig;
127: }
128:
129: public void setLog4jconfig(File log4jconfig) {
130: this.log4jconfig = log4jconfig;
131: }
132:
133: }
|