001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.admin.repo;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentRef;
028import org.nuxeo.runtime.transaction.TransactionHelper;
029
030/**
031 * Runnable Class that is executed in the ThreadPool of {@link RepoStat} to gather statistics.
032 *
033 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
034 */
035public class StatsTask implements Runnable {
036
037    protected static final Log log = LogFactory.getLog(StatsTask.class);
038
039    private final DocumentRef rootDocRef;
040
041    protected final boolean includeBlob;
042
043    protected final RepoStat cmdInstance;
044
045    protected final String repositoryName;
046
047    public StatsTask(String repoName, DocumentRef rootDocRef, boolean includeBlob, RepoStat instance) {
048        this.repositoryName = repoName;
049        this.rootDocRef = rootDocRef;
050        this.includeBlob = includeBlob;
051        this.cmdInstance = instance;
052    }
053
054    public synchronized void run() {
055        StatsTaskRunner runner = new StatsTaskRunner(repositoryName, includeBlob, rootDocRef, this);
056
057        try {
058            TransactionHelper.startTransaction();
059            runner.runUnrestricted();
060        } finally {
061            TransactionHelper.commitOrRollbackTransaction();
062        }
063    }
064
065    public void exec(StatsTask task) {
066        cmdInstance.exec(task);
067    }
068
069    protected StatsTask getNextTask(DocumentModel root) {
070        if (cmdInstance.isPoolFull()) {
071            return null;
072        }
073        return new StatsTask(repositoryName, root.getRef(), includeBlob, cmdInstance);
074    }
075
076    protected RepoStatInfo getInfo() {
077        return cmdInstance.getInfo();
078    }
079
080}