001/*
002 * (C) Copyright 2012-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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.service.impl;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.commons.collections.MapUtils;
025import org.apache.commons.lang3.StringUtils;
026import org.apache.logging.log4j.LogManager;
027import org.apache.logging.log4j.Logger;
028import org.nuxeo.drive.service.TopLevelFolderItemFactory;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.runtime.model.ContributionFragmentRegistry;
031
032/**
033 * Registry for {@code topLevelFolderItemFactory} contributions.
034 *
035 * @author Antoine Taillefer
036 * @see FileSystemItemAdapterServiceImpl
037 */
038public class TopLevelFolderItemFactoryRegistry
039        extends ContributionFragmentRegistry<TopLevelFolderItemFactoryDescriptor> {
040
041    private static final Logger log = LogManager.getLogger(TopLevelFolderItemFactoryRegistry.class);
042
043    protected Map<String, TopLevelFolderItemFactory> factories = new HashMap<>();
044
045    @Override
046    public String getContributionId(TopLevelFolderItemFactoryDescriptor contrib) {
047        String name = contrib.getName();
048        if (StringUtils.isEmpty(name)) {
049            throw new NuxeoException("Cannot register topLevelFolderItemFactory without a name.");
050        }
051        return name;
052    }
053
054    @Override
055    public void contributionUpdated(String id, TopLevelFolderItemFactoryDescriptor contrib,
056            TopLevelFolderItemFactoryDescriptor newOrigContrib) {
057        try {
058            log.trace("Putting contribution with class name {} in factory registry.", contrib::getName);
059            factories.put(id, contrib.getFactory());
060        } catch (ReflectiveOperationException e) {
061            throw new NuxeoException("Cannot update topLevelFolderItemFactory contribution.", e);
062        }
063    }
064
065    @Override
066    public void contributionRemoved(String id, TopLevelFolderItemFactoryDescriptor origContrib) {
067        log.trace("Removing contribution with class name {} in factory registry.", id);
068        factories.remove(id);
069    }
070
071    @Override
072    public TopLevelFolderItemFactoryDescriptor clone(TopLevelFolderItemFactoryDescriptor orig) {
073        log.trace("Cloning contribution with class name {}.", orig::getName);
074        TopLevelFolderItemFactoryDescriptor clone = new TopLevelFolderItemFactoryDescriptor();
075        clone.factoryClass = orig.factoryClass;
076        clone.parameters = orig.parameters;
077        return clone;
078    }
079
080    @Override
081    public void merge(TopLevelFolderItemFactoryDescriptor src, TopLevelFolderItemFactoryDescriptor dst) {
082        log.trace("Merging contribution with class name {} to contribution with class name {}", src::getName,
083                dst::getName);
084        // Class
085        if (src.getFactoryClass() != null && !src.getFactoryClass().equals(dst.getFactoryClass())) {
086            dst.setFactoryClass(src.getFactoryClass());
087        }
088        // Parameters
089        if (!MapUtils.isEmpty(src.getParameters())) {
090            for (String name : src.getParameters().keySet()) {
091                dst.setParameter(name, src.getparameter(name));
092            }
093        }
094    }
095
096    protected TopLevelFolderItemFactory getActiveFactory(String activeFactory) {
097        return factories.get(activeFactory);
098    }
099}