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