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