001    /**
002     * Copyright (c) 2000-2011 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.kernel.adaptor;
016    
017    import com.liferay.portal.kernel.util.ServiceLoader;
018    
019    import java.util.List;
020    
021    /**
022     * @author Raymond Augé
023     */
024    public class AdaptorUtil {
025    
026            public static Adaptor getAdaptor() throws AdaptorException {
027                    if (_adaptor != null) {
028                            return _adaptor;
029                    }
030    
031                    try {
032                            List<AdaptorFactory> services = ServiceLoader.load(
033                                    AdaptorFactory.class);
034    
035                            for (AdaptorFactory adaptorFactory : services) {
036                                    _adaptor = adaptorFactory.newAdaptor();
037                            }
038                    }
039                    catch (Exception e) {
040                            throw new AdaptorException(e);
041                    }
042    
043                    return _adaptor;
044            }
045    
046            public static void init(Object applicationContext) throws AdaptorException {
047                    if (hasAdaptor()) {
048                            Adaptor adaptor = getAdaptor();
049    
050                            adaptor.init(applicationContext);
051                    }
052            }
053    
054            public static void start() throws AdaptorException {
055                    if (hasAdaptor()) {
056                            Adaptor adaptor = getAdaptor();
057    
058                            adaptor.start();
059                    }
060            }
061    
062            public static void stop() throws AdaptorException {
063                    if (hasAdaptor()) {
064                            Adaptor adaptor = getAdaptor();
065    
066                            adaptor.stop();
067                    }
068            }
069    
070            public void setAdaptor(Adaptor adaptor) {
071                    _adaptor = adaptor;
072            }
073    
074            protected static boolean hasAdaptor() throws AdaptorException {
075                    Adaptor adaptor = getAdaptor();
076    
077                    if (adaptor != null) {
078                            return true;
079                    }
080    
081                    return false;
082            }
083    
084            private static Adaptor _adaptor;
085    
086    }