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>
16   * The cache is not thread-safe and should not be shared across configurator
17   * instances.
18   *
19   * @author urechm
20   *
21   */
22  public class BeanDescriptionCache extends ContextAwareBase {
23  
24      private Map<Class<?>, BeanDescription> classToBeanDescription = new HashMap<Class<?>, BeanDescription>();
25      private BeanDescriptionFactory beanDescriptionFactory;
26  
27      public BeanDescriptionCache(Context context) {
28          setContext(context);
29      }
30  
31      private BeanDescriptionFactory getBeanDescriptionFactory() {
32          if (beanDescriptionFactory == null) {
33              beanDescriptionFactory = new BeanDescriptionFactory(getContext());
34          }
35          return beanDescriptionFactory;
36      }
37  
38      /**
39       * Returned bean descriptions are hold in a cache. If the cache does not contain
40       * a description for a given class, a new bean description is created and put in
41       * the cache, before it is returned.
42       *
43       * @param clazz to get a bean description for.
44       * @return a bean description for the given class.
45       */
46      public BeanDescription getBeanDescription(Class<?> clazz) {
47          if (!classToBeanDescription.containsKey(clazz)) {
48              BeanDescription beanDescription = getBeanDescriptionFactory().create(clazz);
49              classToBeanDescription.put(clazz, beanDescription);
50          }
51          return classToBeanDescription.get(clazz);
52      }
53  
54  }