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