001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.site; 017 018import java.io.File; 019import java.util.Map; 020 021import org.apache.avalon.framework.parameters.Parameters; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.thread.ThreadSafe; 024import org.apache.cocoon.acting.ServiceableAction; 025import org.apache.cocoon.environment.Redirector; 026import org.apache.commons.io.FileUtils; 027 028/** 029 * Invalidates cached data for a given site. 030 */ 031public class InvalidateSiteAction extends ServiceableAction implements ThreadSafe 032{ 033 private CacheAccessManager _cacheAccess; 034 private CacheAccessCounter _cacheAccessCounter; 035 036 037 /** 038 * Get the cache access counter 039 * @return the CacheAccessCounter 040 */ 041 protected CacheAccessCounter _getCacheAccessCounter() 042 { 043 if (_cacheAccessCounter == null) 044 { 045 try 046 { 047 _cacheAccessCounter = (CacheAccessCounter) manager.lookup(CacheAccessCounter.ROLE); 048 } 049 catch (ServiceException e) 050 { 051 throw new IllegalStateException("Cannot get CacheAccessCounter", e); 052 } 053 } 054 return _cacheAccessCounter; 055 } 056 057 /** 058 * Get the cache access manager 059 * @return the CacheAccessManager 060 */ 061 protected CacheAccessManager _getCacheAccessManager() 062 { 063 if (_cacheAccess == null) 064 { 065 try 066 { 067 _cacheAccess = (CacheAccessManager) manager.lookup(CacheAccessManager.ROLE); 068 } 069 catch (ServiceException e) 070 { 071 throw new IllegalStateException("Cannot get CacheAccessManager", e); 072 } 073 } 074 return _cacheAccess; 075 } 076 077 @Override 078 public Map act(Redirector redirector, org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 079 { 080 String site = parameters.getParameter("site"); 081 082 File root = SiteCacheHelper.getRootCache(); 083 File siteRoot = new File(root, site); 084 085 if (siteRoot.exists()) 086 { 087 FileUtils.forceDelete(siteRoot); 088 } 089 090 if (getLogger().isInfoEnabled()) 091 { 092 getLogger().info("Invalidate cache for site '" + site + "'"); 093 } 094 095 int count = _getCacheAccessCounter().getAskedResources(site); 096 097 if (getLogger().isInfoEnabled()) 098 { 099 getLogger().info("Since last cache invalidation of site '" + site + "', " + count + " resource(s) have been generated from back-office."); 100 } 101 102 _getCacheAccessCounter().resetCount(site); 103 104 // Reset the cache access manager. 105 _getCacheAccessManager().reset(); 106 107 return EMPTY_MAP; 108 } 109}