package com.zaranux.os.client.commands;
import com.zaranux.client.api.AsyncCallback;
import com.zaranux.client.java.io.PrintStream;
import com.zaranux.client.org.apache.commons.cli.CommandLine;
import com.zaranux.client.org.apache.commons.cli.CommandLineParser;
import com.zaranux.client.org.apache.commons.cli.HelpFormatter;
import com.zaranux.client.org.apache.commons.cli.Options;
import com.zaranux.client.org.apache.commons.cli.ParseException;
import com.zaranux.client.org.apache.commons.cli.PosixParser;
import com.zaranux.os.client.core.Program;
import com.zaranux.os.client.programs.shell;
import com.zaranux.os.client.util.Log;
public abstract class Command extends Program {
// create Options object
protected Options options = new Options();
protected CommandLine cmd;
// is there any redirection?
private boolean _redirect_in = false;
private boolean _redirect_out = false;
public void setArgs(String args)
{
this.args = args.split("[\\s]+");
}
@Override
protected final void main(String[] args) {
try
{
CommandLineParser parser = new PosixParser();
cmd = parser.parse( options, args,true);
execute( new AsyncCallback<Boolean>()
{
public void onSuccess(Boolean b) // ture, got user's credential, false user cancelled, ...
{
Log.debug("ending command " + this.getClass().getName());
end();
callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
end();
callback.onFailure(t);
}
});
}
catch(ParseException e)
{
//callback.onFailure(e);
}
}
protected String getAbsolutePath(String relativePath)
{
if(relativePath.startsWith("file://") || relativePath.startsWith("/") )
{
return relativePath; // it is already absoulute
}else
{
return getCurrentDirectory() + relativePath;
}
}
// ensures that always ends with /
protected String getCurrentDirectory()
{
String currentDirectory = getEnvironmentVariables().getValue("PWD");
if(!currentDirectory.endsWith("/"))
currentDirectory += "/";
return currentDirectory;
}
// ensures that always ends with /
protected String getHomeDirectory()
{
String home = getEnvironmentVariables().getValue("HOME");
if(!home.endsWith("/"))
home += "/";
return home;
}
protected void addOption(String opt, boolean hasArg, String description)
{
options.addOption(opt, hasArg, description);
}
protected boolean hasOption(String option)
{
if(cmd == null) return false;
return cmd.hasOption(option);
}
protected String[] getArgs()
{
if(cmd != null)
return cmd.getArgs();
return null;
}
protected void printHelp(AsyncCallback<Boolean> callback)
{
if(! (System.out instanceof PrintStream) )
{
System.out = new PrintStream(System.out, true);
}
final PrintStream outPS = (PrintStream) System.out;
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(outPS,150,"cmd", "help", options,9,9,"footer",true, callback);
}
protected void reportError(String errMsg, AsyncCallback<Boolean> callback)
{
if(System.err == null)
{
System.err.println(errMsg,new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
}
public void onFailure(Throwable t)
{
}
});
}
callback.onFailure(new Exception(errMsg));
}
private AsyncCallback<Boolean> callback;
public void executed(shell s, final AsyncCallback<Boolean> callback)
{
this.callback = callback;
s.execute((Program) this);
}
protected abstract void execute( final AsyncCallback<Boolean> callback);
public final void finish(final AsyncCallback<Boolean> callback)
{
close_in(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
close_out(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
close_err(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
}
private void close_in(final AsyncCallback<Boolean> callback)
{
if (System.in != null)
{
System.out.flush(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
if (_redirect_in)
System.in.close(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
else
callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
}
}
private void close_out(final AsyncCallback<Boolean> callback)
{
if (System.out != null)
{
System.out.flush(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
if (_redirect_out)
System.out.close(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
else
callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
}
}
private void close_err(final AsyncCallback<Boolean> callback)
{
if (System.err != null)
{
System.err.flush(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
if (_redirect_out)
System.err.close(new AsyncCallback<Boolean>(){
public void onSuccess(Boolean b)
{
callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
else
callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
callback.onFailure(t);
}
});
}
}
}