1 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 ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.commons.lang.StringUtils; 31 import org.slf4j.Logger; 32 import org.slf4j.LoggerFactory; 33 34 35 39 public class PageHandlerManager extends ObservedManager { 40 41 private static final String ND_CLASS = "class"; 42 43 private static final String ND_NAME = "name"; 44 45 48 private static Logger log = LoggerFactory.getLogger(PageHandlerManager.class); 49 50 53 private final Map dialogPageHandlers = new HashMap (); 54 55 62 public PageMVCHandler getPageHandler(String name, HttpServletRequest request, HttpServletResponse response) { 63 64 Class dialogPageHandlerClass = (Class ) dialogPageHandlers.get(name); 65 if (dialogPageHandlerClass == null) { 66 throw new InvalidDialogPageHandlerException(name); 67 } 68 69 try { 70 Constructor constructor = dialogPageHandlerClass.getConstructor(new Class []{ 71 String .class, 72 HttpServletRequest .class, 73 HttpServletResponse .class}); 74 return (PageMVCHandler) constructor.newInstance(new Object []{name, request, response}); 75 } 76 catch (Exception e) { 77 log.error("can't instantiate page [" + name + "]", e); 78 throw new InvalidDialogPageHandlerException(name, e); 79 } 80 } 81 82 protected void registerPageHandler(String name, Class dialogPageHandler) { 83 if (log.isDebugEnabled()) { 84 log.debug("Registering page handler [{}]", name); } 86 dialogPageHandlers.put(name, dialogPageHandler); 87 } 88 89 93 protected void onRegister(Content defNode) { 94 try { 96 Collection pages = defNode.getChildren(ItemType.CONTENT.getSystemName()); 97 pages.addAll(defNode.getChildren(ItemType.CONTENTNODE.getSystemName())); 98 for (Iterator iter = pages.iterator(); iter.hasNext();) { 99 Content page = (Content) iter.next(); 100 String name = page.getNodeData(ND_NAME).getString(); 101 if (StringUtils.isEmpty(name)) { 102 name = page.getName(); 103 } 104 105 String className = page.getNodeData(ND_CLASS).getString(); 106 try { 107 registerPageHandler(name, ClassUtil.classForName(className)); 108 } 109 catch (ClassNotFoundException e) { 110 log.warn("can't find dialogpage handler class " + className, e); } 112 } 113 } 114 catch (Exception e) { 115 log.warn("can't find pages configuration", e); } 117 } 118 119 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 |