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.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
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 Log log = LogFactory.getLog(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            if (log.isTraceEnabled()) {
052                log.trace(String.format("Updating change finder contribution %s.", contrib));
053            }
054            changeFinder = contrib.getChangeFinder();
055        } catch (InstantiationException | IllegalAccessException e) {
056            throw new NuxeoException("Cannot update changeFinder contribution.", e);
057        }
058    }
059
060    @Override
061    public void contributionRemoved(String id, ChangeFinderDescriptor origContrib) {
062        log.trace("Clearing change finder.");
063        changeFinder = null;
064    }
065
066    @Override
067    public ChangeFinderDescriptor clone(ChangeFinderDescriptor orig) {
068        if (log.isTraceEnabled()) {
069            log.trace(String.format("Cloning contribution %s.", orig));
070        }
071        ChangeFinderDescriptor clone = new ChangeFinderDescriptor();
072        clone.changeFinderClass = orig.changeFinderClass;
073        clone.parameters = orig.parameters;
074        return clone;
075    }
076
077    @Override
078    public void merge(ChangeFinderDescriptor src, ChangeFinderDescriptor dst) {
079        if (log.isTraceEnabled()) {
080            log.trace(String.format("Merging contribution %s to contribution %s.", src, dst));
081        }
082        // Class
083        if (src.getChangeFinderClass() != null && !src.getChangeFinderClass().equals(dst.getChangeFinderClass())) {
084            dst.setChangeFinderClass(src.getChangeFinderClass());
085        }
086        // Parameters
087        if (!MapUtils.isEmpty(src.getParameters())) {
088            for (String name : src.getParameters().keySet()) {
089                dst.setParameter(name, src.getparameter(name));
090            }
091        }
092    }
093}