001/*
002 * (C) Copyright 2006-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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.opencmis.impl.server;
020
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Map;
024import java.util.concurrent.ConcurrentHashMap;
025
026import org.nuxeo.ecm.core.api.CoreInstance;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.repository.RepositoryManager;
029import org.nuxeo.runtime.api.Framework;
030import org.nuxeo.runtime.model.ComponentContext;
031import org.nuxeo.runtime.model.DefaultComponent;
032
033/**
034 * Information about all Nuxeo repositories.
035 * <p>
036 * Information is cached, and an initial connection to the repository is needed to get the root folder id.
037 */
038public class NuxeoRepositories extends DefaultComponent {
039
040    protected Map<String, NuxeoRepository> repositories;
041
042    @Override
043    public void start(ComponentContext context) {
044        repositories = new ConcurrentHashMap<>();
045    }
046
047    @Override
048    public void stop(ComponentContext context) {
049        repositories = null;
050    }
051
052    public NuxeoRepository getRepository(String repositoryId) {
053        initRepositories();
054        return repositories.get(repositoryId);
055    }
056
057    public List<NuxeoRepository> getRepositories() {
058        initRepositories();
059        return new ArrayList<>(repositories.values());
060    }
061
062    protected void initRepositories() {
063        if (!repositories.isEmpty()) {
064            return;
065        }
066        RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
067        for (String repositoryName : repositoryManager.getRepositoryNames()) {
068            CoreSession coreSession = CoreInstance.getCoreSession(repositoryName);
069            String rootFolderId = coreSession.getRootDocument().getId();
070            repositories.put(repositoryName, new NuxeoRepository(repositoryName, rootFolderId));
071        }
072    }
073
074}