Java源码示例:org.apache.tools.ant.types.Commandline

示例1
/**
 * Performs a compile using the JDT batch compiler
 * @throws BuildException if anything wrong happen during the compilation
 * @return boolean true if the compilation is ok, false otherwise
 */
public boolean execute() throws BuildException {
	this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.usingJDTCompiler"), Project.MSG_VERBOSE); //$NON-NLS-1$
	Commandline cmd = setupJavacCommand();

	try {
		Class c = Class.forName(compilerClass);
		Constructor batchCompilerConstructor = c.getConstructor(new Class[] { PrintWriter.class, PrintWriter.class, Boolean.TYPE, Map.class});
		Object batchCompilerInstance = batchCompilerConstructor.newInstance(new Object[] {new PrintWriter(System.out), new PrintWriter(System.err), Boolean.TRUE, this.customDefaultOptions});
		Method compile = c.getMethod("compile", new Class[] {String[].class}); //$NON-NLS-1$
		Object result = compile.invoke(batchCompilerInstance, new Object[] { cmd.getArguments()});
		final boolean resultValue = ((Boolean) result).booleanValue();
		if (!resultValue && this.logFileName != null) {
			this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.error.compilationFailed", this.logFileName)); //$NON-NLS-1$
		}
		return resultValue;
	} catch (ClassNotFoundException cnfe) {
		throw new BuildException(AntAdapterMessages.getString("ant.jdtadapter.error.cannotFindJDTCompiler")); //$NON-NLS-1$
	} catch (Exception ex) {
		throw new BuildException(ex);
	}
}
 
示例2
@Test
public void testExecuteHash() {
    int exitCode = BcryptTool.execute(CLIParser.parse(Commandline.translateCommandline("\"mySecretPw\" -b 8 8e270d6129fd45f30a9b3fe44b4a8d9a")), out, err);
    verify(out).println("$2a$08$hgaLWQl7PdKIkx9iQyoLkeuIqizWtPErpyC7aDBasi2Pav97wwW9G");
    verify(err, never()).println(any(String.class));
    assertEquals(0, exitCode);
}
 
示例3
@Test
public void testExecuteCheck() {
    int exitCode = BcryptTool.execute(CLIParser.parse(Commandline.translateCommandline("\"mySecretPw\" -c '$2a$08$hgaLWQl7PdKIkx9iQyoLkeuIqizWtPErpyC7aDBasi2Pav97wwW9G'")), out, err);
    verify(out).println(any(String.class));
    verify(err, never()).println(any(String.class));
    assertEquals(0, exitCode);
}
 
示例4
public static Map<String, String> parseBuildArgs(final Dockerfile dockerfile, final String commandLine) {
    // this also accounts for quote, escapes, ...
    Commandline parsed = new Commandline(commandLine);
    Map<String, String> result = new HashMap<>();
    if (dockerfile != null) {
        result.putAll(dockerfile.getArgs());
    }

    String[] arguments = parsed.getArguments();
    for (int i = 0; i < arguments.length; i++) {
        String arg = arguments[i];
        if (arg.equals("--build-arg")) {
            if (arguments.length <= i + 1) {
                throw new IllegalArgumentException("Missing parameter for --build-arg: " + commandLine);
            }
            String keyVal = arguments[i+1];

            String parts[] = keyVal.split("=", 2);
            if (parts.length != 2) {
                throw new IllegalArgumentException("Illegal syntax for --build-arg " + keyVal + ", need KEY=VALUE");
            }
            String key = parts[0];
            String value = parts[1];

            result.put(key, value);
        }
    }
    return result;
}
 
示例5
public static void main(String[] args) {
    final GroovyShell shell = new GroovyShell(new Binding());
    final Groovy groovy = new Groovy();
    for (int i = 1; i < args.length; i++) {
        final Commandline.Argument argument = groovy.createArg();
        argument.setValue(args[i]);
    }
    final AntBuilder builder = new AntBuilder();
    groovy.setProject(builder.getProject());
    groovy.parseAndRunScript(shell, null, null, null, new File(args[0]), builder);
}
 
示例6
private void createNewArgs(String txt) throws IOException {
    final String[] args = cmdline.getCommandline();
    // Temporary file - delete on exit, create (assured unique name).
    final File tempFile = FileUtils.getFileUtils().createTempFile(PREFIX, SUFFIX, null, true, true);
    final String[] commandline = new String[args.length + 1];
    ResourceGroovyMethods.write(tempFile, txt);
    commandline[0] = tempFile.getCanonicalPath();
    System.arraycopy(args, 0, commandline, 1, args.length);
    super.clearArgs();
    for (String arg : commandline) {
        final Commandline.Argument argument = super.createArg();
        argument.setValue(arg);
    }
}
 
示例7
private ProcessExecutor createProcessExecutor(File targetDir, DbProperties dbProperties) {
    ConnectionUrl connectionUrlInstance = ConnectionUrl.getConnectionUrlInstance(dbProperties.url(), dbProperties.connectionProperties());

    LinkedHashMap<String, String> env = new LinkedHashMap<>();
    if (isNotBlank(dbProperties.password())) {
        env.put("MYSQL_PWD", dbProperties.password());
    }
    // override with any user specified environment
    env.putAll(dbProperties.extraBackupEnv());

    ArrayList<String> argv = new ArrayList<>();
    argv.add("mysqldump");


    String dbName = connectionUrlInstance.getDatabase();
    HostInfo mainHost = connectionUrlInstance.getMainHost();

    if (mainHost != null) {
        argv.add("--host=" + mainHost.getHost());
        argv.add("--port=" + mainHost.getPort());
    }
    if (isNotBlank(dbProperties.user())) {
        argv.add("--user=" + dbProperties.user());
    }

    // append any user specified args for mysqldump
    if (isNotBlank(dbProperties.extraBackupCommandArgs())) {
        Collections.addAll(argv, Commandline.translateCommandline(dbProperties.extraBackupCommandArgs()));
    }

    argv.add("--result-file=" + new File(targetDir, "db." + dbName).toString());
    argv.add(connectionUrlInstance.getDatabase());

    ProcessExecutor processExecutor = new ProcessExecutor();
    processExecutor.redirectOutputAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.redirectErrorAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.environment(env);
    processExecutor.command(argv);
    return processExecutor;
}
 
示例8
ProcessExecutor createProcessExecutor(File targetDir, DbProperties dbProperties) {
    Properties connectionProperties = dbProperties.connectionProperties();
    Properties pgProperties = Driver.parseURL(dbProperties.url(), connectionProperties);

    ArrayList<String> argv = new ArrayList<>();
    LinkedHashMap<String, String> env = new LinkedHashMap<>();
    if (isNotBlank(dbProperties.password())) {
        env.put("PGPASSWORD", dbProperties.password());
    }

    // override with any user specified environment
    env.putAll(dbProperties.extraBackupEnv());

    String dbName = pgProperties.getProperty("PGDBNAME");
    argv.add("pg_dump");
    argv.add("--no-password");
    argv.add("--host=" + pgProperties.getProperty("PGHOST"));
    argv.add("--port=" + pgProperties.getProperty("PGPORT"));
    if (isNotBlank(dbProperties.user())) {
        argv.add("--username=" + dbProperties.user());
    }
    argv.add(pgProperties.getProperty("PGDBNAME"));
    // append any user specified args for pg_dump
    if (isNotBlank(dbProperties.extraBackupCommandArgs())) {
        Collections.addAll(argv, Commandline.translateCommandline(dbProperties.extraBackupCommandArgs()));
    }
    argv.add("--file=" + new File(targetDir, "db." + dbName).toString());
    ProcessExecutor processExecutor = new ProcessExecutor();
    processExecutor.redirectOutputAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.redirectErrorAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.environment(env);
    processExecutor.command(argv);
    return processExecutor;
}
 
示例9
public static String[] asArgArray(String cmd) {
    return Commandline.translateCommandline(cmd);
}
 
示例10
@Test
public void testCheckInvalidFormat() {
    assertEquals(3, BcryptTool.execute(CLIParser.parse(Commandline.translateCommandline("\"mySecretPw\" -c '$2$$08$hgaLWQl7PdKIkx9iQyoLkeuIqizWtPErpyC7aDBasi2Pav97wwW9G'")), out, err));
}
 
示例11
@Test
public void testCheckDoesNotVerify() {
    assertEquals(1, BcryptTool.execute(CLIParser.parse(Commandline.translateCommandline("\"mySecretPw\" -c '$2a$07$hgaLWQl7PdKIkx9iQyoLkeuIqizWtPErpyC7aDBasi2Pav97wwW9G'")), out, err));
}
 
示例12
@Option(description = "Set application arguments", option = "quarkus-args")
public void setArgsString(String argsString) {
    this.setArgs(Arrays.asList(Commandline.translateCommandline(argsString)));
}
 
示例13
public String prependVMArguments(final String arguments, final File agentJarFile) {

			CommandlineJava commandlineJava = new CommandlineJava() {
				@Override
				public void setVm(String vm) {
					//do not set "**/java" as the first command
				}
			};

			//add the javaagent
			commandlineJava.createVmArgument().setLine(format("-javaagent:%s", agentJarFile));

			//remove any javaagent with the same file name from the arguments
			final String[] args = Commandline.translateCommandline(arguments);
			// the new javaagent, as used by the prepare-vulas-agent goal
			final String regexForCurrentVulasAgent = format("-javaagent:(\"?)%s(\"?)", Pattern.quote(agentJarFile.toString()));
			// the default name of the legacy JAR
			final String regexForOldVulasAgent = format("-javaagent:(\"?).*%s(\"?)", Pattern.quote("/vulas/lib/vulas-core-latest-jar-with-dependencies.jar"));

			ArrayList<String> patterns = new ArrayList<>();
			patterns.add(regexForCurrentVulasAgent);
			patterns.add(regexForOldVulasAgent);
			// go through the arguments to check for existing javaagents
			argprocess: for (String arg : args) {

				// check if one of vulas's agents is already defined as an arg
				// if yes ignore the arg
				for (String regExExp : patterns) {
					if (arg.matches(regExExp)) {
						continue argprocess;
					}

				}
				commandlineJava.createArgument().setLine(arg);

			}
			
			//add my properties
			for (final String key : this.agentOptions.keySet()) {
				final String value = this.agentOptions.get(key);
				if (value != null && !value.isEmpty()) {
					Environment.Variable variable = new Environment.Variable();
					variable.setKey(key);
					variable.setValue(value);
					commandlineJava.addSysproperty(variable);
				}
			}

			//add -noverify
			commandlineJava.createVmArgument().setValue("-noverify");

			return commandlineJava.toString();
		}
 
示例14
public Commandline.Argument createJvmarg() {
    return jvmarg.createVmArgument();
}
 
示例15
public static String[] asArgArray(String cmd) {
    return Commandline.translateCommandline(cmd);
}
 
示例16
public static String[] asArgArray(String cmd) {
    return Commandline.translateCommandline(cmd);
}
 
示例17
public static String[] asArgArray(String cmd) {
    return Commandline.translateCommandline(cmd);
}
 
示例18
public Commandline.Argument createArg() {
    return cmdline.createArgument();
}
 
示例19
/**
 * Modified from base class, Logs the compilation parameters, adds the files
 * to compile and logs the &quot;niceSourceList&quot;
 * Appends encoding information at the end of arguments
 *
 * @param cmd the given command line
 */
protected void logAndAddFilesToCompile(Commandline cmd) {
	this.attributes.log("Compilation " + cmd.describeArguments(), //$NON-NLS-1$
			Project.MSG_VERBOSE);

	StringBuffer niceSourceList = new StringBuffer("File"); //$NON-NLS-1$
	if (this.compileList.length != 1) {
		niceSourceList.append("s"); //$NON-NLS-1$
	}
	niceSourceList.append(" to be compiled:"); //$NON-NLS-1$
	niceSourceList.append(lSep);

	String[] encodedFiles = null, encodedDirs = null;
	int encodedFilesLength = 0, encodedDirsLength = 0;
	if (this.fileEncodings != null) {
		encodedFilesLength = this.fileEncodings.size();
		encodedFiles = new String[encodedFilesLength];
		this.fileEncodings.keySet().toArray(encodedFiles);
	}
	if (this.dirEncodings != null) {
		encodedDirsLength = this.dirEncodings.size();
		encodedDirs = new String[encodedDirsLength];
		this.dirEncodings.keySet().toArray(encodedDirs);
		//we need the directories sorted, longest first,since sub directories can
		//override encodings for their parent directories
		Comparator comparator = new Comparator() {
			public int compare(Object o1, Object o2) {
				return ((String) o2).length() - ((String) o1).length();
			}
		};
		Arrays.sort(encodedDirs, comparator);
	}

	for (int i = 0; i < this.compileList.length; i++) {
		String arg = this.compileList[i].getAbsolutePath();
		boolean encoded = false;
		if (encodedFiles != null) {
			//check for file level custom encoding
			for (int j = 0; j < encodedFilesLength; j++) {
				if (arg.endsWith(encodedFiles[j])) {
					//found encoding, remove it from the list to speed things up next time around
					arg = arg + (String) this.fileEncodings.get(encodedFiles[j]);
					if (j < encodedFilesLength - 1) {
						System.arraycopy(encodedFiles, j + 1, encodedFiles, j, encodedFilesLength - j - 1);
					}
					encodedFiles[--encodedFilesLength] = null;
					encoded = true;
					break;
				}
			}
		}
		if (!encoded && encodedDirs != null) {
			//check folder level custom encoding
			for (int j = 0; j < encodedDirsLength; j++) {
				if (arg.lastIndexOf(encodedDirs[j]) != -1) {
					arg = arg + (String) this.dirEncodings.get(encodedDirs[j]);
					break;
				}
			}
		}
		cmd.createArgument().setValue(arg);
		niceSourceList.append("    " + arg + lSep); //$NON-NLS-1$
	}

	this.attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
}
 
示例20
public Commandline.Argument createArg() {
    return cmdLine.createArgument();
}