001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    import com.liferay.portal.util.PropsValues;
020    
021    import java.util.Set;
022    import java.util.concurrent.ConcurrentHashMap;
023    
024    /**
025     * @author Mika Koivisto
026     */
027    public class RepositoryFactoryUtil {
028    
029            public static BaseRepository getInstance(String className)
030                    throws Exception {
031    
032                    RepositoryFactory repositoryFactory = _repositoryFactories.get(
033                            className);
034    
035                    BaseRepository baseRepository = null;
036    
037                    if (repositoryFactory != null) {
038                            baseRepository = repositoryFactory.getInstance();
039                    }
040    
041                    if (baseRepository != null) {
042                            return baseRepository;
043                    }
044    
045                    throw new RepositoryException(
046                            "Repository with class name " + className + " is unavailable");
047            }
048    
049            public static String[] getRepositoryClassNames() {
050                    Set<String> classNames = _repositoryFactories.keySet();
051    
052                    return classNames.toArray(new String[classNames.size()]);
053            }
054    
055            public static void registerRepositoryFactory(
056                    String className, RepositoryFactory repositoryFactory) {
057    
058                    _repositoryFactories.put(className, repositoryFactory);
059            }
060    
061            public static void unregisterRepositoryFactory(String className) {
062                    _repositoryFactories.remove(className);
063            }
064    
065            private static ConcurrentHashMap<String, RepositoryFactory>
066                    _repositoryFactories =
067                            new ConcurrentHashMap<String, RepositoryFactory>();
068    
069            static {
070                    for (String className : PropsValues.DL_REPOSITORY_IMPL) {
071                            RepositoryFactory repositoryFactory = new RepositoryFactoryImpl(
072                                    className);
073    
074                            _repositoryFactories.put(className, repositoryFactory);
075                    }
076            }
077    
078    }