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.impl;
020
021import org.nuxeo.ecm.automation.AutomationFilter;
022import org.nuxeo.ecm.automation.OperationException;
023import org.nuxeo.runtime.model.ContributionFragmentRegistry;
024
025import java.util.HashMap;
026import java.util.Map;
027
028/**
029 * @since 5.7.3
030 */
031public class AutomationFilterRegistry extends ContributionFragmentRegistry<AutomationFilter> {
032
033    /**
034     * Modifiable exception chain 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, AutomationFilter> automationFilters = new HashMap<>();
038
039    /**
040     * Read only cache for exception chain lookup. Thread safe. Not using synchronization if cache already created.
041     */
042    protected volatile Map<String, AutomationFilter> lookup;
043
044    public synchronized void addContribution(AutomationFilter automationFilter, boolean replace)
045            throws OperationException {
046        if (!replace && automationFilters.containsKey(automationFilter.getId())) {
047            throw new OperationException("An automation filter is already bound to: " + automationFilter.getId()
048                    + ". Use 'replace=true' to replace an existing automation filter");
049        }
050        super.addContribution(automationFilter);
051    }
052
053    @Override
054    public boolean isSupportingMerge() {
055        return false;
056    }
057
058    @Override
059    public String getContributionId(AutomationFilter contrib) {
060        return contrib.getId();
061    }
062
063    @Override
064    public void contributionUpdated(String id, AutomationFilter contrib, AutomationFilter newOrigContrib) {
065        automationFilters.put(id, contrib);
066        lookup = null;
067    }
068
069    @Override
070    public void contributionRemoved(String id, AutomationFilter origContrib) {
071        automationFilters.remove(id);
072        lookup = null;
073    }
074
075    @Override
076    public AutomationFilter clone(AutomationFilter orig) {
077        throw new UnsupportedOperationException();
078    }
079
080    @Override
081    public void merge(AutomationFilter src, AutomationFilter dst) {
082        throw new UnsupportedOperationException();
083    }
084
085    public Map<String, AutomationFilter> lookup() {
086        Map<String, AutomationFilter> _lookup = lookup;
087        if (_lookup == null) {
088            synchronized (this) {
089                lookup = new HashMap<>(automationFilters);
090                _lookup = lookup;
091            }
092        }
093        return _lookup;
094    }
095
096    public AutomationFilter getAutomationFilter(String id) {
097        return automationFilters.get(id);
098    }
099}