001    /**
002     * Copyright (c) 2000-2013 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    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    }