001/*
002 * (C) Copyright 2015 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.web.resources.core.service;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.ecm.web.resources.api.ResourceBundle;
027import org.nuxeo.runtime.model.ContributionFragmentRegistry;
028
029/**
030 * Registry for resource elements, handling merge on referenced resources.
031 *
032 * @since 7.3
033 */
034public class ResourceBundleRegistry extends ContributionFragmentRegistry<ResourceBundle> {
035
036    protected Map<String, ResourceBundle> bundles = new HashMap<String, ResourceBundle>();
037
038    @Override
039    public String getContributionId(ResourceBundle contrib) {
040        return contrib.getName();
041    }
042
043    @Override
044    public void contributionUpdated(String id, ResourceBundle contrib, ResourceBundle newOrigContrib) {
045        bundles.put(id, contrib);
046    }
047
048    @Override
049    public synchronized void removeContribution(ResourceBundle contrib) {
050        removeContribution(contrib, true);
051    }
052
053    @Override
054    public void contributionRemoved(String id, ResourceBundle origContrib) {
055        bundles.remove(id);
056    }
057
058    @Override
059    public ResourceBundle clone(ResourceBundle orig) {
060        return orig.clone();
061    }
062
063    @Override
064    public void merge(ResourceBundle src, ResourceBundle dst) {
065        dst.merge(src);
066    }
067
068    // custom API
069
070    public ResourceBundle getResourceBundle(String id) {
071        return bundles.get(id);
072    }
073
074    public List<ResourceBundle> getResourceBundles() {
075        return new ArrayList<ResourceBundle>(bundles.values());
076    }
077
078}