001/*
002 * (C) Copyright 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 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.automation.core.events;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.Iterator;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.runtime.model.ContributionFragmentRegistry;
026
027/**
028 * @since 5.6
029 */
030public class EventRegistry extends ContributionFragmentRegistry<EventHandler> {
031
032    protected Map<String, List<EventHandler>> handlers = new HashMap<String, List<EventHandler>>();
033
034    protected volatile Map<String, List<EventHandler>> lookup;
035
036    @Override
037    public String getContributionId(EventHandler contrib) {
038        // useless, never used
039        return contrib.getChainId();
040    }
041
042    @Override
043    public void contributionUpdated(String id, EventHandler handler, EventHandler newOrigContrib) {
044        for (String eventId : handler.getEvents()) {
045            putEventHandler(eventId, handler);
046        }
047        lookup = null;
048    }
049
050    protected void putEventHandler(String eventId, EventHandler handler) {
051        List<EventHandler> handlers = this.handlers.get(eventId);
052        if (handlers == null) {
053            handlers = new ArrayList<EventHandler>();
054        }
055        if (!handlers.contains(handler)) {
056            handlers.add(handler);
057        }
058        this.handlers.put(eventId, handlers);
059    }
060
061    @Override
062    public void contributionRemoved(String id, EventHandler handler) {
063        for (String eventId : handler.getEvents()) {
064            List<EventHandler> handlers = this.handlers.get(eventId);
065            if (handlers != null) {
066                Iterator<EventHandler> it = handlers.iterator();
067                while (it.hasNext()) {
068                    EventHandler h = it.next();
069                    // TODO chainId is not really an unique ID for the event
070                    // handler...
071                    if (h.chainId.equals(handler.chainId)) {
072                        it.remove();
073                        break;
074                    }
075                }
076            }
077        }
078        lookup = null;
079    }
080
081    @Override
082    public boolean isSupportingMerge() {
083        return false;
084    }
085
086    @Override
087    public EventHandler clone(EventHandler orig) {
088        throw new UnsupportedOperationException();
089    }
090
091    @Override
092    public void merge(EventHandler src, EventHandler dst) {
093        throw new UnsupportedOperationException();
094    }
095
096    // API
097
098    public Map<String, List<EventHandler>> lookup() {
099        Map<String, List<EventHandler>> _lookup = lookup;
100        if (_lookup == null) {
101            synchronized (this) {
102                if (lookup == null) {
103                    lookup = new HashMap<String, List<EventHandler>>(handlers);
104                }
105                _lookup = lookup;
106            }
107        }
108        return _lookup;
109    }
110
111}