KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.config.Paragraph;
17 import info.magnolia.cms.beans.config.ParagraphManager;
18 import info.magnolia.cms.core.Content;
19 import info.magnolia.cms.core.ItemType;
20 import info.magnolia.cms.util.ClassUtil;
21 import info.magnolia.cms.util.ContentUtil;
22 import info.magnolia.cms.util.FactoryUtil;
23 import info.magnolia.cms.util.NodeDataUtil;
24 import info.magnolia.module.admininterface.dialogs.ParagraphEditDialog;
25
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Map.Entry;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.commons.lang.StringUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 /**
42  * Manages all the dialog handlers
43  * @todo The paragraph dialogs should not differ from the other ones.
44  * @author philipp
45  */

46 public class DialogHandlerManager extends ObservedManager {
47
48     private static final String JavaDoc PARAGRAPH_EDIT_DIALOG = "info.magnolia.module.admininterface.dialogs.ParagraphEditDialog";
49
50     private static final String JavaDoc CLASS = "class";
51
52     private static final String JavaDoc ND_NAME = "name";
53
54     /**
55      * Logger
56      */

57     private Logger log = LoggerFactory.getLogger(DialogHandlerManager.class);
58
59     /**
60      * All handlers are registered here
61      */

62     private final Map JavaDoc dialogHandlers = new HashMap JavaDoc();
63
64     /**
65      * register the dialogs from the config
66      * @param node
67      */

68     protected void onRegister(Content node) {
69         List JavaDoc dialogs = ContentUtil.collectAllChildren(node, ItemType.CONTENT);
70         for (Iterator JavaDoc iter = dialogs.iterator(); iter.hasNext();) {
71             Content dialog = (Content) iter.next();
72             // if this paragraph is used from a dialog register it under the name of the paragraph too
73
registerAsParagraphDialog(node.getHandle(), dialog);
74
75             String JavaDoc name = dialog.getNodeData(ND_NAME).getString();
76             if (StringUtils.isEmpty(name)) {
77                 name = dialog.getName();
78             }
79             String JavaDoc className = NodeDataUtil.getString(dialog, CLASS, PARAGRAPH_EDIT_DIALOG);
80             try {
81                 // there are paragraphs dialogs without a name!
82
registerDialogHandler(name, ClassUtil.classForName(className), dialog);
83             }
84             catch (ClassNotFoundException JavaDoc e) {
85                 log.warn("can't find dialog handler class " + className, e); //$NON-NLS-1$
86
}
87         }
88     }
89
90     /**
91      * Check if this dialog is used by a paragraph. If so register it under the paragraphs name.
92      * @param dialog
93      */

94     private void registerAsParagraphDialog(String JavaDoc basePath, Content dialog) {
95
96         String JavaDoc dialogPath = StringUtils.removeStart(dialog.getHandle(), basePath + "/");
97         String JavaDoc dialogName = dialog.getNodeData(ND_NAME).getString();
98         if (StringUtils.isEmpty(dialogName)) {
99             dialogName = dialog.getName();
100         }
101
102         Map JavaDoc paragraphs = ParagraphManager.getInstance().getParagraphs();
103         for (Iterator JavaDoc iter = paragraphs.entrySet().iterator(); iter.hasNext();) {
104             Paragraph paragraph = (Paragraph) ((Entry) iter.next()).getValue();
105             String JavaDoc paragraphDialogPath = paragraph.getDialogPath();
106             String JavaDoc paragraphDialogName = paragraph.getDialog();
107
108             if (StringUtils.equals(paragraphDialogPath, dialogPath)
109                 || StringUtils.equals(paragraphDialogName, dialogName)) {
110                 Class JavaDoc handler = ParagraphEditDialog.class;
111
112                 String JavaDoc className = dialog.getNodeData(CLASS).getString();
113                 if (StringUtils.isNotEmpty(className)) {
114                     try {
115                         handler = ClassUtil.classForName(className);
116                     }
117                     catch (ClassNotFoundException JavaDoc e) {
118                         log.error("Registering paragraph: class [" + className + "] not found", e); //$NON-NLS-1$ //$NON-NLS-2$
119
}
120                 }
121
122                 registerDialogHandler(paragraph.getName(), handler, dialog);
123             }
124         }
125
126     }
127
128     protected void onClear() {
129         this.dialogHandlers.clear();
130     }
131
132     protected void registerDialogHandler(String JavaDoc name, Class JavaDoc dialogHandler, Content configNode) {
133         if (log.isDebugEnabled()) {
134             log.debug("Registering dialog handler [{}]", name); //$NON-NLS-1$
135
}
136         // remember the uuid for a reload
137
dialogHandlers.put(name, new Object JavaDoc[]{dialogHandler, configNode});
138     }
139
140     public DialogMVCHandler getDialogHandler(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
141
142         Object JavaDoc[] handlerConfig = (Object JavaDoc[]) dialogHandlers.get(name);
143
144         if (handlerConfig == null) {
145             throw new InvalidDialogHandlerException(name);
146         }
147
148         return instantiateHandler(name, request, response, handlerConfig);
149
150     }
151
152     protected DialogMVCHandler instantiateHandler(String JavaDoc name, HttpServletRequest JavaDoc request,
153         HttpServletResponse JavaDoc response, Object JavaDoc[] handlerConfig) {
154
155         try {
156
157             Class JavaDoc dialogHandlerClass = (Class JavaDoc) handlerConfig[0];
158             Content configNode = (Content) handlerConfig[1];
159             if (configNode != null) {
160                 try {
161                     Constructor JavaDoc constructor = dialogHandlerClass.getConstructor(new Class JavaDoc[]{
162                         String JavaDoc.class,
163                         HttpServletRequest JavaDoc.class,
164                         HttpServletResponse JavaDoc.class,
165                         Content.class});
166                     return (DialogMVCHandler) constructor
167                         .newInstance(new Object JavaDoc[]{name, request, response, configNode});
168                 }
169                 catch (NoSuchMethodException JavaDoc e) {
170                     Constructor JavaDoc constructor = dialogHandlerClass.getConstructor(new Class JavaDoc[]{
171                         String JavaDoc.class,
172                         HttpServletRequest JavaDoc.class,
173                         HttpServletResponse JavaDoc.class});
174                     return (DialogMVCHandler) constructor.newInstance(new Object JavaDoc[]{name, request, response});
175                 }
176             }
177
178             Constructor JavaDoc constructor = dialogHandlerClass.getConstructor(new Class JavaDoc[]{
179                 String JavaDoc.class,
180                 HttpServletRequest JavaDoc.class,
181                 HttpServletResponse JavaDoc.class});
182             return (DialogMVCHandler) constructor.newInstance(new Object JavaDoc[]{name, request, response});
183         }
184         catch (Exception JavaDoc e) {
185             throw new InvalidDialogHandlerException(name, e);
186         }
187     }
188
189     /**
190      * @return Returns the instance.
191      */

192     public static DialogHandlerManager getInstance() {
193         return (DialogHandlerManager) FactoryUtil.getSingleton(DialogHandlerManager.class);
194     }
195
196 }
197
Popular Tags