1 package info.magnolia.cms.beans.config; 2 3 import info.magnolia.cms.beans.runtime.TemplateRenderer; 4 import info.magnolia.cms.core.Content; 5 import info.magnolia.cms.core.ItemType; 6 import info.magnolia.cms.util.ClassUtil; 7 import info.magnolia.cms.util.FactoryUtil; 8 9 import java.text.MessageFormat ; 10 import java.util.Collection ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import org.apache.commons.lang.StringUtils; 16 17 18 22 public class TemplateRendererManager extends ObservedManager { 23 24 private static final String ND_RENDERER = "renderer"; 25 26 private static final String ND_TYPE = "type"; 27 28 private Map renderers = new HashMap (); 29 30 33 public static TemplateRendererManager getInstance() { 34 return (TemplateRendererManager) FactoryUtil.getSingleton(TemplateRendererManager.class); 35 } 36 37 40 protected void onRegister(Content node) { 41 Collection list = node.getChildren(ItemType.CONTENTNODE); 42 for (Iterator iter = list.iterator(); iter.hasNext();) { 43 Content tr = (Content) iter.next(); 44 String type = tr.getNodeData(ND_TYPE).getString(); 45 String rendererClass = tr.getNodeData(ND_RENDERER).getString(); 46 47 if (StringUtils.isEmpty(type)) { 48 type = tr.getName(); 49 } 50 51 if (StringUtils.isBlank(type) || StringUtils.isBlank(rendererClass)) { 52 log.warn("Can't register template render at {}, type=\"{}\" renderer=\"{}\"", new Object []{ 53 tr.getHandle(), 54 type, 55 rendererClass}); 56 continue; 57 } 58 59 TemplateRenderer renderer; 60 61 try { 62 renderer = (TemplateRenderer) ClassUtil.newInstance(rendererClass); 63 } 64 catch (Exception e) { 65 log.warn( 66 67 MessageFormat.format( 68 "Can't register template render at {0}, type=\"{1}\" renderer=\"{2}\" due to a {3} exception: {4}", 69 new Object []{tr.getHandle(), type, rendererClass, e.getClass().getName(), e.getMessage()}), e); 70 continue; 71 } 72 73 if (log.isDebugEnabled()) { 74 log.debug("Registering template render [{}] for type {}", rendererClass, type); 75 } 76 registerTemplateRenderer(type, renderer); 77 } 78 79 } 80 81 84 protected void onClear() { 85 this.renderers.clear(); 86 } 87 88 public void registerTemplateRenderer(String type, TemplateRenderer instance) { 89 synchronized (renderers) { 90 renderers.put(type, instance); 91 } 92 } 93 94 public TemplateRenderer getRenderer(String type) { 95 return (TemplateRenderer) renderers.get(type); 96 } 97 98 } 99 | Popular Tags |