001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     mcedica
011 */
012package org.nuxeo.ecm.core.management.api;
013
014import java.util.HashMap;
015import java.util.Map;
016
017import static java.lang.Boolean.FALSE;
018import static java.lang.Boolean.TRUE;
019
020public class ProbeStatus {
021
022    protected boolean neverExecuted = false;
023
024    protected final boolean success;
025
026    protected final Map<String, String> infos = new HashMap<String, String>();
027
028    public static final String DEFAULT_INFO_FIELD = "info";
029
030    public static final String ERROR_FIELD = "error";
031
032    protected ProbeStatus(String info, Boolean success) {
033        this.infos.put(DEFAULT_INFO_FIELD, info);
034        this.success = success;
035    }
036
037    protected ProbeStatus(Map<String, String> infos, Boolean success) {
038        this.infos.putAll(infos);
039        this.success = success;
040    }
041
042    public static ProbeStatus newBlankProbStatus() {
043        ProbeStatus status = new ProbeStatus("[unavailable]", false);
044        status.neverExecuted = true;
045        return status;
046    }
047
048    public static ProbeStatus newFailure(String info) {
049        return new ProbeStatus(info, FALSE);
050    }
051
052    public static ProbeStatus newFailure(Map<String, String> infos) {
053        return new ProbeStatus(infos, FALSE);
054    }
055
056    public static ProbeStatus newError(Throwable t) {
057        Map<String, String> infos = new HashMap<String, String>();
058        infos.put(ERROR_FIELD, t.toString());
059        infos.put(DEFAULT_INFO_FIELD, "Caught error " + t.toString());
060        return new ProbeStatus(infos, FALSE);
061    }
062
063    public static ProbeStatus newSuccess(String info) {
064        return new ProbeStatus(info, TRUE);
065    }
066
067    public static ProbeStatus newSuccess(Map<String, String> infos) {
068        return new ProbeStatus(infos, TRUE);
069    }
070
071    public boolean isSuccess() {
072        return success;
073    }
074
075    public boolean isFailure() {
076        return !success;
077    }
078
079    public boolean isNeverExecuted() {
080        return neverExecuted;
081    }
082
083    public String getAsString() {
084        if (infos == null || infos.isEmpty()) {
085            return Boolean.toString(success);
086        }
087        if (infos.size() == 1) {
088            return infos.values().iterator().next();
089        }
090
091        StringBuilder sb = new StringBuilder();
092        for (String key : infos.keySet()) {
093            sb.append(key);
094            sb.append(" : ");
095            sb.append(infos.get(key));
096            sb.append("\n");
097        }
098        return sb.toString();
099    }
100
101    @Override
102    public String toString() {
103        return getAsString();
104    }
105
106    public String getAsXML() {
107        StringBuilder sb = new StringBuilder();
108        sb.append("<dl>");
109        for (String key : infos.keySet()) {
110            sb.append("<dt>");
111            sb.append(key);
112            sb.append("</dt>");
113            sb.append("<dd class='").append(key).append("'>");
114            sb.append(infos.get(key));
115            sb.append("</dd>");
116        }
117        sb.append("</dl>");
118        return sb.toString();
119    }
120
121}