001    /**
002     * Copyright (c) 2000-2013 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<String, Date>(triggerDates);
055            }
056    
057            public void addException(Exception exception, Date date) {
058                    if (_exceptions == null) {
059                            _exceptions = new LinkedList<ObjectValuePair<Exception, Date>>();
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<ObjectValuePair<Exception, Date>>();
082    
083                            exceptions.addAll(_exceptions);
084    
085                            jobState._exceptions = exceptions;
086                    }
087    
088                    if (_triggerDates != null) {
089                            Map<String, Date> triggerTimeInfomation =
090                                    new HashMap<String, Date>();
091    
092                            triggerTimeInfomation.putAll(_triggerDates);
093    
094                            jobState._triggerDates = triggerTimeInfomation;
095                    }
096    
097                    return jobState;
098            }
099    
100            public ObjectValuePair<Exception, Date>[] getExceptions() {
101                    if (_exceptions == null) {
102                            return null;
103                    }
104    
105                    return _exceptions.toArray(new ObjectValuePair[_exceptions.size()]);
106            }
107    
108            public int getExceptionsMaxSize() {
109                    return _exceptionsMaxSize;
110            }
111    
112            public Date getTriggerDate(String key) {
113                    if (_triggerDates == null) {
114                            return null;
115                    }
116    
117                    return _triggerDates.get(key);
118            }
119    
120            public Map<String, Date> getTriggerDates() {
121                    if (_triggerDates == null) {
122                            return Collections.emptyMap();
123                    }
124    
125                    return _triggerDates;
126            }
127    
128            public TriggerState getTriggerState() {
129                    return _triggerState;
130            }
131    
132            public void setTriggerDate(String key, Date date) {
133                    if (_triggerDates == null) {
134                            _triggerDates = new HashMap<String, Date>();
135                    }
136    
137                    _triggerDates.put(key, date);
138            }
139    
140            public void setTriggerState(TriggerState triggerState) {
141                    _triggerState = triggerState;
142            }
143    
144            private static final int _EXCEPTIONS_MAX_SIZE = 10;
145    
146            private static final long serialVersionUID = 5747422831990881126L;
147    
148            private Queue<ObjectValuePair<Exception, Date>> _exceptions;
149            private int _exceptionsMaxSize;
150            private Map<String, Date> _triggerDates;
151            private TriggerState _triggerState;
152    
153    }