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.Collection;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.runtime.model.ContributionFragmentRegistry;
027import org.nuxeo.template.api.descriptor.TemplateProcessorDescriptor;
028
029public class TemplateProcessorRegistry extends ContributionFragmentRegistry<TemplateProcessorDescriptor> {
030
031    protected Map<String, TemplateProcessorDescriptor> processors = new HashMap<String, TemplateProcessorDescriptor>();
032
033    @Override
034    public TemplateProcessorDescriptor clone(TemplateProcessorDescriptor tpd) {
035        return tpd.clone();
036    }
037
038    public TemplateProcessorDescriptor getProcessorByName(String name) {
039        return processors.get(name);
040    }
041
042    public Collection<TemplateProcessorDescriptor> getRegistredProcessors() {
043        return processors.values();
044    }
045
046    @Override
047    public void contributionRemoved(String id, TemplateProcessorDescriptor tpd) {
048        processors.remove(id);
049    }
050
051    @Override
052    public void contributionUpdated(String id, TemplateProcessorDescriptor tpd, TemplateProcessorDescriptor newTpd) {
053        if (tpd == null || !tpd.isEnabled()) {
054            processors.remove(id);
055        } else {
056            if (tpd.init()) {
057                processors.put(id, tpd);
058            } else {
059                throw new NuxeoException("Unable to register processor");
060            }
061        }
062    }
063
064    @Override
065    public String getContributionId(TemplateProcessorDescriptor tpd) {
066        return tpd.getName();
067    }
068
069    @Override
070    public void merge(TemplateProcessorDescriptor srcTpd, TemplateProcessorDescriptor descTpd) {
071        descTpd.merge(srcTpd);
072    }
073
074}