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; 030import org.nuxeo.runtime.mongodb.MongoDBConnectionService; 031 032/** 033 * Service holding the configuration for MongoDB repositories. 034 * 035 * @since 5.9.4 036 */ 037public class MongoDBRepositoryService extends DefaultComponent { 038 039 public static final String DB_DEFAULT = "nuxeo"; 040 041 private static final String XP_REPOSITORY = "repository"; 042 043 @Override 044 public void registerContribution(Object contrib, String xpoint, ComponentInstance contributor) { 045 if (XP_REPOSITORY.equals(xpoint)) { 046 MongoDBRepositoryDescriptor desc = (MongoDBRepositoryDescriptor) contrib; 047 Framework.getService(DBSRepositoryService.class).addContribution(desc, MongoDBRepositoryFactory.class); 048 handleConnectionContribution(desc, (c, d) -> c.registerContribution(d, "connection", contributor)); 049 } 050 } 051 052 @Override 053 public void unregisterContribution(Object contrib, String xpoint, ComponentInstance contributor) { 054 if (XP_REPOSITORY.equals(xpoint)) { 055 MongoDBRepositoryDescriptor desc = (MongoDBRepositoryDescriptor) contrib; 056 Framework.getService(DBSRepositoryService.class).removeContribution(desc, MongoDBRepositoryFactory.class); 057 handleConnectionContribution(desc, (c, d) -> c.unregisterContribution(d, "connection", contributor)); 058 } 059 } 060 061 /** 062 * Backward compatibility for {@link MongoDBRepositoryDescriptor#server descriptor.server} and 063 * {@link MongoDBRepositoryDescriptor#dbname descriptor.dbname} 064 * 065 * @since 9.3 066 * @deprecated since 9.3 067 */ 068 @Deprecated 069 protected void handleConnectionContribution(MongoDBRepositoryDescriptor descriptor, 070 BiConsumer<DefaultComponent, MongoDBConnectionConfig> consumer) { 071 if (StringUtils.isNotBlank(descriptor.server)) { 072 String id = "repository/" + descriptor.name; 073 String server = descriptor.server; 074 String dbName = StringUtils.defaultIfBlank(descriptor.dbname, DB_DEFAULT); 075 MongoDBConnectionConfig connection = new MongoDBConnectionConfig(); 076 connection.server = server; 077 connection.dbname = dbName; 078 connection.id = id; 079 080 MongoDBComponent component = (MongoDBComponent) Framework.getService(MongoDBConnectionService.class); 081 082 consumer.accept(component, connection); 083 } 084 } 085 086}