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            MongoDBRepositoryDescriptor desc = (MongoDBRepositoryDescriptor) contrib;
046            Framework.getService(DBSRepositoryService.class).addContribution(desc, MongoDBRepositoryFactory.class);
047            handleConnectionContribution(desc, (c, d) -> c.registerContribution(d, "connection", contributor));
048        }
049    }
050
051    @Override
052    public void unregisterContribution(Object contrib, String xpoint, ComponentInstance contributor) {
053        if (XP_REPOSITORY.equals(xpoint)) {
054            MongoDBRepositoryDescriptor desc = (MongoDBRepositoryDescriptor) contrib;
055            Framework.getService(DBSRepositoryService.class).removeContribution(desc, MongoDBRepositoryFactory.class);
056            handleConnectionContribution(desc, (c, d) -> c.unregisterContribution(d, "connection", contributor));
057        }
058    }
059
060    /**
061     * Backward compatibility for {@link MongoDBRepositoryDescriptor#server descriptor.server} and
062     * {@link MongoDBRepositoryDescriptor#dbname descriptor.dbname}
063     *
064     * @since 9.3
065     * @deprecated since 9.3
066     */
067    @Deprecated
068    protected void handleConnectionContribution(MongoDBRepositoryDescriptor descriptor,
069            BiConsumer<DefaultComponent, MongoDBConnectionConfig> consumer) {
070        if (StringUtils.isNotBlank(descriptor.server)) {
071            String id = "repository/" + descriptor.name;
072            String server = descriptor.server;
073            String dbName = StringUtils.defaultIfBlank(descriptor.dbname, DB_DEFAULT);
074            MongoDBConnectionConfig connection = new MongoDBConnectionConfig();
075            connection.server = server;
076            connection.dbname = dbName;
077            connection.id = id;
078
079            DefaultComponent component = (DefaultComponent) Framework.getRuntime()
080                                                                     .getComponent(MongoDBComponent.COMPONENT_NAME);
081            consumer.accept(component, connection);
082        }
083    }
084
085}