001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Mariana Cedica
018 */
019package org.nuxeo.ecm.core.management.statuses;
020
021import java.util.List;
022
023import org.nuxeo.ecm.core.api.CoreInstance;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.management.api.Probe;
026import org.nuxeo.ecm.core.management.api.ProbeStatus;
027import org.nuxeo.ecm.core.repository.RepositoryService;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.transaction.TransactionHelper;
030
031/**
032 * Probe that checks that the repository is available by fetching the root doc
033 * 
034 * @since 9.3
035 */
036public class RepositoryStatusProbe implements Probe {
037
038    @Override
039    public ProbeStatus run() {
040        ProbeStatus status;
041        status = TransactionHelper.runInTransaction(() -> {
042            List<String> repositories = getRepositoryName();
043            boolean success = !repositories.isEmpty();
044            for (String repository : repositories) {
045                success = success && CoreInstance.doPrivileged(repository, (CoreSession session) -> {
046                    return session.exists(session.getRootDocument().getRef());
047                });
048            }
049            if (success) {
050                return ProbeStatus.newSuccess("Repository started");
051            } else {
052                return ProbeStatus.newFailure("Repository not started corectly");
053            }
054        });
055        return status;
056
057    }
058
059    protected List<String> getRepositoryName() {
060        return Framework.getService(RepositoryService.class).getRepositoryNames();
061    }
062}