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.util.ContentUtil;
39 import info.magnolia.cms.util.SystemContentWrapper;
40 import info.magnolia.cms.util.NodeDataUtil;
41 import info.magnolia.content2bean.Content2BeanException;
42 import info.magnolia.content2bean.Content2BeanUtil;
43 import info.magnolia.content2bean.TransformationState;
44 import info.magnolia.content2bean.impl.Content2BeanTransformerImpl;
45 import info.magnolia.objectfactory.Components;
46
47 import java.lang.reflect.Constructor;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.Map;
51
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55 import org.apache.commons.beanutils.BeanUtils;
56 import org.apache.commons.beanutils.ConstructorUtils;
57
58
59
60
61
62
63 public class PageHandlerManager extends ObservedManager {
64
65
66
67
68 private final Map dialogPageHandlers = new HashMap();
69
70
71
72
73
74
75
76
77 public PageMVCHandler getPageHandler(String name, HttpServletRequest request, HttpServletResponse response) {
78
79 PageDefinition pageDefinition = (PageDefinition) dialogPageHandlers.get(name);
80
81 if (pageDefinition == null) {
82 log.warn("Page definition not found: \"{}\"", name);
83 return null;
84 }
85 return pageDefinition.newInstance(name, request, response);
86 }
87
88
89
90
91
92 protected void onRegister(Content defNode) {
93
94
95 for (Iterator iter = ContentUtil.getAllChildren(defNode).iterator(); iter.hasNext();) {
96 Content pageNode = (Content) iter.next();
97
98 PageDefinition pd = new RepositoryPageDefinition(new SystemContentWrapper(pageNode));
99 registerPageDefinition(pd);
100 }
101
102 }
103
104 public void registerPageDefinition(PageDefinition pageDefinition) {
105 dialogPageHandlers.put(pageDefinition.getName(), pageDefinition);
106 }
107
108
109
110
111 public void registerPageDefinition(String name, PageDefinition pageDefinition) {
112 dialogPageHandlers.put(name, pageDefinition);
113 }
114
115
116
117
118 public static PageHandlerManager getInstance() {
119 return Components.getSingleton(PageHandlerManager.class);
120 }
121
122 protected void onClear() {
123 this.dialogPageHandlers.clear();
124 }
125
126 public static interface PageDefinition {
127
128 public String getName();
129
130 public PageMVCHandler newInstance(String name, HttpServletRequest request, HttpServletResponse response);
131 }
132
133
134
135
136
137
138 public static class BasePageDefinition implements PageDefinition {
139 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BasePageDefinition.class);
140
141 private Map defaultProperties = new HashMap();
142
143 private Class handlerClass;
144
145 private String name;
146
147 public BasePageDefinition(String name, Class handlerClass) {
148 this.name = name;
149 this.handlerClass = handlerClass;
150 }
151
152 public Map getDefaultProperties() {
153 return this.defaultProperties;
154 }
155
156 public void setDefaultProperties(Map defaultProperties) {
157 this.defaultProperties = defaultProperties;
158 }
159
160 public Class getHandlerClass() {
161 return this.handlerClass;
162 }
163
164 public void setHandlerClass(Class handlerClass) {
165 this.handlerClass = handlerClass;
166 }
167
168 public String getName() {
169 return this.name;
170 }
171
172 public void setName(String name) {
173 this.name = name;
174 }
175
176 public PageMVCHandler newInstance(String name, HttpServletRequest request, HttpServletResponse response) {
177
178 try {
179 Constructor constructor = getHandlerClass().getConstructor(
180 new Class[]{String.class, HttpServletRequest.class, HttpServletResponse.class});
181 PageMVCHandler page = (PageMVCHandler) constructor.newInstance(new Object[]{name, request, response});
182 BeanUtils.populate(page, getDefaultProperties());
183 return page;
184 }
185 catch (Exception e) {
186 log.error("Can't instantiate page [" + name + "]", e);
187 throw new InvalidDialogPageHandlerException(name, e);
188 }
189
190 }
191 }
192
193 public static class RepositoryPageDefinition implements PageDefinition {
194
195 private Content node;
196
197 public RepositoryPageDefinition(Content node) {
198 this.node = node;
199 }
200
201 public String getName() {
202 return NodeDataUtil.getString(this.node, "name", this.node.getName());
203 }
204
205 public PageMVCHandler newInstance(String name, final HttpServletRequest request,
206 final HttpServletResponse response) {
207 try {
208 return (PageMVCHandler) Content2BeanUtil.toBean(node, true, new Content2BeanTransformerImpl() {
209
210 public Object newBeanInstance(TransformationState state, Map properties)
211 throws Content2BeanException {
212 if (state.getLevel() == 1) {
213 try {
214 return ConstructorUtils.invokeConstructor(
215 state.getCurrentType().getType(),
216 new Object[]{getName(), request, response});
217 }
218 catch (Exception e) {
219 throw new Content2BeanException("no proper constructor found", e);
220 }
221 }
222
223 return super.newBeanInstance(state, properties);
224 }
225 });
226 }
227 catch (Content2BeanException e) {
228 throw new InvalidDialogPageHandlerException(this.getName(), e);
229 }
230 }
231 }
232
233 }