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    import java.util.concurrent.ConcurrentMap;
023    
024    /**
025     * @author Adolfo P??rez
026     * @author Mika Koivisto
027     */
028    public class ExternalRepositoryFactoryUtil {
029    
030            /**
031             * @deprecated As of 7.0.0 replaced by {@link
032             *             com.liferay.portal.repository.registry.RepositoryDefinitionCatalogUtil#getExternalRepositoryClassNames(
033             *             )}
034             */
035            @Deprecated
036            public static String[] getExternalRepositoryClassNames() {
037                    Set<String> classNames = _externalRepositoryFactories.keySet();
038    
039                    return classNames.toArray(new String[classNames.size()]);
040            }
041    
042            public static BaseRepository getInstance(String className)
043                    throws Exception {
044    
045                    ExternalRepositoryFactory externalRepositoryFactory =
046                            _externalRepositoryFactories.get(className);
047    
048                    BaseRepository baseRepository = null;
049    
050                    if (externalRepositoryFactory != null) {
051                            baseRepository = externalRepositoryFactory.getInstance();
052                    }
053    
054                    if (baseRepository != null) {
055                            return baseRepository;
056                    }
057    
058                    throw new RepositoryException(
059                            "Repository with class name " + className + " is unavailable");
060            }
061    
062            public static void registerExternalRepositoryFactory(
063                    String className, ExternalRepositoryFactory externalRepositoryFactory) {
064    
065                    _externalRepositoryFactories.put(className, externalRepositoryFactory);
066            }
067    
068            public static void unregisterExternalRepositoryFactory(String className) {
069                    _externalRepositoryFactories.remove(className);
070            }
071    
072            private static final ConcurrentMap<String, ExternalRepositoryFactory>
073                    _externalRepositoryFactories = new ConcurrentHashMap<>();
074    
075    }