001: /******************************************************************************
002: * ResponderCLEARCACHE.java
003: * ****************************************************************************/package org.openlaszlo.servlets.responders;
004:
005: import java.io.*;
006: import java.util.Properties;
007: import java.util.StringTokenizer;
008: import javax.servlet.ServletConfig;
009: import javax.servlet.ServletException;
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012: import javax.servlet.ServletOutputStream;
013: import org.openlaszlo.utils.FileUtils;
014: import org.openlaszlo.cache.RequestCache;
015: import org.openlaszlo.sc.ScriptCompiler;
016: import org.openlaszlo.cm.CompilationManager;
017: import org.apache.log4j.Logger;
018:
019: public final class ResponderCLEARCACHE extends ResponderAdmin {
020: private static Logger mLogger = Logger
021: .getLogger(ResponderCLEARCACHE.class);
022:
023: final static int CLEAR_COMPILATION = 0x01;
024: final static int CLEAR_DATA = 0x02;
025: final static int CLEAR_MEDIA = 0x04;
026: final static int CLEAR_SCRIPT = 0x08;
027:
028: protected void respondAdmin(HttpServletRequest req,
029: HttpServletResponse res) throws IOException {
030: boolean cleared;
031: boolean isOk = true;
032: StringBuffer buf = new StringBuffer();
033:
034: int clearOptions = getClearOptions(req);
035:
036: if (doClear(clearOptions, CLEAR_COMPILATION)) {
037: cleared = clearCompilationCache();
038: buf.append("<compilation-cache cleared=\"" + cleared
039: + "\" />\n");
040: if (!cleared)
041: isOk = false;
042: }
043:
044: if (doClear(clearOptions, CLEAR_DATA)) {
045: cleared = clearDataCache();
046: buf.append("<data-cache cleared=\"" + cleared + "\" />\n");
047: if (!cleared)
048: isOk = false;
049: }
050:
051: if (doClear(clearOptions, CLEAR_MEDIA)) {
052: cleared = clearMediaCache();
053: buf.append("<media-cache cleared=\"" + cleared + "\" />\n");
054: if (!cleared)
055: isOk = false;
056: }
057:
058: if (doClear(clearOptions, CLEAR_SCRIPT)) {
059: cleared = clearScriptCache();
060: buf
061: .append("<script-cache cleared=\"" + cleared
062: + "\" />\n");
063: if (!cleared)
064: isOk = false;
065: }
066:
067: StringBuffer xml = new StringBuffer().append(
068: "<clearcache cleared=\"").append(isOk).append("\" >\n")
069: .append(buf).append("</clearcache>\n");
070:
071: if (isOk) {
072: mLogger.info(
073: /* (non-Javadoc)
074: * @i18n.test
075: * @org-mes="Cache cleared"
076: */
077: org.openlaszlo.i18n.LaszloMessages.getMessage(
078: ResponderCLEARCACHE.class.getName(), "051018-79"));
079: } else {
080: mLogger.info(
081: /* (non-Javadoc)
082: * @i18n.test
083: * @org-mes="Problems clearing cache:\n" + p[0]
084: */
085: org.openlaszlo.i18n.LaszloMessages.getMessage(
086: ResponderCLEARCACHE.class.getName(), "051018-88",
087: new Object[] { buf.toString() }));
088: }
089:
090: respondWithXML(res, xml.toString());
091: }
092:
093: boolean doClear(int options, int clearType) {
094: return (options & clearType) != 0;
095: }
096:
097: int getClearOptions(HttpServletRequest req) {
098: String tokens = req.getParameter("cache");
099: if (tokens == null || tokens.equals("")) {
100: return CLEAR_COMPILATION | CLEAR_DATA | CLEAR_MEDIA
101: | CLEAR_SCRIPT;
102: }
103:
104: int options = 0x00;
105: StringTokenizer st = new StringTokenizer(tokens, ", ");
106: while (st.hasMoreTokens()) {
107: String o = st.nextToken();
108: if (o.equals("compilation")) {
109: options |= CLEAR_COMPILATION;
110: } else if (o.equals("data")) {
111: options |= CLEAR_DATA;
112: } else if (o.equals("media")) {
113: options |= CLEAR_MEDIA;
114: } else if (o.equals("script")) {
115: options |= CLEAR_SCRIPT;
116: }
117: }
118:
119: return options;
120: }
121:
122: boolean clearScriptCache() {
123: return ScriptCompiler.clearCacheStatic();
124: }
125:
126: boolean clearMediaCache() {
127: RequestCache mediaCache = ResponderMEDIA.getCache();
128: if (mediaCache == null)
129: return false;
130: return mediaCache.clearCache();
131: }
132:
133: boolean clearDataCache() {
134: RequestCache dataCache = ResponderDATA.getCache();
135: if (dataCache == null)
136: return false;
137: return dataCache.clearCache();
138: }
139:
140: boolean clearCompilationCache() {
141: CompilationManager compMgr = ResponderCompile
142: .getCompilationManager();
143: if (compMgr == null)
144: return false;
145: return compMgr.clearCacheDirectory();
146: }
147:
148: public int getMimeType() {
149: return MIME_TYPE_XML;
150: }
151: }
|