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      public void add(Class<?> hostClass, String propertyName, Class<?> componentClass) {
31          HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(hostClass, propertyName.toLowerCase());
32          defaultComponentMap.put(hpDouble, componentClass);
33          tagToClassMap.put(propertyName, componentClass);
34      }
35  
36      public String findDefaultComponentTypeByTag(String tagName) {
37          Class<?> defaultClass = tagToClassMap.get(tagName);
38          if (defaultClass == null)
39              return null;
40          else
41              return defaultClass.getCanonicalName();
42      }
43  
44      public Class<?> findDefaultComponentType(Class<?> hostClass, String propertyName) {
45          propertyName = propertyName.toLowerCase();
46          while (hostClass != null) {
47              Class<?> componentClass = oneShotFind(hostClass, propertyName);
48              if (componentClass != null) {
49                  return componentClass;
50              }
51              hostClass = hostClass.getSuperclass();
52          }
53          return null;
54      }
55  
56      private Class<?> oneShotFind(Class<?> hostClass, String propertyName) {
57          HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(hostClass, propertyName);
58          return defaultComponentMap.get(hpDouble);
59      }
60  
61  }