1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.joran.util.beans;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import ch.qos.logback.core.Context;
21  import ch.qos.logback.core.spi.ContextAwareBase;
22  
23  /**
24   *
25   * Cache for {@link BeanDescription} instances. All the cache users which use
26   * the same instance of BeanDescriptionCache can profit from each others cached
27   * bean descriptions.
28   * 
29   * <p>
30   * The cache is not thread-safe and should not be shared across configurator
31   * instances.
32   *
33   * @author urechm
34   *
35   */
36  public class BeanDescriptionCache extends ContextAwareBase {
37  
38      private Map<Class<?>, BeanDescription> classToBeanDescription = new HashMap<Class<?>, BeanDescription>();
39      private BeanDescriptionFactory beanDescriptionFactory;
40  
41      public BeanDescriptionCache(Context context) {
42          setContext(context);
43      }
44  
45      private BeanDescriptionFactory getBeanDescriptionFactory() {
46          if (beanDescriptionFactory == null) {
47              beanDescriptionFactory = new BeanDescriptionFactory(getContext());
48          }
49          return beanDescriptionFactory;
50      }
51  
52      /**
53       * Returned bean descriptions are hold in a cache. If the cache does not contain
54       * a description for a given class, a new bean description is created and put in
55       * the cache, before it is returned.
56       *
57       * @param clazz to get a bean description for.
58       * @return a bean description for the given class.
59       */
60      public BeanDescription getBeanDescription(Class<?> clazz) {
61          if (!classToBeanDescription.containsKey(clazz)) {
62              BeanDescription beanDescription = getBeanDescriptionFactory().create(clazz);
63              classToBeanDescription.put(clazz, beanDescription);
64          }
65          return classToBeanDescription.get(clazz);
66      }
67  
68  }