⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 executableapplicationtype.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.vpn.util.types;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Vector;

import com.sslexplorer.vpn.util.ApplicationLauncher;
import com.sslexplorer.vpn.util.ApplicationLauncherEvents;
import com.sslexplorer.vpn.util.ApplicationType;
import com.sslexplorer.vpn.util.ProcessMonitor;
import com.sslexplorer.vpn.util.XMLElement;

public class ExecutableApplicationType implements ApplicationType {

  private ApplicationLauncherEvents events;
  private ApplicationLauncher launcher;
  private String program;
  private File workingDir;
  private Vector programArgs = new Vector();
  private Vector jvmArgs = new Vector();
  private ProcessMonitor process;

  /*
   * (non-Javadoc)
   *
   * @see com.sslexplorer.vpn.util.ApplicationType#prepare(com.sslexplorer.vpn.util.ApplicationLauncher,
   *      com.sslexplorer.vpn.util.XMLElement)
   */
  public void prepare(ApplicationLauncher launcher, ApplicationLauncherEvents events, XMLElement element) throws IOException {
    this.launcher = launcher;
    this.events = events;

    if (element.getName().equalsIgnoreCase("executable")) {
      program = launcher.replaceTokens((String)element.getAttribute("program"));
      String dir = (String) element.getAttribute("dir");
      if (dir != null) {
        workingDir = new File(launcher.replaceTokens(dir));
      } else {
        workingDir = null;
      }
      buildProgramArguments(element);
    }
  }

  public void start() {
    execute(program, workingDir);
  }

  private void addArgument(XMLElement e) throws IOException {
    if (e.getName().equalsIgnoreCase("arg")) {

      String arg = launcher.replaceTokens(e.getContent());
      if(arg.indexOf(' ') > -1)
        arg = "\"" + arg + "\"";
      programArgs.addElement(arg);
    }
    else {
      throw new IOException("Unexpected element <" + e.getName() + "> found");
    }
  }

  private void buildProgramArguments(XMLElement element) throws IOException {

    Enumeration en = element.enumerateChildren();

    while (en.hasMoreElements()) {

      XMLElement e = (XMLElement) en.nextElement();
      if (e.getName().equalsIgnoreCase("arg"))
        addArgument(e);
      else if (e.getName().equalsIgnoreCase("if")) {

        try {
          if (checkFileCondition(e)) {
            buildProgramArguments(e);
          }
        } catch (IllegalArgumentException iae) {

          String parameter = (String) e.getAttribute("parameter");
          boolean not = "true".equalsIgnoreCase(((String)e.getAttribute("not")));

          if (parameter != null) {
            String requiredValue = (String) e.getAttribute("value");

            // Check the parameter
            String value = (String) launcher.getDescriptorParams().get(parameter);

            if ((!not && requiredValue.equalsIgnoreCase(value)) || (not && !requiredValue.equalsIgnoreCase(value)) ) {
              buildProgramArguments(e);
            }

          } else
            throw new IOException("<if> element requires parameter attribute");
        }

      } else
        throw new IOException("Unexpected element <" + e.getName() + "> found in <executable>");
    }

  }

  private void execute(String program, File workingDir) {

    String[] args = new String[programArgs.size()];
    programArgs.copyInto(args);

    // LDP - Look for the program in our working dir.. looks like we need to specify
    // this fully in order for it to be executed properly. Windows will not search
    // the working directory for the executable file!!
    File tmp = new File(workingDir!=null ?
                        workingDir.getAbsolutePath()
                        : launcher.getInstallDir().getAbsolutePath(), program);
    if(tmp.exists())
      program = tmp.getAbsolutePath();

    String[] cmdargs = new String[args.length + 1];
    System.arraycopy(args, 0, cmdargs, 1, args.length);
    if(program.indexOf(' ') > -1)
      program = "\"" + program + "\"";
    cmdargs[0] = program;

    String cmdline = "";
    for (int i = 0; i < cmdargs.length; i++)
      cmdline += " " + cmdargs[i];

    if (events != null)
      events.debug("Executing command: " + cmdline);

    try {

      if (events != null)
        events.executingApplication(launcher.getName(), cmdline.trim());

      // Can we change the working directory of the process?
      try {
        Method m = Runtime.class.getMethod("exec", new Class[]{String[].class, String[].class, File.class});
        process = new ProcessMonitor(launcher.getName(),
              (Process)m.invoke(Runtime.getRuntime(), new Object[]{cmdargs, null, workingDir}));
      } catch (Throwable t) {
        if (workingDir != null) {
            t.printStackTrace();

            // Try cmd.exe on windows

          throw new IOException("Application requires that the working directory be "
              + "changed, but this Java Virtual Machine does not support that.");
        }
        process = new ProcessMonitor(launcher.getName(), Runtime.getRuntime().exec(cmdargs));
      }
    } catch (IOException ex) {
      if (events != null)
        events.debug("Process execution failed: " + ex.getMessage());
    }

  }

  /*
   * (non-Javadoc)
   *
   * @see com.sslexplorer.vpn.util.ApplicationType#checkFileCondition(com.sslexplorer.vpn.util.XMLElement)
   */
  public boolean checkFileCondition(XMLElement el) throws IOException, IllegalArgumentException {
    throw new IllegalArgumentException("No supported attributes in condition.");
  }

  /*
   * (non-Javadoc)
   *
   * @see com.sslexplorer.vpn.util.ApplicationType#getProcessMonitor()
   */
  public ProcessMonitor getProcessMonitor() {
    return process;
  }

  /* (non-Javadoc)
   * @see com.sslexplorer.vpn.util.ApplicationType#getRedirectParameters()
   */
  public String getRedirectParameters() {
    return null;
  }

}

⌨️ 快捷键说明

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