View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 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.rendering.renderer.registry;
35  
36  import info.magnolia.cms.util.ModuleConfigurationObservingManager;
37  import info.magnolia.jcr.predicate.AbstractPredicate;
38  import info.magnolia.jcr.predicate.NodeTypePredicate;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.jcr.util.NodeUtil;
41  import info.magnolia.jcr.util.NodeVisitor;
42  import info.magnolia.module.ModuleRegistry;
43  
44  import java.util.ArrayList;
45  import java.util.HashSet;
46  import java.util.List;
47  import java.util.Set;
48  
49  import javax.inject.Inject;
50  import javax.inject.Singleton;
51  import javax.jcr.Node;
52  import javax.jcr.RepositoryException;
53  
54  import org.apache.commons.lang.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * ObservedManager for {@link info.magnolia.rendering.renderer.Renderer} configured in repository.
60   */
61  @Singleton
62  public class ConfiguredRendererManager extends ModuleConfigurationObservingManager {
63  
64      protected final Logger log = LoggerFactory.getLogger(getClass());
65  
66      private Set<String> registeredIds = new HashSet<String>();
67      private final RendererRegistry registry;
68  
69      public static final String LISTENERS_NODE_NAME = "listeners";
70      public static final String RENDER_EMPTY_AREAS_PROPERTY = "renderEmptyAreas";
71  
72      private boolean renderEmptyAreas = true;
73  
74      @Inject
75      public ConfiguredRendererManager(ModuleRegistry moduleRegistry, RendererRegistry registry) {
76          super("renderers", moduleRegistry);
77          this.registry = registry;
78      }
79  
80      @Override
81      protected void reload(List<Node> nodes) throws RepositoryException {
82  
83          final List<RendererProvider> providers = new ArrayList<RendererProvider>();
84  
85          for (Node node : nodes) {
86  
87              NodeUtil.visit(node, new NodeVisitor() {
88  
89                  @Override
90                  public void visit(Node parent) throws RepositoryException {
91                      for (Node configNode : NodeUtil.getNodes(parent, LISTENERS_FILTER)) {
92                          RendererProvider provider = readProvider(configNode);
93                          if (provider != null) {
94                              providers.add(provider);
95                          }
96                      }
97                  }
98              }, new NodeTypePredicate(NodeTypes.Content.NAME, false));
99  
100             readListeners(node); // register listeners
101 
102             // Rendering of areas without components policy, take configuration only from rendering module to avoid possible configuration duplicity
103             if (!node.hasProperty(RENDER_EMPTY_AREAS_PROPERTY)) {
104                 continue;
105             }
106             final String parentNode = StringUtils.substringAfterLast(StringUtils.substringBeforeLast(node.getPath(), "/"), "/");
107             if ("rendering".equals(parentNode)) {
108                 renderEmptyAreas = node.getProperty(RENDER_EMPTY_AREAS_PROPERTY).getBoolean();
109                 log.info("Rendering of areas without components is '{}'.", isRenderEmptyAreas() ? "enabled" : "disabled");
110             }
111         }
112 
113         this.registeredIds = registry.unregisterAndRegister(registeredIds, providers);
114     }
115 
116     protected RendererProvider readProvider(Node rendererNode) throws RepositoryException {
117         final String id = createId(rendererNode);
118 
119         try {
120             return new ConfiguredRendererProvider(id, rendererNode);
121         } catch (Exception e) {
122             log.error("Unable to create provider renderer [" + id + "]", e);
123             return null;
124         }
125     }
126 
127     private String createId(Node rendererNode) throws RepositoryException {
128         return rendererNode.getName();
129     }
130 
131     private void readListeners(Node configNode) throws RepositoryException {
132         if (!configNode.hasNode(LISTENERS_NODE_NAME)) {
133             return; // no listeners
134         }
135         for (Node node : NodeUtil.getNodes(configNode.getNode("listeners"), NodeTypes.ContentNode.NAME)) {
136             if (!node.hasProperty("class") || StringUtils.isEmpty(node.getProperty("class").getString())) { // editing might be not finished
137                 continue;
138             }
139             final String className = node.getProperty("class").getString();
140             registry.registerListener(className);
141         }
142     }
143 
144     public boolean isRenderEmptyAreas() {
145         return renderEmptyAreas;
146     }
147 
148     private final AbstractPredicate<Node> LISTENERS_FILTER = new AbstractPredicate<Node>() {
149 
150         @Override
151         public boolean evaluateTyped(Node node) {
152 
153             try {
154                 String nodeTypeName = node.getPrimaryNodeType().getName();
155                 return nodeTypeName.equals(NodeTypes.ContentNode.NAME) && !LISTENERS_NODE_NAME.equals(node.getName());
156             } catch (RepositoryException e) {
157                 log.error("Unable to read nodeType for node {}", node);
158             }
159             return false;
160         }
161     };
162 }