001/*
002 * (C) Copyright 2013 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-2.1.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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.core.exception;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.nuxeo.ecm.automation.ChainException;
023import org.nuxeo.ecm.automation.OperationException;
024import org.nuxeo.runtime.model.ContributionFragmentRegistry;
025
026/**
027 * @since 5.7.3
028 */
029public class ChainExceptionRegistry extends ContributionFragmentRegistry<ChainException> {
030
031    /**
032     * Modifiable automation filter registry. Modifying the registry is using a lock and it's thread safe. Modifications
033     * are removing the cache.
034     */
035    protected final Map<String, ChainException> chainExceptions = new HashMap<>();
036
037    /**
038     * Read only cache for automation filter lookup. Thread safe. Not using synchronization if cache already created.
039     */
040    protected volatile Map<String, ChainException> lookup;
041
042    public synchronized void addContribution(ChainException chainException, boolean replace) throws OperationException {
043        if (!replace && chainExceptions.containsKey(chainException.getId())) {
044            throw new OperationException("An exception chain is already bound to: " + chainException.getId()
045                    + ". Use 'replace=true' to replace an existing exception chain");
046        }
047        super.addContribution(chainException);
048    }
049
050    @Override
051    public boolean isSupportingMerge() {
052        return false;
053    }
054
055    @Override
056    public String getContributionId(ChainException contrib) {
057        return contrib.getId();
058    }
059
060    @Override
061    public void contributionUpdated(String id, ChainException contrib, ChainException newOrigContrib) {
062        chainExceptions.put(id, contrib);
063        lookup = null;
064    }
065
066    @Override
067    public void contributionRemoved(String id, ChainException origContrib) {
068        chainExceptions.remove(id);
069        lookup = null;
070    }
071
072    @Override
073    public ChainException clone(ChainException orig) {
074        throw new UnsupportedOperationException();
075    }
076
077    @Override
078    public void merge(ChainException src, ChainException dst) {
079        throw new UnsupportedOperationException();
080    }
081
082    public Map<String, ChainException> lookup() {
083        Map<String, ChainException> _lookup = lookup;
084        if (_lookup == null) {
085            synchronized (this) {
086                lookup = new HashMap<String, ChainException>(chainExceptions);
087                _lookup = lookup;
088            }
089        }
090        return _lookup;
091    }
092
093    public ChainException getChainException(String onChainId) {
094        for (ChainException chainException : lookup().values()) {
095            if (onChainId.equals(chainException.getOnChainId())) {
096                return chainException;
097            }
098        }
099        return null;
100    }
101}