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