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