001/*
002 * (C) Copyright 2006-2014 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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.api.repository;
021
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.LinkedHashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.concurrent.ConcurrentHashMap;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.runtime.model.ComponentInstance;
033import org.nuxeo.runtime.model.DefaultComponent;
034
035/**
036 * High-level service to get to a {@link org.nuxeo.ecm.core.api.repository.Repository Repository} and from there to
037 * {@link org.nuxeo.ecm.core.api.CoreSession CoreSession} objects.
038 */
039public class RepositoryManagerImpl extends DefaultComponent implements RepositoryManager {
040
041    private static final Log log = LogFactory.getLog(RepositoryManagerImpl.class);
042
043    private Map<String, Repository> repositories = Collections.synchronizedMap(new LinkedHashMap<String, Repository>());
044
045    // compat from old extension point
046    private Map<String, Repository> compatRepositories = new ConcurrentHashMap<String, Repository>();
047
048    // compat
049    private static final String XP_REPOSITORIES = "repositories";
050
051    @Override
052    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
053        if (XP_REPOSITORIES.equals(extensionPoint)) {
054            Repository repo = (Repository) contribution;
055            log.warn("Using old-style extension point" + " org.nuxeo.ecm.core.api.repository.RepositoryManager"
056                    + " for repository \"" + repo.getName()
057                    + "\", use org.nuxeo.ecm.core.storage.sql.RepositoryService instead");
058            compatRepositories.put(repo.getName(), repo);
059        } else {
060            throw new RuntimeException("Unknown extension point: " + extensionPoint);
061        }
062    }
063
064    @Override
065    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
066        if (XP_REPOSITORIES.equals(extensionPoint)) {
067            Repository repo = (Repository) contribution;
068            compatRepositories.remove(repo.getName());
069        } else {
070            throw new RuntimeException("Unknown extension point: " + extensionPoint);
071        }
072    }
073
074    // called by low-level repository service
075    @Override
076    public void addRepository(Repository repository) {
077        String name = repository.getName();
078        if (repositories.containsKey(name)) {
079            log.info("Overriding repository: " + name);
080        } else {
081            log.info("Registering repository: " + name);
082        }
083        Repository compat = compatRepositories.get(name);
084        if (compat != null) {
085            if (repository.getLabel() == null) {
086                repository.setLabel(compat.getLabel());
087            }
088            if (repository.getDefault() != null) {
089                repository.setDefault(compat.getDefault());
090            }
091        }
092        repositories.put(name, repository);
093    }
094
095    // call by low-level repository service
096    @Override
097    public void removeRepository(String name) {
098        log.info("Removing repository: " + name);
099        Repository repo = repositories.remove(name);
100    }
101
102    @Override
103    public Collection<Repository> getRepositories() {
104        return new ArrayList<Repository>(repositories.values());
105    }
106
107    @Override
108    public List<String> getRepositoryNames() {
109        return new ArrayList<String>(repositories.keySet());
110    }
111
112    @Override
113    public Repository getRepository(String name) {
114        return repositories.get(name);
115    }
116
117    @Override
118    public Repository getDefaultRepository() {
119        for (Repository repository : repositories.values()) {
120            if (repository.isDefault()) {
121                return repository;
122            }
123            if ("default".equals(repository.getName())) {
124                return repository;
125            }
126        }
127        // fallback to first in list
128        if (!repositories.isEmpty()) {
129            return repositories.values().iterator().next();
130        }
131        // no repository at all
132        throw new RuntimeException("No repository defined");
133    }
134
135    @Override
136    public String getDefaultRepositoryName() {
137        return getDefaultRepository().getName();
138    }
139
140    @Override
141    @SuppressWarnings("unchecked")
142    public <T> T getAdapter(Class<T> adapter) {
143        if (RepositoryManager.class.isAssignableFrom(adapter)) {
144            return (T) this;
145        }
146        return null;
147    }
148
149}