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.util;
016    
017    import com.liferay.portal.kernel.repository.BaseRepository;
018    import com.liferay.portal.kernel.repository.proxy.BaseRepositoryProxyBean;
019    import com.liferay.portal.kernel.util.AggregateClassLoader;
020    import com.liferay.portal.kernel.util.ClassLoaderUtil;
021    import com.liferay.portal.kernel.util.InstanceFactory;
022    import com.liferay.portal.kernel.util.ProxyFactory;
023    
024    /**
025     * @author Adolfo P??rez
026     * @author Mika Koivisto
027     */
028    public class ExternalRepositoryFactoryImpl
029            implements ExternalRepositoryFactory {
030    
031            public ExternalRepositoryFactoryImpl(String className) {
032                    _className = className;
033            }
034    
035            public ExternalRepositoryFactoryImpl(
036                    String className, ClassLoader classLoader) {
037    
038                    _className = className;
039    
040                    AggregateClassLoader aggregateClassLoader = new AggregateClassLoader(
041                            ClassLoaderUtil.getPortalClassLoader());
042    
043                    aggregateClassLoader.addClassLoader(classLoader);
044    
045                    _classLoader = aggregateClassLoader;
046            }
047    
048            @Override
049            public BaseRepository getInstance() throws Exception {
050                    if (_classLoader == null) {
051                            return (BaseRepository)InstanceFactory.newInstance(_className);
052                    }
053    
054                    BaseRepository baseRepository =
055                            (BaseRepository)ProxyFactory.newInstance(
056                                    _classLoader, BaseRepository.class, _className);
057    
058                    return new BaseRepositoryProxyBean(baseRepository, _classLoader);
059            }
060    
061            private ClassLoader _classLoader;
062            private final String _className;
063    
064    }