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 java.util.concurrent.LinkedBlockingQueue;
025import java.util.concurrent.ThreadFactory;
026import java.util.concurrent.ThreadPoolExecutor;
027import java.util.concurrent.TimeUnit;
028import java.util.concurrent.atomic.AtomicInteger;
029
030import org.nuxeo.ecm.core.api.DocumentRef;
031
032/**
033 * This class holds and manage the threads used to compute stats on the document repository
034 *
035 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
036 */
037public class RepoStat {
038
039    protected final ThreadPoolExecutor pool;
040
041    protected int nbThreads = 5;
042
043    protected final String repoName;
044
045    protected final boolean includeBlob;
046
047    protected RepoStatInfo info;
048
049    public RepoStat(String repoName, int nbThreads, boolean includeBlob) {
050        this.nbThreads = nbThreads;
051        this.repoName = repoName;
052        this.includeBlob = includeBlob;
053        pool = new ThreadPoolExecutor(nbThreads, nbThreads, 500L, TimeUnit.MILLISECONDS,
054                new LinkedBlockingQueue<Runnable>(100), new DaemonThreadFactory());
055    }
056
057    public void exec(StatsTask task) {
058        pool.execute(task);
059    }
060
061    public void run(DocumentRef root) {
062        info = new RepoStatInfo();
063        StatsTask task = new StatsTask(repoName, root, includeBlob, this);
064        exec(task);
065    }
066
067    protected boolean isPoolFull() {
068        return pool.getQueue().size() > 1;
069    }
070
071    public RepoStatInfo getInfo() {
072        return info;
073    }
074
075    public boolean isRunning() {
076        return pool.getActiveCount() > 0;
077    }
078
079    protected static class DaemonThreadFactory implements ThreadFactory {
080
081        private final ThreadGroup group;
082
083        private final String namePrefix;
084
085        private static final AtomicInteger poolNumber = new AtomicInteger();
086
087        private final AtomicInteger threadNumber = new AtomicInteger();
088
089        public DaemonThreadFactory() {
090            SecurityManager s = System.getSecurityManager();
091            group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
092            namePrefix = "RepoStatThread-" + poolNumber.incrementAndGet() + '-';
093        }
094
095        @Override
096        public Thread newThread(Runnable r) {
097            String name = namePrefix + threadNumber.incrementAndGet();
098            Thread t = new Thread(group, r, name);
099            t.setDaemon(true);
100            t.setPriority(Thread.NORM_PRIORITY);
101            return t;
102        }
103
104    }
105}