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;
016    
017    import com.liferay.portal.kernel.bean.BeanReference;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.LocalRepository;
020    import com.liferay.portal.kernel.repository.Repository;
021    import com.liferay.portal.kernel.repository.RepositoryFactory;
022    import com.liferay.portal.kernel.repository.UndeployedExternalRepositoryException;
023    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
024    import com.liferay.portal.repository.registry.RepositoryClassDefinition;
025    import com.liferay.portal.repository.registry.RepositoryClassDefinitionCatalog;
026    import com.liferay.portal.service.RepositoryLocalService;
027    
028    /**
029     * @author Adolfo P??rez
030     */
031    public class RepositoryFactoryImpl implements RepositoryFactory {
032    
033            @Override
034            public LocalRepository createLocalRepository(long repositoryId)
035                    throws PortalException {
036    
037                    String className = getRepositoryClassName(repositoryId);
038    
039                    RepositoryFactory repositoryFactory = getRepositoryFactory(className);
040    
041                    return repositoryFactory.createLocalRepository(repositoryId);
042            }
043    
044            @Override
045            public Repository createRepository(long repositoryId)
046                    throws PortalException {
047    
048                    String className = getRepositoryClassName(repositoryId);
049    
050                    RepositoryFactory repositoryFactory = getRepositoryFactory(className);
051    
052                    return repositoryFactory.createRepository(repositoryId);
053            }
054    
055            protected String getRepositoryClassName(long repositoryId) {
056                    com.liferay.portal.model.Repository repository =
057                            _repositoryLocalService.fetchRepository(repositoryId);
058    
059                    if (repository != null) {
060                            return repository.getClassName();
061                    }
062    
063                    return LiferayRepository.class.getName();
064            }
065    
066            protected RepositoryFactory getRepositoryFactory(String className) {
067                    RepositoryClassDefinition repositoryDefinition =
068                            _repositoryClassDefinitionCatalog.getRepositoryClassDefinition(
069                                    className);
070    
071                    if (repositoryDefinition == null) {
072                            throw new UndeployedExternalRepositoryException(className);
073                    }
074    
075                    return repositoryDefinition;
076            }
077    
078            @BeanReference(type = RepositoryClassDefinitionCatalog.class)
079            private RepositoryClassDefinitionCatalog _repositoryClassDefinitionCatalog;
080    
081            @BeanReference(type = RepositoryLocalService.class)
082            private RepositoryLocalService _repositoryLocalService;
083    
084    }