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.scheduler.config;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.DestinationNames;
020    import com.liferay.portal.kernel.messaging.MessageListener;
021    import com.liferay.portal.kernel.messaging.proxy.ProxyModeThreadLocal;
022    import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
023    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
024    import com.liferay.portal.kernel.scheduler.SchedulerEntry;
025    import com.liferay.portal.kernel.util.InstanceFactory;
026    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
027    
028    import java.util.Collections;
029    import java.util.HashMap;
030    import java.util.List;
031    import java.util.Map;
032    
033    /**
034     * @author Shuyang Zhou
035     * @author Tina Tian
036     */
037    public class PluginSchedulingConfigurator {
038    
039            public void afterPropertiesSet() {
040                    Thread currentThread = Thread.currentThread();
041    
042                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
043    
044                    boolean forceSync = ProxyModeThreadLocal.isForceSync();
045    
046                    ProxyModeThreadLocal.setForceSync(true);
047    
048                    try {
049                            ClassLoader portalClassLoader =
050                                    PortalClassLoaderUtil.getClassLoader();
051    
052                            currentThread.setContextClassLoader(portalClassLoader);
053    
054                            for (SchedulerEntry schedulerEntry : _schedulerEntries) {
055                                    try {
056                                            MessageListener messageListener =
057                                                    (MessageListener)InstanceFactory.newInstance(
058                                                            PortletClassLoaderUtil.getClassLoader(),
059                                                            schedulerEntry.getEventListenerClass());
060    
061                                            SchedulerEngineHelperUtil.register(
062                                                    messageListener, schedulerEntry,
063                                                    DestinationNames.SCHEDULER_DISPATCH);
064    
065                                            _messageListeners.put(
066                                                    schedulerEntry.getEventListenerClass(),
067                                                    messageListener);
068                                    }
069                                    catch (Exception e) {
070                                            _log.error("Unable to schedule " + schedulerEntry, e);
071                                    }
072                            }
073                    }
074                    finally {
075                            ProxyModeThreadLocal.setForceSync(forceSync);
076    
077                            currentThread.setContextClassLoader(contextClassLoader);
078                    }
079            }
080    
081            public void destroy() {
082                    for (MessageListener messageListener : _messageListeners.values()) {
083                            SchedulerEngineHelperUtil.unregister(messageListener);
084                    }
085    
086                    _messageListeners.clear();
087                    _schedulerEntries.clear();
088            }
089    
090            public void setSchedulerEntries(List<SchedulerEntry> schedulerEntries) {
091                    _schedulerEntries = schedulerEntries;
092            }
093    
094            private static final Log _log = LogFactoryUtil.getLog(
095                    PluginSchedulingConfigurator.class);
096    
097            private final Map<String, MessageListener> _messageListeners =
098                    new HashMap<>();
099            private List<SchedulerEntry> _schedulerEntries = Collections.emptyList();
100    
101    }