001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.admin.repo;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.DocumentRef;
026import org.nuxeo.runtime.transaction.TransactionHelper;
027
028/**
029 * Runnable Class that is executed in the ThreadPool of {@link RepoStat} to gather statistics.
030 *
031 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
032 */
033public class StatsTask implements Runnable {
034
035    protected static final Log log = LogFactory.getLog(StatsTask.class);
036
037    private final DocumentRef rootDocRef;
038
039    protected final boolean includeBlob;
040
041    protected final RepoStat cmdInstance;
042
043    protected final String repositoryName;
044
045    public StatsTask(String repoName, DocumentRef rootDocRef, boolean includeBlob, RepoStat instance) {
046        this.repositoryName = repoName;
047        this.rootDocRef = rootDocRef;
048        this.includeBlob = includeBlob;
049        this.cmdInstance = instance;
050    }
051
052    public synchronized void run() {
053        StatsTaskRunner runner = new StatsTaskRunner(repositoryName, includeBlob, rootDocRef, this);
054
055        try {
056            TransactionHelper.startTransaction();
057            runner.runUnrestricted();
058        } finally {
059            TransactionHelper.commitOrRollbackTransaction();
060        }
061    }
062
063    public void exec(StatsTask task) {
064        cmdInstance.exec(task);
065    }
066
067    protected StatsTask getNextTask(DocumentModel root) {
068        if (cmdInstance.isPoolFull()) {
069            return null;
070        }
071        return new StatsTask(repositoryName, root.getRef(), includeBlob, cmdInstance);
072    }
073
074    protected RepoStatInfo getInfo() {
075        return cmdInstance.getInfo();
076    }
077
078}