001    /**
002     * Copyright (c) 2000-2012 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.MessageBus;
020    import com.liferay.portal.kernel.scheduler.SchedulerEngine;
021    import com.liferay.portal.kernel.scheduler.SchedulerEntry;
022    import com.liferay.portal.kernel.scheduler.Trigger;
023    
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public abstract class AbstractSchedulingConfigurator
032            implements SchedulingConfigurator {
033    
034            public void destroy() {
035                    for (Map.Entry<String, List<SchedulerEntry>> schedulerEntries :
036                                    _schedulerEntries.entrySet()) {
037    
038                            for (SchedulerEntry schedulerEntry : schedulerEntries.getValue()) {
039                                    try {
040                                            destroySchedulerEntry(schedulerEntry);
041                                    }
042                                    catch (Exception e) {
043                                            _log.error("Unable to unschedule " + schedulerEntry, e);
044                                    }
045                            }
046                    }
047    
048                    _schedulerEntries.clear();
049            }
050    
051            public void init() {
052                    Thread currentThread = Thread.currentThread();
053    
054                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
055    
056                    try {
057                            ClassLoader operatingClassLoader = getOperatingClassloader();
058    
059                            currentThread.setContextClassLoader(operatingClassLoader);
060    
061                            for (Map.Entry<String, List<SchedulerEntry>> schedulerEntries :
062                                            _schedulerEntries.entrySet()) {
063    
064                                    String destinationName = schedulerEntries.getKey();
065    
066                                    for (SchedulerEntry schedulerEntry :
067                                                    schedulerEntries.getValue()) {
068    
069                                            try {
070                                                    initSchedulerEntry(destinationName, schedulerEntry);
071                                            }
072                                            catch (Exception e) {
073                                                    _log.error("Unable to schedule " + schedulerEntry, e);
074                                            }
075                                    }
076                            }
077                    }
078                    finally {
079                            currentThread.setContextClassLoader(contextClassLoader);
080                    }
081            }
082    
083            public void setMessageBus(MessageBus messageBus) {
084                    _messageBus = messageBus;
085            }
086    
087            public void setSchedulerEngine(SchedulerEngine schedulerEngine) {
088                    _schedulerEngine = schedulerEngine;
089            }
090    
091            public void setSchedulerEntries(
092                    Map<String, List<SchedulerEntry>> schedulerEntries) {
093    
094                    _schedulerEntries = schedulerEntries;
095            }
096    
097            protected void destroySchedulerEntry(SchedulerEntry schedulerEntry)
098                    throws Exception {
099    
100                    Trigger trigger = schedulerEntry.getTrigger();
101    
102                    _schedulerEngine.unschedule(
103                            trigger.getJobName(), trigger.getGroupName());
104            }
105    
106            protected abstract ClassLoader getOperatingClassloader();
107    
108            protected void initSchedulerEntry(
109                            String destinationName, SchedulerEntry schedulerEntry)
110                    throws Exception {
111    
112                    _messageBus.registerMessageListener(
113                            destinationName, schedulerEntry.getEventListener());
114    
115                    _schedulerEngine.schedule(
116                            schedulerEntry.getTrigger(), schedulerEntry.getDescription(),
117                            destinationName, null);
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(
121                    AbstractSchedulingConfigurator.class);
122    
123            private MessageBus _messageBus;
124            private SchedulerEngine _schedulerEngine;
125            private Map<String, List<SchedulerEntry>> _schedulerEntries =
126                    new HashMap<String, List<SchedulerEntry>>();
127    
128    }