View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 v1.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  package ch.qos.logback.core.joran.spi;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  /**
20   * A registry which maps a property in a host class to a default class.
21   * 
22   * @author Ceki Gülcü
23   * 
24   */
25  public class DefaultNestedComponentRegistry {
26  
27      Map<HostClassAndPropertyDouble, Class<?>> defaultComponentMap = new HashMap<HostClassAndPropertyDouble, Class<?>>();
28      Map<String, Class<?>> tagToClassMap = new HashMap<>();
29  
30  
31      public void duplicate(DefaultNestedComponentRegistry other) {
32          this.defaultComponentMap.putAll(other.defaultComponentMap);
33          this.tagToClassMap.putAll(other.tagToClassMap);
34      }
35  
36      public void add(Class<?> hostClass, String propertyName, Class<?> componentClass) {
37          HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(hostClass, propertyName.toLowerCase());
38          defaultComponentMap.put(hpDouble, componentClass);
39          tagToClassMap.put(propertyName, componentClass);
40      }
41  
42  
43  
44      public String findDefaultComponentTypeByTag(String tagName) {
45          Class<?> defaultClass = tagToClassMap.get(tagName);
46          if (defaultClass == null)
47              return null;
48          else
49              return defaultClass.getCanonicalName();
50      }
51  
52      public Class<?> findDefaultComponentType(Class<?> hostClass, String propertyName) {
53          propertyName = propertyName.toLowerCase();
54          while (hostClass != null) {
55              Class<?> componentClass = oneShotFind(hostClass, propertyName);
56              if (componentClass != null) {
57                  return componentClass;
58              }
59              hostClass = hostClass.getSuperclass();
60          }
61          return null;
62      }
63  
64      private Class<?> oneShotFind(Class<?> hostClass, String propertyName) {
65          HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(hostClass, propertyName);
66          return defaultComponentMap.get(hpDouble);
67      }
68  
69      
70  
71  }