001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.service;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.runtime.model.ContributionFragmentRegistry;
025import org.nuxeo.template.api.descriptor.ContextExtensionFactoryDescriptor;
026
027public class ContextFactoryRegistry extends ContributionFragmentRegistry<ContextExtensionFactoryDescriptor> {
028
029    protected Map<String, ContextExtensionFactoryDescriptor> factories = new HashMap<String, ContextExtensionFactoryDescriptor>();
030
031    @Override
032    public ContextExtensionFactoryDescriptor clone(ContextExtensionFactoryDescriptor desc) {
033        return desc.clone();
034    }
035
036    @Override
037    public void contributionRemoved(String id, ContextExtensionFactoryDescriptor desc) {
038        factories.remove(id);
039    }
040
041    @Override
042    public void contributionUpdated(String id, ContextExtensionFactoryDescriptor mergedDesc,
043            ContextExtensionFactoryDescriptor orgDesc) {
044
045        if (mergedDesc == null || !mergedDesc.isEnabled()) {
046            factories.remove(id);
047        } else {
048            factories.put(id, mergedDesc);
049        }
050    }
051
052    @Override
053    public String getContributionId(ContextExtensionFactoryDescriptor desc) {
054        return desc.getName();
055    }
056
057    @Override
058    public void merge(ContextExtensionFactoryDescriptor src, ContextExtensionFactoryDescriptor dest) {
059        dest.merge(src);
060    }
061
062    public Map<String, ContextExtensionFactoryDescriptor> getExtensionFactories() {
063        return factories;
064    }
065}