001/*
002 * (C) Copyright 2010-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 */
015
016package org.nuxeo.ecm.platform.ec.notification.service;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.platform.ec.notification.NotificationListenerVeto;
025import org.nuxeo.runtime.model.ContributionFragmentRegistry;
026
027/**
028 * Registry for {@link org.nuxeo.ecm.platform.ec.notification.service.NotificationListenerVetoDescriptor} elements
029 *
030 * @author <a href="mailto:tm@nuxeo.com">Thierry Martins</a>
031 * @since 5.6
032 */
033public class NotificationListenerVetoRegistry extends ContributionFragmentRegistry<NotificationListenerVetoDescriptor> {
034
035    private static final Log log = LogFactory.getLog(NotificationListenerVetoRegistry.class);
036
037    private Map<String, NotificationListenerVeto> vetos;
038
039    public NotificationListenerVetoRegistry() {
040        super();
041        vetos = new HashMap<String, NotificationListenerVeto>();
042    }
043
044    @Override
045    public NotificationListenerVetoDescriptor clone(NotificationListenerVetoDescriptor descriptor) {
046        throw new UnsupportedOperationException();
047    }
048
049    @Override
050    public void contributionRemoved(String id, NotificationListenerVetoDescriptor contrib) {
051        vetos.remove(id);
052    }
053
054    @Override
055    public void contributionUpdated(String id, NotificationListenerVetoDescriptor contrib,
056            NotificationListenerVetoDescriptor newOrigContrib) {
057        if (contrib.isRemove()) {
058            contributionRemoved(id, contrib);
059        } else {
060            try {
061                vetos.put(id, contrib.getNotificationVeto().newInstance());
062            } catch (ReflectiveOperationException e) {
063                log.error(e);
064            }
065        }
066    }
067
068    @Override
069    public String getContributionId(NotificationListenerVetoDescriptor contrib) {
070        return contrib.getName();
071    }
072
073    @Override
074    public void merge(NotificationListenerVetoDescriptor arg0, NotificationListenerVetoDescriptor arg1) {
075        throw new UnsupportedOperationException();
076    }
077
078    public NotificationListenerVeto getVeto(String id) {
079        return vetos.get(id);
080    }
081
082    @Override
083    public boolean isSupportingMerge() {
084        return false;
085    }
086
087    public Collection<NotificationListenerVeto> getVetos() {
088        return vetos.values();
089    }
090
091    public void clear() {
092        vetos.clear();
093        contribs.clear();
094    }
095
096}