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

📄 javaapplicationtype.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/* HEADER  */
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 JavaApplicationType implements ApplicationType {

	protected ApplicationLauncherEvents events;

	protected ApplicationLauncher launcher;

	private String classpath = "";

	private String mainclass;

	private File workingDir;

	private String[] jvm;

	private Vector programArgs = new Vector();

	private Vector jvmArgs = new Vector();

	private ProcessMonitor process;
	
	private String javaLibraryPath = "";

	/*
	 * (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 {
		
		if(events!=null)
			events.debug("Processing <" + element.getName() + "> for java application type");

		this.launcher = launcher;
		this.events = events;

		if (element.getName().equals("java")) {

			String jre = (String) element.getAttribute("jre");

			if (events != null)
				events
						.debug("Checking our version against the required application version "
								+ jre);

			if (!ApplicationLauncher.checkVersion(jre)) {
				throw new IOException(
						"Application requires Java Runtime Environment " + jre);
			}

			/**
			 * LDP - Don't reset the classpath as this stops extended 
			 * extensions (such as the agent extension itself) from 
			 * adding addtional classpath entries.
			 */
			//Reset the classpath
			//classpath = "";

			if (System.getProperty("java.version").startsWith("1.1")
					&& !System.getProperty("java.vendor").startsWith(
							"Microsoft"))
				classpath = System.getProperty("java.home")
						+ File.pathSeparator + "lib" + File.pathSeparator
						+ "classes.zip";

			Enumeration e = element.enumerateChildren();

			while (e.hasMoreElements()) {
				XMLElement el = (XMLElement) e.nextElement();

				if (el.getName().equalsIgnoreCase("classpath")) {
					buildClassPath(el);
				} else if (el.getName().equalsIgnoreCase("main")) {
					mainclass = (String) el.getAttribute("class");
					if (events != null)
						events.debug("Main class is " + mainclass);
					String dir = (String) el.getAttribute("dir");
					if (events != null)
						events.debug("Dir is " + dir);
					if (dir != null) {
						workingDir = new File(launcher.replaceTokens(dir));
					} else {
						workingDir = null;
					}
					buildProgramArguments(el);
				}
			}
			
			if (events != null)
				events.debug("Finished preparing application descriptor.");
		} else {
			if(events!=null)
				events.debug("Ignoring <" + element.getName() + "> tag as it is not a java application tag");
		}
			
	}

	public void start() {
		execute(classpath, mainclass, workingDir);
	}

	public void addClasspathEntry(XMLElement e) throws IOException {
		addClasspathEntry(e, null);
	}

	public void addClasspathEntry(XMLElement e, String app) throws IOException {

		String shared = (String) e.getAttribute("shared");
		File entry;
		if (shared != null && shared.equalsIgnoreCase("true")) {
			entry = launcher.addShared(e);
		} else {
			entry = launcher.addFile(e, app);
		}

		if (entry != null && events != null)
			events.debug("Adding " + entry.getAbsolutePath() + " to CLASSPATH");
		
		// The entry may be null because were not the correct platform
		if (entry != null)
			classpath += (!classpath.equals("") ? File.pathSeparator : "")
					+ entry.getAbsolutePath();
	}

	protected void buildClassPath(XMLElement element) throws IOException {
		buildClassPath(element, null);
	}

	protected void buildClassPath(XMLElement element, String app)
			throws IOException {

		if (events != null)
			events.debug("Building classpath");
		Enumeration en = element.enumerateChildren();
		XMLElement e;

		while (en.hasMoreElements()) {
			e = (XMLElement) en.nextElement();
			if (e.getName().equalsIgnoreCase("jar")) {
				addClasspathEntry(e, app);
			} else if (e.getName().equals("if")) {

				String jre = (String) e.getAttribute("jre");
				if (jre == null) {
					String parameter = (String) e.getAttribute("parameter");

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

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

						if ((!not && requiredValue.equalsIgnoreCase(value))
								|| (not && !requiredValue
										.equalsIgnoreCase(value))) {
							buildClassPath(e, app);
						}

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

					if (isSupportedJRE(jre)) {
						buildClassPath(e, app);
					}
				}
			} else
				throw new IOException("Invalid element <" + e.getName()
						+ "> found in <classpath>");
		}

	}

	private boolean isSupportedJRE(String jre) {

		int[] ourVersion = ApplicationLauncher.getVersion(System
				.getProperty("java.version"));

		if (jre.startsWith(">")) {

			// Our JRE must be greater than the value specified
			int[] requiredVersion = ApplicationLauncher.getVersion(jre
					.substring(1));
			for (int i = 0; i < ourVersion.length && i < requiredVersion.length; i++) {
				if (ourVersion[i] < requiredVersion[i])
					return false;
			}
			return true;

		} else if (jre.startsWith("<")) {
			// Our JRE must be less than the value specified
			int[] requiredVersion = ApplicationLauncher.getVersion(jre
					.substring(1));
			for (int i = 0; i < ourVersion.length && i < requiredVersion.length; i++) {
				if (ourVersion[i] > requiredVersion[i])
					return false;
			}
			return true;

		} else {
			// Direct comparison
			int[] requiredVersion = ApplicationLauncher.getVersion(jre);
			for (int i = 0; i < ourVersion.length && i < requiredVersion.length; i++) {
				if (ourVersion[i] != requiredVersion[i])
					return false;
			}
			return true;

		}

	}

	protected void addArgument(String arg) {
		if(arg!=null)
		   programArgs.addElement(launcher.replaceTokens(arg));
	}

	protected void addJVMArgument(String arg) {
		if(arg!=null) {
			
		   if(arg.startsWith("java.library.path")) {
			 int idx = arg.indexOf('=');
			 
			 if(idx > -1) {
				 String val = arg.substring(idx+1).replace('/', File.separatorChar);
				 javaLibraryPath += (javaLibraryPath.equals("") ? val : System.getProperty("path.separator") + val);

				 if(events!=null)
					 events.debug(val + " has been appened to system property java.library.path");
			 } else if(events!=null)
				 events.debug("Invalid java.library.path system property: " + arg);
				 
			   
		   } else
			 jvmArgs.addElement(launcher.replaceTokens(arg));
		}
	}

	private void addArgument(XMLElement e) throws IOException {
		if (e.getName().equalsIgnoreCase("arg"))
			addArgument(e.getContent());
		else if (e.getName().equalsIgnoreCase("jvm")) {
			addJVMArgument(e.getContent());
		} 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("jvm")) {
				addArgument(e);
			} else if (e.getName().equalsIgnoreCase("if")) {

				String jre = (String) e.getAttribute("jre");
				if (jre == null) {
					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 jre or parameter attribute");
				} else {
					// Check the jre
					if (isSupportedJRE(jre)) {
						buildProgramArguments(e);
					}

				}

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

	}

	private void execute(String classpath, String[] args, String mainclass) {
		execute(classpath, mainclass, null);
	}

	private void execute(String classpath, String mainclass, File workingDir) {

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

		if(!javaLibraryPath.equals(""))
			jvmArgs.addElement("java.library.path=" + launcher.replaceTokens(javaLibraryPath));
		
		jvm = new String[jvmArgs.size()];
		jvmArgs.copyInto(jvm);

		String[] cmdargs = new String[jvm.length + args.length + 4];

		if (!System.getProperty("java.vendor").startsWith("Microsoft")) {
			/**
			 * Setup the command line in the format expected by Sun Microsystems
			 * java command line interpreter
			 */
			cmdargs[0] = System.getProperty("java.home") + File.separator
					+ "bin" + File.separator + "java";
			cmdargs[1] = "-classpath";
			cmdargs[2] = classpath;

			for (int i = 0; i < jvm.length; i++) {
				cmdargs[3 + i] = "-D" + jvm[i];
			}

			cmdargs[jvm.length + 3] = mainclass;

			System.arraycopy(args, 0, cmdargs, jvm.length + 4, args.length);

		} else {
			/**
			 * Were using the Microsoft VM. This requires quotes around any
			 * arguments and different command line syntax
			 */
			cmdargs[0] = "jview.exe";
			cmdargs[1] = "/cp";
			cmdargs[2] = "\"" + classpath + "\"";

			for (int i = 0; i < jvm.length; i++) {
				cmdargs[3 + i] = "\"/d:" + jvm[i] + "\"";
			}

			cmdargs[jvm.length + 3] = mainclass;

			for (int i = 0; i < args.length; i++) {
				if (args[i].indexOf(' ') > -1)
					cmdargs[4 + i + jvm.length] = "\"" + args[i] + "\"";
				else
					cmdargs[4 + i + jvm.length] = args[i];
			}

		}

		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 prc = (Process) m.invoke(Runtime.getRuntime(),
						new Object[] { cmdargs, null, workingDir });
				process = new ProcessMonitor(launcher.getName(), prc);
			} catch (Throwable t) {
				if (workingDir != null) {
					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 {
		String jre = (String) el.getAttribute("jre");
		if (jre == null) {
			throw new IllegalArgumentException(
					"No supported attributes in condition.");
		} else {
			return isSupportedJRE(jre);
		}
	}

	/*
	 * (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 + -