001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
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 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.quota;
019
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.runtime.model.ContributionFragmentRegistry;
027
028/**
029 * Registry for {@link org.nuxeo.ecm.quota.QuotaStatsUpdater}s, handling merge of registered
030 * {@link org.nuxeo.ecm.quota.QuotaStatsUpdaterDescriptor} elements.
031 *
032 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
033 * @since 5.5
034 */
035public class QuotaStatsUpdaterRegistry extends ContributionFragmentRegistry<QuotaStatsUpdaterDescriptor> {
036
037    protected Map<String, QuotaStatsUpdater> quotaStatsUpdaters = new HashMap<String, QuotaStatsUpdater>();
038
039    public QuotaStatsUpdater getQuotaStatsUpdater(String name) {
040        return quotaStatsUpdaters.get(name);
041    }
042
043    public List<QuotaStatsUpdater> getQuotaStatsUpdaters() {
044        return new ArrayList<QuotaStatsUpdater>(quotaStatsUpdaters.values());
045    }
046
047    @Override
048    public String getContributionId(QuotaStatsUpdaterDescriptor descriptor) {
049        return descriptor.getName();
050    }
051
052    @Override
053    public void contributionUpdated(String id, QuotaStatsUpdaterDescriptor contrib,
054            QuotaStatsUpdaterDescriptor newOrigContrib) {
055        if (contrib.isEnabled()) {
056            try {
057                QuotaStatsUpdater updater = contrib.getQuotaStatsUpdaterClass().newInstance();
058                updater.setName(contrib.getName());
059                updater.setLabel(contrib.getLabel());
060                updater.setDescriptionLabel(contrib.getDescriptionLabel());
061                quotaStatsUpdaters.put(id, updater);
062            } catch (ReflectiveOperationException e) {
063                throw new NuxeoException(e);
064            }
065        } else {
066            quotaStatsUpdaters.remove(id);
067        }
068    }
069
070    @Override
071    public void contributionRemoved(String id, QuotaStatsUpdaterDescriptor descriptor) {
072        quotaStatsUpdaters.remove(id);
073    }
074
075    @Override
076    public QuotaStatsUpdaterDescriptor clone(QuotaStatsUpdaterDescriptor descriptor) {
077        return descriptor.clone();
078    }
079
080    @Override
081    public void merge(QuotaStatsUpdaterDescriptor src, QuotaStatsUpdaterDescriptor dst) {
082        dst.setQuotaStatsUpdaterClass(src.getQuotaStatsUpdaterClass());
083        dst.setEnabled(src.isEnabled());
084        dst.setLabel(src.getLabel());
085        dst.setDescriptionLabel(src.getDescriptionLabel());
086    }
087
088}