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.spring.util;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.camel.builder.RouteBuilder;
023    import org.apache.camel.model.ProcessorType;
024    import org.apache.camel.util.ObjectHelper;
025    
026    /**
027     * A simple {@link RouteBuilder} which can be configured directly with one or more from URIs, zero or more to URIs
028     * and an optional bean processing step.
029     *
030     * @version $Revision: 641676 $
031     */
032    public class SimpleRouteBuilder extends RouteBuilder {
033        private List<String> fromUris = new ArrayList<String>();
034        private List<String> toUris = new ArrayList<String>();
035        private Class beanType;
036        private String beanClass;
037        private String beanRef;
038        private String beanMethod;
039    
040        public void configure() throws Exception {
041            if (fromUris.isEmpty()) {
042                throw new IllegalArgumentException("the fromUris property must contain at least one valid URI");
043            }
044            for (String fromUri : fromUris) {
045                ProcessorType route = from(fromUri);
046    
047                addBeanCall(route);
048                for (String toUri : toUris) {
049                    route = route.to(toUri);
050                }
051            }
052        }
053    
054        // Properties
055        //-------------------------------------------------------------------------
056        public void setFromUri(String uri) {
057            setFromUris(singletonList(uri));
058        }
059    
060        public void setToUri(String uri) {
061            setToUris(singletonList(uri));
062        }
063    
064        public List<String> getFromUris() {
065            return fromUris;
066        }
067    
068        public void setFromUris(List<String> fromUris) {
069            this.fromUris = fromUris;
070        }
071    
072        public List<String> getToUris() {
073            return toUris;
074        }
075    
076        public void setToUris(List<String> toUris) {
077            this.toUris = toUris;
078        }
079    
080        public String getBeanClass() {
081            return beanClass;
082        }
083    
084        public void setBeanClass(String beanClass) {
085            this.beanClass = beanClass;
086        }
087    
088        public String getBeanRef() {
089            return beanRef;
090        }
091    
092        public void setBeanRef(String beanRef) {
093            this.beanRef = beanRef;
094        }
095    
096        public Class getBeanType() {
097            if (beanType == null) {
098                if (beanClass != null) {
099                    beanType = ObjectHelper.loadClass(beanClass, getClass().getClassLoader());
100                }
101            }
102            return beanType;
103        }
104    
105        public void setBeanType(Class beanType) {
106            this.beanType = beanType;
107        }
108    
109        public String getBeanMethod() {
110            return beanMethod;
111        }
112    
113        public void setBeanMethod(String beanMethod) {
114            this.beanMethod = beanMethod;
115        }
116    
117        // Implementation methods
118        //-------------------------------------------------------------------------
119    
120        protected void addBeanCall(ProcessorType route) {
121            Class type = getBeanType();
122            if (type != null) {
123                if (beanMethod != null) {
124                    route = route.bean(type, beanMethod);
125                } else {
126                    route = route.bean(type);
127                }
128            } else if (beanRef != null) {
129                if (beanMethod != null) {
130                    route = route.beanRef(beanRef, beanMethod);
131                } else {
132                    route = route.beanRef(beanRef);
133                }
134            }
135        }
136    
137        protected List<String> singletonList(String value) {
138            List<String> uris = new ArrayList<String>();
139            uris.add(value);
140            return uris;
141        }
142    }