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.sender;
016    
017    import com.liferay.registry.Registry;
018    import com.liferay.registry.RegistryUtil;
019    import com.liferay.registry.ServiceTracker;
020    
021    /**
022     * @author Michael C. Han
023     */
024    public class SingleDestinationMessageSenderFactoryUtil {
025    
026            public static SingleDestinationMessageSender
027                    createSingleDestinationMessageSender(String destinationName) {
028    
029                    return _instance.getSingleDestinationMessageSenderFactory().
030                            createSingleDestinationMessageSender(destinationName);
031            }
032    
033            public static SingleDestinationSynchronousMessageSender
034                    createSingleDestinationSynchronousMessageSender(
035                            String destinationName, SynchronousMessageSender.Mode mode) {
036    
037                    return _instance.getSingleDestinationMessageSenderFactory().
038                            createSingleDestinationSynchronousMessageSender(
039                                    destinationName, mode);
040            }
041    
042            public static int getModeCount() {
043                    return _instance.getSingleDestinationMessageSenderFactory().
044                            getModesCount();
045            }
046    
047            public static SynchronousMessageSender getSynchronousMessageSender(
048                    SynchronousMessageSender.Mode mode) {
049    
050                    return _instance.getSingleDestinationMessageSenderFactory().
051                            getSynchronousMessageSender(mode);
052            }
053    
054            protected SingleDestinationMessageSenderFactory
055                    getSingleDestinationMessageSenderFactory() {
056    
057                    try {
058                            while (_serviceTracker.getService() == null) {
059                                    Thread.sleep(500);
060                            }
061    
062                            return _serviceTracker.getService();
063                    }
064                    catch (InterruptedException e) {
065                            throw new IllegalStateException(
066                                    "Unable to initialize " +
067                                            "SingleDestinationMessageSenderFactoryUtil",
068                                    e);
069                    }
070            }
071    
072            private SingleDestinationMessageSenderFactoryUtil() {
073                    Registry registry = RegistryUtil.getRegistry();
074    
075                    _serviceTracker = registry.trackServices(
076                            SingleDestinationMessageSenderFactory.class);
077    
078                    _serviceTracker.open();
079            }
080    
081            private static final SingleDestinationMessageSenderFactoryUtil _instance =
082                    new SingleDestinationMessageSenderFactoryUtil();
083    
084            private final ServiceTracker
085                    <SingleDestinationMessageSenderFactory,
086                            SingleDestinationMessageSenderFactory> _serviceTracker;
087    
088    }