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