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