Search in sources :

Example 36 with ArrayList

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;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ValueBuilder(org.apache.camel.builder.ValueBuilder) Expression(org.apache.camel.Expression) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate)

Example 37 with ArrayList

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);
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) BlockStart(org.apache.camel.language.simple.ast.BlockStart) BlockEnd(org.apache.camel.language.simple.ast.BlockEnd) ArrayList(java.util.ArrayList) Block(org.apache.camel.language.simple.ast.Block) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode) Stack(java.util.Stack)

Example 38 with ArrayList

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;
}
Also used : WeightedRoundRobinLoadBalancer(org.apache.camel.processor.loadbalancer.WeightedRoundRobinLoadBalancer) WeightedRandomLoadBalancer(org.apache.camel.processor.loadbalancer.WeightedRandomLoadBalancer) ArrayList(java.util.ArrayList) WeightedLoadBalancer(org.apache.camel.processor.loadbalancer.WeightedLoadBalancer)

Example 39 with ArrayList

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();
}
Also used : ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Document(org.w3c.dom.Document) CountDownLatch(java.util.concurrent.CountDownLatch) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Example 40 with ArrayList

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());
}
Also used : NotifyBuilder(org.apache.camel.builder.NotifyBuilder) Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)55702 Test (org.junit.Test)8169 List (java.util.List)6815 HashMap (java.util.HashMap)5856 IOException (java.io.IOException)3899 Map (java.util.Map)3195 File (java.io.File)3090 HashSet (java.util.HashSet)2245 Iterator (java.util.Iterator)1591 Test (org.testng.annotations.Test)1074 SQLException (java.sql.SQLException)1046 ResultSet (java.sql.ResultSet)1017 Date (java.util.Date)997 Set (java.util.Set)917 LinkedHashMap (java.util.LinkedHashMap)886 PreparedStatement (java.sql.PreparedStatement)882 Collection (java.util.Collection)751 LinkedList (java.util.LinkedList)677 BufferedReader (java.io.BufferedReader)663 Path (org.apache.hadoop.fs.Path)611