cvsrunner.java

来自「很棒的web服务器源代码」· Java 代码 · 共 1,029 行 · 第 1/3 页

JAVA
1,029
字号
// CvsRunner.java// $Id: CvsRunner.java,v 1.34 2003/06/04 09:06:55 ylafon Exp $  // (c) COPYRIGHT MIT and INRIA, 1997.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.cvs;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.FilterInputStream;import java.io.FilterOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintStream;import java.io.Reader;import java.util.NoSuchElementException;import java.util.StringTokenizer;//// FIXME add extra environment parameter to all public methods// witch run cvs.//class CvsRunner implements CVS {//    private static final File tmpdir = new File("/tmp");    public static boolean debug = true;    /**     * Dump the given string into a temporary file.     * This is used for th <code>-f</code> argument of the cvs commit command.     * This method should only be used from a synchronized method.     * @param string The string to dump.     */    File getTemporaryFile (String string) 	throws CvsException    {	// Create a pseudo-random temporary filename	String fn = "cvs-" + System.currentTimeMillis()+"-"+string.length();	File temp = null;	try {	    temp = File.createTempFile (fn, "-"+string.length()) ;	    temp.deleteOnExit();	    PrintStream out  = new PrintStream (new FileOutputStream(temp)) ;	    out.print(string) ;	    out.close() ;	    return temp ;	} catch (IOException ex) {	    error ("temporaryFile"		   , "unable to create/use temporary file: " 		   + temp.getAbsolutePath()) ;	}	return temp ;    }    /**     * Emit an error.     * Some abnormal situation occured, emit an error message.     * @param mth The method in which the error occured.     * @param msg The message to emit.     * @exception CvsException The exception that will be thrown as a      *     result of the error.     */    protected void error (String mth, String msg) 	throws CvsException    {	String emsg = this.getClass().getName()+"["+mth+"]: "+msg ;	System.err.println(emsg);	throw new CvsException (emsg) ;    }    /**     * Read the given input stream as a text.     * @param in The input stream to read from.     * @param into The StringBuffer to fill.     * @return The provided StringBuffer, filled with the stream content.     */    private StringBuffer readText(InputStream procin, StringBuffer into) 	throws IOException     {	DataInputStream in   = new DataInputStream(procin) ;	String          line = null ;	while ((line = in.readLine()) != null) {	    if ( into != null )		into.append (line+"\n") ;	}	return into ;    }    /**     * Get a filename between quote contained in this String.     * @return the filename of null.     */    protected String getQuotedFilename(String line) {	int idx = line.indexOf('\'');	if (idx == -1)	    return null;	char ch;	StringBuffer buffer = new StringBuffer();	try {	    while ( (ch = line.charAt(idx++)) != '\'' )		buffer.append(ch);	} catch (ArrayIndexOutOfBoundsException ex) {	}	return buffer.toString();    }    /**     * Parse the error message and throw an exception if necessary.     * @exception UpToDateCheckFailedException if file was not up to date.     * @exception CvsAddException if an error ocurs during add.     * @exception IOException if an IO error occurs.     */    protected StringBuffer readError(InputStream procin, StringBuffer into) 	throws IOException, CvsException    {	DataInputStream in   = new DataInputStream(procin) ;	String          line = null ;	while ((line = in.readLine()) != null) {	    if ( into != null )		into.append (line+"\n") ;	    if ( line.startsWith("cvs commit:") ) {		line = line.substring(12);		if ( line.startsWith("Up-to-date check failed") ) {		    String filename = getQuotedFilename(line);		    throw new UpToDateCheckFailedException(filename);		} else {		    throw new CvsCommitException(line);		}	    } else if ( line.startsWith("cvs add:") ) {		throw new CvsAddException(line.substring(8));	    } else if ( line.startsWith("cvs update:") ) {		throw new CvsUpdateException(line.substring(11));	    }	}	return into ;    }    /**     * Wait for the underlying CVS process to finish.     * Once the process is terminated, all relevant streams are closed, and     * an exception if potentially thrown if the process indicated failure.     * @param proc The CVS process.     * @param ccode Should we expect a zero status from the child process.     * @exception CvsException If a zero status is expected, and the CVS     * process exit status is not zero.     */    protected synchronized void waitForCompletion (Process proc, boolean ccode)	throws CvsException     {	while ( true ) {	    try {		CvsException exception = null;		// Try reading the error stream:		StringBuffer errorlog = new StringBuffer() ;		try {		    errorlog = readError(proc.getErrorStream(), errorlog) ;		} catch (IOException ex) {		    ex.printStackTrace();		} catch (CvsException cvs_ex) {		    exception = cvs_ex;		} finally {		    // Close all streams, just to make sure...		    try { proc.getInputStream().close(); } 		    catch (Exception ex) {}		    try { proc.getOutputStream().close(); } 		    catch (Exception ex) {}		    try { proc.getErrorStream().close(); } 		    catch (Exception ex) {}		    // Check ecode if requested to do so:		    proc.waitFor() ;		}		//no exception thrown, that's an unknown exception.		int ecode = proc.exitValue() ;		if ( ecode != 0 ) {		    String msg = ("Process exited with error code: " + ecode				  + " error ["+errorlog+"]");		    if ( debug )			System.err.println(msg) ;		    if ( ccode ) {			if (exception == null)			    throw new CvsException (msg) ;			else			    throw exception;		    }		} 		return ;	    } catch (InterruptedException e) {	    }	}    }    /**     * Run a cvs command, return the process object.     * @param args The command to run.     * @exception IOException If the process couldn't be launched.     */    protected Process runCvsProcess (String args[]) 	throws IOException    {	if (debug) {	    for (int i = 0 ; i < args.length ; i++)		System.out.print(args[i]+" ");	    System.out.println();	}	return Runtime.getRuntime().exec(args) ;    }    /**     * Run a cvs command, return the process object.     * @param args The command to run.     * @param env The extra environment.     * @exception IOException If the process couldn't be launched.     */    protected Process runCvsProcess (String args[], String env[]) 	throws IOException    {	if (debug) {	    for (int i = 0 ; i < args.length ; i++)		System.out.print(args[i]+" ");	    System.out.println();	}	return Runtime.getRuntime().exec(args,env) ;    }    /**     * Get the CVS command String array.     * @param cvs The target CvsDirectory in which the command is to be run.     * @param cvsopts The CVS wide options.     * @param cmdopts The command, and its optional options.     * @return A String array, giving the command to be executed.     */    protected String[] getCommand(CvsDirectory cvs				  , String cvsopts[]				  , String cmdopts[]) {	String cvswrapper[] = cvs.getCvsWrapper();	String cvsdefs[]    = cvs.getCvsDefaults();	String cmd[] = new String[cvswrapper.length				 + cvsdefs.length				 + ((cvsopts != null) ? cvsopts.length : 0)				 + ((cmdopts != null) ? cmdopts.length : 0)];	int cmdptr   = 0;	// Copy everything into the command:	if ( cvswrapper != null ) {	    for (int i = 0 ; i < cvswrapper.length; i++)		cmd[cmdptr++] = cvswrapper[i];	}	if ( cvsdefs != null ) {	    for (int i = 0 ; i < cvsdefs.length ; i++)		cmd[cmdptr++] = cvsdefs[i];	}	if ( cvsopts != null ) {	    for (int i = 0; i < cvsopts.length; i++)		cmd[cmdptr++] = cvsopts[i];	}	if ( cmdopts != null ) {	    for (int i = 0 ; i < cmdopts.length ; i++)		cmd[cmdptr++] = cmdopts[i];	}	return cmd;    }//     private void parseUpdateDirectoriesOutput(InputStream procin, // 					      UpdateHandler handler)// 	throws IOException, CvsException//     {// 	DataInputStream in   = new DataInputStream (procin) ;// 	String          line = null ;// 	while ((line = in.readLine()) != null) {// 	    // Make sure the line isn't empty:// 	    if ( line.length() <= 0 )// 		continue;// 	    System.out.println("READING : "+line);// 	    if ( line.startsWith("cvs update:") ) {// 		line = line.substring(13);// 		if ( line.startsWith("Updating") ) {// 		    String dirname = line.substring(9);// 		    if ( dirname.equals(".") )// 			continue;// 		    System.out.println("Found Ckecked out : "+dirname);// 		    //handler.notifyEntry(dirname, DIR_CO);// 		} else if ( line.startsWith("New directory") ) {// 		    int idx = 15;// 		    char ch;// 		    StringBuffer buffer = new StringBuffer();// 		    while ( (ch = line.charAt(idx++)) != '\'' ) {// 			buffer.append(ch);// 		    }// 		    String dirname = buffer.toString();// 		    System.out.println("Found UnCkecked out : "+dirname);// 		    //handler.notifyEntry(dirname, DIR_NCO);// 		}// 	    }// 	}//     }    /**     * Parse the CVS output for the update command.     * @param procin The CVS process output.     * @param handler The handler to callback.     * @exception IOException If IO error occurs.     * @exception CvsException If the CVS process failed.     */    private void parseUpdateOutput (InputStream procin, UpdateHandler handler) 	throws IOException, CvsException    {	DataInputStream in   = new DataInputStream (procin) ;	String          line = null ;	while ((line = in.readLine()) != null) {	    // Make sure the line isn't empty:	    if ( line.length() <= 0 )		continue;	    int status = -1 ;	    int ch     = line.charAt(0) ;	    // Skip CVS verbose:	    if ( line.charAt(1) != ' ' )		continue;	    // Parse the status:	    switch (ch) {	    case 'U': status = FILE_OK; break ;	    case 'A': status = FILE_A ; break ;	    case 'R': status = FILE_R ; break ;	    case 'M': status = FILE_M ; break ;	    case 'C': status = FILE_C ; break ;	    case '?': status = FILE_Q ; break ;	    default:		// We just ignore this right now.		continue ;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?