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.message;
020
021import java.util.Date;
022import java.util.HashMap;
023import java.util.Map;
024
025import javax.jms.JMSException;
026import javax.jms.Message;
027
028import org.apache.oozie.AppType;
029import org.apache.oozie.client.event.Event.MessageType;
030import org.apache.oozie.client.event.JobEvent.EventStatus;
031import org.apache.oozie.client.event.JobEvent;
032import org.apache.oozie.client.event.jms.JMSHeaderConstants;
033import org.codehaus.jackson.annotate.JsonIgnore;
034import org.codehaus.jackson.annotate.JsonProperty;
035import org.codehaus.jackson.map.annotate.JsonSerialize;
036
037
038/**
039 * Class holding attributes related to a job message
040 *
041 */
042@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
043public class JobMessage extends EventMessage {
044
045    @JsonProperty
046    private String id;
047    @JsonProperty
048    private String parentId;
049    @JsonProperty
050    private Date startTime;
051    @JsonProperty
052    private Date endTime;
053
054    private Map<String, String> jmsMessageProperties = new HashMap<String, String>();
055    private JobEvent.EventStatus eventStatus;
056    private String appName;
057    private String user;
058
059    /**
060     * Default constructor
061     */
062    public JobMessage() {
063        // Default constructor for jackson
064    }
065
066    /**
067     * Constructs a job message
068     *
069     * @param eventStatus the event status
070     * @param appType the appType for event
071     * @param id the id of job
072     * @param parentId the parent id of job
073     * @param startTime the start time of job
074     * @param endTime the end time of job
075     * @param user user of the job
076     * @param appName appName for job
077     */
078    public JobMessage(JobEvent.EventStatus eventStatus, AppType appType, String id, String parentId, Date startTime,
079            Date endTime, String user, String appName) {
080        super(MessageType.JOB, appType);
081        this.eventStatus = eventStatus;
082        this.id = id;
083        this.parentId = parentId;
084        this.startTime = startTime;
085        this.appName = appName;
086        this.user = user;
087        this.endTime = endTime;
088
089        jmsMessageProperties = new HashMap<String, String>();
090        jmsMessageProperties.put(JMSHeaderConstants.APP_TYPE, appType.toString());
091        jmsMessageProperties.put(JMSHeaderConstants.MESSAGE_TYPE, MessageType.JOB.toString());
092        jmsMessageProperties.put(JMSHeaderConstants.EVENT_STATUS, eventStatus.toString());
093        jmsMessageProperties.put(JMSHeaderConstants.APP_NAME, appName);
094        jmsMessageProperties.put(JMSHeaderConstants.USER, user);
095    }
096
097    /**
098     * Sets the Job id for message
099     *
100     * @param id the job id
101     */
102    public void setId(String id) {
103        this.id = id;
104    }
105
106    /**
107     * Gets the job id
108     *
109     * @return the Job id
110     */
111    public String getId() {
112        return id;
113    }
114
115    /**
116     * Sets the parent job id for message
117     *
118     * @param parentId the parent job id
119     */
120    public void setParentId(String parentId) {
121        this.parentId = parentId;
122    }
123
124    /**
125     * Gets the parent job id
126     *
127     * @return the parentId
128     */
129    public String getParentId() {
130        return parentId;
131    }
132
133    /**
134     * Sets the job start time for message
135     *
136     * @param startTime
137     */
138    public void setStartTime(Date startTime) {
139        this.startTime = startTime;
140    }
141
142    /**
143     * Gets the job start time
144     *
145     * @return the start time
146     */
147    public Date getStartTime() {
148        return startTime;
149    }
150
151    /**
152     * Sets the job end time for message
153     *
154     * @param endTime
155     */
156    public void setEndTime(Date endTime) {
157        this.endTime = endTime;
158    }
159
160    /**
161     * Gets the job end time
162     *
163     * @return the end time
164     */
165    public Date getEndTime() {
166        return endTime;
167    }
168
169    /**
170     * Sets the job's app name for message
171     *
172     * @param appName
173     */
174    public void setAppName(String appName) {
175        this.appName = appName;
176    }
177
178    /**
179     * Gets the job's app name
180     *
181     * @return the app name
182     */
183    @JsonIgnore
184    public String getAppName() {
185        return appName;
186    }
187
188    /**
189     * Sets the JMS selectors for message
190     *
191     * @param properties the jms selector key value pair
192     */
193    void setMessageProperties(Map<String, String> properties) {
194        jmsMessageProperties = properties;
195    }
196
197    /**
198     * Gets the message properties
199     *
200     * @return the message properties
201     */
202    @JsonIgnore
203    public Map<String, String> getMessageProperties() {
204        return jmsMessageProperties;
205    }
206
207    /**
208     * sets the job user for the msg
209     *
210     * @param user
211     */
212    public void setUser(String user) {
213        this.user = user;
214    }
215
216    /**
217     * Gets the job user
218     *
219     * @return the user
220     */
221    @JsonIgnore
222    public String getUser() {
223        return user;
224    }
225
226    /**
227     * Sets the event status
228     *
229     * @param eventStatus
230     */
231    public void setEventStatus(JobEvent.EventStatus eventStatus) {
232        this.eventStatus = eventStatus;
233    }
234
235    /**
236     * Gets the event status
237     *
238     * @return the event status
239     */
240    @JsonIgnore
241    public JobEvent.EventStatus getEventStatus() {
242        return eventStatus;
243    }
244
245    @Override
246    public void setProperties(Message message) throws JMSException {
247        super.setProperties(message);
248        setEventStatus(EventStatus.valueOf(message.getStringProperty(JMSHeaderConstants.EVENT_STATUS)));
249        setAppName(message.getStringProperty(JMSHeaderConstants.APP_NAME));
250        setUser(message.getStringProperty(JMSHeaderConstants.USER));
251    }
252}