Java Code Examples for org.apache.brooklyn.util.collections.MutableList#add()

The following examples show how to use org.apache.brooklyn.util.collections.MutableList#add() . 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: KnifeTaskFactory.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> getCommands() {
    MutableList<String> result = new MutableList<String>();
    String setupCommands = knifeSetupCommands();
    if (setupCommands != null && Strings.isNonBlank(setupCommands))
        result.add(setupCommands);
    int numKnifes = 0;
    for (String c: super.getCommands()) {
        if (c==KNIFE_PLACEHOLDER)
            result.add(buildKnifeCommand(numKnifes++));
        else
            result.add(c);
    }
    if (numKnifes==0)
        result.add(buildKnifeCommand(numKnifes++));
    return result.asUnmodifiable();
}
 
Example 2
Source File: PlanInterpretationNode.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected void applyToIterable() {
    MutableList<Object> input = MutableList.copyOf((Iterable<?>)originalValue);
    MutableList<Object> result = new MutableList<Object>();
    newValue = result;

    // first do a "whole-node" application
    if (getContext().getAllInterpreter().applyListBefore(this, input)) {

        for (Object entry: input) {
            // then recurse in to this node and do various in-the-node applications
            PlanInterpretationNode value = newPlanInterpretation(this, Role.LIST_ENTRY, entry);
            value.apply();

            if (value.isChanged()) 
                changed = true;

            if (getContext().getAllInterpreter().applyListEntry(this, input, result, value))
                result.add(value.getNewValue());
        }

        // finally try applying to this node again
        getContext().getAllInterpreter().applyListAfter(this, input, result);
    }

    if (changed==null) changed = false;
}
 
Example 3
Source File: StringEscapes.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** as {@link #unwrapJsonishListIfPossible(String)} but throwing errors 
 * if something which looks like a string or set of brackets is not well-formed
 * (this does the work for that method) 
 * @throws IllegalArgumentException if looks to have quoted list or surrounding brackets but they are not syntactically valid */
public static List<String> unwrapOptionallyQuotedJavaStringList(String input) {
    if (input==null) return null;
    MutableList<String> result = MutableList.of();
    String i1 = input.trim();
    
    boolean inBrackets = (i1.startsWith("[") && i1.endsWith("]"));
    if (inBrackets) i1 = i1.substring(1, i1.length()-1).trim();
        
    QuotedStringTokenizer qst = new QuotedStringTokenizer(i1, "\"", true, ",", false);
    while (qst.hasMoreTokens()) {
        String t = qst.nextToken().trim();
        if (isWrappedInDoubleQuotes(t))
            result.add(unwrapJavaString(t));
        else {
            if (inBrackets && (t.indexOf('[')>=0 || t.indexOf(']')>=0))
                throw new IllegalArgumentException("Literal square brackets must be quoted, in element '"+t+"'");
            result.add(t.trim());
        }
    }
    
    return result;
}
 
Example 4
Source File: Strings.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** Returns canonicalized string from the given object, made "unique" by:
 * <li> putting sets into the toString order
 * <li> appending a hash code if it's longer than the max (and the max is bigger than 0) */
public static String toUniqueString(Object x, int optionalMax) {
    if (x instanceof Iterable && !(x instanceof List)) {
        // unsorted collections should have a canonical order imposed
        MutableList<String> result = MutableList.of();
        for (Object xi: (Iterable<?>)x) {
            result.add(toUniqueString(xi, optionalMax));
        }
        Collections.sort(result);
        x = result.toString();
    }
    if (x==null) return "{null}";
    String xs = x.toString();
    if (xs.length()<=optionalMax || optionalMax<=0) return xs;
    return maxlenWithEllipsis(xs, optionalMax-8)+"/"+Integer.toHexString(xs.hashCode());
}
 
Example 5
Source File: TestFrameworkSuiteBuilder.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private Collection<URL> getLocationResources(Collection<String> locations) {
    if (testFrameworkSuiteBuilder.locationsFolder != null) {
        MutableList<URL> resources = MutableList.of();
        for (String location : locations) {
            File locationBom = new File(testFrameworkSuiteBuilder.locationsFolder, location + ".bom");
            if (locationBom.exists()) {
                try {
                    resources.add(locationBom.toURI().toURL());
                } catch (MalformedURLException e) {
                    throw new IllegalStateException("Could not conert the path " + locationBom.getAbsolutePath() + " to URL", e);
                }
            } else {
                log.info("Locationn file {} not found in {}. Assuming it's a location provided by the environment.",
                        location, testFrameworkSuiteBuilder.locationsFolder);
            }
        }
        return resources.asImmutableCopy();
    } else {
        return ImmutableList.of();
    }
}
 
Example 6
Source File: KnifeTaskFactory.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/** creates the command for running knife.
 * in some cases knife may be added multiple times,
 * and in that case the parameter here tells which time it is being added, 
 * on a single run. */
protected String buildKnifeCommand(int knifeCommandIndex) {
    MutableList<String> words = new MutableList<String>();
    words.add(knifeExecutable());
    words.addAll(initialKnifeParameters());
    words.addAll(knifeParameters());
    String x = knifeConfigFileOption();
    if (Strings.isNonBlank(x)) words.add(knifeConfigFileOption());
    return Strings.join(words, " ");
}
 
Example 7
Source File: KnifeConvergeTaskFactory.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/** construct the knife command, based on the settings on other methods
 * (called when instantiating the script, after all parameters sent)
 */
@Override
protected List<String> initialKnifeParameters() {
    // runs inside the task so can detect entity/machine at runtime
    MutableList<String> result = new MutableList<String>();
    SshMachineLocation machine = EffectorTasks.findSshMachine();
    
    result.add("bootstrap");
    result.addAll(extraBootstrapParameters);

    HostAndPort hostAndPort = machine.getSshHostAndPort();
    result.add(wrapBash(hostAndPort.getHostText()));
    Integer whichPort = knifeWhichPort(hostAndPort);
    if (whichPort!=null)
        result.add("-p "+whichPort);

    result.add("-x "+wrapBash(checkNotNull(machine.getUser(), "user")));
    
    File keyfile = ChefServerTasks.extractKeyFile(machine);
    if (keyfile!=null) result.add("-i "+keyfile.getPath());
    else result.add("-P "+checkNotNull(machine.findPassword(), "No password or private key data for "+machine));
    
    result.add("--no-host-key-verify");
    
    if (sudo != Boolean.FALSE) result.add("--sudo");

    if (!Strings.isNullOrEmpty(nodeName)) {
        result.add("--node-name");
        result.add(nodeName);
    }

    result.add("-r "+wrapBash(runList.apply(entity())));
    
    if (!knifeAttributes.isEmpty())
        result.add("-j "+wrapBash(new GsonBuilder().create()
                .toJson(knifeAttributes)));

    return result;
}
 
Example 8
Source File: ManifestHelper.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public List<String> getExportedPackages() {
    MutableList<String> result = MutableList.of();
    for (BundleCapability c : parse.getCapabilities()) {
        if (WIRING_PACKAGE.equals(c.getNamespace())) {
            result.add((String) c.getAttributes().get(WIRING_PACKAGE));
        }
    }
    return result;
}
 
Example 9
Source File: TestFrameworkSuiteBuilder.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a list of tests corresponding to the arguments passed to the
 * builder. Adds the cartesion product of all cloud + distribution pairs, 
 * concatenating them like "<cloud>:<distribution>" and adds the result
 * to the test locations. For each resulting location adds an element to
 * the returned list.
 */
public Collection<TestFrameworkRun> build() {
    MutableList<TestFrameworkRun> runs = MutableList.of();
    Collection<String> allLocations = MutableList.of();
    for (String cloud : clouds) {
        for (String dist : distributions) {
            // TODO add a generic template for the location names to handle custom schemes.
            allLocations.add(dist + ":" + cloud);
        }
    }
    allLocations.addAll(locations);
    for (String location : allLocations) {
        runs.add(TestFrameworkRun.builder()
            .prerequisites(getLocationResources(ImmutableList.of(location)))
            .prerequisites(getLocationResources(locationAliases.keySet()))
            .prerequisites(prerequisites)
            .tests(tests)
            // Handles locations with file names using ':' delimiter, but 
            // using '_' for the ID.
            // TODO add a generic file name -> ID transformer to handle custom schemes.
            .location(location.replace(":", "_"))
            // TODO Transform the aliases keys in a similar way to the locations
            .locationAliases(locationAliases)
            .build());
    }
    return runs.asImmutableCopy();
}
 
Example 10
Source File: BrooklynGarbageCollector.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected int expireIfOverCapacityGlobally() {
    Collection<Task<?>> tasksLive = executionManager.allTasksLive();
    if (tasksLive.size() <= brooklynProperties.getConfig(MAX_TASKS_GLOBAL))
        return 0;
    LOG.debug("brooklyn-gc detected "+tasksLive.size()+" tasks in memory, over global limit, looking at deleting some");
    
    try {
        tasksLive = MutableList.copyOf(tasksLive);
    } catch (ConcurrentModificationException e) {
        tasksLive = executionManager.getTasksWithAllTags(MutableList.of());
    }

    MutableList<Task<?>> tasks = MutableList.of();
    for (Task<?> task: tasksLive) {
        if (task.isDone()) {
            tasks.add(task);
        }
    }
    
    int numToDelete = tasks.size() - brooklynProperties.getConfig(MAX_TASKS_GLOBAL);
    if (numToDelete <= 0) {
        LOG.debug("brooklyn-gc detected only "+tasks.size()+" completed tasks in memory, not over global limit, so not deleting any");
        return 0;
    }
        
    Collections.sort(tasks, TASKS_OLDEST_FIRST_COMPARATOR);
    
    int numDeleted = 0;
    while (numDeleted < numToDelete && tasks.size()>numDeleted) {
        executionManager.deleteTask( tasks.get(numDeleted++) );
    }
    if (LOG.isDebugEnabled())
        LOG.debug("brooklyn-gc deleted "+numDeleted+" tasks as was over global limit, now have "+executionManager.allTasksLive().size());
    return numDeleted;
}