KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > web > OscacheServlet


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.web;
6
7 import com.opensymphony.oscache.base.NeedsRefreshException;
8
9 import java.io.IOException JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11
12 import javax.servlet.ServletConfig JavaDoc;
13 import javax.servlet.ServletException JavaDoc;
14 import javax.servlet.http.HttpServlet JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import javax.servlet.jsp.PageContext JavaDoc;
18
19 /**
20  * Servlet used to test the web portion of osCache. It performs the operations
21  * received by parameter
22  *
23  * $Id: OscacheServlet.java,v 1.1.1.1 2003/07/17 20:28:08 chris_miller Exp $
24  * @version $Revision: 1.1.1.1 $
25  * @author <a HREF="mailto:fbeauregard@pyxis-tech.com">Francois Beauregard</a>
26  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
27  */

28 public class OscacheServlet extends HttpServlet JavaDoc {
29     /** Output content type */
30     private static final String JavaDoc CONTENT_TYPE = "text/html";
31
32     /** Clean up resources */
33     public void destroy() {
34     }
35
36     /**
37      * Process the HTTP Get request
38      * <p>
39      * @param request The HTTP request
40      * @param response The servlet response
41      * @throws ServletException
42      * @throws IOException
43      */

44     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
45         boolean varForceRefresh = false;
46         int refreshPeriod = 0;
47         int scope = PageContext.APPLICATION_SCOPE;
48         String JavaDoc forceCacheUse = null;
49         String JavaDoc key = null;
50
51         // Cache item
52
Long JavaDoc item;
53
54         // Get the admin
55
ServletCacheAdministrator admin = ServletCacheAdministrator.getInstance(getServletContext());
56
57         // Translate parameters
58
try {
59             String JavaDoc 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 JavaDoc e) {
80             getServletContext().log("Error while retrieving the servlet parameters: " + e.toString());
81         }
82
83         // Check if all the items should be flushed
84
if (varForceRefresh) {
85             admin.flushAll();
86         }
87
88         try {
89             // Get the data from the cache
90
item = (Long JavaDoc) admin.getFromCache(scope, request, key, refreshPeriod);
91         } catch (NeedsRefreshException nre) {
92             // Check if we want to force the use of an item already in cache
93
if ("yes".equals(forceCacheUse)) {
94                 admin.cancelUpdate(scope, request, key);
95                 item = (Long JavaDoc) nre.getCacheContent();
96             } else {
97                 item = new Long JavaDoc(System.currentTimeMillis());
98                 admin.putInCache(scope, request, key, item);
99             }
100         }
101
102         // Generate the output
103
response.setContentType(CONTENT_TYPE);
104
105         PrintWriter JavaDoc 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     /**Initialize global variables*/
116     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
117         super.init(config);
118     }
119
120     /**
121      * Return the scope number corresponding to it's string name
122      */

123     private int getScope(String JavaDoc 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