001/*
002 * (C) Copyright 2015-2016 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.dbs;
020
021import org.nuxeo.ecm.core.api.NuxeoException;
022import org.nuxeo.ecm.core.api.repository.Repository;
023import org.nuxeo.ecm.core.api.repository.RepositoryManager;
024import org.nuxeo.ecm.core.repository.RepositoryFactory;
025import org.nuxeo.runtime.api.Framework;
026import org.nuxeo.runtime.model.ComponentContext;
027import org.nuxeo.runtime.model.DefaultComponent;
028import org.nuxeo.runtime.model.SimpleContributionRegistry;
029
030/**
031 * Service holding the configuration for DBS repositories.
032 *
033 * @since 7.10-HF04, 8.1
034 */
035public class DBSRepositoryService extends DefaultComponent {
036
037    protected DBSRepositoryDescriptorRegistry registry = new DBSRepositoryDescriptorRegistry();
038
039    protected static class DBSRepositoryDescriptorRegistry extends SimpleContributionRegistry<DBSRepositoryDescriptor> {
040
041        @Override
042        public String getContributionId(DBSRepositoryDescriptor contrib) {
043            return contrib.name;
044        }
045
046        @Override
047        public DBSRepositoryDescriptor clone(DBSRepositoryDescriptor orig) {
048            return orig.clone();
049        }
050
051        @Override
052        public void merge(DBSRepositoryDescriptor src, DBSRepositoryDescriptor 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 DBSRepositoryDescriptor 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    public void addContribution(DBSRepositoryDescriptor descriptor,
081            Class<? extends DBSRepositoryFactory> factoryClass) {
082        registry.addContribution(descriptor);
083        updateRegistration(descriptor.name, factoryClass);
084    }
085
086    public void removeContribution(DBSRepositoryDescriptor descriptor,
087            Class<? extends DBSRepositoryFactory> factoryClass) {
088        registry.removeContribution(descriptor);
089        updateRegistration(descriptor.name, factoryClass);
090    }
091
092    /**
093     * Update repository registration in high-level repository service.
094     */
095    protected void updateRegistration(String repositoryName, Class<? extends DBSRepositoryFactory> factoryClass) {
096        RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class);
097        DBSRepositoryDescriptor descriptor = registry.getRepositoryDescriptor(repositoryName);
098        if (descriptor == null) {
099            // last contribution removed
100            repositoryManager.removeRepository(repositoryName);
101            return;
102        }
103        // extract label, isDefault
104        // and pass it to high-level registry
105        RepositoryFactory repositoryFactory;
106        try {
107            repositoryFactory = factoryClass.getConstructor(String.class).newInstance(repositoryName);
108        } catch (ReflectiveOperationException e) {
109            throw new NuxeoException(e);
110        }
111        Repository repository = new Repository(repositoryName, descriptor.label, descriptor.isDefault(),
112                repositoryFactory);
113        repositoryManager.addRepository(repository);
114    }
115
116    public DBSRepositoryDescriptor getRepositoryDescriptor(String name) {
117        return registry.getRepositoryDescriptor(name);
118    }
119
120}