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.builder;
018    
019    import org.apache.camel.processor.CompositeProcessor;
020    import org.apache.camel.Endpoint;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.processor.InterceptorProcessor;
023    import org.apache.camel.Predicate;
024    import org.apache.camel.Processor;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @version $Revision: 520404 $
031     */
032    public class DestinationBuilder<E extends Exchange> extends BuilderSupport<E> implements ProcessorBuilder<E> {
033        private RouteBuilder<E> builder;
034        private Endpoint<E> from;
035        private List<Processor<E>> processors = new ArrayList<Processor<E>>();
036        private List<ProcessorBuilder<E>> processBuilders = new ArrayList<ProcessorBuilder<E>>();
037    
038        public DestinationBuilder(RouteBuilder<E> builder, Endpoint<E> from) {
039            this.builder = builder;
040            this.from = from;
041        }
042    
043        public DestinationBuilder(DestinationBuilder<E> parent) {
044            this.builder = parent.getBuilder();
045            this.from = parent.getFrom();
046        }
047    
048        /**
049         * Resolves the given URI to an endpoint
050         */
051        public Endpoint<E> endpoint(String uri) {
052            return getBuilder().endpoint(uri);
053        }
054    
055        /**
056         * Sends the exchange to the given endpoint URI
057         */
058        public ProcessorBuilder<E> to(String uri) {
059            return to(endpoint(uri));
060        }
061    
062        /**
063         * Sends the exchange to the given endpoint
064         */
065        public ProcessorBuilder<E> to(Endpoint<E> endpoint) {
066            ConfiguredDestinationBuilder<E> answer = new ConfiguredDestinationBuilder<E>(this, endpoint);
067            addProcessBuilder(answer);
068            return answer;
069        }
070    
071    
072        /**
073         * Sends the exchange to the given endpoint URI
074         */
075        public ProcessorBuilder<E> to(String... uris) {
076            ProcessorBuilder<E> answer = null;
077            for (String uri : uris) {
078                answer = to(endpoint(uri));
079            }
080            return answer;
081        }
082    
083        /**
084         * Sends the exchange to the given endpoint
085         */
086        public ProcessorBuilder<E> to(Endpoint<E>... endpoints) {
087            ProcessorBuilder<E> answer = null;
088            for (Endpoint<E> endpoint : endpoints) {
089                answer = to(endpoint);          
090            }
091            return answer;
092        }
093    
094    
095        /**
096         * Adds the custom processor to this destination
097         */
098        public ProcessorBuilder<E> process(Processor<E> processor) {
099            ConstantProcessorBuilder<E> answer = new ConstantProcessorBuilder<E>(processor);
100            addProcessBuilder(answer);
101            return answer;
102        }
103    
104    
105    
106        /**
107         * Creates a predicate which is applied and only if it is true then
108         * the exchange is forwarded to the destination
109         *
110         * @return the builder for a predicate
111         */
112        public PredicateBuilder<E> filter(Predicate<E> predicate) {
113            PredicateBuilder<E> answer = new PredicateBuilder<E>(this, predicate);
114            addProcessBuilder(answer);
115            return answer;
116        }
117    
118    
119        /**
120         * Creates a choice of one or more predicates with an otherwise clause
121         *
122         * @return the builder for a choice expression
123         */
124        public ChoiceBuilder<E> choice() {
125            ChoiceBuilder<E> answer = new ChoiceBuilder<E>(this);
126            addProcessBuilder(answer);
127            return answer;
128        }
129    
130        /**
131         * Creates a dynamic <a href="http://activemq.apache.org/camel/recipient-list.html">Recipient List</a> pattern.
132         * 
133         * @param valueBuilder
134         */
135        public RecipientListBuilder<E> recipientList(ValueBuilder<E> valueBuilder) {
136            RecipientListBuilder<E> answer = new RecipientListBuilder<E>(this, valueBuilder);
137            addProcessBuilder(answer);
138            return answer;
139        }
140    
141    
142        // Properties
143        //-------------------------------------------------------------------------
144        public RouteBuilder<E> getBuilder() {
145            return builder;
146        }
147    
148        public Endpoint<E> getFrom() {
149            return from;
150        }
151    
152        public void addProcessBuilder(ProcessorBuilder<E> processBuilder) {
153            processBuilders.add(processBuilder);
154        }
155    
156        public void addProcessor(Processor<E> processor) {
157            processors.add(processor);
158        }
159    
160        public Processor<E> createProcessor() {
161            List<Processor<E>> answer = new ArrayList<Processor<E>>();
162    
163            for (ProcessorBuilder<E> processBuilder : processBuilders) {
164                Processor<E> processor = processBuilder.createProcessor();
165                if (processor == null) {
166                    throw new IllegalArgumentException("No processor created for processBuilder: " + processBuilder);
167                }
168                answer.add(processor);
169            }
170            if (answer.size() == 0) {
171                return null;
172            }
173            if (answer.size() == 1) {
174                return answer.get(0);
175            }
176            else {
177                return new CompositeProcessor<E>(answer);
178            }
179        }
180    
181        public List<Processor<E>> getProcessors() {
182            return processors;
183        }
184    
185            public InterceptorBuilder<E> intercept() {
186                    InterceptorBuilder<E> answer = new InterceptorBuilder<E>(this);
187            addProcessBuilder(answer);
188            return answer;
189            }
190            
191            public InterceptorBuilder<E> intercept(InterceptorProcessor<E> interceptor) {
192                    InterceptorBuilder<E> answer = new InterceptorBuilder<E>(this);
193                    answer.add(interceptor);
194            addProcessBuilder(answer);
195            return answer;
196            }
197    }