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.component.bean;
018
019 import java.lang.reflect.Method;
020 import java.util.Map;
021
022 import org.apache.camel.CamelContext;
023 import org.apache.camel.util.CastUtils;
024 import org.apache.camel.util.LRUCache;
025
026 /**
027 * Represents a cache of MethodInfo objects to avoid the expense of introspection for each invocation of a method
028 * via a proxy
029 *
030 * @version $Revision: 835276 $
031 */
032 public class MethodInfoCache {
033 private final CamelContext camelContext;
034 private Map<Method, MethodInfo> methodCache;
035 private Map<Class<?>, BeanInfo> classCache;
036
037 public MethodInfoCache(CamelContext camelContext) {
038 this(camelContext, 1000, 10000);
039 }
040
041 @SuppressWarnings("unchecked")
042 public MethodInfoCache(CamelContext camelContext, int classCacheSize, int methodCacheSize) {
043 this(camelContext, createClassCache(classCacheSize), createMethodCache(methodCacheSize));
044 }
045
046 public MethodInfoCache(CamelContext camelContext, Map<Class<?>, BeanInfo> classCache, Map<Method, MethodInfo> methodCache) {
047 this.camelContext = camelContext;
048 this.classCache = classCache;
049 this.methodCache = methodCache;
050 }
051
052 public synchronized MethodInfo getMethodInfo(Method method) {
053 MethodInfo answer = methodCache.get(method);
054 if (answer == null) {
055 answer = createMethodInfo(method);
056 methodCache.put(method, answer);
057 }
058 return answer;
059 }
060
061 protected MethodInfo createMethodInfo(Method method) {
062 Class<?> declaringClass = method.getDeclaringClass();
063 BeanInfo info = getBeanInfo(declaringClass);
064 return info.getMethodInfo(method);
065 }
066
067 protected synchronized BeanInfo getBeanInfo(Class<?> declaringClass) {
068 BeanInfo beanInfo = classCache.get(declaringClass);
069 if (beanInfo == null) {
070 beanInfo = createBeanInfo(declaringClass);
071 classCache.put(declaringClass, beanInfo);
072 }
073 return beanInfo;
074 }
075
076 protected BeanInfo createBeanInfo(Class<?> declaringClass) {
077 return new BeanInfo(camelContext, declaringClass);
078 }
079
080 protected static Map<Object, Object> createLruCache(int size) {
081 return new LRUCache<Object, Object>(size);
082 }
083
084 private static Map<Class<?>, BeanInfo> createClassCache(int size) {
085 return CastUtils.cast(createLruCache(size));
086 }
087
088 private static Map<Method, MethodInfo> createMethodCache(int size) {
089 return CastUtils.cast(createLruCache(size));
090 }
091 }