001/*
002 * (C) Copyright 2006-2011 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.sql.management;
020
021import java.util.List;
022
023import org.nuxeo.ecm.core.blob.DocumentBlobManager;
024import org.nuxeo.ecm.core.blob.binary.BinaryManagerStatus;
025import org.nuxeo.ecm.core.storage.sql.RepositoryManagement;
026import org.nuxeo.ecm.core.storage.sql.coremodel.SQLRepositoryService;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * An MBean to manage SQL storage repositories.
031 */
032public class SQLRepositoryStatus implements SQLRepositoryStatusMBean {
033
034    protected static List<RepositoryManagement> getRepositories() {
035        SQLRepositoryService sqlRepositoryService = Framework.getService(SQLRepositoryService.class);
036        return sqlRepositoryService.getRepositories();
037    }
038
039    @Override
040    public String listActiveSessions() {
041        StringBuilder buf = new StringBuilder();
042        buf.append("Actives sessions for SQL repositories:<br />");
043        for (RepositoryManagement repository : getRepositories()) {
044            buf.append("<b>").append(repository.getName()).append("</b>: ");
045            buf.append(repository.getActiveSessionsCount());
046            buf.append("<br />");
047        }
048        return buf.toString();
049    }
050
051    @Override
052    public int getActiveSessionsCount() {
053        int count = 0;
054        for (RepositoryManagement repository : getRepositories()) {
055            count += repository.getActiveSessionsCount();
056        }
057        return count;
058    }
059
060    @Override
061    public String clearCaches() {
062        StringBuilder buf = new StringBuilder();
063        buf.append("Cleared cached objects for SQL repositories:<br />");
064        for (RepositoryManagement repository : getRepositories()) {
065            buf.append("<b>").append(repository.getName()).append("</b>: ");
066            buf.append(repository.clearCaches());
067            buf.append("<br />");
068        }
069        return buf.toString();
070    }
071
072    @Override
073    public long getCachesSize() {
074        long size = 0;
075        for (RepositoryManagement repository : getRepositories()) {
076            size += repository.getCacheSize();
077        }
078        return size;
079    }
080
081    @Override
082    public String listRemoteSessions() {
083        StringBuilder buf = new StringBuilder();
084        buf.append("Actives remote session for SQL repositories:<br />");
085        for (RepositoryManagement repository : getRepositories()) {
086            buf.append("<b>").append(repository.getName()).append("</b>");
087            buf.append("<br/>");
088        }
089        return buf.toString();
090    }
091
092    @Override
093    public BinaryManagerStatus gcBinaries(boolean delete) {
094        return Framework.getService(DocumentBlobManager.class).garbageCollectBinaries(delete);
095    }
096
097    @Override
098    public boolean isBinariesGCInProgress() {
099        return Framework.getService(DocumentBlobManager.class).isBinariesGarbageCollectionInProgress();
100    }
101
102}