View Javadoc
1   package ch.qos.logback.core.joran.util.beans;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import ch.qos.logback.core.Context;
7   import ch.qos.logback.core.spi.ContextAwareBase;
8   
9   /**
10   *
11   * Cache for {@link BeanDescription} instances. All the cache users which use
12   * the same instance of BeanDescriptionCache can profit from each others cached
13   * bean descriptions. 
14   * 
15   * <p>The cache is not thread-safe and should not be shared across configurator instances.
16   *
17   * @author urechm
18   *
19   */
20  public class BeanDescriptionCache extends ContextAwareBase {
21  
22      private Map<Class<?>, BeanDescription> classToBeanDescription = new HashMap<Class<?>, BeanDescription>();
23      private BeanDescriptionFactory beanDescriptionFactory;
24  
25      public BeanDescriptionCache(Context context) {
26          setContext(context);
27      }
28  
29      private BeanDescriptionFactory getBeanDescriptionFactory() {
30          if (beanDescriptionFactory == null) {
31              beanDescriptionFactory = new BeanDescriptionFactory(getContext());
32          }
33          return beanDescriptionFactory;
34      }
35  
36      /**
37       * Returned bean descriptions are hold in a cache. If the cache does not
38       * contain a description for a given class, a new bean description is
39       * created and put in the cache, before it is returned.
40       *
41       * @param clazz
42       *            to get a bean description for.
43       * @return a bean description for the given class.
44       */
45      public BeanDescription getBeanDescription(Class<?> clazz) {
46          if (!classToBeanDescription.containsKey(clazz)) {
47              BeanDescription beanDescription = getBeanDescriptionFactory().create(clazz);
48              classToBeanDescription.put(clazz, beanDescription);
49          }
50          return classToBeanDescription.get(clazz);
51      }
52  
53  }