1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.scheduler.config;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.messaging.MessageBus;
20  import com.liferay.portal.kernel.scheduler.SchedulerEngine;
21  import com.liferay.portal.kernel.scheduler.SchedulerEntry;
22  
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * <a href="AbstractSchedulingConfigurator.java.html"><b><i>View Source</i></b>
29   * </a>
30   *
31   * @author Shuyang Zhou
32   */
33  public abstract class AbstractSchedulingConfigurator
34      implements SchedulingConfigurator {
35  
36      public void destroy() {
37          for (Map.Entry<String, List<SchedulerEntry>> schedulerEntries :
38                  _schedulerEntries.entrySet()) {
39  
40              for (SchedulerEntry schedulerEntry : schedulerEntries.getValue()) {
41                  try {
42                      destroySchedulerEntry(schedulerEntry);
43                  }
44                  catch (Exception e) {
45                      _log.error("Unable to unschedule " + schedulerEntry, e);
46                  }
47              }
48          }
49  
50          _schedulerEntries.clear();
51      }
52  
53      public void init() {
54          ClassLoader contextClassLoader =
55              Thread.currentThread().getContextClassLoader();
56  
57          try {
58              ClassLoader operatingClassLoader = getOperatingClassloader();
59  
60              Thread.currentThread().setContextClassLoader(operatingClassLoader);
61  
62              for (Map.Entry<String, List<SchedulerEntry>> schedulerEntries :
63                      _schedulerEntries.entrySet()) {
64  
65                  String destinationName = schedulerEntries.getKey();
66  
67                  for (SchedulerEntry schedulerEntry :
68                          schedulerEntries.getValue()) {
69  
70                      try {
71                          initSchedulerEntry(destinationName, schedulerEntry);
72                      }
73                      catch (Exception e) {
74                          _log.error("Unable to schedule " + schedulerEntry, e);
75                      }
76                  }
77              }
78          }
79          finally {
80              Thread.currentThread().setContextClassLoader(contextClassLoader);
81          }
82      }
83  
84      public void setMessageBus(MessageBus messageBus) {
85          _messageBus = messageBus;
86      }
87  
88      public void setSchedulerEngine(SchedulerEngine schedulerEngine) {
89          _schedulerEngine = schedulerEngine;
90      }
91  
92      public void setSchedulerEntries(
93          Map<String, List<SchedulerEntry>> schedulerEntries) {
94  
95          _schedulerEntries = schedulerEntries;
96      }
97  
98      protected abstract ClassLoader getOperatingClassloader();
99  
100     protected void destroySchedulerEntry(SchedulerEntry schedulerEntry)
101         throws Exception {
102 
103         _schedulerEngine.unschedule(schedulerEntry.getTrigger());
104     }
105 
106     protected void initSchedulerEntry(
107             String destinationName, SchedulerEntry schedulerEntry)
108         throws Exception {
109 
110         _messageBus.registerMessageListener(
111             destinationName, schedulerEntry.getEventListener());
112 
113         _schedulerEngine.schedule(
114             schedulerEntry.getTrigger(), schedulerEntry.getDescription(),
115             destinationName, null);
116     }
117 
118     private static Log _log = LogFactoryUtil.getLog(
119         AbstractSchedulingConfigurator.class);
120 
121     private MessageBus _messageBus;
122     private SchedulerEngine _schedulerEngine;
123     private Map<String, List<SchedulerEntry>> _schedulerEntries =
124         new HashMap<String, List<SchedulerEntry>>();
125 
126 }