001/*
002 * (C) Copyright 2015 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 org.apache.commons.collections.MapUtils;
022import org.apache.logging.log4j.LogManager;
023import org.apache.logging.log4j.Logger;
024import org.nuxeo.drive.service.FileSystemChangeFinder;
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.runtime.model.ContributionFragmentRegistry;
027
028/**
029 * Registry for the {@code changeFinder} contributions.
030 *
031 * @author Antoine Taillefer
032 * @see NuxeoDriveManagerImpl
033 * @since 7.3
034 */
035public class ChangeFinderRegistry extends ContributionFragmentRegistry<ChangeFinderDescriptor> {
036
037    private static final Logger log = LogManager.getLogger(ChangeFinderRegistry.class);
038
039    protected static final String CONTRIBUTION_ID = "changeFinderContrib";
040
041    protected FileSystemChangeFinder changeFinder;
042
043    @Override
044    public String getContributionId(ChangeFinderDescriptor contrib) {
045        return CONTRIBUTION_ID;
046    }
047
048    @Override
049    public void contributionUpdated(String id, ChangeFinderDescriptor contrib, ChangeFinderDescriptor newOrigContrib) {
050        try {
051            log.trace("Updating change finder contribution {}.", contrib);
052            changeFinder = contrib.getChangeFinder();
053        } catch (InstantiationException | IllegalAccessException e) {
054            throw new NuxeoException("Cannot update changeFinder contribution.", e);
055        }
056    }
057
058    @Override
059    public void contributionRemoved(String id, ChangeFinderDescriptor origContrib) {
060        log.trace("Clearing change finder.");
061        changeFinder = null;
062    }
063
064    @Override
065    public ChangeFinderDescriptor clone(ChangeFinderDescriptor orig) {
066        log.trace("Cloning contribution {}.", orig);
067        ChangeFinderDescriptor clone = new ChangeFinderDescriptor();
068        clone.changeFinderClass = orig.changeFinderClass;
069        clone.parameters = orig.parameters;
070        return clone;
071    }
072
073    @Override
074    public void merge(ChangeFinderDescriptor src, ChangeFinderDescriptor dst) {
075        log.trace("Merging contribution {} to contribution {}.", src, dst);
076        // Class
077        if (src.getChangeFinderClass() != null && !src.getChangeFinderClass().equals(dst.getChangeFinderClass())) {
078            dst.setChangeFinderClass(src.getChangeFinderClass());
079        }
080        // Parameters
081        if (!MapUtils.isEmpty(src.getParameters())) {
082            for (String name : src.getParameters().keySet()) {
083                dst.setParameter(name, src.getparameter(name));
084            }
085        }
086    }
087}