use of java.util.ArrayList in project camel by apache.
the class BinaryExpression method createInExpression.
private Expression createInExpression(final Expression leftExp, final Expression rightExp) {
return new Expression() {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
// okay the in operator is a bit more complex as we need to build a list of values
// from the right hand side expression.
// each element on the right hand side must be separated by comma (default for create iterator)
Iterator<Object> it = ObjectHelper.createIterator(rightExp.evaluate(exchange, Object.class));
List<Object> values = new ArrayList<Object>();
while (it.hasNext()) {
values.add(it.next());
}
// then reuse value builder to create the in predicate with the list of values
ValueBuilder vb = new ValueBuilder(leftExp);
Predicate predicate = vb.in(values.toArray());
if (operator == BinaryOperatorType.NOT_IN) {
predicate = PredicateBuilder.not(predicate);
}
boolean answer = predicate.matches(exchange);
return exchange.getContext().getTypeConverter().convertTo(type, answer);
}
@Override
public String toString() {
return left + " " + token.getText() + " " + right;
}
};
}
use of java.util.ArrayList in project camel by apache.
the class BaseSimpleParser method prepareBlocks.
/**
* Prepares blocks, such as functions, single or double quoted texts.
* <p/>
* This process prepares the {@link Block}s in the AST. This is done
* by linking child {@link SimpleNode nodes} which are within the start and end of the blocks,
* as child to the given block. This is done to have the AST graph updated and prepared properly.
* <p/>
* So when the AST node is later used to create the {@link org.apache.camel.Predicate}s
* or {@link org.apache.camel.Expression}s to be used by Camel then the AST graph
* has a linked and prepared graph of nodes which represent the input expression.
*/
protected void prepareBlocks() {
List<SimpleNode> answer = new ArrayList<SimpleNode>();
Stack<Block> stack = new Stack<Block>();
for (SimpleNode token : nodes) {
if (token instanceof BlockStart) {
// a new block is started, so push on the stack
stack.push((Block) token);
} else if (token instanceof BlockEnd) {
// end block is just an abstract mode, so we should not add it
if (stack.isEmpty()) {
throw new SimpleParserException(token.getToken().getType().getType() + " has no matching start token", token.getToken().getIndex());
}
Block top = stack.pop();
// if there is a block on the stack then it should accept the child token
Block block = stack.isEmpty() ? null : stack.peek();
if (block != null) {
if (!block.acceptAndAddNode(top)) {
throw new SimpleParserException(block.getToken().getType() + " cannot accept " + token.getToken().getType(), token.getToken().getIndex());
}
} else {
// no block, so add to answer
answer.add(top);
}
} else {
// if there is a block on the stack then it should accept the child token
Block block = stack.isEmpty() ? null : stack.peek();
if (block != null) {
if (!block.acceptAndAddNode(token)) {
throw new SimpleParserException(block.getToken().getType() + " cannot accept " + token.getToken().getType(), token.getToken().getIndex());
}
} else {
// no block, so add to answer
answer.add(token);
}
}
}
// replace nodes from the stack
nodes.clear();
nodes.addAll(answer);
}
use of java.util.ArrayList in project camel by apache.
the class WeightedLoadBalancerDefinition method createLoadBalancer.
@Override
protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
WeightedLoadBalancer loadBalancer;
List<Integer> distributionRatioList = new ArrayList<Integer>();
try {
String[] ratios = distributionRatio.split(getDistributionRatioDelimiter());
for (String ratio : ratios) {
distributionRatioList.add(new Integer(ratio.trim()));
}
boolean isRoundRobin = getRoundRobin() != null && getRoundRobin();
if (isRoundRobin) {
loadBalancer = new WeightedRoundRobinLoadBalancer(distributionRatioList);
} else {
loadBalancer = new WeightedRandomLoadBalancer(distributionRatioList);
}
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
return loadBalancer;
}
use of java.util.ArrayList in project camel by apache.
the class XPathTest method testXPathSplitConcurrent.
public void testXPathSplitConcurrent() throws Exception {
int size = 100;
final Object node = XPathBuilder.xpath("foo/bar").nodeResult().evaluate(createExchange("<foo><bar>cheese</bar><bar>cake</bar><bar>beer</bar></foo>"));
assertNotNull(node);
// convert the node concurrently to test that XML Parser is not thread safe when
// importing nodes to a new Document, so try a test for that
final List<Document> result = new ArrayList<Document>();
ExecutorService executor = Executors.newFixedThreadPool(size);
final CountDownLatch latch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
executor.submit(new Callable<Document>() {
public Document call() throws Exception {
try {
Document doc = context.getTypeConverter().convertTo(Document.class, node);
result.add(doc);
return doc;
} finally {
latch.countDown();
}
}
});
}
// give time to convert concurrently
assertTrue(latch.await(20, TimeUnit.SECONDS));
Iterator<Document> it = result.iterator();
int count = 0;
while (it.hasNext()) {
count++;
Document doc = it.next();
assertNotNull(doc);
}
assertEquals(size, count);
executor.shutdownNow();
}
use of java.util.ArrayList in project camel by apache.
the class SamplingThrottlerTest method testBurstySampling.
public void testBurstySampling() throws Exception {
NotifyBuilder notify = new NotifyBuilder(context).whenDone(5).create();
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(2);
mock.setResultWaitTime(3000);
List<Exchange> sentExchanges = new ArrayList<Exchange>();
// send a burst of 5 exchanges, expecting only one to get through
sendExchangesThroughDroppingThrottler(sentExchanges, 5);
// sleep through a complete period
Thread.sleep(1100);
// send another 5 now
sendExchangesThroughDroppingThrottler(sentExchanges, 5);
notify.matchesMockWaitTime();
mock.assertIsSatisfied();
validateDroppedExchanges(sentExchanges, mock.getReceivedCounter());
}
Aggregations