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) {
34  
35          _messageListener = messageListener;
36  
37          String className = messageListener.getClass().getName();
38  
39          String jobName = className;
40  
41          if (className.length() > SchedulerEngine.JOB_NAME_MAX_LENGTH) {
42              jobName = className.substring(
43                  0, SchedulerEngine.JOB_NAME_MAX_LENGTH);
44          }
45  
46          String groupName = className;
47  
48          if (className.length() > SchedulerEngine.GROUP_NAME_MAX_LENGTH) {
49              groupName = className.substring(
50                  0, SchedulerEngine.GROUP_NAME_MAX_LENGTH);
51          }
52  
53          _key = jobName.concat(StringPool.COLON).concat(groupName);
54      }
55  
56      public void receive(Message message) {
57          String receiverKey = GetterUtil.getString(
58              message.getString(SchedulerEngine.RECEIVER_KEY));
59  
60          if (receiverKey.equals(_key)) {
61              try{
62                  _messageListener.receive(message);
63              }
64              finally {
65                  if (message.getBoolean(SchedulerEngine.DISABLE)) {
66                      String destinationName = message.getDestinationName();
67  
68                      MessageBusUtil.unregisterMessageListener(
69                          destinationName, this);
70                  }
71              }
72          }
73      }
74  
75      private String _key;
76      private MessageListener _messageListener;
77  
78  }