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 *     mcedica
018 */
019package org.nuxeo.ecm.core.management.api;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import static java.lang.Boolean.FALSE;
025import static java.lang.Boolean.TRUE;
026
027public class ProbeStatus {
028
029    protected boolean neverExecuted = false;
030
031    protected final boolean success;
032
033    protected final Map<String, String> infos = new HashMap<String, String>();
034
035    public static final String DEFAULT_INFO_FIELD = "info";
036
037    public static final String ERROR_FIELD = "error";
038
039    protected ProbeStatus(String info, Boolean success) {
040        this.infos.put(DEFAULT_INFO_FIELD, info);
041        this.success = success;
042    }
043
044    protected ProbeStatus(Map<String, String> infos, Boolean success) {
045        this.infos.putAll(infos);
046        this.success = success;
047    }
048
049    public static ProbeStatus newBlankProbStatus() {
050        ProbeStatus status = new ProbeStatus("[unavailable]", false);
051        status.neverExecuted = true;
052        return status;
053    }
054
055    public static ProbeStatus newFailure(String info) {
056        return new ProbeStatus(info, FALSE);
057    }
058
059    public static ProbeStatus newFailure(Map<String, String> infos) {
060        return new ProbeStatus(infos, FALSE);
061    }
062
063    public static ProbeStatus newError(Throwable t) {
064        Map<String, String> infos = new HashMap<String, String>();
065        infos.put(ERROR_FIELD, t.toString());
066        infos.put(DEFAULT_INFO_FIELD, "Caught error " + t.toString());
067        return new ProbeStatus(infos, FALSE);
068    }
069
070    public static ProbeStatus newSuccess(String info) {
071        return new ProbeStatus(info, TRUE);
072    }
073
074    public static ProbeStatus newSuccess(Map<String, String> infos) {
075        return new ProbeStatus(infos, TRUE);
076    }
077
078    public boolean isSuccess() {
079        return success;
080    }
081
082    public boolean isFailure() {
083        return !success;
084    }
085
086    public boolean isNeverExecuted() {
087        return neverExecuted;
088    }
089
090    public String getAsString() {
091        if (infos == null || infos.isEmpty()) {
092            return Boolean.toString(success);
093        }
094        if (infos.size() == 1) {
095            return infos.values().iterator().next();
096        }
097
098        StringBuilder sb = new StringBuilder();
099        for (String key : infos.keySet()) {
100            sb.append(key);
101            sb.append(" : ");
102            sb.append(infos.get(key));
103            sb.append("\n");
104        }
105        return sb.toString();
106    }
107
108    @Override
109    public String toString() {
110        return getAsString();
111    }
112
113    public String getAsXML() {
114        StringBuilder sb = new StringBuilder();
115        sb.append("<dl>");
116        for (String key : infos.keySet()) {
117            sb.append("<dt>");
118            sb.append(key);
119            sb.append("</dt>");
120            sb.append("<dd class='").append(key).append("'>");
121            sb.append(infos.get(key));
122            sb.append("</dd>");
123        }
124        sb.append("</dl>");
125        return sb.toString();
126    }
127
128}