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 javax.xml.bind.annotation.XmlAccessType; 020 import javax.xml.bind.annotation.XmlAccessorType; 021 import javax.xml.bind.annotation.XmlAttribute; 022 import javax.xml.bind.annotation.XmlRootElement; 023 import javax.xml.bind.annotation.XmlTransient; 024 025 import org.apache.camel.Processor; 026 import org.apache.camel.component.bean.BeanProcessor; 027 import org.apache.camel.component.bean.RegistryBean; 028 import org.apache.camel.spi.RouteContext; 029 import org.apache.camel.util.CamelContextHelper; 030 import org.apache.camel.util.ObjectHelper; 031 032 /** 033 * Represents an XML <bean/> element 034 * 035 * @version $Revision: 41908 $ 036 */ 037 @XmlRootElement(name = "bean") 038 @XmlAccessorType(XmlAccessType.FIELD) 039 public class BeanRef extends OutputType<ProcessorType> { 040 @XmlAttribute(required = false) 041 private String ref; 042 @XmlAttribute(required = false) 043 private String method; 044 @XmlAttribute(required = false) 045 private Class beanType; 046 @XmlTransient 047 private Object bean; 048 049 public BeanRef() { 050 } 051 052 public BeanRef(String ref) { 053 this.ref = ref; 054 } 055 056 public BeanRef(String ref, String method) { 057 this.ref = ref; 058 this.method = method; 059 } 060 061 @Override 062 public String toString() { 063 return "Bean[" + getLabel() + "]"; 064 } 065 066 public String getRef() { 067 return ref; 068 } 069 070 public void setRef(String ref) { 071 this.ref = ref; 072 } 073 074 public String getMethod() { 075 return method; 076 } 077 078 public void setMethod(String method) { 079 this.method = method; 080 } 081 082 public void setBean(Object bean) { 083 this.bean = bean; 084 } 085 086 public Class getBeanType() { 087 return beanType; 088 } 089 090 public void setBeanType(Class beanType) { 091 this.beanType = beanType; 092 } 093 094 @Override 095 public Processor createProcessor(RouteContext routeContext) { 096 BeanProcessor answer; 097 if (ref != null) { 098 answer = new BeanProcessor(new RegistryBean(routeContext.getCamelContext(), ref)); 099 } else { 100 if (bean == null) { 101 ObjectHelper.notNull(beanType, "bean, ref or beanType"); 102 bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), beanType); 103 } 104 answer = new BeanProcessor(bean, routeContext.getCamelContext()); 105 } 106 if (method != null) { 107 answer.setMethod(method); 108 } 109 return answer; 110 } 111 112 @Override 113 public String getLabel() { 114 if (ref != null) { 115 String methodText = ""; 116 if (method != null) { 117 methodText = " method: " + method; 118 } 119 return "ref: " + ref + methodText; 120 } else if (bean != null) { 121 return bean.toString(); 122 } else if (beanType != null) { 123 return beanType.getName(); 124 } else { 125 return ""; 126 } 127 } 128 }