📄 abstractcvstask.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;import java.util.Vector;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.apache.tools.ant.types.Commandline;import org.apache.tools.ant.types.Environment;import org.apache.tools.ant.util.StringUtils;import org.apache.tools.ant.util.FileUtils;/** * original Cvs.java 1.20 * * NOTE: This implementation has been moved here from Cvs.java with * the addition of some accessors for extensibility. Another task * can extend this with some customized output processing. * * @since Ant 1.5 */public abstract class AbstractCvsTask extends Task { /** * Default compression level to use, if compression is enabled via * setCompression( true ). */ public static final int DEFAULT_COMPRESSION_LEVEL = 3; private static final int MAXIMUM_COMRESSION_LEVEL = 9; private Commandline cmd = new Commandline(); /** list of Commandline children */ private Vector vecCommandlines = new Vector(); /** * the CVSROOT variable. */ private String cvsRoot; /** * the CVS_RSH variable. */ private String cvsRsh; /** * the package/module to check out. */ private String cvsPackage; /** * the tag */ private String tag; /** * the default command. */ private static final String DEFAULT_COMMAND = "checkout"; /** * the CVS command to execute. */ private String command = null; /** * suppress information messages. */ private boolean quiet = false; /** * suppress all messages. */ private boolean reallyquiet = false; /** * compression level to use. */ private int compression = 0; /** * report only, don't change any files. */ private boolean noexec = false; /** * CVS port */ private int port = 0; /** * CVS password file */ private File passFile = null; /** * the directory where the checked out files should be placed. */ private File dest; /** whether or not to append stdout/stderr to existing files */ private boolean append = false; /** * the file to direct standard output from the command. */ private File output; /** * the file to direct standard error from the command. */ private File error; /** * If true it will stop the build if cvs exits with error. * Default is false. (Iulian) */ private boolean failOnError = false; /** * Create accessors for the following, to allow different handling of * the output. */ private ExecuteStreamHandler executeStreamHandler; private OutputStream outputStream; private OutputStream errorStream; /** empty no-arg constructor*/ public AbstractCvsTask() { super(); } /** * sets the handler * @param handler a handler able of processing the output and error streams from the cvs exe */ public void setExecuteStreamHandler(ExecuteStreamHandler handler) { this.executeStreamHandler = handler; } /** * find the handler and instantiate it if it does not exist yet * @return handler for output and error streams */ protected ExecuteStreamHandler getExecuteStreamHandler() { if (this.executeStreamHandler == null) { setExecuteStreamHandler(new PumpStreamHandler(getOutputStream(), getErrorStream())); } return this.executeStreamHandler; } /** * sets a stream to which the output from the cvs executable should be sent * @param outputStream stream to which the stdout from cvs should go */ protected void setOutputStream(OutputStream outputStream) { this.outputStream = outputStream; } /** * access the stream to which the stdout from cvs should go * if this stream has already been set, it will be returned * if the stream has not yet been set, if the attribute output * has been set, the output stream will go to the output file * otherwise the output will go to ant's logging system * @return output stream to which cvs' stdout should go to */ protected OutputStream getOutputStream() { if (this.outputStream == null) { if (output != null) { try { setOutputStream(new PrintStream( new BufferedOutputStream( new FileOutputStream(output .getPath(), append)))); } catch (IOException e) { throw new BuildException(e, getLocation()); } } else { setOutputStream(new LogOutputStream(this, Project.MSG_INFO)); } } return this.outputStream; } /** * sets a stream to which the stderr from the cvs exe should go * @param errorStream an output stream willing to process stderr */ protected void setErrorStream(OutputStream errorStream) { this.errorStream = errorStream; } /** * access the stream to which the stderr from cvs should go * if this stream has already been set, it will be returned * if the stream has not yet been set, if the attribute error * has been set, the output stream will go to the file denoted by the error attribute * otherwise the stderr output will go to ant's logging system * @return output stream to which cvs' stderr should go to */ protected OutputStream getErrorStream() { if (this.errorStream == null) { if (error != null) { try { setErrorStream(new PrintStream( new BufferedOutputStream( new FileOutputStream(error.getPath(), append)))); } catch (IOException e) { throw new BuildException(e, getLocation()); } } else { setErrorStream(new LogOutputStream(this, Project.MSG_WARN)); } } return this.errorStream; } /** * Sets up the environment for toExecute and then runs it. * @param toExecute the command line to execute * @throws BuildException if failonError is set to true and the cvs command fails */ protected void runCommand(Commandline toExecute) throws BuildException { // XXX: we should use JCVS (www.ice.com/JCVS) instead of // command line execution so that we don't rely on having // native CVS stuff around (SM) // We can't do it ourselves as jCVS is GPLed, a third party task // outside of jakarta repositories would be possible though (SB). Environment env = new Environment(); if (port > 0) { Environment.Variable var = new Environment.Variable(); var.setKey("CVS_CLIENT_PORT"); var.setValue(String.valueOf(port)); env.addVariable(var); } /** * Need a better cross platform integration with <cvspass>, so * use the same filename. */ if (passFile == null) { File defaultPassFile = new File( System.getProperty("cygwin.user.home", System.getProperty("user.home")) + File.separatorChar + ".cvspass"); if (defaultPassFile.exists()) { this.setPassfile(defaultPassFile); } } if (passFile != null) { if (passFile.isFile() && passFile.canRead()) { Environment.Variable var = new Environment.Variable(); var.setKey("CVS_PASSFILE"); var.setValue(String.valueOf(passFile)); env.addVariable(var); log("Using cvs passfile: " + String.valueOf(passFile), Project.MSG_VERBOSE); } else if (!passFile.canRead()) { log("cvs passfile: " + String.valueOf(passFile) + " ignored as it is not readable", Project.MSG_WARN); } else { log("cvs passfile: " + String.valueOf(passFile) + " ignored as it is not a file", Project.MSG_WARN); } } if (cvsRsh != null) { Environment.Variable var = new Environment.Variable(); var.setKey("CVS_RSH"); var.setValue(String.valueOf(cvsRsh)); env.addVariable(var); } // // Just call the getExecuteStreamHandler() and let it handle // the semantics of instantiation or retrieval. // Execute exe = new Execute(getExecuteStreamHandler(), null); exe.setAntRun(getProject()); if (dest == null) { dest = getProject().getBaseDir(); } if (!dest.exists()) { dest.mkdirs(); } exe.setWorkingDirectory(dest); exe.setCommandline(toExecute.getCommandline()); exe.setEnvironment(env.getVariables()); try { String actualCommandLine = executeToString(exe); log(actualCommandLine, Project.MSG_VERBOSE); int retCode = exe.execute(); log("retCode=" + retCode, Project.MSG_DEBUG); if (failOnError && Execute.isFailure(retCode)) { throw new BuildException("cvs exited with error code " + retCode + StringUtils.LINE_SEP + "Command line was [" + actualCommandLine + "]", getLocation()); } } catch (IOException e) { if (failOnError) { throw new BuildException(e, getLocation()); } log("Caught exception: " + e.getMessage(), Project.MSG_WARN); } catch (BuildException e) { if (failOnError) { throw(e); } Throwable t = e.getException(); if (t == null) { t = e; } log("Caught exception: " + t.getMessage(), Project.MSG_WARN); } catch (Exception e) { if (failOnError) { throw new BuildException(e, getLocation()); } log("Caught exception: " + e.getMessage(), Project.MSG_WARN); } } /** * do the work * @throws BuildException if failonerror is set to true and the * cvs command fails. */ public void execute() throws BuildException { String savedCommand = getCommand(); if (this.getCommand() == null && vecCommandlines.size() == 0) { // re-implement legacy behaviour: this.setCommand(AbstractCvsTask.DEFAULT_COMMAND); } String c = this.getCommand(); Commandline cloned = null; if (c != null) { cloned = (Commandline) cmd.clone(); cloned.createArgument(true).setLine(c); this.addConfiguredCommandline(cloned, true); } try { for (int i = 0; i < vecCommandlines.size(); i++) { this.runCommand((Commandline) vecCommandlines.elementAt(i)); } } finally { if (cloned != null) { removeCommandline(cloned); } setCommand(savedCommand); FileUtils.close(outputStream); FileUtils.close(errorStream); } } private String executeToString(Execute execute) { String cmdLine = Commandline.describeCommand(execute .getCommandline()); StringBuffer stringBuffer = removeCvsPassword(cmdLine); String newLine = StringUtils.LINE_SEP; String[] variableArray = execute.getEnvironment(); if (variableArray != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -