1 16 17 package org.apache.log4j.or; 18 19 import org.apache.log4j.spi.RendererSupport; 20 import org.apache.log4j.helpers.LogLog; 21 import org.apache.log4j.helpers.Loader; 22 import org.apache.log4j.helpers.OptionConverter; 23 import java.util.Hashtable ; 24 25 30 public class RendererMap { 31 32 Hashtable map; 33 34 static ObjectRenderer defaultRenderer = new DefaultRenderer(); 35 36 public 37 RendererMap() { 38 map = new Hashtable (); 39 } 40 41 44 static 45 public 46 void addRenderer(RendererSupport repository, String renderedClassName, 47 String renderingClassName) { 48 LogLog.debug("Rendering class: ["+renderingClassName+"], Rendered class: ["+ 49 renderedClassName+"]."); 50 ObjectRenderer renderer = (ObjectRenderer) 51 OptionConverter.instantiateByClassName(renderingClassName, 52 ObjectRenderer.class, 53 null); 54 if(renderer == null) { 55 LogLog.error("Could not instantiate renderer ["+renderingClassName+"]."); 56 return; 57 } else { 58 try { 59 Class renderedClass = Loader.loadClass(renderedClassName); 60 repository.setRenderer(renderedClass, renderer); 61 } catch(ClassNotFoundException e) { 62 LogLog.error("Could not find class ["+renderedClassName+"].", e); 63 } 64 } 65 } 66 67 68 74 public 75 String findAndRender(Object o) { 76 if(o == null) 77 return null; 78 else 79 return get(o.getClass()).doRender(o); 80 } 81 82 83 86 public 87 ObjectRenderer get(Object o) { 88 if(o == null) 89 return null; 90 else 91 return get(o.getClass()); 92 } 93 94 95 144 public 145 ObjectRenderer get(Class clazz) { 146 ObjectRenderer r = null; 148 for(Class c = clazz; c != null; c = c.getSuperclass()) { 149 r = (ObjectRenderer) map.get(c); 151 if(r != null) { 152 return r; 153 } 154 r = searchInterfaces(c); 155 if(r != null) 156 return r; 157 } 158 return defaultRenderer; 159 } 160 161 ObjectRenderer searchInterfaces(Class c) { 162 164 ObjectRenderer r = (ObjectRenderer) map.get(c); 165 if(r != null) { 166 return r; 167 } else { 168 Class [] ia = c.getInterfaces(); 169 for(int i = 0; i < ia.length; i++) { 170 r = searchInterfaces(ia[i]); 171 if(r != null) 172 return r; 173 } 174 } 175 return null; 176 } 177 178 179 public 180 ObjectRenderer getDefaultRenderer() { 181 return defaultRenderer; 182 } 183 184 185 public 186 void clear() { 187 map.clear(); 188 } 189 190 193 public 194 void put(Class clazz, ObjectRenderer or) { 195 map.put(clazz, or); 196 } 197 } 198 | Popular Tags |