001    /**
002     * Copyright (c) 2000-2011 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(
063                            new ObjectValuePair<Exception, Date>(exception, date));
064    
065                    while (_exceptions.size() > _exceptionsMaxSize) {
066                            _exceptions.poll();
067                    }
068            }
069    
070            public void clearExceptions() {
071                    if (_exceptions != null && !_exceptions.isEmpty()) {
072                            _exceptions.clear();
073                    }
074            }
075    
076            @Override
077            public Object clone() {
078                    JobState jobState = new JobState(_triggerState, _exceptionsMaxSize);
079    
080                    if (_exceptions != null) {
081                            Queue<ObjectValuePair<Exception, Date>> exceptions =
082                                    new LinkedList<ObjectValuePair<Exception, Date>>();
083    
084                            exceptions.addAll(_exceptions);
085    
086                            jobState._exceptions = exceptions;
087                    }
088    
089                    if (_triggerDates != null) {
090                            Map<String, Date> triggerTimeInfomation =
091                                    new HashMap<String, Date>();
092    
093                            triggerTimeInfomation.putAll(_triggerDates);
094    
095                            jobState._triggerDates = triggerTimeInfomation;
096                    }
097    
098                    return jobState;
099            }
100    
101            public ObjectValuePair<Exception, Date>[] getExceptions() {
102                    if (_exceptions == null) {
103                            return null;
104                    }
105    
106                    return _exceptions.toArray(new ObjectValuePair[_exceptions.size()]);
107            }
108    
109            public int getExceptionsMaxSize() {
110                    return _exceptionsMaxSize;
111            }
112    
113            public Date getTriggerDate(String key) {
114                    if (_triggerDates == null) {
115                            return null;
116                    }
117    
118                    return _triggerDates.get(key);
119            }
120    
121            public Map<String, Date> getTriggerDates() {
122                    if (_triggerDates == null) {
123                            return Collections.EMPTY_MAP;
124                    }
125    
126                    return _triggerDates;
127            }
128    
129            public TriggerState getTriggerState() {
130                    return _triggerState;
131            }
132    
133            public void setTriggerDate(String key, Date date) {
134                    if (_triggerDates == null) {
135                            _triggerDates = new HashMap<String, Date>();
136                    }
137    
138                    _triggerDates.put(key, date);
139            }
140    
141            public void setTriggerState(TriggerState triggerState) {
142                    _triggerState = triggerState;
143            }
144    
145            private static final int _EXCEPTIONS_MAX_SIZE = 10;
146    
147            private static final long serialVersionUID = 5747422831990881126L;
148    
149            private Queue<ObjectValuePair<Exception, Date>> _exceptions;
150            private int _exceptionsMaxSize;
151            private Map<String, Date> _triggerDates;
152            private TriggerState _triggerState;
153    
154    }