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 */
017package org.apache.camel.component.bean;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.Processor;
021import org.apache.camel.util.CamelContextHelper;
022import org.apache.camel.util.ObjectHelper;
023
024/**
025 * A constant (singleton) bean implementation of {@link BeanHolder}
026 *
027 * @version 
028 */
029public class ConstantBeanHolder implements BeanHolder {
030    private final Object bean;
031    private final BeanInfo beanInfo;
032    private Processor processor;
033
034    public ConstantBeanHolder(Object bean, BeanInfo beanInfo) {
035        ObjectHelper.notNull(bean, "bean");
036        ObjectHelper.notNull(beanInfo, "beanInfo");
037
038        this.bean = bean;
039        this.beanInfo = beanInfo;
040    }
041
042    public ConstantBeanHolder(Object bean, CamelContext context) {
043        this(bean, new BeanInfo(context, bean.getClass()));
044    }
045
046    public ConstantBeanHolder(Object bean, CamelContext context, ParameterMappingStrategy parameterMappingStrategy) {
047        this(bean, new BeanInfo(context, bean.getClass(), parameterMappingStrategy));
048    }
049
050    @Override
051    public String toString() {
052        // avoid invoke toString on bean as it may be a remote proxy
053        return ObjectHelper.className(bean) + "(" + ObjectHelper.getIdentityHashCode(bean) + ")";
054    }
055
056    public Object getBean()  {
057        return bean;
058    }
059
060    public Processor getProcessor() {
061        if (this.processor == null) {
062            this.processor = CamelContextHelper.convertTo(beanInfo.getCamelContext(), Processor.class, bean);
063        }
064        return this.processor;
065    }
066
067    public boolean supportProcessor() {
068        return true;
069    }
070
071    public BeanInfo getBeanInfo() {
072        return beanInfo;
073    }
074
075    public BeanInfo getBeanInfo(Object bean) {
076        return null;
077    }
078}