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.ArrayList;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import org.apache.commons.collections.MapUtils;
029import org.apache.commons.lang3.SerializationUtils;
030import org.apache.commons.lang3.StringUtils;
031import org.apache.logging.log4j.LogManager;
032import org.apache.logging.log4j.Logger;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.runtime.model.ContributionFragmentRegistry;
035
036/**
037 * Registry for {@code fileSystemItemFactory} contributions.
038 *
039 * @author Antoine Taillefer
040 * @see FileSystemItemAdapterServiceImpl
041 */
042public class FileSystemItemFactoryRegistry extends ContributionFragmentRegistry<FileSystemItemFactoryDescriptor> {
043
044    private static final Logger log = LogManager.getLogger(FileSystemItemFactoryRegistry.class);
045
046    protected final Map<String, FileSystemItemFactoryDescriptor> factoryDescriptors = new HashMap<>();
047
048    @Override
049    public String getContributionId(FileSystemItemFactoryDescriptor contrib) {
050        String name = contrib.getName();
051        if (StringUtils.isEmpty(name)) {
052            throw new NuxeoException("Cannot register fileSystemItemFactory without a name.");
053        }
054        return name;
055    }
056
057    @Override
058    public void contributionUpdated(String id, FileSystemItemFactoryDescriptor contrib,
059            FileSystemItemFactoryDescriptor newOrigContrib) {
060        log.trace("Putting contribution {} with id {} in factory registry", contrib, id);
061        factoryDescriptors.put(id, contrib);
062    }
063
064    @Override
065    public void contributionRemoved(String id, FileSystemItemFactoryDescriptor origContrib) {
066        log.trace("Removing contribution with id {} from factory registry", id);
067        factoryDescriptors.remove(id);
068    }
069
070    @Override
071    public FileSystemItemFactoryDescriptor clone(FileSystemItemFactoryDescriptor orig) {
072        log.trace("Cloning contribution with id {}", orig::getName);
073        return SerializationUtils.clone(orig);
074    }
075
076    @Override
077    public void merge(FileSystemItemFactoryDescriptor src, FileSystemItemFactoryDescriptor dst) {
078        log.trace("Merging contribution with id {} to contribution with id {}", src::getName, dst::getName);
079        // Order
080        int srcOrder = src.getOrder();
081        if (srcOrder > 0 && srcOrder != dst.getOrder()) {
082            dst.setOrder(srcOrder);
083        }
084        // Doc type
085        if (!StringUtils.isEmpty(src.getDocType()) && !src.getDocType().equals(dst.getDocType())) {
086            dst.setDocType(src.getDocType());
087        }
088        // Facet
089        if (!StringUtils.isEmpty(src.getFacet()) && !src.getFacet().equals(dst.getFacet())) {
090            dst.setFacet(src.getFacet());
091        }
092        // Class
093        if (src.getFactoryClass() != null && !src.getFactoryClass().equals(dst.getFactoryClass())) {
094            dst.setFactoryClass(src.getFactoryClass());
095        }
096        // Parameters
097        if (!MapUtils.isEmpty(src.getParameters())) {
098            for (String name : src.getParameters().keySet()) {
099                dst.setParameter(name, src.getParameter(name));
100            }
101        }
102    }
103
104    protected List<FileSystemItemFactoryWrapper> getOrderedActiveFactories(Set<String> activeFactories) {
105        List<FileSystemItemFactoryWrapper> factories = new ArrayList<>();
106        List<FileSystemItemFactoryDescriptor> orderedFactoryDescriptors = new ArrayList<>(factoryDescriptors.values());
107        Collections.sort(orderedFactoryDescriptors);
108        for (FileSystemItemFactoryDescriptor factoryDesc : orderedFactoryDescriptors) {
109            // Only include active factories
110            if (activeFactories.contains(factoryDesc.getName())) {
111                FileSystemItemFactoryWrapper factoryWrapper = new FileSystemItemFactoryWrapper(factoryDesc.getDocType(),
112                        factoryDesc.getFacet(), factoryDesc.getFactory());
113                factories.add(factoryWrapper);
114            }
115        }
116        return factories;
117    }
118
119}