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 java.util.Map;
020
021import org.apache.camel.Endpoint;
022import org.apache.camel.impl.UriEndpointComponent;
023import org.apache.camel.spi.Metadata;
024import org.apache.camel.util.IntrospectionSupport;
025import org.apache.camel.util.LRUCache;
026import org.apache.camel.util.LRUCacheFactory;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * The <a href="http://camel.apache.org/bean.html">Bean Component</a> is for invoking Java beans from Camel.
032 */
033public class BeanComponent extends UriEndpointComponent {
034
035    private static final Logger LOG = LoggerFactory.getLogger(BeanComponent.class);
036    // use an internal soft cache for BeanInfo as they are costly to introspect
037    // for example the bean language using OGNL expression runs much faster reusing the BeanInfo from this cache
038    @SuppressWarnings("unchecked")
039    private final Map<BeanInfoCacheKey, BeanInfo> beanInfoCache = LRUCacheFactory.newLRUSoftCache(1000);
040
041    @Metadata(label = "advanced", description = "If enabled, Camel will cache the result of the first Registry look-up."
042        + " Cache can be enabled if the bean in the Registry is defined as a singleton scope.")
043    private Boolean cache;
044
045    public BeanComponent() {
046        super(BeanEndpoint.class);
047    }
048    
049    public BeanComponent(Class<? extends Endpoint> endpointClass) {
050        super(endpointClass);
051    }
052
053    // Implementation methods
054    //-----------------------------------------------------------------------
055    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
056        BeanEndpoint endpoint = new BeanEndpoint(uri, this);
057        endpoint.setBeanName(remaining);
058        endpoint.setCache(cache);
059        setProperties(endpoint, parameters);
060
061        // the bean.xxx options is for the bean
062        Map<String, Object> options = IntrospectionSupport.extractProperties(parameters, "bean.");
063        endpoint.setParameters(options);
064        return endpoint;
065    }
066    
067    BeanInfo getBeanInfoFromCache(BeanInfoCacheKey key) {
068        return beanInfoCache.get(key);
069    }
070
071    void addBeanInfoToCache(BeanInfoCacheKey key, BeanInfo beanInfo) {
072        beanInfoCache.put(key, beanInfo);
073    }
074
075    @Override
076    protected void doShutdown() throws Exception {
077        if (LOG.isDebugEnabled() && beanInfoCache instanceof LRUCache) {
078            LRUCache cache = (LRUCache) this.beanInfoCache;
079            LOG.debug("Clearing BeanInfo cache[size={}, hits={}, misses={}, evicted={}]", cache.size(), cache.getHits(), cache.getMisses(), cache.getEvicted());
080        }
081        beanInfoCache.clear();
082    }
083
084    public Boolean getCache() {
085        return cache;
086    }
087
088    /**
089     * If enabled, Camel will cache the result of the first Registry look-up.
090     * Cache can be enabled if the bean in the Registry is defined as a singleton scope.
091     */
092    public void setCache(Boolean cache) {
093        this.cache = cache;
094    }
095}