View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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 Cek 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(
31          hostClass, propertyName.toLowerCase());
32      defaultComponentMap.put(hpDouble, componentClass);
33    }
34  
35    public Class findDefaultComponentType(Class hostClass, String propertyName) {
36      propertyName = propertyName.toLowerCase();
37      while (hostClass != null) {
38        Class componentClass = oneShotFind(hostClass, propertyName);
39        if (componentClass != null) {
40          return componentClass;
41        }
42        hostClass = hostClass.getSuperclass();
43      }
44      return null;
45    }
46  
47    private Class oneShotFind(Class hostClass, String propertyName) {
48      HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(
49          hostClass, propertyName);
50      return defaultComponentMap.get(hpDouble);
51    }
52  
53  }