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.kernel.util.PortalClassLoaderUtil;
020    import com.liferay.portal.util.PropsValues;
021    
022    import java.util.Set;
023    import java.util.concurrent.ConcurrentHashMap;
024    
025    /**
026     * @author Mika Koivisto
027     */
028    public class RepositoryFactoryUtil {
029    
030            public static BaseRepository getInstance(String className)
031                    throws Exception {
032    
033                    RepositoryFactory repositoryFactory = _repositoryFactories.get(
034                            className);
035    
036                    BaseRepository baseRepository = null;
037    
038                    if (repositoryFactory != null) {
039                            baseRepository = repositoryFactory.getInstance();
040                    }
041    
042                    if (baseRepository != null) {
043                            return baseRepository;
044                    }
045    
046                    throw new RepositoryException(
047                            "Repository with class name " + className + " is unavailable");
048            }
049    
050            public static String[] getRepositoryClassNames() {
051                    Set<String> classNames = _repositoryFactories.keySet();
052    
053                    return classNames.toArray(new String[classNames.size()]);
054            }
055    
056            public static void registerRepositoryFactory(
057                    String className, RepositoryFactory repositoryFactory) {
058    
059                    _repositoryFactories.put(className, repositoryFactory);
060            }
061    
062            public static void unregisterRepositoryFactory(String className) {
063                    _repositoryFactories.remove(className);
064            }
065    
066            private static ConcurrentHashMap<String, RepositoryFactory>
067                    _repositoryFactories =
068                            new ConcurrentHashMap<String, RepositoryFactory>();
069    
070            static {
071                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
072    
073                    for (String className : PropsValues.DL_REPOSITORY_IMPL) {
074                            RepositoryFactory repositoryFactory = new RepositoryFactoryImpl(
075                                    className, classLoader);
076    
077                            _repositoryFactories.put(className, repositoryFactory);
078                    }
079            }
080    
081    }