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.util.IntrospectionSupport; 024import org.apache.camel.util.LRUSoftCache; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028/** 029 * The <a href="http://camel.apache.org/bean.html">Bean Component</a> is for invoking Java beans from Camel. 030 */ 031public class BeanComponent extends UriEndpointComponent { 032 033 private static final Logger LOG = LoggerFactory.getLogger(BeanComponent.class); 034 // use an internal soft cache for BeanInfo as they are costly to introspect 035 // for example the bean language using OGNL expression runs much faster reusing the BeanInfo from this cache 036 private final LRUSoftCache<BeanInfoCacheKey, BeanInfo> cache = new LRUSoftCache<BeanInfoCacheKey, BeanInfo>(1000); 037 038 public BeanComponent() { 039 super(BeanEndpoint.class); 040 } 041 042 public BeanComponent(Class<? extends Endpoint> endpointClass) { 043 super(endpointClass); 044 } 045 046 // Implementation methods 047 //----------------------------------------------------------------------- 048 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { 049 BeanEndpoint endpoint = new BeanEndpoint(uri, this); 050 endpoint.setBeanName(remaining); 051 setProperties(endpoint, parameters); 052 053 // the bean.xxx options is for the bean 054 Map<String, Object> options = IntrospectionSupport.extractProperties(parameters, "bean."); 055 endpoint.setParameters(options); 056 return endpoint; 057 } 058 059 BeanInfo getBeanInfoFromCache(BeanInfoCacheKey key) { 060 return cache.get(key); 061 } 062 063 void addBeanInfoToCache(BeanInfoCacheKey key, BeanInfo beanInfo) { 064 cache.put(key, beanInfo); 065 } 066 067 @Override 068 protected void doShutdown() throws Exception { 069 if (LOG.isDebugEnabled()) { 070 LOG.debug("Clearing BeanInfo cache[size={}, hits={}, misses={}, evicted={}]", new Object[]{cache.size(), cache.getHits(), cache.getMisses(), cache.getEvicted()}); 071 } 072 cache.clear(); 073 } 074}