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.messaging;
16  
17  import com.liferay.portal.kernel.messaging.Message;
18  import com.liferay.portal.kernel.messaging.MessageBusUtil;
19  import com.liferay.portal.kernel.messaging.MessageListener;
20  import com.liferay.portal.kernel.scheduler.SchedulerEngine;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  
24  /**
25   * <a href="SchedulerEventMessageListenerWrapper.java.html"><b><i>View Source
26   * </i></b></a>
27   *
28   * @author Shuyang Zhou
29   */
30  public class SchedulerEventMessageListenerWrapper implements MessageListener {
31  
32      public SchedulerEventMessageListenerWrapper(
33          MessageListener messageListener, String className) {
34  
35          _messageListener = messageListener;
36  
37          String jobName = className;
38  
39          if (className.length() > SchedulerEngine.JOB_NAME_MAX_LENGTH) {
40              jobName = className.substring(
41                  0, SchedulerEngine.JOB_NAME_MAX_LENGTH);
42          }
43  
44          String groupName = className;
45  
46          if (className.length() > SchedulerEngine.GROUP_NAME_MAX_LENGTH) {
47              groupName = className.substring(
48                  0, SchedulerEngine.GROUP_NAME_MAX_LENGTH);
49          }
50  
51          _key = jobName.concat(StringPool.COLON).concat(groupName);
52      }
53  
54      public void receive(Message message) {
55          String receiverKey = GetterUtil.getString(
56              message.getString(SchedulerEngine.RECEIVER_KEY));
57  
58          if (receiverKey.equals(_key)) {
59              try{
60                  _messageListener.receive(message);
61              }
62              finally {
63                  if (message.getBoolean(SchedulerEngine.DISABLE)) {
64                      String destinationName = message.getDestinationName();
65  
66                      MessageBusUtil.unregisterMessageListener(
67                          destinationName, this);
68                  }
69              }
70          }
71      }
72  
73      private String _key;
74      private MessageListener _messageListener;
75  
76  }