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