KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.module.admininterface;
14
15 import info.magnolia.cms.beans.config.ObservedManager;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.ItemType;
18 import info.magnolia.cms.util.ClassUtil;
19 import info.magnolia.cms.util.FactoryUtil;
20
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 /**
36  * Manages the page handlers. A page is a very simple dialog without any configuration.
37  * @author philipp
38  */

39 public class PageHandlerManager extends ObservedManager {
40
41     private static final String JavaDoc ND_CLASS = "class";
42
43     private static final String JavaDoc ND_NAME = "name";
44
45     /**
46      * Logger
47      */

48     private static Logger log = LoggerFactory.getLogger(PageHandlerManager.class);
49
50     /**
51      * The handlers
52      */

53     private final Map JavaDoc dialogPageHandlers = new HashMap JavaDoc();
54
55     /**
56      * Find a handler by name
57      * @param name
58      * @param request
59      * @param response
60      * @returnn an instance of the handlers
61      */

62     public PageMVCHandler getPageHandler(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
63
64         Class JavaDoc dialogPageHandlerClass = (Class JavaDoc) dialogPageHandlers.get(name);
65         if (dialogPageHandlerClass == null) {
66             throw new InvalidDialogPageHandlerException(name);
67         }
68
69         try {
70             Constructor JavaDoc constructor = dialogPageHandlerClass.getConstructor(new Class JavaDoc[]{
71                 String JavaDoc.class,
72                 HttpServletRequest JavaDoc.class,
73                 HttpServletResponse JavaDoc.class});
74             return (PageMVCHandler) constructor.newInstance(new Object JavaDoc[]{name, request, response});
75         }
76         catch (Exception JavaDoc e) {
77             log.error("can't instantiate page [" + name + "]", e);
78             throw new InvalidDialogPageHandlerException(name, e);
79         }
80     }
81
82     protected void registerPageHandler(String JavaDoc name, Class JavaDoc dialogPageHandler) {
83         if (log.isDebugEnabled()) {
84             log.debug("Registering page handler [{}]", name); //$NON-NLS-1$
85
}
86         dialogPageHandlers.put(name, dialogPageHandler);
87     }
88
89     /**
90      * register the pages from the config
91      * @param defNode
92      */

93     protected void onRegister(Content defNode) {
94         // read the dialog configuration
95
try {
96             Collection JavaDoc pages = defNode.getChildren(ItemType.CONTENT.getSystemName());
97             pages.addAll(defNode.getChildren(ItemType.CONTENTNODE.getSystemName()));
98             for (Iterator JavaDoc iter = pages.iterator(); iter.hasNext();) {
99                 Content page = (Content) iter.next();
100                 String JavaDoc name = page.getNodeData(ND_NAME).getString();
101                 if (StringUtils.isEmpty(name)) {
102                     name = page.getName();
103                 }
104
105                 String JavaDoc className = page.getNodeData(ND_CLASS).getString();
106                 try {
107                     registerPageHandler(name, ClassUtil.classForName(className));
108                 }
109                 catch (ClassNotFoundException JavaDoc e) {
110                     log.warn("can't find dialogpage handler class " + className, e); //$NON-NLS-1$
111
}
112             }
113         }
114         catch (Exception JavaDoc e) {
115             log.warn("can't find pages configuration", e); //$NON-NLS-1$
116
}
117     }
118
119     /**
120      * @return Returns the instance.
121      */

122     public static PageHandlerManager getInstance() {
123         return (PageHandlerManager) FactoryUtil.getSingleton(PageHandlerManager.class);
124     }
125
126     protected void onClear() {
127         this.dialogPageHandlers.clear();
128     }
129
130 }
131
Popular Tags