001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.scheduler;
016    
017    import com.liferay.portal.kernel.util.ObjectValuePair;
018    
019    import java.io.Serializable;
020    
021    import java.util.Collections;
022    import java.util.Date;
023    import java.util.HashMap;
024    import java.util.LinkedList;
025    import java.util.Map;
026    import java.util.Queue;
027    
028    /**
029     * @author Tina Tian
030     */
031    public class JobState implements Cloneable, Serializable {
032    
033            public static final int VERSION = 1;
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 JobState(
049                    TriggerState triggerState, int exceptionsMaxSize,
050                    Map<String, Date> triggerDates) {
051    
052                    this(triggerState, exceptionsMaxSize);
053    
054                    _triggerDates = new HashMap<>(triggerDates);
055            }
056    
057            public void addException(Exception exception, Date date) {
058                    if (_exceptions == null) {
059                            _exceptions = new LinkedList<>();
060                    }
061    
062                    _exceptions.add(new ObjectValuePair<Exception, Date>(exception, date));
063    
064                    while (_exceptions.size() > _exceptionsMaxSize) {
065                            _exceptions.poll();
066                    }
067            }
068    
069            public void clearExceptions() {
070                    if ((_exceptions != null) && !_exceptions.isEmpty()) {
071                            _exceptions.clear();
072                    }
073            }
074    
075            @Override
076            public Object clone() {
077                    JobState jobState = new JobState(_triggerState, _exceptionsMaxSize);
078    
079                    if (_exceptions != null) {
080                            Queue<ObjectValuePair<Exception, Date>> exceptions =
081                                    new LinkedList<>();
082    
083                            exceptions.addAll(_exceptions);
084    
085                            jobState._exceptions = exceptions;
086                    }
087    
088                    if (_triggerDates != null) {
089                            Map<String, Date> triggerTimeInfomation = new HashMap<>();
090    
091                            triggerTimeInfomation.putAll(_triggerDates);
092    
093                            jobState._triggerDates = triggerTimeInfomation;
094                    }
095    
096                    return jobState;
097            }
098    
099            public ObjectValuePair<Exception, Date>[] getExceptions() {
100                    if (_exceptions == null) {
101                            return null;
102                    }
103    
104                    return _exceptions.toArray(new ObjectValuePair[_exceptions.size()]);
105            }
106    
107            public int getExceptionsMaxSize() {
108                    return _exceptionsMaxSize;
109            }
110    
111            public Date getTriggerDate(String key) {
112                    if (_triggerDates == null) {
113                            return null;
114                    }
115    
116                    return _triggerDates.get(key);
117            }
118    
119            public Map<String, Date> getTriggerDates() {
120                    if (_triggerDates == null) {
121                            return Collections.emptyMap();
122                    }
123    
124                    return _triggerDates;
125            }
126    
127            public TriggerState getTriggerState() {
128                    return _triggerState;
129            }
130    
131            public void setTriggerDate(String key, Date date) {
132                    if (_triggerDates == null) {
133                            _triggerDates = new HashMap<>();
134                    }
135    
136                    _triggerDates.put(key, date);
137            }
138    
139            public void setTriggerState(TriggerState triggerState) {
140                    _triggerState = triggerState;
141            }
142    
143            private static final int _EXCEPTIONS_MAX_SIZE = 10;
144    
145            private static final long serialVersionUID = 5747422831990881126L;
146    
147            private Queue<ObjectValuePair<Exception, Date>> _exceptions;
148            private final int _exceptionsMaxSize;
149            private Map<String, Date> _triggerDates;
150            private TriggerState _triggerState;
151    
152    }