001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 */
017package org.nuxeo.ecm.core.storage.mongodb;
018
019import org.nuxeo.ecm.core.api.repository.Repository;
020import org.nuxeo.ecm.core.api.repository.RepositoryManager;
021import org.nuxeo.ecm.core.repository.RepositoryFactory;
022import org.nuxeo.runtime.api.Framework;
023import org.nuxeo.runtime.model.ComponentContext;
024import org.nuxeo.runtime.model.ComponentInstance;
025import org.nuxeo.runtime.model.DefaultComponent;
026import org.nuxeo.runtime.model.SimpleContributionRegistry;
027
028/**
029 * Service holding the configuration for MongoDB repositories.
030 *
031 * @since 5.9.4
032 */
033public class MongoDBRepositoryService extends DefaultComponent {
034
035    private static final String XP_REPOSITORY = "repository";
036
037    protected RepositoryDescriptorRegistry registry = new RepositoryDescriptorRegistry();
038
039    protected static class RepositoryDescriptorRegistry extends SimpleContributionRegistry<MongoDBRepositoryDescriptor> {
040
041        @Override
042        public String getContributionId(MongoDBRepositoryDescriptor contrib) {
043            return contrib.name;
044        }
045
046        @Override
047        public MongoDBRepositoryDescriptor clone(MongoDBRepositoryDescriptor orig) {
048            return new MongoDBRepositoryDescriptor(orig);
049        }
050
051        @Override
052        public void merge(MongoDBRepositoryDescriptor src, MongoDBRepositoryDescriptor dst) {
053            dst.merge(src);
054        }
055
056        @Override
057        public boolean isSupportingMerge() {
058            return true;
059        }
060
061        public void clear() {
062            currentContribs.clear();
063        }
064
065        public MongoDBRepositoryDescriptor getRepositoryDescriptor(String id) {
066            return getCurrentContribution(id);
067        }
068    }
069
070    @Override
071    public void activate(ComponentContext context) {
072        registry.clear();
073    }
074
075    @Override
076    public void deactivate(ComponentContext context) {
077        registry.clear();
078    }
079
080    @Override
081    public void registerContribution(Object contrib, String xpoint, ComponentInstance contributor) {
082        if (XP_REPOSITORY.equals(xpoint)) {
083            addContribution((MongoDBRepositoryDescriptor) contrib);
084        } else {
085            throw new RuntimeException("Unknown extension point: " + xpoint);
086        }
087    }
088
089    @Override
090    public void unregisterContribution(Object contrib, String xpoint, ComponentInstance contributor) {
091        if (XP_REPOSITORY.equals(xpoint)) {
092            removeContribution((MongoDBRepositoryDescriptor) contrib);
093        } else {
094            throw new RuntimeException("Unknown extension point: " + xpoint);
095        }
096    }
097
098    protected void addContribution(MongoDBRepositoryDescriptor descriptor) {
099        registry.addContribution(descriptor);
100        updateRegistration(descriptor.name);
101    }
102
103    protected void removeContribution(MongoDBRepositoryDescriptor descriptor) {
104        registry.removeContribution(descriptor);
105        updateRegistration(descriptor.name);
106    }
107
108    /**
109     * Update repository registration in high-level repository service.
110     */
111    protected void updateRegistration(String repositoryName) {
112        RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class);
113        MongoDBRepositoryDescriptor descriptor = registry.getRepositoryDescriptor(repositoryName);
114        if (descriptor == null) {
115            // last contribution removed
116            repositoryManager.removeRepository(repositoryName);
117            return;
118        }
119        // extract label, isDefault and factory
120        // and pass it to high-level registry
121        RepositoryFactory repositoryFactory = new MongoDBRepositoryFactory();
122        repositoryFactory.init(repositoryName);
123        Repository repository = new Repository(repositoryName, descriptor.label, descriptor.isDefault(),
124                repositoryFactory);
125        repositoryManager.addRepository(repository);
126    }
127
128    public MongoDBRepositoryDescriptor getRepositoryDescriptor(String name) {
129        return registry.getRepositoryDescriptor(name);
130    }
131
132}