001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     matic
018 */
019package org.nuxeo.ecm.core.management.probes;
020
021import java.io.Serializable;
022import java.util.Date;
023
024import org.nuxeo.ecm.core.management.api.ProbeInfo;
025import org.nuxeo.ecm.core.management.api.ProbeMBean;
026import org.nuxeo.ecm.core.management.api.ProbeStatus;
027
028public class ProbeInfoImpl implements ProbeMBean, ProbeInfo, Serializable {
029
030    private static final long serialVersionUID = 1L;
031
032    protected final ProbeDescriptor descriptor;
033
034    protected boolean isEnabled = true;
035
036    protected String shortcutName;
037
038    protected String qualifiedName;
039
040    protected ProbeStatus lastStatus = ProbeStatus.newBlankProbStatus();
041
042    protected long runnedCount = 0L;
043
044    protected Date lastRunnedDate;
045
046    protected long lastDuration = 0L;
047
048    protected long successCount = 0L;
049
050    protected Date lastSucceedDate = new Date(0);
051
052    protected ProbeStatus lastSuccessStatus = ProbeStatus.newBlankProbStatus();
053
054    protected long failureCount = 0L;
055
056    protected Date lastFailureDate = new Date(0);
057
058    protected ProbeStatus lastFailureStatus = ProbeStatus.newBlankProbStatus();
059
060    protected ProbeInfoImpl(ProbeDescriptor descriptor) {
061        this.descriptor = descriptor;
062        shortcutName = descriptor.getShortcut();
063        qualifiedName = descriptor.getQualifiedName();
064    }
065
066    @Override
067    public long getFailedCount() {
068        return failureCount;
069    }
070
071    @Override
072    public long getLastDuration() {
073        return lastDuration;
074    }
075
076    @Override
077    public ProbeStatus getLastFailureStatus() {
078        return lastFailureStatus;
079    }
080
081    @Override
082    public Date getLastFailedDate() {
083        return lastFailureDate;
084    }
085
086    @Override
087    public Date getLastRunnedDate() {
088        return lastRunnedDate;
089    }
090
091    @Override
092    public Date getLastSucceedDate() {
093        return lastSucceedDate;
094    }
095
096    @Override
097    public long getRunnedCount() {
098        return runnedCount;
099    }
100
101    @Override
102    public long getSucceedCount() {
103        return successCount;
104    }
105
106    @Override
107    public void disable() {
108        isEnabled = false;
109    }
110
111    @Override
112    public void enable() {
113        isEnabled = true;
114    }
115
116    @Override
117    public boolean isEnabled() {
118        return isEnabled;
119    }
120
121    @Override
122    public boolean isInError() {
123        if (lastFailureDate == null) {
124            return false;
125        }
126        if (lastSucceedDate != null) {
127            return lastFailureDate.after(lastSucceedDate);
128        }
129        return true;
130    }
131
132    @Override
133    public ProbeStatus getStatus() {
134        return lastStatus;
135    }
136
137    @Override
138    public String getShortcutName() {
139        return shortcutName;
140    }
141
142    @Override
143    public ProbeDescriptor getDescriptor() {
144        return descriptor;
145    }
146
147    @Override
148    public String getQualifiedName() {
149        return qualifiedName;
150    }
151
152    public void setQualifiedName(String qualifiedName) {
153        this.qualifiedName = qualifiedName;
154    }
155
156    public void setShortcutName(String shortcutName) {
157        this.shortcutName = shortcutName;
158    }
159
160}