1 5 package com.opensymphony.oscache.web; 6 7 import com.opensymphony.oscache.base.NeedsRefreshException; 8 9 import java.io.IOException ; 10 import java.io.PrintWriter ; 11 12 import javax.servlet.ServletConfig ; 13 import javax.servlet.ServletException ; 14 import javax.servlet.http.HttpServlet ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 import javax.servlet.jsp.PageContext ; 18 19 28 public class OscacheServlet extends HttpServlet { 29 30 private static final String CONTENT_TYPE = "text/html"; 31 32 33 public void destroy() { 34 } 35 36 44 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 45 boolean varForceRefresh = false; 46 int refreshPeriod = 0; 47 int scope = PageContext.APPLICATION_SCOPE; 48 String forceCacheUse = null; 49 String key = null; 50 51 Long item; 53 54 ServletCacheAdministrator admin = ServletCacheAdministrator.getInstance(getServletContext()); 56 57 try { 59 String paramValue = request.getParameter("forceRefresh"); 60 61 if ((paramValue != null) && (paramValue.length() > 0)) { 62 varForceRefresh = Boolean.valueOf(paramValue).booleanValue(); 63 } 64 65 paramValue = request.getParameter("scope"); 66 67 if ((paramValue != null) && (paramValue.length() > 0)) { 68 scope = getScope(paramValue); 69 } 70 71 paramValue = request.getParameter("refreshPeriod"); 72 73 if ((paramValue != null) && (paramValue.length() > 0)) { 74 refreshPeriod = Integer.valueOf(paramValue).intValue(); 75 } 76 77 forceCacheUse = request.getParameter("forcecacheuse"); 78 key = request.getParameter("key"); 79 } catch (Exception e) { 80 getServletContext().log("Error while retrieving the servlet parameters: " + e.toString()); 81 } 82 83 if (varForceRefresh) { 85 admin.flushAll(); 86 } 87 88 try { 89 item = (Long ) admin.getFromCache(scope, request, key, refreshPeriod); 91 } catch (NeedsRefreshException nre) { 92 if ("yes".equals(forceCacheUse)) { 94 admin.cancelUpdate(scope, request, key); 95 item = (Long ) nre.getCacheContent(); 96 } else { 97 item = new Long (System.currentTimeMillis()); 98 admin.putInCache(scope, request, key, item); 99 } 100 } 101 102 response.setContentType(CONTENT_TYPE); 104 105 PrintWriter out = response.getWriter(); 106 out.println("<html>"); 107 out.println("<head><title>OscacheServlet</title></head>"); 108 out.println("<body>"); 109 out.println("<b>This is some cache content </b>: " + item.toString() + "<br>"); 110 out.println("<b>Cache key</b>: " + admin.getCacheKey() + "<br>"); 111 out.println("<b>Entry key</b>: " + admin.generateEntryKey("Test_key", request, scope) + "<br>"); 112 out.println("</body></html>"); 113 } 114 115 116 public void init(ServletConfig config) throws ServletException { 117 super.init(config); 118 } 119 120 123 private int getScope(String value) { 124 if ((value != null) && (value.equalsIgnoreCase("session"))) { 125 return PageContext.SESSION_SCOPE; 126 } else { 127 return PageContext.APPLICATION_SCOPE; 128 } 129 } 130 } 131 | Popular Tags |