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