1 24 package org.riotfamily.components.render; 25 26 import java.io.IOException ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 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 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 request, HttpServletResponse response, 60 Cache cache) throws IOException { 61 62 super(dao, repository, config, request, response); 63 this.cache = cache; 64 } 65 66 70 public void render(Location location) throws IOException { 71 String 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 super.render(location); 80 } 81 } 82 83 public void render(ComponentList list) throws IOException { 84 String 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 super.render(list); 93 } 94 } 95 96 protected String getCacheKey(Location location) { 97 StringBuffer sb = new StringBuffer ("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 { 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 positionClassName) 135 throws IOException { 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 tags = component.getCacheTags(version); 149 if (tags != null) { 150 Iterator it = tags.iterator(); 151 while (it.hasNext()) { 152 String tag = (String ) it.next(); 153 TaggingContext.tag(request, tag); 154 } 155 } 156 } 157 158 protected void renderCacheableComponent(Component component, 159 ComponentVersion version, String positionClassName) 160 throws IOException { 161 162 String 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 getComponentCacheKey(ComponentVersion version) { 187 StringBuffer key = new StringBuffer (); 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 |