001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)AntScriptRunner.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.internal.support;
030:
031: import java.io.File;
032: import org.apache.tools.ant.Project;
033: import org.apache.tools.ant.ProjectHelper;
034: import org.apache.tools.ant.DefaultLogger;
035: import org.apache.tools.ant.BuildException;
036: import java.util.logging.Logger;
037: import java.util.logging.Level;
038:
039: /**
040: * AntScriptRunner is a class that can be used to run
041: * an ant task from a specified ant file in a specified directory.
042: *
043: * @author Sun Microsystems, Inc.
044: */
045: public class AntScriptRunner {
046:
047: /**
048: * Run a target in an ant file.
049: *
050: * @param basedir the ant basedir
051: * @param antfn the name of the ant file.
052: * @param targetname the target you want to run
053: * @return true if ant task is successfully run
054: */
055: public static boolean runAntTarget(String basedir, String antfn,
056: String targetname) {
057: Project project = new Project();
058: boolean taskstatus = true;
059:
060: project.setBaseDir(new File(basedir));
061: project.init();
062:
063: File buildFile = new File(antfn);
064: ProjectHelper.configureProject(project, buildFile);
065:
066: if (targetname == null || targetname.equals("")) {
067: targetname = project.getDefaultTarget();
068: }
069:
070: DefaultLogger mylogger = new DefaultLogger();
071:
072: /*
073: ** this defaults to MSG_ERR
074: ** The order of the levels, from least to most verbose:
075: ** {MSG_ERR, MSG_WARN, MSG_INFO, MSG_VERBOSE, MSG_DEBUG}
076: */
077:
078: Level logLevel = Level.parse(System.getProperty(
079: "com.sun.jbi.defaultLogLevel", "INFO"));
080:
081: if (logLevel.intValue() >= Level.INFO.intValue()) {
082: //this is the normal setting:
083: mylogger
084: .setMessageOutputLevel(org.apache.tools.ant.Project.MSG_WARN);
085: } else if (logLevel.intValue() >= Level.FINE.intValue()) {
086: mylogger
087: .setMessageOutputLevel(org.apache.tools.ant.Project.MSG_INFO);
088: } else {
089: mylogger
090: .setMessageOutputLevel(org.apache.tools.ant.Project.MSG_VERBOSE);
091: }
092:
093: //you must set these out, err!!
094: mylogger.setOutputPrintStream(System.out);
095: mylogger.setErrorPrintStream(System.err);
096: mylogger.setEmacsMode(true);
097:
098: project.addBuildListener(mylogger);
099:
100: //TODO: read in a set of properties, and set each one
101: project.setUserProperty("fooprop", "foovalue");
102:
103: try {
104: project.executeTarget(targetname);
105: } catch (Exception e) {
106: System.out.println("AntScriptRunner: ERROR: "
107: + e.getMessage());
108: taskstatus = false;
109: }
110:
111: return taskstatus;
112: }
113: }
|