1 21 22 package org.opensubsystems.core.www; 23 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.servlet.jsp.JspException ; 31 import javax.servlet.jsp.tagext.BodyTagSupport ; 32 33 import org.opensubsystems.core.util.CallContext; 34 35 49 public class PageElementCacheTag extends BodyTagSupport 50 { 51 53 56 public static final String PAGE_ELEMENTS_CACHE = "pageelements"; 57 58 61 public static final String CSS_ELEMENT = "css"; 62 63 66 public static final String JS_ELEMENT = "js"; 67 68 71 public static final String SCRIPT_ELEMENT = "script"; 72 73 76 public static final String CACHE_INDICATOR = "cache:"; 77 78 80 83 private static final long serialVersionUID = -2506816420084812132L; 84 85 88 protected String m_strId; 89 90 94 protected String m_strCacheas; 95 96 98 101 public PageElementCacheTag() 102 { 103 super(); 104 105 m_strId = ""; 106 m_strCacheas = ""; 107 } 108 109 111 114 public int doStartTag( 115 ) throws JspException 116 { 117 String strOutput; 118 119 strOutput = getCachedContent(getId()); 120 121 if ((m_strCacheas != null) && (m_strCacheas.length() > 0)) 122 { 123 if (strOutput != null) 124 { 125 cache(m_strCacheas, strOutput); 126 } 127 } 128 else 129 { 130 TagUtils.write(pageContext, strOutput); 131 } 132 133 return (SKIP_BODY); 134 } 135 136 139 public int doEndTag( 140 ) throws JspException 141 { 142 return (EVAL_PAGE); 143 } 144 145 148 public String getId( 149 ) 150 { 151 return m_strId; 152 } 153 154 157 public String getCacheas( 158 ) 159 { 160 return m_strCacheas; 161 } 162 163 166 public void setId( 167 String strId 168 ) 169 { 170 m_strId = strId; 171 } 172 173 176 public void setCacheas( 177 String strCacheas 178 ) 179 { 180 m_strCacheas = strCacheas; 181 } 182 183 185 192 protected void cache( 193 String strPageElementId, 194 String strItemToCache 195 ) 196 { 197 Map cache = (Map )CallContext.getInstance().getCache(PAGE_ELEMENTS_CACHE); 200 201 List items; 202 203 items = (List )cache.get(strPageElementId.toLowerCase()); 204 if (items == null) 205 { 206 items = new ArrayList (); 207 cache.put(strPageElementId.toLowerCase(), items); 208 } 209 items.add(strItemToCache); 210 } 211 212 219 protected String getCachedContent( 220 String strPageElementId 221 ) 222 { 223 return getCachedContent(strPageElementId, true); 224 } 225 226 227 236 protected String getCachedContent( 237 String strPageElementId, 238 boolean bRemove, 239 String strSeparator 240 ) 241 { 242 Map cache = (Map )CallContext.getInstance().getCache(PAGE_ELEMENTS_CACHE); 245 StringBuffer sbHtml = new StringBuffer (); 246 247 if (cache != null) 248 { 249 List items; 250 251 if (bRemove) 252 { 253 items = (List )cache.remove(strPageElementId.toLowerCase()); 254 } 255 else 256 { 257 items = (List )cache.get(strPageElementId.toLowerCase()); 258 } 259 if ((items != null) && (!items.isEmpty())) 260 { 261 Iterator iter; 262 263 for (iter = items.iterator(); iter.hasNext();) 264 { 265 sbHtml.append(iter.next()); 266 sbHtml.append(strSeparator); 267 } 268 } 269 } 270 271 return sbHtml.toString(); 272 } 273 274 282 protected String getCachedContent( 283 String strPageElementId, 284 boolean bRemove 285 ) 286 { 287 return getCachedContent(strPageElementId, bRemove, "\n"); 288 } 289 290 298 protected List getCachedContentElements( 299 String strPageElementId, 300 boolean bRemove 301 ) 302 { 303 Map cache = (Map )CallContext.getInstance().getCache(PAGE_ELEMENTS_CACHE); 306 List items = null; 307 308 if (cache != null) 309 { 310 if (bRemove) 311 { 312 items = (List )cache.remove(strPageElementId.toLowerCase()); 313 } 314 else 315 { 316 items = (List )cache.get(strPageElementId.toLowerCase()); 317 } 318 } 319 if (items == null) 320 { 321 items = Collections.EMPTY_LIST; 322 } 323 324 return items; 325 } 326 } 327 | Popular Tags |