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: 776 $ 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 @Override 067 public String getShortName() { 068 return "bean"; 069 } 070 071 public String getRef() { 072 return ref; 073 } 074 075 public void setRef(String ref) { 076 this.ref = ref; 077 } 078 079 public String getMethod() { 080 return method; 081 } 082 083 public void setMethod(String method) { 084 this.method = method; 085 } 086 087 public void setBean(Object bean) { 088 this.bean = bean; 089 } 090 091 public Class getBeanType() { 092 return beanType; 093 } 094 095 public void setBeanType(Class beanType) { 096 this.beanType = beanType; 097 } 098 099 @Override 100 public Processor createProcessor(RouteContext routeContext) { 101 BeanProcessor answer; 102 if (ref != null) { 103 answer = new BeanProcessor(new RegistryBean(routeContext.getCamelContext(), ref)); 104 } else { 105 if (bean == null) { 106 ObjectHelper.notNull(beanType, "bean, ref or beanType"); 107 bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), beanType); 108 } 109 answer = new BeanProcessor(bean, routeContext.getCamelContext()); 110 } 111 if (method != null) { 112 answer.setMethod(method); 113 } 114 return answer; 115 } 116 117 @Override 118 public String getLabel() { 119 if (ref != null) { 120 String methodText = ""; 121 if (method != null) { 122 methodText = " method: " + method; 123 } 124 return "ref: " + ref + methodText; 125 } else if (bean != null) { 126 return bean.toString(); 127 } else if (beanType != null) { 128 return beanType.getName(); 129 } else { 130 return ""; 131 } 132 } 133 }