001
014
015 package com.liferay.portal.kernel.scheduler;
016
017 import com.liferay.portal.kernel.util.ObjectValuePair;
018
019 import java.io.IOException;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.io.Serializable;
023
024 import java.util.Date;
025 import java.util.HashMap;
026 import java.util.LinkedList;
027 import java.util.Map;
028 import java.util.Queue;
029
030
033 public class JobState implements Cloneable, Serializable {
034
035 public JobState(TriggerState triggerState) {
036 this(triggerState, _EXCEPTIONS_MAX_SIZE);
037 }
038
039 public JobState(TriggerState triggerState, int exceptionsMaxSize) {
040 if (exceptionsMaxSize <= 0) {
041 exceptionsMaxSize = _EXCEPTIONS_MAX_SIZE;
042 }
043
044 _triggerState = triggerState;
045 _exceptionsMaxSize = exceptionsMaxSize;
046 }
047
048 public void addException(Exception exception) {
049 if (_exceptions == null) {
050 _exceptions = new LinkedList<ObjectValuePair<Exception, Date>>();
051 }
052
053 _exceptions.add(
054 new ObjectValuePair<Exception, Date>(exception, new Date()));
055
056 while (_exceptions.size() > _exceptionsMaxSize) {
057 _exceptions.poll();
058 }
059 }
060
061 public void clearExceptions() {
062 if (_exceptions != null && !_exceptions.isEmpty()) {
063 _exceptions.clear();
064 }
065 }
066
067 @Override
068 public Object clone() {
069 JobState jobState = new JobState(_triggerState, _exceptionsMaxSize);
070
071 if (_exceptions != null) {
072 Queue<ObjectValuePair<Exception, Date>> exceptions =
073 new LinkedList<ObjectValuePair<Exception, Date>>();
074
075 exceptions.addAll(_exceptions);
076
077 jobState._exceptions = exceptions;
078 }
079
080 if (_triggerTimeInfomation != null) {
081 Map<String, Date> triggerTimeInfomation =
082 new HashMap<String, Date>();
083
084 triggerTimeInfomation.putAll(_triggerTimeInfomation);
085
086 jobState._triggerTimeInfomation = triggerTimeInfomation;
087 }
088
089 return jobState;
090 }
091
092 public ObjectValuePair<Exception, Date>[] getExceptions() {
093 if (_exceptions == null) {
094 return null;
095 }
096
097 return _exceptions.toArray(new ObjectValuePair[_exceptions.size()]);
098 }
099
100 public TriggerState getTriggerState() {
101 return _triggerState;
102 }
103
104 public Date getTriggerTimeInfomation(String key) {
105 if (_triggerTimeInfomation == null) {
106 return null;
107 }
108
109 return _triggerTimeInfomation.get(key);
110 }
111
112 public void setTriggerState(TriggerState triggerState) {
113 _triggerState = triggerState;
114 }
115
116 public void setTriggerTimeInfomation(String key, Date date) {
117 if (_triggerTimeInfomation == null) {
118 _triggerTimeInfomation = new HashMap<String, Date>();
119 }
120
121 _triggerTimeInfomation.put(key, date);
122 }
123
124 private void readObject(ObjectInputStream inputStream)
125 throws ClassNotFoundException, IOException {
126
127 _exceptions =
128 (Queue<ObjectValuePair<Exception, Date>>)inputStream.readObject();
129 _exceptionsMaxSize = inputStream.readInt();
130 _triggerState = TriggerState.values()[inputStream.readInt()];
131 _triggerTimeInfomation = (Map<String, Date>)inputStream.readObject();
132 }
133
134 private void writeObject(ObjectOutputStream outputStream)
135 throws IOException {
136
137 outputStream.writeObject(_exceptions);
138 outputStream.writeInt(_exceptionsMaxSize);
139 outputStream.writeInt(_triggerState.ordinal());
140 outputStream.writeObject(_triggerTimeInfomation);
141 }
142
143 private static final int _EXCEPTIONS_MAX_SIZE = 10;
144 private static final long serialVersionUID = 5747422831990881126L;
145
146 private Queue<ObjectValuePair<Exception, Date>> _exceptions;
147 private int _exceptionsMaxSize;
148 private TriggerState _triggerState;
149 private Map<String, Date> _triggerTimeInfomation;
150
151 }