KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > responders > ResponderCLEARCACHE


1 /******************************************************************************
2  * ResponderCLEARCACHE.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.servlets.responders;
11
12 import java.io.*;
13 import java.util.Properties JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15 import javax.servlet.ServletConfig JavaDoc;
16 import javax.servlet.ServletException JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19 import javax.servlet.ServletOutputStream JavaDoc;
20 import org.openlaszlo.utils.FileUtils;
21 import org.openlaszlo.cache.RequestCache;
22 import org.openlaszlo.sc.ScriptCompiler;
23 import org.openlaszlo.cm.CompilationManager;
24 import org.apache.log4j.Logger;
25
26 public final class ResponderCLEARCACHE extends ResponderAdmin
27 {
28     private static Logger mLogger = Logger.getLogger(ResponderCLEARCACHE.class);
29
30     final static int CLEAR_COMPILATION = 0x01;
31     final static int CLEAR_DATA = 0x02;
32     final static int CLEAR_MEDIA = 0x04;
33     final static int CLEAR_SCRIPT = 0x08;
34
35     protected void respondAdmin(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
36         throws IOException
37     {
38         boolean cleared;
39         boolean isOk = true;
40         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
41
42         int clearOptions = getClearOptions(req);
43
44         if ( doClear(clearOptions, CLEAR_COMPILATION) ) {
45             cleared = clearCompilationCache();
46             buf.append("<compilation-cache cleared=\"" + cleared + "\" />\n");
47             if (! cleared) isOk = false;
48         }
49             
50         if ( doClear(clearOptions, CLEAR_DATA) ) {
51             cleared = clearDataCache();
52             buf.append("<data-cache cleared=\"" + cleared + "\" />\n");
53             if (! cleared) isOk = false;
54         }
55
56         if ( doClear(clearOptions, CLEAR_MEDIA) ) {
57             cleared = clearMediaCache();
58             buf.append("<media-cache cleared=\"" + cleared + "\" />\n");
59             if (! cleared) isOk = false;
60         }
61
62         if ( doClear(clearOptions, CLEAR_SCRIPT) ) {
63             cleared = clearScriptCache();
64             buf.append("<script-cache cleared=\"" + cleared + "\" />\n");
65             if (! cleared) isOk = false;
66         }
67
68         StringBuffer JavaDoc xml = new StringBuffer JavaDoc()
69             .append("<clearcache cleared=\"").append(isOk).append("\" >\n")
70             .append(buf)
71             .append("</clearcache>\n");
72
73         if (isOk) {
74             mLogger.info("Cache cleared");
75         } else {
76             mLogger.info("Problems clearing cache:\n" + buf.toString());
77         }
78
79         respondWithXML(res, xml.toString());
80     }
81
82
83     boolean doClear(int options, int clearType) {
84         return (options & clearType) != 0;
85     }
86
87     int getClearOptions(HttpServletRequest JavaDoc req) {
88         String JavaDoc tokens = req.getParameter("cache");
89         if (tokens == null || tokens.equals("")) {
90             return CLEAR_COMPILATION | CLEAR_DATA | CLEAR_MEDIA | CLEAR_SCRIPT;
91         }
92
93         int options = 0x00;
94         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(tokens, ", ");
95         while (st.hasMoreTokens()) {
96             String JavaDoc o = st.nextToken();
97             if ( o.equals("compilation") ) {
98                 options |= CLEAR_COMPILATION;
99             } else if ( o.equals("data") ) {
100                 options |= CLEAR_DATA;
101             } else if ( o.equals("media") ) {
102                 options |= CLEAR_MEDIA;
103             } else if ( o.equals("script") ) {
104                 options |= CLEAR_SCRIPT;
105             }
106         }
107
108         return options;
109     }
110
111     boolean clearScriptCache() {
112         return ScriptCompiler.clearCacheStatic();
113     }
114
115     boolean clearMediaCache() {
116         RequestCache mediaCache = ResponderMEDIA.getCache();
117         if (mediaCache == null) return false;
118         return mediaCache.clearCache();
119     }
120
121     boolean clearDataCache() {
122         RequestCache dataCache = ResponderDATA.getCache();
123         if (dataCache == null) return false;
124         return dataCache.clearCache();
125     }
126
127
128     boolean clearCompilationCache() {
129         CompilationManager compMgr = ResponderCompile.getCompilationManager();
130         if (compMgr == null) return false;
131         return compMgr.clearCacheDirectory();
132     }
133
134
135
136     public int getMimeType()
137     {
138         return MIME_TYPE_XML;
139     }
140 }
141
Popular Tags