001/*
002 * (C) Copyright 2006-2013 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 */
016package org.nuxeo.ecm.core.management.works;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.nuxeo.ecm.core.work.api.Work;
022import org.nuxeo.ecm.core.work.api.Work.State;
023import org.nuxeo.ecm.core.work.api.WorkManager;
024import org.nuxeo.runtime.api.Framework;
025
026public class WorksQueueMonitoring implements WorksQueueMonitoringMBean {
027
028    protected final String queueId;
029
030    public WorksQueueMonitoring(String id) {
031        queueId = id;
032    }
033
034    protected WorkManager manager() {
035        return Framework.getLocalService(WorkManager.class);
036    }
037
038    @Override
039    public int getScheduledCount() {
040        return manager().getQueueSize(queueId, State.SCHEDULED);
041    }
042
043    @Override
044    public int getRunningCount() {
045        return manager().getQueueSize(queueId, State.RUNNING);
046    }
047
048    @Override
049    public int getCompletedCount() {
050        return manager().getQueueSize(queueId, State.COMPLETED);
051    }
052
053    @Override
054    public String[] getScheduledWorks() {
055        return listWorks(State.SCHEDULED);
056    }
057
058    @Override
059    public String[] getRunningWorks() {
060        return listWorks(State.RUNNING);
061    }
062
063    protected String[] listWorks(State state) {
064        List<String> works = new ArrayList<String>();
065        for (Work work : manager().listWork(queueId, state)) {
066            works.add(work.toString());
067        }
068        return works.toArray(new String[works.size()]);
069    }
070}