1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.scheduler.quartz;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.messaging.MessageBusUtil;
28  import com.liferay.portal.kernel.messaging.MessageListener;
29  import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
30  
31  import java.util.List;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /**
37   * <a href="QuartzMessageListener.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Bruno Farache
40   *
41   */
42  public class QuartzMessageListener implements MessageListener {
43  
44      public void receive(Object message) {
45          throw new UnsupportedOperationException();
46      }
47  
48      public void receive(String message) {
49          try {
50              doReceive(message);
51          }
52          catch (Exception e) {
53              _log.error("Unable to process message " + message, e);
54          }
55      }
56  
57      protected void doReceive(String message) throws Exception {
58          JSONObject jsonObj = JSONFactoryUtil.createJSONObject(message);
59  
60          String responseDestination = jsonObj.getString(
61              "lfrResponseDestination");
62          String responseId = jsonObj.getString("lfrResponseId");
63  
64          jsonObj.remove("lfrResponseDestination");
65          jsonObj.remove("lfrResponseId");
66  
67          SchedulerRequest schedulerRequest =
68              (SchedulerRequest)JSONFactoryUtil.deserialize(jsonObj);
69  
70          String command = schedulerRequest.getCommand();
71  
72          if (command.equals(SchedulerRequest.COMMAND_REGISTER)) {
73              QuartzSchedulerEngineUtil.schedule(
74                  schedulerRequest.getGroupName(), schedulerRequest.getCronText(),
75                  schedulerRequest.getStartDate(), schedulerRequest.getEndDate(),
76                  schedulerRequest.getDescription(),
77                  schedulerRequest.getDestination(),
78                  schedulerRequest.getMessageBody());
79          }
80          else if (command.equals(SchedulerRequest.COMMAND_RETRIEVE)) {
81              doCommandRetrieve(
82                  responseDestination, responseId, schedulerRequest);
83          }
84          else if (command.equals(SchedulerRequest.COMMAND_SHUTDOWN)) {
85              QuartzSchedulerEngineUtil.shutdown();
86          }
87          else if (command.equals(SchedulerRequest.COMMAND_UNREGISTER)) {
88              QuartzSchedulerEngineUtil.unschedule(
89                  schedulerRequest.getJobName(), schedulerRequest.getGroupName());
90          }
91      }
92  
93      protected void doCommandRetrieve(
94              String responseDestination, String responseId,
95              SchedulerRequest schedulerRequest)
96          throws Exception {
97  
98          List<SchedulerRequest> schedulerRequests =
99              QuartzSchedulerEngineUtil.getScheduledJobs(
100                 schedulerRequest.getGroupName());
101 
102         JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
103 
104         jsonObj.put("lfrResponseId", responseId);
105         jsonObj.put(
106             "schedulerRequests", JSONFactoryUtil.serialize(schedulerRequests));
107 
108         MessageBusUtil.sendMessage(responseDestination, jsonObj.toString());
109     }
110 
111     private static Log _log = LogFactory.getLog(QuartzMessageListener.class);
112 
113 }