001: package com.sun.portal.fabric.util;
002:
003: import java.util.Vector;
004: import java.util.logging.Level;
005: import java.util.logging.Logger;
006: import java.io.PrintStream;
007: import java.io.FileOutputStream;
008: import java.io.File;
009:
010: import org.apache.tools.ant.BuildEvent;
011: import org.apache.tools.ant.BuildException;
012: import org.apache.tools.ant.BuildListener;
013: import org.apache.tools.ant.DefaultLogger;
014: import org.apache.tools.ant.Project;
015: import org.apache.tools.ant.ProjectHelper;
016:
017: import com.sun.portal.log.common.PortalLogger;
018:
019: import com.sun.portal.util.Platform;
020:
021: public class AntUtil implements BuildListener {
022:
023: private static String fs = Platform.fs;
024:
025: private static String logDir;
026: private static String tmpDir;
027: protected static Logger logger;
028:
029: private static String LOG = ".log";
030:
031: public AntUtil(String psDataDir) {
032: logger = PortalLogger.getLogger(AntUtil.class);
033: logDir = psDataDir + fs + "logs" + fs + "config";
034: tmpDir = psDataDir + fs + "tmp";
035: }
036:
037: /*
038: * Use ant API to configure the sample portals with the <code>buildfile</code>
039: * and the <code>targets</code>
040: *
041: * @param buildfile Ant buildfile
042: * @param targets Ant targets
043: * @param logName Ant logfile prefix
044: * @param key Ant property key
045: * @param val Ant property value
046: */
047: public void runant(String buildfile, Vector targets,
048: String logName, String key, String val) {
049: String logfile = logDir + fs + logName + LOG;
050: Project project = new Project();
051: DefaultLogger antlogger = new DefaultLogger();
052:
053: try {
054: // setup logger
055: PrintStream logstream = new PrintStream(
056: new FileOutputStream(logfile));
057: antlogger.setErrorPrintStream(logstream);
058: antlogger.setOutputPrintStream(logstream);
059: antlogger.setMessageOutputLevel(Project.MSG_INFO);
060:
061: // add build listeners and fire event
062: project.addBuildListener(antlogger);
063: project.addBuildListener(this );
064: project.fireBuildStarted();
065:
066: // init project and go
067: project.init();
068: project.setUserProperty("ant.file", buildfile);
069: project.setUserProperty("config.location", tmpDir);
070:
071: if (key != null) {
072: project.setUserProperty(key, val);
073: }
074:
075: // debug information
076: String[] params = new String[2];
077: params[0] = project.getUserProperties().toString();
078: if (targets != null) {
079: params[1] = targets.toString();
080: } else {
081: params[1] = project.getDefaultTarget();
082: }
083:
084: logger.log(Level.INFO, "PSFB_CSPFU0020", params);
085:
086: // buildfile integrity check
087: ProjectHelper helper = ProjectHelper.getProjectHelper();
088: helper.parse(project, new File(buildfile));
089:
090: // execute targets
091: if (targets != null) {
092: project.executeTargets(targets);
093: } else {
094: project.executeTarget(project.getDefaultTarget());
095: }
096: project.fireBuildFinished(null);
097:
098: // close printstream
099: logstream.close();
100: } catch (Exception e) {
101: project.fireBuildFinished(e);
102: }
103: }
104:
105: /*
106: * Signals that the last target has finished.
107: */
108: public void buildFinished(BuildEvent event) {
109: Throwable error = event.getException();
110:
111: if (error != null) {
112: String[] param = new String[1];
113:
114: if (error instanceof BuildException) {
115: param[0] = error.toString();
116: } else {
117: param[0] = error.getMessage();
118: }
119:
120: logger.log(Level.SEVERE, "PSFB_CSPFU0019", param);
121: }
122: }
123:
124: /*
125: * Signals that a build has started.
126: */
127: public void buildStarted(BuildEvent event) {
128: }
129:
130: /*
131: * Signals a message logging event.
132: */
133: public void messageLogged(BuildEvent event) {
134: }
135:
136: /*
137: * Signals that a target has finished.
138: */
139: public void targetFinished(BuildEvent event) {
140: }
141:
142: /*
143: * Signals that a target is starting.
144: */
145: public void targetStarted(BuildEvent event) {
146: }
147:
148: /*
149: * Signals that a task has finished.
150: */
151: public void taskFinished(BuildEvent event) {
152: }
153:
154: /*
155: * Signals that a task is starting.
156: */
157: public void taskStarted(BuildEvent event) {
158: }
159: }
|