001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.repository.util;
016    
017    import com.liferay.portal.kernel.repository.BaseRepository;
018    import com.liferay.portal.kernel.repository.RepositoryException;
019    
020    import java.util.Set;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    /**
024     * @author Adolfo P??rez
025     * @author Mika Koivisto
026     */
027    public class ExternalRepositoryFactoryUtil {
028    
029            /**
030             * @deprecated As of 7.0.0 replaced by {@link
031             *             com.liferay.portal.repository.registry.RepositoryDefinitionCatalogUtil#getExternalRepositoryClassNames(
032             *             )}
033             */
034            @Deprecated
035            public static String[] getExternalRepositoryClassNames() {
036                    Set<String> classNames = externalRepositoryFactories.keySet();
037    
038                    return classNames.toArray(new String[classNames.size()]);
039            }
040    
041            public static BaseRepository getInstance(String className)
042                    throws Exception {
043    
044                    ExternalRepositoryFactory externalRepositoryFactory =
045                            externalRepositoryFactories.get(className);
046    
047                    BaseRepository baseRepository = null;
048    
049                    if (externalRepositoryFactory != null) {
050                            baseRepository = externalRepositoryFactory.getInstance();
051                    }
052    
053                    if (baseRepository != null) {
054                            return baseRepository;
055                    }
056    
057                    throw new RepositoryException(
058                            "Repository with class name " + className + " is unavailable");
059            }
060    
061            public static void registerExternalRepositoryFactory(
062                    String className, ExternalRepositoryFactory externalRepositoryFactory) {
063    
064                    externalRepositoryFactories.put(className, externalRepositoryFactory);
065            }
066    
067            public static void unregisterExternalRepositoryFactory(String className) {
068                    externalRepositoryFactories.remove(className);
069            }
070    
071            private static final ConcurrentHashMap<String, ExternalRepositoryFactory>
072                    externalRepositoryFactories = new ConcurrentHashMap<>();
073    
074    }