001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.client.event.jms;
020
021import org.apache.oozie.client.event.Event.MessageType;
022import org.apache.oozie.client.event.message.CoordinatorActionMessage;
023import org.apache.oozie.client.event.message.EventMessage;
024import org.apache.oozie.client.event.message.WorkflowJobMessage;
025import org.apache.oozie.client.event.message.SLAMessage;
026import org.apache.oozie.AppType;
027import javax.jms.Message;
028import javax.jms.TextMessage;
029import javax.jms.JMSException;
030
031/**
032 * Class to deserialize the jms message to java object
033 */
034public abstract class MessageDeserializer {
035
036    /**
037     * Constructs the event message from JMS message
038     *
039     * @param <T> the type
040     * @param message the JMS message
041     * @return EventMessage
042     * @throws JMSException in case of JMS error
043     */
044    @SuppressWarnings("unchecked")
045    public <T extends EventMessage> T getEventMessage(Message message) throws JMSException {
046        TextMessage textMessage = (TextMessage) message;
047        String appTypeString = textMessage.getStringProperty(JMSHeaderConstants.APP_TYPE);
048        String msgType = textMessage.getStringProperty(JMSHeaderConstants.MESSAGE_TYPE);
049        String messageBody = textMessage.getText();
050        T eventMsg = null;
051
052        if (appTypeString == null || appTypeString.isEmpty() || messageBody == null || messageBody.isEmpty()) {
053            throw new IllegalArgumentException("Could not extract OozieEventMessage. "
054                    + "AppType and/or MessageBody is null/empty." + "Apptype is " + appTypeString + " MessageBody is "
055                    + messageBody);
056        }
057
058        if (MessageType.valueOf(msgType) == MessageType.JOB) {
059            switch (AppType.valueOf(appTypeString)) {
060                case WORKFLOW_JOB:
061                    WorkflowJobMessage wfJobMsg = getDeserializedObject(messageBody, WorkflowJobMessage.class);
062                    wfJobMsg.setProperties(textMessage);
063                    eventMsg = (T) wfJobMsg;
064                    break;
065                case COORDINATOR_ACTION:
066                    CoordinatorActionMessage caActionMsg = getDeserializedObject(messageBody,
067                            CoordinatorActionMessage.class);
068                    caActionMsg.setProperties(textMessage);
069                    eventMsg = (T) caActionMsg;
070                    break;
071                default:
072                    throw new UnsupportedOperationException("Conversion of " + appTypeString
073                            + " to Event message is not supported");
074            }
075        }
076        else if (MessageType.valueOf(msgType) == MessageType.SLA) {
077            SLAMessage SLAMsg = getDeserializedObject(messageBody, SLAMessage.class);
078            SLAMsg.setProperties(textMessage);
079            eventMsg = (T) SLAMsg;
080        }
081
082        return eventMsg;
083    }
084
085    protected abstract <T> T getDeserializedObject(String s, Class<T> clazz);
086
087}