View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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.core.ItemType;
39  import info.magnolia.cms.util.SystemContentWrapper;
40  import info.magnolia.content2bean.Content2BeanUtil;
41  import info.magnolia.objectfactory.Classes;
42  import info.magnolia.objectfactory.Components;
43  
44  import java.lang.reflect.Constructor;
45  import java.util.Collection;
46  import java.util.HashMap;
47  import java.util.Map;
48  
49  import javax.jcr.RepositoryException;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  import org.apache.commons.lang.StringUtils;
54  
55  public class TreeHandlerManager<H extends AdminTreeMVCHandler> extends ObservedManager {
56  
57      private static final String ND_CLASS = "class";
58  
59      private static final String ND_REPOSITORY = "repository";
60  
61      private static final String ND_NAME = "name";
62  
63      /**
64       * Map with repository name/handler class for admin tree. When this servlet will receive a call with a parameter
65       * <code>repository</code>, the corresponding handler will be used top display the admin tree.
66       */
67      private final Map<String, TreeHandlerConfig> treeHandlers = new HashMap<String, TreeHandlerConfig>();
68  
69      /**
70       * Get the tree handler registered under a particular name.
71       * @param name
72       * @param request
73       * @param response
74       * @return
75       */
76      public AdminTreeMVCHandler getTreeHandler(String name, HttpServletRequest request, HttpServletResponse response) {
77  
78          TreeHandlerConfig th = treeHandlers.get(name);
79  
80          if (th == null) {
81              throw new InvalidTreeHandlerException(name);
82          }
83  
84          Class<H> treeHandlerClass = th.getHandler();
85  
86          try {
87              Constructor<H> constructor = treeHandlerClass.getConstructor(String.class, HttpServletRequest.class, HttpServletResponse.class);
88              AdminTreeMVCHandler newInstance = constructor.newInstance(name, request, response);
89              Content2BeanUtil.setProperties(newInstance, th.getTreeDefinition(), true);
90              return newInstance;
91          }
92          catch (Exception e) {
93              throw new InvalidTreeHandlerException(name, e);
94          }
95      }
96  
97      protected void registerTreeHandler(String name, String repository, Class<H> treeHandler, Content treeDefinition) {
98          log.info("Registering tree handler {}", name); //$NON-NLS-1$
99          treeHandlers.put(name, new TreeHandlerConfig(treeHandler, repository, treeDefinition));
100     }
101 
102     protected void onRegister(Content defNode) {
103         Collection<Content> trees = defNode.getChildren(ItemType.CONTENTNODE.getSystemName());
104         for (Object tree1 : trees) {
105             Content tree = (Content) tree1;
106             String name = tree.getNodeData(ND_NAME).getString();
107 
108             if (StringUtils.isEmpty(name)) {
109                 name = tree.getName();
110             }
111 
112             String repository = tree.getNodeData(ND_REPOSITORY).getString();
113             String className = tree.getNodeData(ND_CLASS).getString();
114 
115             if (StringUtils.isEmpty(repository)) {
116                 repository = name;
117             }
118 
119             try {
120                 final Class<H> treeHandler = Classes.getClassFactory().forName(className);
121                 this.registerTreeHandler(name, repository, treeHandler, new SystemContentWrapper(tree));
122             }
123             catch (ClassNotFoundException e) {
124                 log.error("Can't register tree handler [{}]: class [{}] not found", name, className);
125             }
126 
127             // register commands if defined
128             try {
129                 // TODO - this should go - maybe still needs an update task
130                 if (tree.hasContent("commands")) {
131                     log.error("The definition of commands at the tree level is no longer supported. Move them to the modules commands node! [" + tree.getHandle() + "]");
132                 }
133             }
134             catch (RepositoryException e) {
135                 log.error("Can't check commands node of the tree node [" + tree.getHandle() + "]", e);
136             }
137         }
138     }
139 
140     /**
141      * @return Returns the instance.
142      */
143     public static <H extends AdminTreeMVCHandler> TreeHandlerManager<H> getInstance() {
144         return Components.getSingleton(TreeHandlerManager.class);
145     }
146 
147     /**
148      * Clear the handlers
149      */
150     protected void onClear() {
151         this.treeHandlers.clear();
152     }
153 
154     class TreeHandlerConfig {
155 
156         private Class<H> handler;
157 
158         private String repository;
159 
160         private Content treeDefinition;
161 
162         TreeHandlerConfig(Class<H> handler, String repository, Content treeDefinition) {
163             this.handler = handler;
164             this.repository = repository;
165             this.treeDefinition = treeDefinition;
166         }
167 
168         public Class<H> getHandler() {
169             return this.handler;
170         }
171 
172         public String getRepository() {
173             return this.repository;
174         }
175 
176         public Content getTreeDefinition() {
177             return treeDefinition;
178         }
179     }
180 
181 }