Package com.zaranux.os.client.commands

Source Code of com.zaranux.os.client.commands.run

package com.zaranux.os.client.commands;

import com.zaranux.client.api.AsyncCallback;
import com.zaranux.client.java.io.File;
import com.zaranux.client.java.io.FileInputStream;
import com.zaranux.os.client.programs.ThirdPartyApp;

import com.zaranux.client.java.util.Properties;
import com.zaranux.os.client.core.Program;
import com.zaranux.os.client.programs.util.TypedFile;
import com.zaranux.os.client.util.Log;

public class run extends Command {

  String path;
  public run(String path)
  {
    this.path = path;
  }
  @Override
  protected void execute(final AsyncCallback<Boolean> callback) {

    final String absolutePath = getAbsolutePath(path);
    File f = new File(absolutePath);
    f.isFile(new AsyncCallback<Boolean>()
          {
            public void onSuccess(Boolean b)
            {
              if(b)
              {
                Log.debug(path + " exists!");
                executeFile(absolutePath,callback);
                //callback.onSuccess(true); // return immediately, files will run in background
              }else
              {
                Log.debug(path + " does not exist!");
                // looks like domain name
                if(path.indexOf('.') >)
                {
                  //and is not sub domain of zaranux.com (TODO: imporve this search, zaranux.com maybe in other parts ) for security reason
                  if(path.toLowerCase().indexOf("zaranux.com" ) < 0)
                  {
                    String url = "";
                    if((path.startsWith("http://") || path.startsWith("https://")) )
                    {
                      url = path;
                    }else
                    {
                      url = "http://" + path;
                    }
                    Log.debug("spliting " +url);
                    String[] args = url.split("[\\s]+");
                    Log.debug("Len : " + args.length);
                    ThirdPartyApp tpa = new ThirdPartyApp(args);
                    run(tpa,true);
                    callback.onSuccess(true);
                  }else
                  {
                    System.out.print("For security reason cannot run from zaranu.com : " + path , callback);
                  }
                 
                }else
                {
                  System.out.print(path + ": command not found",callback);
                }
              }
             
            }
            public void onFailure(Throwable t)
            {
              System.out.print("Failure: " + t ,callback);
            }
          }
          );
  }
 
  private void executeFile(String absolutePath, AsyncCallback<Boolean> callback)
  {
    TypedFile tf = new TypedFile(absolutePath);
      if(tf.isProperites())
      {
        listProperties(absolutePath, callback);
      }
      else if(tf.isExecutable())
      {
        String[] args = new String[1];
        args[0] = absolutePath;
        Log.debug("going to play");

        Program program = tf.getProgram();
        if(program != null)
        {
          // run in the background do not waith for callback call just return success
          // make inti its parent so even if the terminal closes it stays up
          run(program, true);
          callback.onSuccess(true);
         
        }else
        {
          System.out.print(absolutePath + ": failed to get the associated program.",callback);

        }
      }else
      {
        System.out.print(absolutePath + ": is not executiable.",callback);
      }
  }
 
  private void listProperties(String absolutePath, final AsyncCallback<Boolean> callback)
  {
    FileInputStream fis = new FileInputStream(absolutePath);
   
    final Properties properties = new Properties();
    properties.load(fis, new AsyncCallback<Boolean>()
        {
        public void onSuccess(Boolean b)
        {
          properties.list(System.out, callback);
        }
        public void onFailure(Throwable t)
        {
          callback.onFailure(t);
        }
        });
  }
 
}
TOP

Related Classes of com.zaranux.os.client.commands.run

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.