001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }