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