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  
29      public void add(Class<?> hostClass, String propertyName, Class<?> componentClass) {
30          HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(hostClass, propertyName.toLowerCase());
31          defaultComponentMap.put(hpDouble, componentClass);
32      }
33  
34      public Class<?> findDefaultComponentType(Class<?> hostClass, String propertyName) {
35          propertyName = propertyName.toLowerCase();
36          while (hostClass != null) {
37              Class<?> componentClass = oneShotFind(hostClass, propertyName);
38              if (componentClass != null) {
39                  return componentClass;
40              }
41              hostClass = hostClass.getSuperclass();
42          }
43          return null;
44      }
45  
46      private Class<?> oneShotFind(Class<?> hostClass, String propertyName) {
47          HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(hostClass, propertyName);
48          return defaultComponentMap.get(hpDouble);
49      }
50  
51  }