Example usage for org.apache.hadoop.util StringUtils escapeString

List of usage examples for org.apache.hadoop.util StringUtils escapeString

Introduction

In this page you can find the example usage for org.apache.hadoop.util StringUtils escapeString.

Prototype

public static String escapeString(String str, char escapeChar, char[] charsToEscape) 

Source Link

Usage

From source file:com.chinamobile.bcbsp.bspcontroller.Counters.java

License:Apache License

/**
 *  Escapes all the delimiters for counters i.e {,[,(,),],}
 * @param string// w w w  .j a  v a2  s.  c  o m
 *        counter record to escape.
 * @return
 *        counter record has been escaped
 */
private static String escape(String string) {
    return StringUtils.escapeString(string, StringUtils.ESCAPE_CHAR, charsToEscape);
}

From source file:org.apache.accumulo.core.client.BatchWriterConfig.java

License:Apache License

private void addField(List<String> fields, String name, Object value) {
    String key = StringUtils.escapeString(name, '\\', new char[] { ',', '=' });
    String val = StringUtils.escapeString(String.valueOf(value), '\\', new char[] { ',', '=' });
    fields.add(key + '=' + val);
}

From source file:org.apache.ambari.TestJobHistoryParsing.java

License:Apache License

private static String log(String recordType, String[] keys, String[] values) {
    int length = recordType.length() + keys.length * 4 + 2;
    for (int i = 0; i < keys.length; i++) {
        values[i] = StringUtils.escapeString(values[i], StringUtils.ESCAPE_CHAR, charsToEscape);
        length += values[i].length() + keys[i].toString().length();
    }// ww  w  . jav  a2 s  .  c om

    // We have the length of the buffer, now construct it.
    StringBuilder builder = new StringBuilder(length);
    builder.append(recordType);
    builder.append(DELIMITER);
    for (int i = 0; i < keys.length; i++) {
        builder.append(keys[i]);
        builder.append("=\"");
        builder.append(values[i]);
        builder.append("\"");
        builder.append(DELIMITER);
    }
    builder.append(LINE_DELIMITER_CHAR);

    return builder.toString();
}

From source file:org.apache.hama.bsp.Counters.java

License:Apache License

private static String escape(String string) {
    return StringUtils.escapeString(string, StringUtils.ESCAPE_CHAR, charsToEscape);
}

From source file:org.apache.oozie.action.ssh.SshActionExecutor.java

License:Apache License

/**
 * Start the ssh action execution.//from ww  w. j  a v a2  s  .c  o m
 *
 * @param context action execution context.
 * @param action action object.
 */
@SuppressWarnings("unchecked")
@Override
public void start(final Context context, final WorkflowAction action) throws ActionExecutorException {
    XLog log = XLog.getLog(getClass());
    log.info("start() begins");
    String confStr = action.getConf();
    Element conf;
    try {
        conf = XmlUtils.parseXml(confStr);
    } catch (Exception ex) {
        throw convertException(ex);
    }
    Namespace nameSpace = conf.getNamespace();
    Element hostElement = conf.getChild("host", nameSpace);
    String hostString = hostElement.getValue().trim();
    hostString = prepareUserHost(hostString, context);
    final String host = hostString;
    final String dirLocation = execute(new Callable<String>() {
        public String call() throws Exception {
            return setupRemote(host, context, action);
        }

    });

    String runningPid = execute(new Callable<String>() {
        public String call() throws Exception {
            return checkIfRunning(host, context, action);
        }
    });
    String pid = "";

    if (runningPid == null) {
        final Element commandElement = conf.getChild("command", nameSpace);
        final boolean ignoreOutput = conf.getChild("capture-output", nameSpace) == null;

        boolean preserve = false;
        if (commandElement != null) {
            String[] args = null;
            // Will either have <args>, <arg>, or neither (but not both)
            List<Element> argsList = conf.getChildren("args", nameSpace);
            // Arguments in an <args> are "flattened" (spaces are delimiters)
            if (argsList != null && argsList.size() > 0) {
                StringBuilder argsString = new StringBuilder("");
                for (Element argsElement : argsList) {
                    argsString = argsString.append(argsElement.getValue()).append(" ");
                }
                args = new String[] { argsString.toString() };
            } else {
                // Arguments in an <arg> are preserved, even with spaces
                argsList = conf.getChildren("arg", nameSpace);
                if (argsList != null && argsList.size() > 0) {
                    preserve = true;
                    args = new String[argsList.size()];
                    for (int i = 0; i < argsList.size(); i++) {
                        Element argsElement = argsList.get(i);
                        args[i] = argsElement.getValue();
                        // Even though we're keeping the args as an array, if they contain a space we still have to either quote
                        // them or escape their space (because the scripts will split them up otherwise)
                        if (args[i].contains(" ") && !(args[i].startsWith("\"") && args[i].endsWith("\"")
                                || args[i].startsWith("'") && args[i].endsWith("'"))) {
                            args[i] = StringUtils.escapeString(args[i], '\\', ' ');
                        }
                    }
                }
            }
            final String[] argsF = args;
            final String recoveryId = context.getRecoveryId();
            final boolean preserveF = preserve;
            pid = execute(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    return doExecute(host, dirLocation, commandElement.getValue(), argsF, ignoreOutput, action,
                            recoveryId, preserveF);
                }

            });
        }
        context.setStartData(pid, host, host);
    } else {
        pid = runningPid;
        context.setStartData(pid, host, host);
        check(context, action);
    }
    log.info("start() ends");
}