|
What this is
Other links
The source code/* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE.txt file. */ package org.apache.log4j.or; import org.apache.log4j.spi.RendererSupport; import org.apache.log4j.helpers.LogLog; import org.apache.log4j.helpers.Loader; import org.apache.log4j.helpers.OptionConverter; import java.util.Hashtable; /** Map class objects to an {@link ObjectRenderer}. @author Ceki Gülcü @since version 1.0 */ public class RendererMap { Hashtable map; static ObjectRenderer defaultRenderer = new DefaultRenderer(); public RendererMap() { map = new Hashtable(); } /** Add a renderer to a hierarchy passed as parameter. */ static public void addRenderer(RendererSupport repository, String renderedClassName, String renderingClassName) { LogLog.debug("Rendering class: ["+renderingClassName+"], Rendered class: ["+ renderedClassName+"]."); ObjectRenderer renderer = (ObjectRenderer) OptionConverter.instantiateByClassName(renderingClassName, ObjectRenderer.class, null); if(renderer == null) { LogLog.error("Could not instantiate renderer ["+renderingClassName+"]."); return; } else { try { Class renderedClass = Loader.loadClass(renderedClassName); repository.setRenderer(renderedClass, renderer); } catch(ClassNotFoundException e) { LogLog.error("Could not find class ["+renderedClassName+"].", e); } } } /** Find the appropriate renderer for the class type of the
This search algorithm is not the most natural, although it is
particularly easy to implement. Future log4j versions
may implement a more intuitive search
algorithm. However, the present algorithm should be acceptable in
the vast majority of circumstances.
*/
public
ObjectRenderer get(Class clazz) {
//System.out.println("\nget: "+clazz);
ObjectRenderer r = null;
for(Class c = clazz; c != null; c = c.getSuperclass()) {
//System.out.println("Searching for class: "+c);
r = (ObjectRenderer) map.get(c);
if(r != null) {
return r;
}
r = searchInterfaces(c);
if(r != null)
return r;
}
return defaultRenderer;
}
ObjectRenderer searchInterfaces(Class c) {
//System.out.println("Searching interfaces of class: "+c);
ObjectRenderer r = (ObjectRenderer) map.get(c);
if(r != null) {
return r;
} else {
Class[] ia = c.getInterfaces();
for(int i = 0; i < ia.length; i++) {
r = searchInterfaces(ia[i]);
if(r != null)
return r;
}
}
return null;
}
public
ObjectRenderer getDefaultRenderer() {
return defaultRenderer;
}
public
void clear() {
map.clear();
}
/**
Register an {@link ObjectRenderer} for |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.