001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.model;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
022    
023    import javax.xml.bind.annotation.XmlElement;
024    import javax.xml.bind.annotation.XmlElementRef;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    
028    import org.apache.camel.Endpoint;
029    import org.apache.camel.Exchange;
030    import org.apache.camel.Expression;
031    import org.apache.camel.Processor;
032    import org.apache.camel.Route;
033    import org.apache.camel.impl.RouteContext;
034    import org.apache.camel.model.language.ExpressionType;
035    import org.apache.camel.processor.Resequencer;
036    
037    /**
038     * @version $Revision: 1.1 $
039     */
040    @XmlRootElement(name = "resequencer")
041    public class ResequencerType extends ProcessorType {
042        @XmlElementRef
043        private List<InterceptorType> interceptors = new ArrayList<InterceptorType>();
044        @XmlElementRef
045        private List<ExpressionType> expressions = new ArrayList<ExpressionType>();
046        @XmlElementRef
047        private List<ProcessorType> outputs = new ArrayList<ProcessorType>();
048        @XmlTransient
049        private List<Expression> expressionList;
050    
051        public ResequencerType() {
052        }
053    
054        public ResequencerType(List<Expression> expressions) {
055            this.expressionList = expressions;
056        }
057    
058        @Override
059        public String toString() {
060            return "Resequencer[ " + getExpressions() + " -> " + getOutputs() + "]";
061        }
062    
063        public List<ExpressionType> getExpressions() {
064            return expressions;
065        }
066    
067        public List<InterceptorType> getInterceptors() {
068            return interceptors;
069        }
070    
071        public void setInterceptors(List<InterceptorType> interceptors) {
072            this.interceptors = interceptors;
073        }
074    
075        public List<ProcessorType> getOutputs() {
076            return outputs;
077        }
078    
079        public void setOutputs(List<ProcessorType> outputs) {
080            this.outputs = outputs;
081        }
082    
083        public void addRoutes(RouteContext routeContext, Collection<Route> routes) throws Exception {
084            Endpoint from = routeContext.getEndpoint();
085            final Processor processor = routeContext.createProcessor(this);
086            final Resequencer resequencer = new Resequencer(from, processor, resolveExpressionList(routeContext));
087    
088            Route route = new Route<Exchange>(from, resequencer) {
089                @Override
090                public String toString() {
091                    return "ResequencerRoute[" + getEndpoint() + " -> " + processor + "]";
092                }
093            };
094    
095            routes.add(route);
096        }
097    
098        private List<Expression> resolveExpressionList(RouteContext routeContext) {
099            if (expressionList == null) {
100                expressionList = new ArrayList<Expression>();
101                for (ExpressionType expression : expressions) {
102                    expressionList.add(expression.createExpression(routeContext));
103                }
104            }
105            if (expressionList.isEmpty()) {
106                throw new IllegalArgumentException("No expressions configured for: " + this);
107            }
108            return expressionList;
109        }
110    }