/*
*
* Perforce/JBuilder Opentool
* Copyright (C) 2001 David Freels
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package com.dafreels.opentools.command;
import com.dafreels.opentools.Main;
import com.dafreels.opentools.properties.*;
import com.borland.primetime.*;
import com.borland.primetime.actions.*;
import com.borland.primetime.node.*;
import com.borland.jbuilder.*;
import com.borland.primetime.ide.*;
import com.borland.primetime.properties.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company: DF Systems
* @author David Freels
* @version 1.0
*/
public class CommandTool
{
private static Process proc;
private static boolean _ready = false;
private static StringBuffer _output = new StringBuffer("");
private static boolean _writeOut = true;
public CommandTool()
{
}
private static String setCommandLine(String command, boolean debug)
{
//Get the Path to the P4 executable
StringBuffer commandLine = new StringBuffer(
PerforceGroup.P4EXECUTABLE.getValue(Browser.getActiveBrowser().getActiveProject()));
if(debug &&
(PerforceGroup.DEBUG.getValue(Browser.getActiveBrowser().getActiveProject()) != null
&& PerforceGroup.DEBUG.getValue(Browser.getActiveBrowser().getActiveProject()).equalsIgnoreCase("true")))
{
commandLine.append(" -v"
+PerforceGroup.DEBUGLEVEL.getValue(Browser.getActiveBrowser().getActiveProject()));
}
//Add the client spec
commandLine.append(" -c "+PerforceGroup.CLIENTSPEC.getValue(Browser.getActiveBrowser().getActiveProject()));
//Add the host information
commandLine.append(" -p "+PerforceGroup.PORT.getValue(Browser.getActiveBrowser().getActiveProject()));
//Add the command
commandLine.append(" "+command);
//Display the command
writeMessage(commandLine.toString());
return commandLine.toString();
}
public synchronized static void runCommand(String command)
{
runCommand(command, false);
}
public synchronized static void runCommand(String command, boolean useStdIn)
{
runCommand(command, useStdIn, true, false);
}
public synchronized static void runCommand(String command, boolean useStdIn,
boolean output, boolean debug)
{
_ready = false;
_writeOut = output;
String commandLine = setCommandLine(command, debug);
//Execute the command and trap the messages to be sent to the Message View
try
{
proc = Runtime.getRuntime().exec(commandLine);
_ready = true;
if(!useStdIn)
{
finishCommand();
}
} catch(java.io.IOException ioe)
{
System.out.println("[CommandTool] (runCommand)\n"+ioe.toString());
}
}
public static void writeToOut(String message)
{
writeMessage("ChangeList: ");
writeMessage(message);
try
{
java.io.BufferedWriter sw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(proc.getOutputStream()));
sw.write(message);
sw.flush();
sw.close();
finishCommand();
} catch(java.io.IOException ioe)
{
System.out.println("[CommandTool] (writeToOut)\n"+ioe.toString());
}
}
public static boolean isStreamReady()
{
return _ready;
}
public static void finishCommand()
{
//Hold the errors produced by the command
String errors = "";
//Hold messages produced by the command
String message = "";
try
{
//Create a thread to read the output stream and store the output
ProcessReader pr = new ProcessReader(proc.getInputStream());
new Thread(pr).start();
//Read the error stream
ProcessReader er = new ProcessReader(proc.getErrorStream());
new Thread(er).start();
//Wait for the process to complete
int code = proc.waitFor();
//Clean the output buffer
_output.delete(0, _output.length());
//Wait for the ProcessReaders to finish
while(!pr.isFinished() || !er.isFinished())
{
}
_output.append(pr.getProcessOutput());
errors = er.getProcessOutput();
message = pr.getProcessOutput();
if(code == 0)
{
message = pr.getProcessOutput();
_output.append(message);
writeMessage("The command produced the following messages:");
while(message.indexOf(System.getProperty("line.separator")) != -1)
{
writeMessage(message.substring(0, message.indexOf(System.getProperty("line.separator"))));
message = message.substring(message.indexOf(System.getProperty("line.separator")) + System.getProperty("line.separator").length());
}
writeMessage(message);
} else
{
errors = er.getProcessOutput();
_output.append(errors);
writeMessage("The command produced the following errors:");
while(errors.indexOf(System.getProperty("line.separator")) != -1)
{
writeMessage(errors.substring(0, errors.indexOf(System.getProperty("line.separator"))));
errors = errors.substring(errors.indexOf(System.getProperty("line.separator")) + System.getProperty("line.separator").length());
}
writeMessage(errors);
}
pr = null;
er = null;
proc.destroy();
System.gc();
} catch(InterruptedException ie)
{
System.out.println("[CommandTool] (finishCommand)"+System.getProperty("line.separator")+ie.toString());
}
}
public static String getOutput()
{
return _output.toString();
}
private static void writeMessage(String message)
{
if(_writeOut)
{
Browser.getActiveBrowser().getMessageView().addMessage(Main.PERFORCEMESSAGES,
new Message(message));
}
}
public static void refreshNodes(Node[] nodes)
{
for(int i = 0; i < nodes.length; i++)
{
((FileNode)nodes[i]).check();
}
System.gc();
}
}