001/*
002 * (C) Copyright 2014 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 *
018 */
019package org.nuxeo.ecm.core.opencmis.bindings;
020
021import java.util.Map;
022
023import org.nuxeo.runtime.model.ComponentContext;
024import org.nuxeo.runtime.model.ComponentInstance;
025import org.nuxeo.runtime.model.DefaultComponent;
026import org.nuxeo.runtime.model.SimpleContributionRegistry;
027
028/**
029 * Service holding the definition
030 */
031public class NuxeoCmisServiceFactoryManager extends DefaultComponent {
032
033    private static final String XP_FACTORY = "factory";
034
035    protected NuxeoCmisServiceFactoryDescriptorRegistry registry = new NuxeoCmisServiceFactoryDescriptorRegistry();
036
037    protected static class NuxeoCmisServiceFactoryDescriptorRegistry extends
038            SimpleContributionRegistry<NuxeoCmisServiceFactoryDescriptor> {
039
040        @Override
041        public String getContributionId(NuxeoCmisServiceFactoryDescriptor contrib) {
042            return XP_FACTORY;
043        }
044
045        @Override
046        public NuxeoCmisServiceFactoryDescriptor clone(NuxeoCmisServiceFactoryDescriptor orig) {
047            return new NuxeoCmisServiceFactoryDescriptor(orig);
048        }
049
050        @Override
051        public void merge(NuxeoCmisServiceFactoryDescriptor src, NuxeoCmisServiceFactoryDescriptor dst) {
052            dst.merge(src);
053        }
054
055        @Override
056        public boolean isSupportingMerge() {
057            return true;
058        }
059
060        public void clear() {
061            currentContribs.clear();
062        }
063
064        public NuxeoCmisServiceFactoryDescriptor getNuxeoCmisServiceFactoryDescriptor() {
065            return getCurrentContribution(XP_FACTORY);
066        }
067    }
068
069    @Override
070    public void activate(ComponentContext context) {
071        registry.clear();
072    }
073
074    @Override
075    public void deactivate(ComponentContext context) {
076        registry.clear();
077    }
078
079    @Override
080    public void registerContribution(Object contrib, String xpoint, ComponentInstance contributor) {
081        if (XP_FACTORY.equals(xpoint)) {
082            addContribution((NuxeoCmisServiceFactoryDescriptor) contrib);
083        } else {
084            throw new RuntimeException("Unknown extension point: " + xpoint);
085        }
086    }
087
088    @Override
089    public void unregisterContribution(Object contrib, String xpoint, ComponentInstance contributor) {
090        if (XP_FACTORY.equals(xpoint)) {
091            removeContribution((NuxeoCmisServiceFactoryDescriptor) contrib);
092        } else {
093            throw new RuntimeException("Unknown extension point: " + xpoint);
094        }
095    }
096
097    protected void addContribution(NuxeoCmisServiceFactoryDescriptor descriptor) {
098        registry.addContribution(descriptor);
099    }
100
101    protected void removeContribution(NuxeoCmisServiceFactoryDescriptor descriptor) {
102        registry.removeContribution(descriptor);
103    }
104
105    /**
106     * Gets the {@link NuxeoCmisServiceFactory} based on contributed {@link NuxeoCmisServiceFactoryDescriptor}s.
107     */
108    public NuxeoCmisServiceFactory getNuxeoCmisServiceFactory() {
109        NuxeoCmisServiceFactoryDescriptor descriptor = registry.getNuxeoCmisServiceFactoryDescriptor();
110
111        Class<? extends NuxeoCmisServiceFactory> factoryClass = null;
112        Map<String, String> factoryParameters = null;
113        if (descriptor != null) {
114            factoryClass = descriptor.getFactoryClass();
115            factoryParameters = descriptor.factoryParameters;
116        }
117        NuxeoCmisServiceFactory nuxeoCmisServiceFactory;
118        try {
119            nuxeoCmisServiceFactory = factoryClass.newInstance();
120        } catch (InstantiationException | IllegalAccessException e) {
121            throw new RuntimeException("Cannot instantiate nuxeoCmisServiceFactory: " + factoryClass.getName(), e);
122        }
123
124        nuxeoCmisServiceFactory.init(factoryParameters);
125        return nuxeoCmisServiceFactory;
126    }
127
128}