001/*
002 * (C) Copyright 2013-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 org.apache.commons.lang3.StringUtils;
022import org.apache.logging.log4j.LogManager;
023import org.apache.logging.log4j.Logger;
024import org.nuxeo.runtime.model.ContributionFragmentRegistry;
025
026/**
027 * Registry for the {@code activeTopLevelFolderItemFactory} contributions.
028 *
029 * @author Antoine Taillefer
030 * @see FileSystemItemAdapterServiceImpl
031 */
032public class ActiveTopLevelFolderItemFactoryRegistry
033        extends ContributionFragmentRegistry<ActiveTopLevelFolderItemFactoryDescriptor> {
034
035    private static final Logger log = LogManager.getLogger(ActiveTopLevelFolderItemFactoryRegistry.class);
036
037    protected static final String CONTRIBUTION_ID = "activeTopLevelFolderItemFactoriesContrib";
038
039    protected String activeFactory;
040
041    @Override
042    public String getContributionId(ActiveTopLevelFolderItemFactoryDescriptor contrib) {
043        return CONTRIBUTION_ID;
044    }
045
046    @Override
047    public void contributionUpdated(String id, ActiveTopLevelFolderItemFactoryDescriptor contrib,
048            ActiveTopLevelFolderItemFactoryDescriptor newOrigContrib) {
049        log.trace("Updating activeTopLevelFolderItemFactory contribution {}.", contrib);
050        log.trace("Setting active factory to {}.", contrib::getName);
051        activeFactory = contrib.getName();
052    }
053
054    @Override
055    public void contributionRemoved(String id, ActiveTopLevelFolderItemFactoryDescriptor origContrib) {
056        log.trace("Clearing active factory.");
057        activeFactory = null;
058    }
059
060    @Override
061    public ActiveTopLevelFolderItemFactoryDescriptor clone(ActiveTopLevelFolderItemFactoryDescriptor orig) {
062        log.trace("Cloning contribution {}.", orig);
063        ActiveTopLevelFolderItemFactoryDescriptor clone = new ActiveTopLevelFolderItemFactoryDescriptor();
064        clone.name = orig.name;
065        return clone;
066    }
067
068    @Override
069    public void merge(ActiveTopLevelFolderItemFactoryDescriptor src, ActiveTopLevelFolderItemFactoryDescriptor dst) {
070        log.trace("Merging contribution {} to contribution {}.", src, dst);
071        if (!StringUtils.isEmpty(src.getName()) && !src.getName().equals(dst.getName())) {
072            dst.setName(src.getName());
073        }
074    }
075}