001/*
002 * (C) Copyright 2014-2018 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.storage.mongodb;
020
021import java.util.function.BiConsumer;
022
023import org.apache.commons.lang3.StringUtils;
024import org.nuxeo.ecm.core.storage.dbs.DBSRepositoryService;
025import org.nuxeo.runtime.api.Framework;
026import org.nuxeo.runtime.model.ComponentInstance;
027import org.nuxeo.runtime.model.DefaultComponent;
028import org.nuxeo.runtime.mongodb.MongoDBComponent;
029import org.nuxeo.runtime.mongodb.MongoDBConnectionConfig;
030
031/**
032 * Service holding the configuration for MongoDB repositories.
033 *
034 * @since 5.9.4
035 */
036public class MongoDBRepositoryService extends DefaultComponent {
037
038    public static final String DB_DEFAULT = "nuxeo";
039
040    private static final String XP_REPOSITORY = "repository";
041
042    @Override
043    public void registerContribution(Object contrib, String xpoint, ComponentInstance contributor) {
044        if (XP_REPOSITORY.equals(xpoint)) {
045            addContribution((MongoDBRepositoryDescriptor) contrib);
046            handleConnectionContribution((MongoDBRepositoryDescriptor) contrib,
047                    (c, d) -> c.registerContribution(d, "connection", contributor));
048        } else {
049            throw new RuntimeException("Unknown extension point: " + xpoint);
050        }
051    }
052
053    @Override
054    public void unregisterContribution(Object contrib, String xpoint, ComponentInstance contributor) {
055        if (XP_REPOSITORY.equals(xpoint)) {
056            removeContribution((MongoDBRepositoryDescriptor) contrib);
057            handleConnectionContribution((MongoDBRepositoryDescriptor) contrib,
058                    (c, d) -> c.unregisterContribution(d, "connection", contributor));
059        } else {
060            throw new RuntimeException("Unknown extension point: " + xpoint);
061        }
062    }
063
064    protected void addContribution(MongoDBRepositoryDescriptor descriptor) {
065        Framework.getService(DBSRepositoryService.class).addContribution(descriptor, MongoDBRepositoryFactory.class);
066    }
067
068    protected void removeContribution(MongoDBRepositoryDescriptor descriptor) {
069        Framework.getService(DBSRepositoryService.class).removeContribution(descriptor, MongoDBRepositoryFactory.class);
070    }
071
072    /**
073     * Backward compatibility for {@link MongoDBRepositoryDescriptor#server descriptor.server} and
074     * {@link MongoDBRepositoryDescriptor#dbname descriptor.dbname}
075     *
076     * @since 9.3
077     * @deprecated since 9.3
078     */
079    @Deprecated
080    protected void handleConnectionContribution(MongoDBRepositoryDescriptor descriptor,
081            BiConsumer<DefaultComponent, MongoDBConnectionConfig> consumer) {
082        if (StringUtils.isNotBlank(descriptor.server)) {
083            String id = "repository/" + descriptor.name;
084            String server = descriptor.server;
085            String dbName = StringUtils.defaultIfBlank(descriptor.dbname, DB_DEFAULT);
086            MongoDBConnectionConfig connection = new MongoDBConnectionConfig(id, server, dbName);
087
088            DefaultComponent component = (DefaultComponent) Framework.getRuntime().getComponent(MongoDBComponent.NAME);
089            consumer.accept(component, connection);
090        }
091    }
092
093}