001/*
002 * (C) Copyright 2012 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.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 java.util.HashMap;
020import java.util.Map;
021
022import org.apache.commons.collections.MapUtils;
023import org.apache.commons.lang.StringUtils;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.drive.service.TopLevelFolderItemFactory;
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.runtime.model.ContributionFragmentRegistry;
029
030/**
031 * Registry for {@code topLevelFolderItemFactory} contributions.
032 *
033 * @author Antoine Taillefer
034 * @see FileSystemItemAdapterServiceImpl
035 */
036public class TopLevelFolderItemFactoryRegistry extends
037        ContributionFragmentRegistry<TopLevelFolderItemFactoryDescriptor> {
038
039    private static final Log log = LogFactory.getLog(TopLevelFolderItemFactoryRegistry.class);
040
041    protected Map<String, TopLevelFolderItemFactory> factories = new HashMap<String, TopLevelFolderItemFactory>();
042
043    @Override
044    public String getContributionId(TopLevelFolderItemFactoryDescriptor contrib) {
045        String name = contrib.getName();
046        if (StringUtils.isEmpty(name)) {
047            throw new NuxeoException("Cannot register topLevelFolderItemFactory without a name.");
048        }
049        return name;
050    }
051
052    @Override
053    public void contributionUpdated(String id, TopLevelFolderItemFactoryDescriptor contrib,
054            TopLevelFolderItemFactoryDescriptor newOrigContrib) {
055        try {
056            if (log.isTraceEnabled()) {
057                log.trace(String.format("Putting contribution with class name %s in factory registry.",
058                        contrib.getName()));
059            }
060            factories.put(id, contrib.getFactory());
061        } catch (InstantiationException | IllegalAccessException e) {
062            throw new NuxeoException("Cannot update topLevelFolderItemFactory contribution.", e);
063        }
064    }
065
066    @Override
067    public void contributionRemoved(String id, TopLevelFolderItemFactoryDescriptor origContrib) {
068        if (log.isTraceEnabled()) {
069            log.trace(String.format("Removing contribution with class name %s in factory registry.", id));
070        }
071        factories.remove(id);
072    }
073
074    @Override
075    public TopLevelFolderItemFactoryDescriptor clone(TopLevelFolderItemFactoryDescriptor orig) {
076        if (log.isTraceEnabled()) {
077            log.trace(String.format("Cloning contribution with class name %s.", orig.getName()));
078        }
079        TopLevelFolderItemFactoryDescriptor clone = new TopLevelFolderItemFactoryDescriptor();
080        clone.factoryClass = orig.factoryClass;
081        clone.parameters = orig.parameters;
082        return clone;
083    }
084
085    @Override
086    public void merge(TopLevelFolderItemFactoryDescriptor src, TopLevelFolderItemFactoryDescriptor dst) {
087        if (log.isTraceEnabled()) {
088            log.trace(String.format("Merging contribution with class name %s to contribution with class name %s",
089                    src.getName(), dst.getName()));
090        }
091        // Class
092        if (src.getFactoryClass() != null && !src.getFactoryClass().equals(dst.getFactoryClass())) {
093            dst.setFactoryClass(src.getFactoryClass());
094        }
095        // Parameters
096        if (!MapUtils.isEmpty(src.getParameters())) {
097            for (String name : src.getParameters().keySet()) {
098                dst.setParameter(name, src.getparameter(name));
099            }
100        }
101    }
102
103    protected TopLevelFolderItemFactory getActiveFactory(String activeFactory) {
104        return factories.get(activeFactory);
105    }
106}