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.kernel.messaging;
016    
017    import com.liferay.registry.Registry;
018    import com.liferay.registry.RegistryUtil;
019    import com.liferay.registry.ServiceTracker;
020    
021    import java.util.Collection;
022    
023    /**
024     * @author Michael C. Han
025     */
026    public class DestinationFactoryUtil {
027    
028            public static Destination createDestination(
029                    DestinationConfiguration destinationConfiguration) {
030    
031                    return _instance.getDestinationFactory().createDestination(
032                            destinationConfiguration);
033            }
034    
035            public static Collection<String> getDestinationTypes() {
036                    return _instance.getDestinationFactory().getDestinationTypes();
037            }
038    
039            protected DestinationFactory getDestinationFactory() {
040                    try {
041                            while (_serviceTracker.getService() == null) {
042                                    Thread.sleep(500);
043                            }
044    
045                            return _serviceTracker.getService();
046                    }
047                    catch (InterruptedException e) {
048                            throw new IllegalStateException(
049                                    "Unable to obtain reference for destination factory", e);
050                    }
051            }
052    
053            private DestinationFactoryUtil() {
054                    Registry registry = RegistryUtil.getRegistry();
055    
056                    _serviceTracker = registry.trackServices(DestinationFactory.class);
057    
058                    _serviceTracker.open();
059            }
060    
061            private static final DestinationFactoryUtil _instance =
062                    new DestinationFactoryUtil();
063    
064            private final ServiceTracker<DestinationFactory, DestinationFactory>
065                    _serviceTracker;
066    
067    }