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        ObjectHelper.notNull(bean, "bean");
044
045        this.bean = bean;
046        this.beanInfo = new BeanInfo(context, bean.getClass());
047    }
048
049    public ConstantBeanHolder(Object bean, CamelContext context, ParameterMappingStrategy parameterMappingStrategy) {
050        ObjectHelper.notNull(bean, "bean");
051
052        this.bean = bean;
053        this.beanInfo = new BeanInfo(context, bean.getClass(), parameterMappingStrategy);
054    }
055
056    @Override
057    public String toString() {
058        // avoid invoke toString on bean as it may be a remote proxy
059        return ObjectHelper.className(bean) + "(" + ObjectHelper.getIdentityHashCode(bean) + ")";
060    }
061
062    public Object getBean()  {
063        return bean;
064    }
065
066    public Processor getProcessor() {
067        if (this.processor == null) {
068            this.processor = CamelContextHelper.convertTo(beanInfo.getCamelContext(), Processor.class, bean);
069        }
070        return this.processor;
071    }
072
073    public boolean supportProcessor() {
074        return true;
075    }
076
077    public BeanInfo getBeanInfo() {
078        return beanInfo;
079    }
080
081    public BeanInfo getBeanInfo(Object bean) {
082        return null;
083    }
084}