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