001/*
002 * (C) Copyright 2010-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 *     Olivier Grisel
018 */
019package org.nuxeo.ecm.platform.suggestbox.service.registries;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.platform.suggestbox.service.ComponentInitializationException;
026import org.nuxeo.ecm.platform.suggestbox.service.descriptors.SuggesterDescriptor;
027import org.nuxeo.runtime.model.ContributionFragmentRegistry;
028
029public class SuggesterRegistry extends ContributionFragmentRegistry<SuggesterDescriptor> {
030
031    protected final Map<String, SuggesterDescriptor> suggesterDescriptors = new HashMap<String, SuggesterDescriptor>();
032
033    public SuggesterDescriptor getSuggesterDescriptor(String name) {
034        return suggesterDescriptors.get(name);
035    }
036
037    @Override
038    public void contributionRemoved(String id, SuggesterDescriptor descriptor) {
039        suggesterDescriptors.remove(id);
040    }
041
042    @Override
043    public String getContributionId(SuggesterDescriptor descriptor) {
044        return descriptor.getName();
045    }
046
047    @Override
048    public void contributionUpdated(String id, SuggesterDescriptor contrib, SuggesterDescriptor newOrigContrib) {
049        suggesterDescriptors.put(id, contrib);
050    }
051
052    @Override
053    public SuggesterDescriptor clone(SuggesterDescriptor suggester) {
054        try {
055            return (SuggesterDescriptor) suggester.clone();
056        } catch (CloneNotSupportedException e) {
057            // this should never occur since clone implements Cloneable
058            throw new RuntimeException(e);
059        }
060    }
061
062    @Override
063    public void merge(SuggesterDescriptor src, SuggesterDescriptor dst) {
064        try {
065            dst.mergeFrom(src);
066        } catch (ComponentInitializationException e) {
067            throw new NuxeoException(e);
068        }
069    }
070
071}