001    /**
002     * Copyright (c) 2000-2010 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    
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                    ClassLoader contextClassLoader =
052                            Thread.currentThread().getContextClassLoader();
053    
054                    try {
055                            ClassLoader operatingClassLoader = getOperatingClassloader();
056    
057                            Thread.currentThread().setContextClassLoader(operatingClassLoader);
058    
059                            for (Map.Entry<String, List<SchedulerEntry>> schedulerEntries :
060                                            _schedulerEntries.entrySet()) {
061    
062                                    String destinationName = schedulerEntries.getKey();
063    
064                                    for (SchedulerEntry schedulerEntry :
065                                                    schedulerEntries.getValue()) {
066    
067                                            try {
068                                                    initSchedulerEntry(destinationName, schedulerEntry);
069                                            }
070                                            catch (Exception e) {
071                                                    _log.error("Unable to schedule " + schedulerEntry, e);
072                                            }
073                                    }
074                            }
075                    }
076                    finally {
077                            Thread.currentThread().setContextClassLoader(contextClassLoader);
078                    }
079            }
080    
081            public void setMessageBus(MessageBus messageBus) {
082                    _messageBus = messageBus;
083            }
084    
085            public void setSchedulerEngine(SchedulerEngine schedulerEngine) {
086                    _schedulerEngine = schedulerEngine;
087            }
088    
089            public void setSchedulerEntries(
090                    Map<String, List<SchedulerEntry>> schedulerEntries) {
091    
092                    _schedulerEntries = schedulerEntries;
093            }
094    
095            protected abstract ClassLoader getOperatingClassloader();
096    
097            protected void destroySchedulerEntry(SchedulerEntry schedulerEntry)
098                    throws Exception {
099    
100                    _schedulerEngine.unschedule(schedulerEntry.getTrigger());
101            }
102    
103            protected void initSchedulerEntry(
104                            String destinationName, SchedulerEntry schedulerEntry)
105                    throws Exception {
106    
107                    _messageBus.registerMessageListener(
108                            destinationName, schedulerEntry.getEventListener());
109    
110                    _schedulerEngine.schedule(
111                            schedulerEntry.getTrigger(), schedulerEntry.getDescription(),
112                            destinationName, null);
113            }
114    
115            private static Log _log = LogFactoryUtil.getLog(
116                    AbstractSchedulingConfigurator.class);
117    
118            private MessageBus _messageBus;
119            private SchedulerEngine _schedulerEngine;
120            private Map<String, List<SchedulerEntry>> _schedulerEntries =
121                    new HashMap<String, List<SchedulerEntry>>();
122    
123    }