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.messaging;
016    
017    import com.liferay.portal.kernel.messaging.Message;
018    import com.liferay.portal.kernel.messaging.MessageBusUtil;
019    import com.liferay.portal.kernel.messaging.MessageListener;
020    import com.liferay.portal.kernel.scheduler.SchedulerEngine;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class SchedulerEventMessageListenerWrapper implements MessageListener {
028    
029            public SchedulerEventMessageListenerWrapper(
030                    MessageListener messageListener, String className) {
031    
032                    _messageListener = messageListener;
033    
034                    String jobName = className;
035    
036                    if (className.length() > SchedulerEngine.JOB_NAME_MAX_LENGTH) {
037                            jobName = className.substring(
038                                    0, SchedulerEngine.JOB_NAME_MAX_LENGTH);
039                    }
040    
041                    String groupName = className;
042    
043                    if (className.length() > SchedulerEngine.GROUP_NAME_MAX_LENGTH) {
044                            groupName = className.substring(
045                                    0, SchedulerEngine.GROUP_NAME_MAX_LENGTH);
046                    }
047    
048                    _key = jobName.concat(StringPool.COLON).concat(groupName);
049            }
050    
051            public void receive(Message message) {
052                    String receiverKey = GetterUtil.getString(
053                            message.getString(SchedulerEngine.RECEIVER_KEY));
054    
055                    if (receiverKey.equals(_key)) {
056                            try{
057                                    _messageListener.receive(message);
058                            }
059                            finally {
060                                    if (message.getBoolean(SchedulerEngine.DISABLE)) {
061                                            String destinationName = message.getDestinationName();
062    
063                                            MessageBusUtil.unregisterMessageListener(
064                                                    destinationName, this);
065                                    }
066                            }
067                    }
068            }
069    
070            private String _key;
071            private MessageListener _messageListener;
072    
073    }