001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.service.impl;
018
019import org.apache.commons.collections.MapUtils;
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.drive.service.FileSystemChangeFinder;
023import org.nuxeo.ecm.core.api.NuxeoException;
024import org.nuxeo.runtime.model.ContributionFragmentRegistry;
025
026/**
027 * Registry for the {@code changeFinder} contributions.
028 *
029 * @author Antoine Taillefer
030 * @see NuxeoDriveManagerImpl
031 * @since 7.3
032 */
033public class ChangeFinderRegistry extends ContributionFragmentRegistry<ChangeFinderDescriptor> {
034
035    private static final Log log = LogFactory.getLog(ChangeFinderRegistry.class);
036
037    protected static final String CONTRIBUTION_ID = "changeFinderContrib";
038
039    protected FileSystemChangeFinder changeFinder;
040
041    @Override
042    public String getContributionId(ChangeFinderDescriptor contrib) {
043        return CONTRIBUTION_ID;
044    }
045
046    @Override
047    public void contributionUpdated(String id, ChangeFinderDescriptor contrib, ChangeFinderDescriptor newOrigContrib) {
048        try {
049            if (log.isTraceEnabled()) {
050                log.trace(String.format("Updating change finder contribution %s.", contrib));
051            }
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        if (log.isTraceEnabled()) {
067            log.trace(String.format("Cloning contribution %s.", orig));
068        }
069        ChangeFinderDescriptor clone = new ChangeFinderDescriptor();
070        clone.changeFinderClass = orig.changeFinderClass;
071        clone.parameters = orig.parameters;
072        return clone;
073    }
074
075    @Override
076    public void merge(ChangeFinderDescriptor src, ChangeFinderDescriptor dst) {
077        if (log.isTraceEnabled()) {
078            log.trace(String.format("Merging contribution %s to contribution %s.", src, dst));
079        }
080        // Class
081        if (src.getChangeFinderClass() != null && !src.getChangeFinderClass().equals(dst.getChangeFinderClass())) {
082            dst.setChangeFinderClass(src.getChangeFinderClass());
083        }
084        // Parameters
085        if (!MapUtils.isEmpty(src.getParameters())) {
086            for (String name : src.getParameters().keySet()) {
087                dst.setParameter(name, src.getparameter(name));
088            }
089        }
090    }
091}