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