001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.scheduler.messaging;
016    
017    import com.liferay.portal.kernel.messaging.DestinationNames;
018    import com.liferay.portal.kernel.messaging.Message;
019    import com.liferay.portal.kernel.messaging.MessageBusUtil;
020    import com.liferay.portal.kernel.messaging.MessageListener;
021    import com.liferay.portal.kernel.messaging.MessageListenerException;
022    import com.liferay.portal.kernel.scheduler.JobState;
023    import com.liferay.portal.kernel.scheduler.SchedulerEngine;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class SchedulerEventMessageListenerWrapper implements MessageListener {
032    
033            public void afterPropertiesSet() {
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.PERIOD).concat(groupName);
049    
050                    if (_messageListenerUUID == null) {
051                            _messageListenerUUID = PortalUUIDUtil.generate();
052                    }
053            }
054    
055            public String getMessageListenerUUID() {
056                    return _messageListenerUUID;
057            }
058    
059            public void receive(Message message) throws MessageListenerException {
060                    String destinationName = GetterUtil.getString(
061                            message.getString(SchedulerEngine.DESTINATION_NAME));
062    
063                    if (destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
064                            String receiverKey = GetterUtil.getString(
065                                    message.getString(SchedulerEngine.RECEIVER_KEY));
066    
067                            if (!receiverKey.equals(_key)) {
068                                    return;
069                            }
070                    }
071    
072                    try{
073                            _messageListener.receive(message);
074                    }
075                    catch (Exception e) {
076                            handleException(message, e);
077    
078                            if (e instanceof MessageListenerException) {
079                                    throw (MessageListenerException)e;
080                            }
081                            else {
082                                    throw new MessageListenerException(e);
083                            }
084                    }
085                    finally {
086                            if (message.getBoolean(SchedulerEngine.DISABLE) &&
087                                    destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
088    
089                                    MessageBusUtil.unregisterMessageListener(
090                                            destinationName, this);
091                            }
092                    }
093            }
094    
095            public void setClassName(String className) {
096                    _className = className;
097            }
098    
099            public void setMessageListener(MessageListener messageListener) {
100                    _messageListener = messageListener;
101            }
102    
103            public void setMessageListenerUUID(String messageListenerUUID) {
104                    _messageListenerUUID = messageListenerUUID;
105            }
106    
107            protected void handleException(Message message, Exception exception) {
108                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
109    
110                    if (jobState != null) {
111                            jobState.addException(exception);
112                    }
113            }
114    
115            private String _className;
116            private String _key;
117            private MessageListener _messageListener;
118            private String _messageListenerUUID;
119    
120    }