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.impl.RouteContext;
028    
029    /**
030     * @version $Revision: 1.1 $
031     */
032    @XmlRootElement(name = "bean")
033    @XmlAccessorType(XmlAccessType.FIELD)
034    public class BeanRef extends OutputType {
035        @XmlAttribute(required = true)
036        private String ref;
037        @XmlAttribute(required = false)
038        private String method;
039        @XmlTransient
040        private Object bean;
041    
042        public BeanRef() {
043        }
044    
045        public BeanRef(Object bean) {
046            this.bean = bean;
047        }
048    
049        public BeanRef(String ref, String method) {
050            this.ref = ref;
051            this.method = method;
052        }
053    
054        @Override
055        public String toString() {
056            return "Bean[" + description() + "]";
057        }
058    
059        public String getRef() {
060            return ref;
061        }
062    
063        public void setRef(String ref) {
064            this.ref = ref;
065        }
066    
067        public String getMethod() {
068            return method;
069        }
070    
071        public void setMethod(String method) {
072            this.method = method;
073        }
074    
075        public void setBean(Object bean) {
076            this.bean = bean;
077        }
078    
079        @Override
080        public Processor createProcessor(RouteContext routeContext) {
081            if (bean == null) {
082                bean = routeContext.lookup(getRef(), Object.class);
083            }
084            BeanProcessor answer = new BeanProcessor(bean, routeContext.getCamelContext());
085            if (method != null) {
086                answer.setMethodName(method);
087            }
088            return answer;
089        }
090    
091        protected String description() {
092            if (bean != null) {
093                return bean.toString();
094            } else {
095                String methodText = "";
096                if (method != null) {
097                    methodText = " method: " + method;
098                }
099                return "ref: " + ref + methodText;
100            }
101        }
102    }