
hudson.util.ArgumentListBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hudson-core Show documentation
Show all versions of hudson-core Show documentation
Contains the core Hudson code and view files to render HTML.
The newest version!
/*******************************************************************************
*
* Copyright (c) 2004-2010 Oracle Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
* Kohsuke Kawaguchi, Alan Harder, Yahoo! Inc.
*
*
*******************************************************************************/
package hudson.util;
import hudson.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.BitSet;
import java.util.Properties;
import java.util.Map.Entry;
import java.io.Serializable;
import java.io.File;
import java.io.IOException;
import java.util.Set;
/**
* Used to build up arguments for a process invocation.
*
* @author Kohsuke Kawaguchi
*/
public class ArgumentListBuilder implements Serializable {
private final List args = new ArrayList();
/**
* Bit mask indicating arguments that shouldn't be echoed-back (e.g.,
* password)
*/
private BitSet mask = new BitSet();
public ArgumentListBuilder() {
}
public ArgumentListBuilder(String... args) {
add(args);
}
public ArgumentListBuilder add(Object a) {
return add(a.toString(), false);
}
/**
* @since 1.378
*/
public ArgumentListBuilder add(Object a, boolean mask) {
return add(a.toString(), mask);
}
public ArgumentListBuilder add(File f) {
return add(f.getAbsolutePath(), false);
}
public ArgumentListBuilder add(String a) {
return add(a, false);
}
/**
* @since 1.378
*/
public ArgumentListBuilder add(String a, boolean mask) {
if (a != null) {
if (mask) {
this.mask.set(args.size());
}
args.add(a);
}
return this;
}
public ArgumentListBuilder prepend(String... args) {
// left-shift the mask
BitSet nm = new BitSet(this.args.size() + args.length);
for (int i = 0; i < this.args.size(); i++) {
nm.set(i + args.length, mask.get(i));
}
mask = nm;
this.args.addAll(0, Arrays.asList(args));
return this;
}
/**
* Adds an argument by quoting it. This is necessary only in a rare
* circumstance, such as when adding argument for ssh and rsh.
*
* Normal process invocations don't need it, because each argument is
* treated as its own string and never merged into one.
*/
public ArgumentListBuilder addQuoted(String a) {
return add('"' + a + '"', false);
}
/**
* @since 1.378
*/
public ArgumentListBuilder addQuoted(String a, boolean mask) {
return add('"' + a + '"', mask);
}
public ArgumentListBuilder add(String... args) {
for (String arg : args) {
add(arg);
}
return this;
}
/**
* Decomposes the given token into multiple arguments by splitting via
* whitespace.
*/
public ArgumentListBuilder addTokenized(String s) {
if (s == null) {
return this;
}
add(Util.tokenize(s));
return this;
}
/**
* @since 1.378
*/
public ArgumentListBuilder addKeyValuePair(String prefix, String key, String value, boolean mask) {
if (key == null) {
return this;
}
add(((prefix == null) ? "-D" : prefix) + key + '=' + value, mask);
return this;
}
/**
* Adds key value pairs as "-Dkey=value -Dkey=value ..."
*
* -D portion is configurable as the 'prefix' parameter.
*
* @since 1.114
*/
public ArgumentListBuilder addKeyValuePairs(String prefix, Map props) {
for (Entry e : props.entrySet()) {
addKeyValuePair(prefix, e.getKey(), e.getValue(), false);
}
return this;
}
/**
* Adds key value pairs as "-Dkey=value -Dkey=value ..." with masking.
*
* @param prefix Configures the -D portion of the example. Defaults to -D if
* null.
* @param props The map of key/value pairs to add
* @param propsToMask Set containing key names to mark as masked in the
* argument list. Key names that do not exist in the set will be added
* unmasked.
* @since 1.378
*/
public ArgumentListBuilder addKeyValuePairs(String prefix, Map props, Set propsToMask) {
for (Entry e : props.entrySet()) {
addKeyValuePair(prefix, e.getKey(), e.getValue(), (propsToMask == null) ? false : propsToMask.contains(e.getKey()));
}
return this;
}
/**
* Adds key value pairs as "-Dkey=value -Dkey=value ..." by parsing a given
* string using {@link Properties}.
*
* @param prefix The '-D' portion of the example. Defaults to -D if null.
* @param properties The persisted form of {@link Properties}. For example,
* "abc=def\nghi=jkl". Can be null, in which case this method becomes no-op.
* @param vr {@link VariableResolver} to be performed on the values.
* @since 1.262
*/
public ArgumentListBuilder addKeyValuePairsFromPropertyString(String prefix, String properties, VariableResolver vr) throws IOException {
if (properties == null) {
return this;
}
for (Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy