001/*
002 * (C) Copyright 2013 Nuxeo SA (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.HashSet;
022import java.util.Set;
023
024import org.apache.commons.lang3.SerializationUtils;
025import org.apache.logging.log4j.LogManager;
026import org.apache.logging.log4j.Logger;
027import org.nuxeo.runtime.model.ContributionFragmentRegistry;
028
029/**
030 * Registry for the {@code activeFileSystemItemFactories} contributions.
031 *
032 * @author Antoine Taillefer
033 * @see FileSystemItemAdapterServiceImpl
034 */
035public class ActiveFileSystemItemFactoryRegistry
036        extends ContributionFragmentRegistry<ActiveFileSystemItemFactoriesDescriptor> {
037
038    private static final Logger log = LogManager.getLogger(ActiveFileSystemItemFactoryRegistry.class);
039
040    protected static final String CONTRIBUTION_ID = "activeFileSystemItemFactoriesContrib";
041
042    protected Set<String> activeFactories = new HashSet<>();
043
044    @Override
045    public String getContributionId(ActiveFileSystemItemFactoriesDescriptor contrib) {
046        return CONTRIBUTION_ID;
047    }
048
049    @Override
050    public void contributionUpdated(String id, ActiveFileSystemItemFactoriesDescriptor contrib,
051            ActiveFileSystemItemFactoriesDescriptor newOrigContrib) {
052        log.trace("Updating activeFileSystemItemFactories contribution {}.", contrib);
053        if (contrib.isMerge()) {
054            // Merge active factories
055            for (ActiveFileSystemItemFactoryDescriptor factory : contrib.getFactories()) {
056                if (activeFactories.contains(factory.getName()) && !factory.isEnabled()) {
057                    log.trace("Removing factory {} from active factories.", factory::getName);
058                    activeFactories.remove(factory.getName());
059                }
060                if (!activeFactories.contains(factory.getName()) && factory.isEnabled()) {
061                    log.trace("Adding factory {} to active factories.", factory::getName);
062                    activeFactories.add(factory.getName());
063                }
064            }
065        } else {
066            // No merge, reset active factories
067            log.trace("Clearing active factories as contribution {} doesn't merge.", contrib);
068            activeFactories.clear();
069            for (ActiveFileSystemItemFactoryDescriptor factory : contrib.getFactories()) {
070                if (factory.isEnabled()) {
071                    log.trace("Adding factory {} to active factories.", factory::getName);
072                    activeFactories.add(factory.getName());
073                }
074            }
075        }
076    }
077
078    @Override
079    public void contributionRemoved(String id, ActiveFileSystemItemFactoriesDescriptor origContrib) {
080        log.trace("Clearing active factories.");
081        activeFactories.clear();
082    }
083
084    @Override
085    public ActiveFileSystemItemFactoriesDescriptor clone(ActiveFileSystemItemFactoriesDescriptor orig) {
086        log.trace("Cloning contribution {}.", orig);
087        return SerializationUtils.clone(orig);
088    }
089
090    @Override
091    public void merge(ActiveFileSystemItemFactoriesDescriptor src, ActiveFileSystemItemFactoriesDescriptor dst) {
092        log.trace("Merging contribution {} to contribution {}.", src, dst);
093        // Merge
094        if (src.isMerge() != dst.isMerge()) {
095            dst.setMerge(src.isMerge());
096        }
097        // Factories
098        for (ActiveFileSystemItemFactoryDescriptor factory : src.getFactories()) {
099            int indexOfFactory = dst.getFactories().indexOf(factory);
100            if (indexOfFactory > -1) {
101                dst.getFactories().get(indexOfFactory).setEnabled(factory.isEnabled());
102            } else {
103                dst.getFactories().add(factory);
104            }
105        }
106    }
107}