Java Code Examples for org.apache.brooklyn.util.collections.MutableList#Builder

The following examples show how to use org.apache.brooklyn.util.collections.MutableList#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SaltSshTasks.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
public static TaskFactory<?> installTopFile(final Set<? extends String> runList, boolean force) {
    // TODO: ignore force?
    // TODO: move this into salt_utilities.sh
    final MutableList.Builder<String> topBuilder = MutableList.<String>builder()
        .add("cat > /tmp/top.sls << BROOKLYN_EOF")
        .add("base:")
        .add("  '*':");
    for (String stateName: runList) {
        topBuilder.add("    - " + stateName);
    }
    topBuilder.add("BROOKLYN_EOF");
    List<String> createTempTopFile = topBuilder.build();

    List<String> commands = MutableList.<String>builder()
        .add(sudo("mkdir -p /srv/salt"))
        .add(Strings.join(createTempTopFile, "\n"))
        .add(sudo("mv /tmp/top.sls /srv/salt"))
        .build();
    return ssh(commands).summary("create top.sls file");
}
 
Example 2
Source File: Reflections.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** Lists all fields declared on the class, with those lowest in the hierarchy first,
 *  filtered and ordered as requested. 
 *  <p>
 *  See {@link ReflectionPredicates} and {@link FieldOrderings} for conveniences.
 *  <p>
 *  Default is no filter and {@link FieldOrderings#SUB_BEST_FIELD_LAST_THEN_ALPHABETICAL}
 *  */
public static List<Field> findFields(final Class<?> clazz, Predicate<Field> filter, Comparator<Field> fieldOrdering) {
    checkNotNull(clazz, "clazz");
    MutableList.Builder<Field> result = MutableList.<Field>builder();
    Stack<Class<?>> tovisit = new Stack<Class<?>>();
    Set<Class<?>> visited = Sets.newLinkedHashSet();
    tovisit.push(clazz);
    
    while (!tovisit.isEmpty()) {
        Class<?> nextclazz = tovisit.pop();
        if (!visited.add(nextclazz)) {
            continue; // already visited
        }
        if (nextclazz.getSuperclass() != null) tovisit.add(nextclazz.getSuperclass());
        tovisit.addAll(Arrays.asList(nextclazz.getInterfaces()));
        
        result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredFields()), 
            filter!=null ? filter : Predicates.<Field>alwaysTrue()));
    }
    
    List<Field> resultList = result.build();
    Collections.sort(resultList, fieldOrdering != null ? fieldOrdering : FieldOrderings.SUB_BEST_FIELD_LAST_THEN_ALPHABETICAL);
    
    return resultList;
}
 
Example 3
Source File: ShellAbstractTool.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** builds the command to run the given script;
 * note that some modes require \$RESULT passed in order to access a variable, whereas most just need $ */
protected List<String> buildRunScriptCommand() {
    String scriptInvocationCmd;
    if (runAsRoot) {
        if (authSudo) {
            scriptInvocationCmd = BashCommands.authSudo(scriptPath, password);
        } else {
            scriptInvocationCmd = BashCommands.sudo(scriptPath) + " < /dev/null";
        }
    } else {
        scriptInvocationCmd = scriptPath + " < /dev/null";
    }
    
    MutableList.Builder<String> cmds = MutableList.<String>builder()
            .add(scriptInvocationCmd)
            .add("RESULT=$?");
    if (!noExtraOutput)
        cmds.add("echo Executed "+scriptPath+", result $RESULT"); 
    if (!noDeleteAfterExec) {
        // use "-f" because some systems have "rm" aliased to "rm -i"
        // use "< /dev/null" to guarantee doesn't hang
        cmds.add("rm -f "+scriptPath+" < /dev/null");
    }
    cmds.add("exit $RESULT");
    return cmds.build();
}
 
Example 4
Source File: ShellAbstractTool.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * Builds the command to run the given script, asynchronously.
 * The executed command will return immediately, but the output from the script
 * will continue to be written 
 * note that some modes require \$RESULT passed in order to access a variable, whereas most just need $ */
@Override
protected List<String> buildRunScriptCommand() {
    String touchCmd = String.format("touch %s %s %s %s", stdoutPath, stderrPath, exitStatusPath, pidPath);
    String cmd = String.format("nohup sh -c \"( %s > %s 2> %s < /dev/null ) ; echo \\$? > %s \" > /dev/null 2>&1 < /dev/null &", scriptPath, stdoutPath, stderrPath, exitStatusPath);
    MutableList.Builder<String> cmds = MutableList.<String>builder()
            .add(runAsRoot ? BashCommands.sudo(touchCmd) : touchCmd)
            .add(runAsRoot ? BashCommands.sudo(cmd) : cmd)
            .add("echo $! > "+pidPath)
            .add("RESULT=$?");
    if (!noExtraOutput) {
        cmds.add("echo Executing async "+scriptPath);
    }
    cmds.add("exit $RESULT");
    return cmds.build();
}
 
Example 5
Source File: Reflections.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public static List<Method> findPublicMethodsOrderedBySuper(Class<?> clazz) {
    checkNotNull(clazz, "clazz");
    MutableList.Builder<Method> result = MutableList.<Method>builder();
    Stack<Class<?>> tovisit = new Stack<Class<?>>();
    Set<Class<?>> visited = Sets.newLinkedHashSet();
    tovisit.push(clazz);
    
    while (!tovisit.isEmpty()) {
        Class<?> nextclazz = tovisit.pop();
        if (!visited.add(nextclazz)) {
            continue; // already visited
        }
        if (nextclazz.getSuperclass() != null) tovisit.add(nextclazz.getSuperclass());
        tovisit.addAll(Arrays.asList(nextclazz.getInterfaces()));
        
        result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredMethods()), new Predicate<Method>() {
            @Override public boolean apply(Method input) {
                return Modifier.isPublic(input.getModifiers());
            }}));
        
    }
    
    List<Method> resultList = result.build();
    Collections.sort(resultList, new Comparator<Method>() {
        @Override public int compare(Method m1, Method m2) {
            Method msubbest = inferSubbestMethod(m1, m2);
            return (msubbest == null) ? 0 : (msubbest == m1 ? 1 : -1);
        }});
    
    return resultList;
}
 
Example 6
Source File: ShellAbstractTool.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the command to retrieve the exit status of the command, written to stdout.
 */
protected List<String> buildRetrieveStatusCommand() {
    // Retrieve exit status from file (writtent to stdout), if populated;
    // if not found and pid still running, then return empty string; else exit code 1.
    List<String> cmdParts = ImmutableList.of(
            "# Retrieve status", // comment is to aid testing - see SshjToolAsyncStubIntegrationTest
            "if test -s "+exitStatusPath+"; then",
            "    cat "+exitStatusPath,
            "elif test -s "+pidPath+"; then",
            "    pid=`cat "+pidPath+"`",
            "    if ! ps -p $pid > /dev/null < /dev/null; then",
            "        # no exit status, and not executing; give a few seconds grace in case just about to write exit status",
            "        sleep 3",
            "        if test -s "+exitStatusPath+"; then",
            "            cat "+exitStatusPath+"",
            "        else",
            "            echo \"No exit status in "+exitStatusPath+", and pid in "+pidPath+" ($pid) not executing\"",
            "            exit 1",
            "        fi",
            "    fi",
            "else",
            "    echo \"No exit status in "+exitStatusPath+", and "+pidPath+" is empty\"",
            "    exit 1",
            "fi"+"\n");
    String cmd = Joiner.on("\n").join(cmdParts);

    MutableList.Builder<String> cmds = MutableList.<String>builder()
            .add((runAsRoot ? BashCommands.sudo(cmd) : cmd))
            .add("RESULT=$?");
    cmds.add("exit $RESULT");
    return cmds.build();
}
 
Example 7
Source File: ShellAbstractTool.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the command to retrieve the stdout and stderr of the async command.
 * An offset can be given, to only retrieve data starting at a particular character (indexed from 0).
 */
protected List<String> buildRetrieveStdoutAndStderrCommand(int stdoutPosition, int stderrPosition) {
    // Note that `tail -c +1` means start at the *first* character (i.e. start counting from 1, not 0)
    String catStdoutCmd = "tail -c +"+(stdoutPosition+1)+" "+stdoutPath+" 2> /dev/null";
    String catStderrCmd = "tail -c +"+(stderrPosition+1)+" "+stderrPath+" 2>&1 > /dev/null";
    MutableList.Builder<String> cmds = MutableList.<String>builder()
            .add((runAsRoot ? BashCommands.sudo(catStdoutCmd) : catStdoutCmd))
            .add((runAsRoot ? BashCommands.sudo(catStderrCmd) : catStderrCmd))
            .add("RESULT=$?");
    cmds.add("exit $RESULT");
    return cmds.build();
}
 
Example 8
Source File: SaltSshTasks.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
public static SshEffectorTaskFactory<Integer> sshCommands(String line, String... lines) {
    final MutableList.Builder<String> builder = MutableList.<String>builder()
        .add(line);
    builder.addAll(lines);
    return ssh(Strings.join(builder.build(), "\n"));
}