/* * ====================================================================== Parts * Copyright 2006 University of Leeds, Oxford University, University of the * Highlands and Islands. Licensed under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law * or agreed to in writing, software distributed under the License is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. * ====================================================================== */ package org.bodington.installation; import java.util.Enumeration; import org.apache.log4j.Logger; import org.bodington.server.BuildingServerException; import org.bodington.server.BuildingSessionManagerImpl; import org.bodington.server.JobScheduler; import org.bodington.server.JobSession; import org.bodington.server.QuotaFactory; import org.bodington.server.QuotaMetadatum; import org.bodington.server.UploadedFileSession; import org.bodington.server.resources.Resource; import org.bodington.server.resources.ResourceTreeManager; /** * Walk the whole of the resource tree checking that the quota is correct. * @author buckett */ public class QuotaRecalculation implements JobSession { private static Logger log = Logger.getLogger(QuotaRecalculation.class); private int resourcesProcessed = 0; public void recalculate(String args) { try { log.info("Started recalculation"); Resource start = null; try { start = ResourceTreeManager.getInstance().findResource(new String[]{"users"}); } catch (BuildingServerException bse) { log.info("Couldn't find /users resource"); } checkResource(start); log.info("Finished recalculation ("+ resourcesProcessed+ ")"); } catch (BuildingServerException bse) { log.error("Problem recalculating quota.", bse); } } public void checkResource(Resource resource) throws BuildingServerException { if (resource == null) { return; } UploadedFileSession session = BuildingSessionManagerImpl.getSession( resource).getUploadedFileSession(); try { session.checkFileQuota(); resourcesProcessed++; if (resourcesProcessed % 1000 == 0) { log.debug("Processed "+ resourcesProcessed+ " resources."); } } catch (BuildingServerException bse) { log.warn("Failed to recalculate quota for "+ resource.getFullName(), bse); } QuotaMetadatum quota = QuotaFactory.getQuota(resource, QuotaFactory.FILES); // Only do children if we don't have quota. if (!quota.hasQuota()) { for (Enumeration children = resource.findChildren(); children .hasMoreElements();) { checkResource((Resource) children.nextElement()); } } } public static void addJob() { try { JobScheduler.getJobScheduler(true).addJobOnce( QuotaRecalculation.class.getName(), "recalculate"); } catch (BuildingServerException bse) { log.error("Problem finding job or adding a new one.", bse); } } }