001    /**
002     * Copyright (c) 2000-2013 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.DestinationNames;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.messaging.MessageBusUtil;
022    import com.liferay.portal.kernel.messaging.MessageListener;
023    import com.liferay.portal.kernel.messaging.MessageListenerException;
024    import com.liferay.portal.kernel.scheduler.JobState;
025    import com.liferay.portal.kernel.scheduler.SchedulerEngine;
026    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
027    import com.liferay.portal.kernel.scheduler.TriggerState;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
030    
031    import java.util.Date;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class SchedulerEventMessageListenerWrapper implements MessageListener {
037    
038            public void afterPropertiesSet() {
039                    if (_jobName.length() > SchedulerEngine.GROUP_NAME_MAX_LENGTH) {
040                            _jobName = _jobName.substring(
041                                    0, SchedulerEngine.GROUP_NAME_MAX_LENGTH);
042                    }
043    
044                    if (_groupName.length() > SchedulerEngine.JOB_NAME_MAX_LENGTH) {
045                            _groupName = _groupName.substring(
046                                    0, SchedulerEngine.JOB_NAME_MAX_LENGTH);
047                    }
048    
049                    _receiverKey = new ReceiverKey(_jobName, _groupName);
050    
051                    if (_messageListenerUUID == null) {
052                            _messageListenerUUID = PortalUUIDUtil.generate();
053                    }
054            }
055    
056            public String getMessageListenerUUID() {
057                    return _messageListenerUUID;
058            }
059    
060            @Override
061            public void receive(Message message) throws MessageListenerException {
062                    String destinationName = GetterUtil.getString(
063                            message.getString(SchedulerEngine.DESTINATION_NAME));
064    
065                    if (destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
066                            ReceiverKey receiverKey = (ReceiverKey)message.get(
067                                    SchedulerEngine.RECEIVER_KEY);
068    
069                            if (!_receiverKey.equals(receiverKey)) {
070                                    return;
071                            }
072                    }
073    
074                    try {
075                            _messageListener.receive(message);
076                    }
077                    catch (Exception e) {
078                            handleException(message, e);
079    
080                            if (e instanceof MessageListenerException) {
081                                    throw (MessageListenerException)e;
082                            }
083                            else {
084                                    throw new MessageListenerException(e);
085                            }
086                    }
087                    finally {
088                            TriggerState triggerState = null;
089    
090                            if (message.getBoolean(SchedulerEngine.DISABLE)) {
091                                    triggerState = TriggerState.COMPLETE;
092    
093                                    if (destinationName.equals(
094                                                    DestinationNames.SCHEDULER_DISPATCH)) {
095    
096                                            MessageBusUtil.unregisterMessageListener(
097                                                    destinationName, this);
098                                    }
099                            }
100                            else {
101                                    triggerState = TriggerState.NORMAL;
102                            }
103    
104                            try {
105                                    SchedulerEngineHelperUtil.auditSchedulerJobs(
106                                            message, triggerState);
107                            }
108                            catch (Exception e) {
109                                    if (_log.isInfoEnabled()) {
110                                            _log.info("Unable to send audit message", e);
111                                    }
112                            }
113                    }
114            }
115    
116            /**
117             * @deprecated As of 6.2.0, replaced by {@link #setGroupName(String)}
118             */
119            public void setClassName(String className) {
120                    _groupName = className;
121                    _jobName = className;
122            }
123    
124            public void setGroupName(String groupName) {
125                    _groupName = groupName;
126            }
127    
128            public void setJobName(String jobName) {
129                    _jobName = jobName;
130            }
131    
132            public void setMessageListener(MessageListener messageListener) {
133                    _messageListener = messageListener;
134            }
135    
136            public void setMessageListenerUUID(String messageListenerUUID) {
137                    _messageListenerUUID = messageListenerUUID;
138            }
139    
140            protected void handleException(Message message, Exception exception) {
141                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
142    
143                    if (jobState != null) {
144                            jobState.addException(exception, new Date());
145                    }
146            }
147    
148            private static Log _log = LogFactoryUtil.getLog(
149                    SchedulerEventMessageListenerWrapper.class);
150    
151            private String _groupName;
152            private String _jobName;
153            private MessageListener _messageListener;
154            private String _messageListenerUUID;
155            private ReceiverKey _receiverKey;
156    
157    }