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