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 *     Florent Guillaume
016 */
017package org.nuxeo.ecm.core.storage.sql.coremodel;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.nuxeo.ecm.core.storage.sql.RepositoryDescriptor;
022import org.nuxeo.runtime.api.Framework;
023import org.nuxeo.runtime.model.ComponentInstance;
024import org.nuxeo.runtime.model.DefaultComponent;
025
026/**
027 * Compatibility component to register old-style VCS repository extension points.
028 *
029 * @since 5.9.3
030 */
031public class SQLRepositoryCompatService extends DefaultComponent {
032
033    private static final Log log = LogFactory.getLog(SQLRepositoryCompatService.class);
034
035    private static final String XP_REPOSITORY = "repository";
036
037    @Override
038    public void registerContribution(Object contrib, String xpoint, ComponentInstance contributor) {
039        if (XP_REPOSITORY.equals(xpoint)) {
040            addContribution((RepositoryDescriptor) contrib);
041        } else {
042            throw new RuntimeException("Unknown extension point: " + xpoint);
043        }
044    }
045
046    @Override
047    public void unregisterContribution(Object contrib, String xpoint, ComponentInstance contributor) {
048        if (XP_REPOSITORY.equals(xpoint)) {
049            removeContribution((RepositoryDescriptor) contrib);
050        } else {
051            throw new RuntimeException("Unknown extension point: " + xpoint);
052        }
053    }
054
055    protected void addContribution(RepositoryDescriptor cdesc) {
056        log.warn("Using old-style extension point" + " org.nuxeo.ecm.core.repository.RepositoryService"
057                + " for repository \"" + cdesc.name
058                + "\", use org.nuxeo.ecm.core.storage.sql.RepositoryService instead");
059        RepositoryDescriptor descriptor = getRepositoryDescriptor(cdesc);
060        SQLRepositoryService sqlRepositoryService = Framework.getLocalService(SQLRepositoryService.class);
061        sqlRepositoryService.addContribution(descriptor);
062    }
063
064    protected void removeContribution(RepositoryDescriptor cdesc) {
065        RepositoryDescriptor descriptor = getRepositoryDescriptor(cdesc);
066        SQLRepositoryService sqlRepositoryService = Framework.getLocalService(SQLRepositoryService.class);
067        sqlRepositoryService.removeContribution(descriptor);
068    }
069
070    protected RepositoryDescriptor getRepositoryDescriptor(RepositoryDescriptor cdesc) {
071        RepositoryDescriptor descriptor = cdesc.repositoryDescriptor;
072        if (descriptor == null) {
073            // old-style extension point with new-style descriptor
074            // without nested repository
075            descriptor = cdesc;
076        } else {
077            descriptor.setRepositoryFactoryClass(cdesc.getRepositoryFactoryClass());
078            if (descriptor.name == null) {
079                descriptor.name = cdesc.name;
080            }
081        }
082        return descriptor;
083    }
084
085}