1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.admininterface;
35
36 import info.magnolia.cms.beans.config.ObservedManager;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.ItemType;
39 import info.magnolia.cms.util.SystemContentWrapper;
40 import info.magnolia.content2bean.Content2BeanUtil;
41 import info.magnolia.objectfactory.Classes;
42 import info.magnolia.objectfactory.Components;
43
44 import java.lang.reflect.Constructor;
45 import java.util.Collection;
46 import java.util.HashMap;
47 import java.util.Map;
48
49 import javax.jcr.RepositoryException;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52
53 import org.apache.commons.lang.StringUtils;
54
55 public class TreeHandlerManager<H extends AdminTreeMVCHandler> extends ObservedManager {
56
57 private static final String ND_CLASS = "class";
58
59 private static final String ND_REPOSITORY = "repository";
60
61 private static final String ND_NAME = "name";
62
63
64
65
66
67 private final Map<String, TreeHandlerConfig> treeHandlers = new HashMap<String, TreeHandlerConfig>();
68
69
70
71
72
73
74
75
76 public AdminTreeMVCHandler getTreeHandler(String name, HttpServletRequest request, HttpServletResponse response) {
77
78 TreeHandlerConfig th = treeHandlers.get(name);
79
80 if (th == null) {
81 throw new InvalidTreeHandlerException(name);
82 }
83
84 Class<H> treeHandlerClass = th.getHandler();
85
86 try {
87 Constructor<H> constructor = treeHandlerClass.getConstructor(String.class, HttpServletRequest.class, HttpServletResponse.class);
88 AdminTreeMVCHandler newInstance = constructor.newInstance(name, request, response);
89 Content2BeanUtil.setProperties(newInstance, th.getTreeDefinition(), true);
90 return newInstance;
91 }
92 catch (Exception e) {
93 throw new InvalidTreeHandlerException(name, e);
94 }
95 }
96
97 protected void registerTreeHandler(String name, String repository, Class<H> treeHandler, Content treeDefinition) {
98 log.info("Registering tree handler {}", name);
99 treeHandlers.put(name, new TreeHandlerConfig(treeHandler, repository, treeDefinition));
100 }
101
102 protected void onRegister(Content defNode) {
103 Collection<Content> trees = defNode.getChildren(ItemType.CONTENTNODE.getSystemName());
104 for (Object tree1 : trees) {
105 Content tree = (Content) tree1;
106 String name = tree.getNodeData(ND_NAME).getString();
107
108 if (StringUtils.isEmpty(name)) {
109 name = tree.getName();
110 }
111
112 String repository = tree.getNodeData(ND_REPOSITORY).getString();
113 String className = tree.getNodeData(ND_CLASS).getString();
114
115 if (StringUtils.isEmpty(repository)) {
116 repository = name;
117 }
118
119 try {
120 final Class<H> treeHandler = Classes.getClassFactory().forName(className);
121 this.registerTreeHandler(name, repository, treeHandler, new SystemContentWrapper(tree));
122 }
123 catch (ClassNotFoundException e) {
124 log.error("Can't register tree handler [{}]: class [{}] not found", name, className);
125 }
126
127
128 try {
129
130 if (tree.hasContent("commands")) {
131 log.error("The definition of commands at the tree level is no longer supported. Move them to the modules commands node! [" + tree.getHandle() + "]");
132 }
133 }
134 catch (RepositoryException e) {
135 log.error("Can't check commands node of the tree node [" + tree.getHandle() + "]", e);
136 }
137 }
138 }
139
140
141
142
143 public static <H extends AdminTreeMVCHandler> TreeHandlerManager<H> getInstance() {
144 return Components.getSingleton(TreeHandlerManager.class);
145 }
146
147
148
149
150 protected void onClear() {
151 this.treeHandlers.clear();
152 }
153
154 class TreeHandlerConfig {
155
156 private Class<H> handler;
157
158 private String repository;
159
160 private Content treeDefinition;
161
162 TreeHandlerConfig(Class<H> handler, String repository, Content treeDefinition) {
163 this.handler = handler;
164 this.repository = repository;
165 this.treeDefinition = treeDefinition;
166 }
167
168 public Class<H> getHandler() {
169 return this.handler;
170 }
171
172 public String getRepository() {
173 return this.repository;
174 }
175
176 public Content getTreeDefinition() {
177 return treeDefinition;
178 }
179 }
180
181 }