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