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            @Override
033            public void configure() {
034                    Thread currentThread = Thread.currentThread();
035    
036                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
037    
038                    String servletContextName =
039                            PortletClassLoaderUtil.getServletContextName();
040    
041                    boolean forceSync = ProxyModeThreadLocal.isForceSync();
042    
043                    ProxyModeThreadLocal.setForceSync(true);
044    
045                    try {
046                            ClassLoader portalClassLoader =
047                                    PortalClassLoaderUtil.getClassLoader();
048    
049                            currentThread.setContextClassLoader(portalClassLoader);
050    
051                            for (SchedulerEntry schedulerEntry : schedulerEntries) {
052                                    try {
053                                            SchedulerEngineHelperUtil.schedule(
054                                                    schedulerEntry, storageType, servletContextName,
055                                                    exceptionsMaxSize);
056                                    }
057                                    catch (Exception e) {
058                                            _log.error("Unable to schedule " + schedulerEntry, e);
059                                    }
060                            }
061                    }
062                    finally {
063                            ProxyModeThreadLocal.setForceSync(forceSync);
064    
065                            currentThread.setContextClassLoader(contextClassLoader);
066                    }
067            }
068    
069            public void destroy() {
070                    for (SchedulerEntry schedulerEntry : schedulerEntries) {
071                            try {
072                                    SchedulerEngineHelperUtil.delete(schedulerEntry, storageType);
073                            }
074                            catch (Exception e) {
075                                    _log.error("Unable to unschedule " + schedulerEntry, e);
076                            }
077                    }
078    
079                    schedulerEntries.clear();
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(
083                    PluginSchedulingConfigurator.class);
084    
085    }