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