KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > render > LiveModeRenderStrategy


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.render;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.riotfamily.cachius.Cache;
34 import org.riotfamily.cachius.CacheItem;
35 import org.riotfamily.cachius.CachiusResponseWrapper;
36 import org.riotfamily.cachius.ItemUpdater;
37 import org.riotfamily.cachius.TaggingContext;
38 import org.riotfamily.cachius.support.SessionUtils;
39 import org.riotfamily.components.Component;
40 import org.riotfamily.components.ComponentList;
41 import org.riotfamily.components.ComponentRepository;
42 import org.riotfamily.components.ComponentVersion;
43 import org.riotfamily.components.Location;
44 import org.riotfamily.components.config.ComponentListConfiguration;
45 import org.riotfamily.components.dao.ComponentDao;
46
47 public class LiveModeRenderStrategy extends AbstractRenderStrategy {
48
49     protected Cache cache;
50
51     protected String JavaDoc listTag;
52
53     protected CacheItem cachedList;
54
55     protected boolean listIsCacheable = true;
56
57     public LiveModeRenderStrategy(ComponentDao dao,
58             ComponentRepository repository, ComponentListConfiguration config,
59             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
60             Cache cache) throws IOException JavaDoc {
61
62         super(dao, repository, config, request, response);
63         this.cache = cache;
64     }
65
66     /**
67      * Overrides the default implementation to render the cached version of
68      * the list (if present).
69      */

70     public void render(Location location) throws IOException JavaDoc {
71         String JavaDoc cacheKey = getCacheKey(location);
72         cachedList = cache.getItem(cacheKey);
73         if (cachedList != null && cachedList.exists() && !cachedList.isNew()) {
74             log.debug("Serving cached list: " + location);
75             cachedList.writeTo(request, response);
76         }
77         else {
78             // List was invalidated or this is the 1st request
79
super.render(location);
80         }
81     }
82
83     public void render(ComponentList list) throws IOException JavaDoc {
84         String JavaDoc cacheKey = getCacheKey(list.getLocation());
85         cachedList = cache.getItem(cacheKey);
86         if (cachedList != null && cachedList.exists() && !cachedList.isNew()) {
87             log.debug("Serving cached list: " + list.getLocation());
88             cachedList.writeTo(request, response);
89         }
90         else {
91             // List was invalidated or this is the 1st request
92
super.render(list);
93         }
94     }
95
96     protected String JavaDoc getCacheKey(Location location) {
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("ComponentList ");
98         if (parent != null) {
99             sb.append(parent.getId()).append('$');
100             sb.append(location.getSlot());
101         }
102         else {
103             sb.append(location);
104         }
105         SessionUtils.addStateToCacheKey(request, sb);
106         return sb.toString();
107     }
108
109     protected void renderComponentList(ComponentList list) throws IOException JavaDoc {
110         listTag = list.getLocation().toString();
111         try {
112             TaggingContext.openNestedContext(request);
113             TaggingContext.tag(request, listTag);
114
115             ItemUpdater updater = new ItemUpdater(cachedList, request);
116             response = new CachiusResponseWrapper(response, updater);
117
118             super.renderComponentList(list);
119             if (!listIsCacheable) {
120                 updater.discard();
121                 cachedList.delete();
122             }
123
124             response.flushBuffer();
125             updater.updateCacheItem();
126
127         }
128         finally {
129             cachedList.setTags(TaggingContext.popTags(request));
130         }
131     }
132
133     protected void renderComponent(Component component,
134             ComponentVersion version, String JavaDoc positionClassName)
135             throws IOException JavaDoc {
136
137         tagCacheItems(component, version);
138         if (component.isDynamic()) {
139             listIsCacheable = false;
140             super.renderComponent(component, version, positionClassName);
141         }
142         else {
143             renderCacheableComponent(component, version, positionClassName);
144         }
145     }
146
147     private void tagCacheItems(Component component, ComponentVersion version) {
148         Collection JavaDoc tags = component.getCacheTags(version);
149         if (tags != null) {
150             Iterator JavaDoc it = tags.iterator();
151             while (it.hasNext()) {
152                 String JavaDoc tag = (String JavaDoc) it.next();
153                 TaggingContext.tag(request, tag);
154             }
155         }
156     }
157
158     protected void renderCacheableComponent(Component component,
159             ComponentVersion version, String JavaDoc positionClassName)
160             throws IOException JavaDoc {
161
162         String JavaDoc key = getComponentCacheKey(version);
163         CacheItem cachedComponent = cache.getItem(key);
164         if (cachedComponent.exists() && !cachedComponent.isNew()) {
165             cachedComponent.writeTo(request, response);
166             return;
167         }
168         try {
169             TaggingContext.openNestedContext(request);
170             TaggingContext.tag(request, listTag);
171
172             ItemUpdater updater = new ItemUpdater(cachedComponent, request);
173             CachiusResponseWrapper wrapper = new CachiusResponseWrapper(
174                     response, updater);
175
176             component.render(version, positionClassName, request, wrapper);
177
178             wrapper.flushBuffer();
179             updater.updateCacheItem();
180         }
181         finally {
182             cachedComponent.setTags(TaggingContext.popTags(request));
183         }
184     }
185
186     protected String JavaDoc getComponentCacheKey(ComponentVersion version) {
187         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
188         key.append(version.getClass().getName());
189         key.append('#');
190         key.append(version.getId());
191         SessionUtils.addStateToCacheKey(request, key);
192         return key.toString();
193     }
194
195 }
196
Popular Tags