001    /**
002     * Copyright (c) 2000-2013 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.proxy.ProxyModeThreadLocal;
020    import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
021    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
022    import com.liferay.portal.kernel.scheduler.SchedulerEntry;
023    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
024    
025    /**
026     * @author Shuyang Zhou
027     * @author Tina Tian
028     */
029    public class PluginSchedulingConfigurator
030            extends AbstractSchedulingConfigurator {
031    
032            public void configure() {
033                    Thread currentThread = Thread.currentThread();
034    
035                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
036    
037                    String servletContextName =
038                            PortletClassLoaderUtil.getServletContextName();
039    
040                    boolean forceSync = ProxyModeThreadLocal.isForceSync();
041    
042                    ProxyModeThreadLocal.setForceSync(true);
043    
044                    try {
045                            ClassLoader portalClassLoader =
046                                    PortalClassLoaderUtil.getClassLoader();
047    
048                            currentThread.setContextClassLoader(portalClassLoader);
049    
050                            for (SchedulerEntry schedulerEntry : schedulerEntries) {
051                                    try {
052                                            SchedulerEngineHelperUtil.schedule(
053                                                    schedulerEntry, storageType, servletContextName,
054                                                    exceptionsMaxSize);
055                                    }
056                                    catch (Exception e) {
057                                            _log.error("Unable to schedule " + schedulerEntry, e);
058                                    }
059                            }
060                    }
061                    finally {
062                            ProxyModeThreadLocal.setForceSync(forceSync);
063    
064                            currentThread.setContextClassLoader(contextClassLoader);
065                    }
066            }
067    
068            public void destroy() {
069                    for (SchedulerEntry schedulerEntry : schedulerEntries) {
070                            try {
071                                    SchedulerEngineHelperUtil.delete(schedulerEntry, storageType);
072                            }
073                            catch (Exception e) {
074                                    _log.error("Unable to unschedule " + schedulerEntry, e);
075                            }
076                    }
077    
078                    schedulerEntries.clear();
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(
082                    PluginSchedulingConfigurator.class);
083    
084    }