001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.joran.spi; 015 016import java.util.HashMap; 017import java.util.Map; 018 019/** 020 * A registry which maps a property in a host class to a default class. 021 * 022 * @author Ceki Gülcü 023 * 024 */ 025public class DefaultNestedComponentRegistry { 026 027 Map<HostClassAndPropertyDouble, Class<?>> defaultComponentMap = new HashMap<HostClassAndPropertyDouble, Class<?>>(); 028 Map<String, Class<?>> tagToClassMap = new HashMap<>(); 029 030 031 public void duplicate(DefaultNestedComponentRegistry other) { 032 this.defaultComponentMap.putAll(other.defaultComponentMap); 033 this.tagToClassMap.putAll(other.tagToClassMap); 034 } 035 036 public void add(Class<?> hostClass, String propertyName, Class<?> componentClass) { 037 HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(hostClass, propertyName.toLowerCase()); 038 defaultComponentMap.put(hpDouble, componentClass); 039 tagToClassMap.put(propertyName, componentClass); 040 } 041 042 043 044 public String findDefaultComponentTypeByTag(String tagName) { 045 Class<?> defaultClass = tagToClassMap.get(tagName); 046 if (defaultClass == null) 047 return null; 048 else 049 return defaultClass.getCanonicalName(); 050 } 051 052 public Class<?> findDefaultComponentType(Class<?> hostClass, String propertyName) { 053 propertyName = propertyName.toLowerCase(); 054 while (hostClass != null) { 055 Class<?> componentClass = oneShotFind(hostClass, propertyName); 056 if (componentClass != null) { 057 return componentClass; 058 } 059 hostClass = hostClass.getSuperclass(); 060 } 061 return null; 062 } 063 064 private Class<?> oneShotFind(Class<?> hostClass, String propertyName) { 065 HostClassAndPropertyDouble hpDouble = new HostClassAndPropertyDouble(hostClass, propertyName); 066 return defaultComponentMap.get(hpDouble); 067 } 068 069 070 071}