KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > TreeHandlerManager


1 package info.magnolia.module.admininterface;
2
3 import info.magnolia.cms.beans.config.ObservedManager;
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.ContentUtil;
8 import info.magnolia.cms.util.FactoryUtil;
9 import info.magnolia.commands.CommandsManager;
10
11 import java.lang.reflect.Constructor JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.jcr.RepositoryException;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25
26 public class TreeHandlerManager extends ObservedManager {
27
28     private static final String JavaDoc ND_CLASS = "class";
29
30     private static final String JavaDoc ND_REPOSITORY = "repository";
31
32     private static final String JavaDoc ND_NAME = "name";
33
34     /**
35      * Logger
36      */

37     private static Logger log = LoggerFactory.getLogger(TreeHandlerManager.class);
38
39     /**
40      * Map with repository name/handler class for admin tree. When this servlet will receive a call with a parameter
41      * <code>repository</code>, the corresponding handler will be used top display the admin tree.
42      */

43     private final Map JavaDoc treeHandlers = new HashMap JavaDoc();
44
45     /**
46      * Get the tree handler registered under a particular name.
47      * @param name
48      * @param request
49      * @param response
50      * @return
51      */

52     public AdminTreeMVCHandler getTreeHandler(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
53
54         TreeHandlerConfig th = (TreeHandlerConfig) treeHandlers.get(name);
55
56         if (th == null) {
57             throw new InvalidTreeHandlerException(name);
58         }
59
60         Class JavaDoc treeHandlerClass = th.getHandler();
61
62         try {
63             Constructor JavaDoc constructor = treeHandlerClass.getConstructor(new Class JavaDoc[]{
64                 String JavaDoc.class,
65                 HttpServletRequest JavaDoc.class,
66                 HttpServletResponse JavaDoc.class});
67             AdminTreeMVCHandler newInstance = (AdminTreeMVCHandler) constructor.newInstance(new Object JavaDoc[]{
68                 name,
69                 request,
70                 response});
71             ContentUtil.setProperties(newInstance, th.getTreeDefinition());
72             return newInstance;
73         }
74         catch (Exception JavaDoc e) {
75             throw new InvalidTreeHandlerException(name, e);
76         }
77     }
78
79     protected void registerTreeHandler(String JavaDoc name, String JavaDoc repository, Class JavaDoc treeHandler, Content treeDefinition) {
80         log.info("Registering tree handler {}", name); //$NON-NLS-1$
81
treeHandlers.put(name, new TreeHandlerConfig(treeHandler, repository, treeDefinition));
82     }
83
84     protected void onRegister(Content defNode) {
85         Collection JavaDoc trees = defNode.getChildren(ItemType.CONTENTNODE.getSystemName());
86         for (Iterator JavaDoc iter = trees.iterator(); iter.hasNext();) {
87             Content tree = (Content) iter.next();
88             String JavaDoc name = tree.getNodeData(ND_NAME).getString();
89
90             if (StringUtils.isEmpty(name)) {
91                 name = tree.getName();
92             }
93
94             String JavaDoc repository = tree.getNodeData(ND_REPOSITORY).getString();
95             String JavaDoc className = tree.getNodeData(ND_CLASS).getString();
96
97             if (StringUtils.isEmpty(repository)) {
98                 repository = name;
99             }
100
101             try {
102                 this.registerTreeHandler(name, repository, ClassUtil.classForName(className), tree);
103             }
104             catch (ClassNotFoundException JavaDoc e) {
105                 log.error("Can't register tree handler [{}]: class [{}] not found", name, className);
106             }
107
108             // register commands if defined
109
try {
110                 if (tree.hasContent("commands")) {
111                     log.error("the definition of commands at the tree level is not longer supported. Move them to the modules commands node! [" + tree.getHandle() + "]");
112                 }
113             }
114             catch (RepositoryException e) {
115                 log.error("can't check commands node of the tree node [" + tree.getHandle() + "]", e);
116             }
117         }
118     }
119
120     /**
121      * @return Returns the instance.
122      */

123     public static TreeHandlerManager getInstance() {
124         return (TreeHandlerManager) FactoryUtil.getSingleton(TreeHandlerManager.class);
125     }
126
127     /**
128      * Clear the handlers
129      */

130     protected void onClear() {
131         this.treeHandlers.clear();
132     }
133
134     class TreeHandlerConfig {
135
136         private Class JavaDoc handler;
137
138         private String JavaDoc repository;
139
140         private Content treeDefinition;
141
142         TreeHandlerConfig(Class JavaDoc handler, String JavaDoc repository, Content treeDefinition) {
143             this.handler = handler;
144             this.repository = repository;
145             this.treeDefinition = treeDefinition;
146         }
147
148         public Class JavaDoc getHandler() {
149             return this.handler;
150         }
151
152         public String JavaDoc getRepository() {
153             return this.repository;
154         }
155
156         public Content getTreeDefinition() {
157             return treeDefinition;
158         }
159     }
160
161 }
162
Popular Tags