View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2009, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.joran.spi;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  /**
16   * A registry which maps a property in a host class to a default class.
17   * 
18   * @author Cek Gülcü
19   * 
20   */
21  public class DefaultNestedComponentRegistry {
22  
23    Map<HostClassAndPropertyDouble, Class> defaultComponentMap = new HashMap<HostClassAndPropertyDouble, Class>();
24  
25    public void add(Class hostClass, String propertyName, Class componentClass) {
26      HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(
27          hostClass, propertyName.toLowerCase());
28      defaultComponentMap.put(hpDouble, componentClass);
29    }
30  
31    public Class findDefaultComponentType(Class hostClass, String propertyName) {
32      propertyName = propertyName.toLowerCase();
33      while (hostClass != null) {
34        Class componentClass = oneShotFind(hostClass, propertyName);
35        if (componentClass != null) {
36          return componentClass;
37        }
38        hostClass = hostClass.getSuperclass();
39      }
40      return null;
41    }
42  
43    private Class oneShotFind(Class hostClass, String propertyName) {
44      HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(
45          hostClass, propertyName);
46      return defaultComponentMap.get(hpDouble);
47    }
48  
49  }