package org.nemesis.forum.webapp.admin.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.nemesis.forum.ForumFactory;
import org.nemesis.forum.impl.DbCacheManager;
import org.nemesis.forum.impl.DbForumFactory;
import org.nemesis.forum.proxy.ForumFactoryProxy;
import org.nemesis.forum.util.cache.Cache;
import org.nemesis.forum.webapp.admin.bean.CacheBean;
public class CacheAction extends BaseAction {
static protected Log log =LogFactory.getLog(CacheAction.class);
static private int[] ctes = {DbCacheManager.MESSAGE_CACHE,DbCacheManager.THREAD_CACHE,DbCacheManager.FORUM_CACHE,DbCacheManager.USER_CACHE,DbCacheManager.GROUP_CACHE};
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
//check logon
checkUser(request);
//check permission
checkPermission(request,OperationConstants.MANAGE_CACHE);
ActionErrors errors = new ActionErrors();
try {
ForumFactory forumFactory = ForumFactory.getInstance(getAuthToken(request));
DbForumFactory dbForumFactory = null;
dbForumFactory = (DbForumFactory)((ForumFactoryProxy)forumFactory).getUnderlyingForumFactory();
DbCacheManager cacheManager = dbForumFactory.getCacheManager();
//action
if(request.getParameter("switchcache")!=null){
cacheManager.setCacheEnabled(!cacheManager.isCacheEnabled());
}
if(request.getParameter("clean")!=null){
try {
cacheManager.clear(Integer.parseInt(request.getParameter("clean")));
} catch (Exception e) {
}
}
if(request.getParameter("newSize")!=null){
try {
Cache cache = cacheManager.getCache(Integer.parseInt(request.getParameter("oid")));
cache.setMaxSize(Integer.parseInt(request.getParameter("newSize"))*1024);
} catch (Exception e) {
}
}
//init
List mcos =new ArrayList(5);
for(int i =0;i<ctes.length;i++){
mcos.add(new CacheBean(ctes[i], "cache.memory.ctes."+(i+1),cacheManager.getCache(ctes[i])));
}
request.setAttribute("mcos",mcos);
request.setAttribute("isCacheEnabled",new Boolean(cacheManager.isCacheEnabled()));
} catch (Exception e) {
String eid=this.getClass().getName()+"_"+System.currentTimeMillis();
log.error("eid:"+eid +"\nsessionID" +request.getSession().getId(),e) ;
errors.add("general", new ActionError("error.general","error id:"+eid));
}
saveErrors(request, errors);
return mapping.findForward("view");
}
}