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.engine;
35  
36  import info.magnolia.registry.RegistrationException;
37  import info.magnolia.rendering.context.RenderingContext;
38  import info.magnolia.rendering.context.RenderingListener;
39  import info.magnolia.rendering.renderer.Renderer;
40  import info.magnolia.rendering.renderer.registry.RendererRegistry;
41  import info.magnolia.rendering.template.RenderableDefinition;
42  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
43  import info.magnolia.rendering.template.variation.RenderableVariationResolver;
44  
45  import java.util.Collections;
46  import java.util.Map;
47  
48  import javax.inject.Provider;
49  import javax.jcr.Node;
50  
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  /**
55   * Default implementation of {@link RenderingEngine}.
56   * 
57   * @version $Id$
58   */
59  public class DefaultRenderingEngine implements RenderingEngine {
60  
61      protected static final Map<String, Object> EMPTY_CONTEXT = Collections.emptyMap();
62  
63      private RendererRegistry rendererRegistry;
64      private TemplateDefinitionAssignment templateDefinitionAssignment;
65      private Provider<RenderingContext> renderingContextProvider;
66      private RenderableVariationResolver variationResolver;
67  
68      private Boolean autoPopulateFromRequest = true;
69      private Boolean renderEmptyAreas = true;
70  
71      private static final Logger log = LoggerFactory.getLogger(DefaultRenderingEngine.class);
72  
73      /**
74       * Used to create an observed proxy object.
75       */
76      protected DefaultRenderingEngine() {
77      }
78  
79      public DefaultRenderingEngine(RendererRegistry rendererRegistry, TemplateDefinitionAssignment templateDefinitionAssignment, RenderableVariationResolver variationResolver, Provider<RenderingContext> renderingContextProvider) {
80          this.rendererRegistry = rendererRegistry;
81          this.templateDefinitionAssignment = templateDefinitionAssignment;
82          this.variationResolver = variationResolver;
83          this.renderingContextProvider = renderingContextProvider;
84      }
85  
86      @Override
87      public void render(Node content, OutputProvider out) throws RenderException {
88          render(content, EMPTY_CONTEXT, out);
89      }
90  
91      @Override
92      public void render(Node content, Map<String, Object> contextObjects, OutputProvider out) throws RenderException {
93          render(content, getRenderableDefinitionFor(content), contextObjects, out);
94      }
95  
96      @Override
97      public void render(Node content, RenderableDefinition definition, Map<String, Object> contextObjects, OutputProvider out) throws RenderException {
98  
99          final Renderer renderer = getRendererFor(definition);
100         final RenderingContext renderingContext = getRenderingContext();
101 
102         RenderableDefinition variation = variationResolver.resolveVariation(definition);
103         definition = variation != null ? variation : definition;
104 
105         renderingContext.push(content, definition, out);
106         renderingContext.before(content, definition, contextObjects, out);
107         try {
108             renderer.render(renderingContext, contextObjects);
109         } catch (RenderException e) {
110             renderingContext.handleException(e);
111         }
112 
113         finally {
114             renderingContext.after(content, definition, contextObjects, out);
115             renderingContext.pop();
116         }
117     }
118 
119     protected RenderableDefinition getRenderableDefinitionFor(Node content) throws RenderException {
120         try {
121             return templateDefinitionAssignment.getAssignedTemplateDefinition(content);
122         } catch (RegistrationException e) {
123             throw new RenderException("Can't resolve RenderableDefinition for node [" + content + "]", e);
124         }
125     }
126 
127     protected Renderer getRendererFor(RenderableDefinition definition) throws RenderException {
128         final String renderType = definition.getRenderType();
129         if (renderType == null) {
130             throw new RenderException("No renderType defined for definition [" + definition + "]");
131         }
132         try {
133             return rendererRegistry.getRenderer(renderType);
134         } catch (RegistrationException e) {
135             throw new RenderException("Can't find renderer [" + renderType + "]", e);
136         }
137     }
138 
139     @Override
140     public RenderingContext getRenderingContext() {
141         return renderingContextProvider.get();
142     }
143 
144     @Override
145     public void initListeners(OutputProvider output) {
146         RenderingContext context = this.getRenderingContext();
147 
148         for (Class<RenderingListener> listener : this.rendererRegistry.getListeners()) {
149             RenderingListener renderingListener;
150             try {
151                 renderingListener = listener.getConstructor(ResponseOutputProvider.class).newInstance(output);
152                 context.addListener(renderingListener);
153             } catch (Exception e) {
154                 log.error("Error when instantiating listener '{}'.", listener, e);
155             }
156         }
157     }
158 
159     @Override
160     public Boolean getAutoPopulateFromRequest() {
161         return autoPopulateFromRequest;
162     }
163 
164     public void setAutoPopulateFromRequest(Boolean autopopulateFromRequest) {
165         this.autoPopulateFromRequest = autopopulateFromRequest;
166     }
167 
168     @Override
169     public Boolean getRenderEmptyAreas() {
170         return renderEmptyAreas;
171     }
172 
173     public void setRenderEmptyAreas(Boolean renderEmptyAreas) {
174         this.renderEmptyAreas = renderEmptyAreas;
175     }
176 }