KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > TemplateRendererManager


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 JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.apache.commons.lang.StringUtils;
16
17
18 /**
19  * @author fgiust
20  * @version $Revision: 6893 $ ($Author: philipp $)
21  */

22 public class TemplateRendererManager extends ObservedManager {
23
24     private static final String JavaDoc ND_RENDERER = "renderer";
25
26     private static final String JavaDoc ND_TYPE = "type";
27
28     private Map JavaDoc renderers = new HashMap JavaDoc();
29
30     /**
31      * @return Returns the instance.
32      */

33     public static TemplateRendererManager getInstance() {
34         return (TemplateRendererManager) FactoryUtil.getSingleton(TemplateRendererManager.class);
35     }
36
37     /**
38      * @see info.magnolia.cms.beans.config.ObservedManager#onRegister(info.magnolia.cms.core.Content)
39      */

40     protected void onRegister(Content node) {
41         Collection JavaDoc list = node.getChildren(ItemType.CONTENTNODE);
42         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
43             Content tr = (Content) iter.next();
44             String JavaDoc type = tr.getNodeData(ND_TYPE).getString();
45             String JavaDoc 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 JavaDoc[]{
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 JavaDoc 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 JavaDoc[]{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     /**
82      * @see info.magnolia.cms.beans.config.ObservedManager#onClear()
83      */

84     protected void onClear() {
85         this.renderers.clear();
86     }
87
88     public void registerTemplateRenderer(String JavaDoc type, TemplateRenderer instance) {
89         synchronized (renderers) {
90             renderers.put(type, instance);
91         }
92     }
93
94     public TemplateRenderer getRenderer(String JavaDoc type) {
95         return (TemplateRenderer) renderers.get(type);
96     }
97
98 }
99
Popular Tags