1 4 5 9 10 package org.openlaszlo.servlets.responders; 11 12 import java.io.*; 13 import java.util.Properties ; 14 import java.util.StringTokenizer ; 15 import javax.servlet.ServletConfig ; 16 import javax.servlet.ServletException ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 import javax.servlet.ServletOutputStream ; 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 req, HttpServletResponse res) 36 throws IOException 37 { 38 boolean cleared; 39 boolean isOk = true; 40 StringBuffer buf = new StringBuffer (); 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 xml = new StringBuffer () 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 req) { 88 String 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 st = new StringTokenizer (tokens, ", "); 95 while (st.hasMoreTokens()) { 96 String 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 |