diff --git a/core/src/main/java/org/zstack/core/Platform.java b/core/src/main/java/org/zstack/core/Platform.java index 0e44336402..a046d3fe5b 100755 --- a/core/src/main/java/org/zstack/core/Platform.java +++ b/core/src/main/java/org/zstack/core/Platform.java @@ -29,7 +29,6 @@ import org.zstack.header.errorcode.SysErrors; import org.zstack.header.exception.CloudRuntimeException; import org.zstack.header.identity.AccountConstant; -import org.zstack.header.identity.IdentityErrors; import org.zstack.header.vo.BaseResource; import org.zstack.utils.*; import org.zstack.utils.data.StringTemplate; @@ -104,7 +103,7 @@ public static Locale getLocale() { } private static Map linkGlobalPropertyMap(String prefix) { - Map ret = new HashMap(); + Map ret = new HashMap<>(); Map map = getGlobalPropertiesStartWith(prefix); if (map.isEmpty()) { return ret; @@ -118,7 +117,7 @@ private static Map linkGlobalPropertyMap(String prefix) { return ret; } - private static void linkGlobalProperty(Class clz, Map propertiesMap) { + private static void linkGlobalProperty(Class clz, Map propertiesMap) { for (Field f : clz.getDeclaredFields()) { GlobalProperty at = f.getAnnotation(GlobalProperty.class); if (at == null) { @@ -214,13 +213,12 @@ public static Map getGlobalProperties() { private static List linkGlobalPropertyList(String name) { Map map = getGlobalPropertiesStartWith(name); - List ret = new ArrayList(map.size()); + List ret = new ArrayList<>(map.size()); if (map.isEmpty()) { return ret; } - List orderedKeys = new ArrayList(); - orderedKeys.addAll(map.keySet()); + List orderedKeys = new ArrayList<>(map.keySet()); Collections.sort(orderedKeys); for (String key : orderedKeys) { @@ -266,7 +264,7 @@ private static void linkGlobalProperty() { logger.debug(String.format("system properties:\n%s", StringUtils.join(lst, ","))); - for (Class clz : clzs) { + for (Class clz : clzs) { linkGlobalProperty(clz, propertiesMap); } } @@ -306,6 +304,7 @@ private static void writePidFile() throws IOException { FileUtils.writeStringToFile(pidFile, pid); } + @SuppressWarnings("unchecked") private static void prepareDefaultDbProperties() { if (DatabaseGlobalProperty.DbUrl != null) { String dbUrl = DatabaseGlobalProperty.DbUrl; @@ -623,21 +622,11 @@ public static String getGlobalProperty(String name) { return System.getProperty(name); } - public static String getGlobalPropertyAnnotationName(Class clz, String fieldName) { - try { - String name = clz.getDeclaredField(fieldName).getAnnotation(GlobalProperty.class).name().trim(); - /* remove the last character '.' */ - return name.substring(0, name.length() - 1); - } catch (Exception e) { - return ""; - } - } - public static Map getGlobalPropertiesStartWith(String prefix) { Properties props = System.getProperties(); - Enumeration e = props.propertyNames(); + Enumeration e = props.propertyNames(); - Map ret = new HashMap(); + Map ret = new HashMap<>(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); if (key.startsWith(prefix)) { @@ -849,8 +838,8 @@ public static String toI18nString(String code, Object... args) { return toI18nString(code, null, args); } - public static String toI18nString(String code, Locale l, List args) { - return toI18nString(code, l, args.toArray(new Object[args.size()])); + public static String toI18nString(String code, Locale l, List args) { + return toI18nString(code, l, args.toArray(new Object[0])); } private static String stringFormat(String fmt, Object...args) { @@ -965,8 +954,6 @@ private static ErrorCodeElaboration elaborate(String fmt, Object...args) { return null; } - private static List allowCode = CollectionDSL.list(IdentityErrors.INVALID_SESSION); - public static ErrorCode err(Enum errCode, String fmt, Object...args) { ErrorCode errorCode = getComponentLoader().getComponent(ErrorFacade.class) .instantiateErrorCode(errCode, fmt, args); @@ -985,155 +972,26 @@ public static ErrorCode err(Enum errCode, String fmt, Object...args) { return errorCode; } - /** - * use err(errCode, fmt, args...).withCause(cause) - */ - @Deprecated - public static ErrorCode err(Enum errCode, ErrorCode cause, String fmt, Object...args) { - ErrorFacade errf = getComponentLoader().getComponent(ErrorFacade.class); - String details = null; - if (fmt != null) { - try { - details = SysErrors.INTERNAL == errCode ? String.format(fmt, args) : toI18nString(fmt, args); - } catch (Exception e) { - logger.warn("exception happened when format error message"); - logger.warn(e.getMessage()); - details = fmt; - } - } - - ErrorCode result = errf.instantiateErrorCode(errCode, details, cause); - handleErrorElaboration(errCode, fmt, result, cause, args); - addErrorCounter(result); - - return result; - } - public static ErrorCode multiErr(Collection causes, String fmt, Object...args) { - return err(SysErrors.MULTIPLE_REASONS, null, fmt, args) + return err(SysErrors.MULTIPLE_REASONS, fmt, args) .withCause(causes); } public static ErrorCode multiErr(Collection causes) { - return err(SysErrors.MULTIPLE_REASONS, (ErrorCode) null, "Multiple errors") + return err(SysErrors.MULTIPLE_REASONS, "Multiple errors") .withCause(causes); } public static ErrorCode multiErr(ErrorCodeList errorCodeList, String fmt, Object...args) { - return err(SysErrors.MULTIPLE_REASONS, null, fmt, args) + return err(SysErrors.MULTIPLE_REASONS, fmt, args) .withCause(errorCodeList); } public static ErrorCode multiErr(ErrorCodeList errorCodeList) { - return err(SysErrors.MULTIPLE_REASONS, (ErrorCode) null, "Multiple errors") + return err(SysErrors.MULTIPLE_REASONS, "Multiple errors") .withCause(errorCodeList); } - private static void findElaborationFromCoreError(ErrorCode cause, ErrorCode result) { - ErrorCode coreError = cause == null ? getCoreError(result) : getCoreError(cause); - // use the core cause as elaboration if it existed - if (coreError.getElaboration() != null) { - result.setCost(coreError.getCost()); - result.setElaboration(coreError.getElaboration()); - result.setMessages(coreError.getMessages()); - } else if (cause != null && !cause.getCauses().isEmpty()) { - // suppose elaborations are existed in causes... - String costs = null; - String elas = null; - ErrorCodeElaboration messages = null; - for (ErrorCode c: cause.getCauses()) { - ErrorCode lcError = getCoreError(c); - if (lcError.getElaboration() != null && !lcError.getElaboration().equals(elas) && !lcError.getMessages().equals(messages)) { - costs = costs == null ? lcError.getCost() : addTwoCosts(costs, lcError.getCost()); - elas = elas == null ? lcError.getElaboration() : String.join(",", elas, lcError.getElaboration()); - messages = messages == null ? lcError.getMessages() : messages.addElaborationMessage(lcError.getMessages()); - } - } - result.setCost(costs); - result.setElaboration(elas); - result.setMessages(messages); - } - } - - private static void generateElaboration(Enum errCode, ErrorCode result, String fmt, Object...args) { - // try to find same error with fmt and args - ErrorCodeElaboration ela = elaborate(fmt, args); - - // only elaborate the error code in allowCode if fmt missed - if (ela == null && allowCode.contains(errCode)) { - ela = elaborate(result.getDescription()); - } - - // failed to find elaboration, add the error code fmt string to missed list - if (ela == null) { - if (args != null) { - StringSimilarity.addMissed(String.format(fmt, args)); - } else { - StringSimilarity.addMissed(fmt); - } - - // note: if allowCode failed to find elaboration, - // we still need to add the description to missed list - if (allowCode.contains(errCode)) { - StringSimilarity.addMissed(result.getDescription()); - } - - return; - } - - String prefix, msg; - if (locale.equals(Locale.SIMPLIFIED_CHINESE)) { - prefix = "错误信息: %s\n"; - msg = ela.getMessage_cn(); - } else { - prefix = "Error message: %s\n"; - msg = ela.getMessage_en(); - } - - // tricky code that we treat the only one args error maybe use the cause or - // error from other component directly, so we need to check if the args is - // matched with the regex at first - if (args != null && args.length == 1 && StringSimilarity.isRegexMatched(ela.getRegex(), String.valueOf(args[0]))) { - result.setMessages(ErrorCodeElaboration.clone(ela) - .removeCnMessageIfLocaleIsNotMatch(locale)); - String formatError = String.format(prefix, args[0]); - result.setElaboration(StringSimilarity.formatElaborationDeprecated(formatError)); - } else { - result.setMessages(ErrorCodeElaboration.cloneSimple(ela, args) - .removeCnMessageIfLocaleIsNotMatch(locale)); - result.setElaboration(StringSimilarity.formatElaborationDeprecated(String.format(prefix, msg), args)); - } - - StringSimilarity.addErrors(fmt, ela); - } - - private static void handleErrorElaboration(Enum errCode, String fmt, ErrorCode result, ErrorCode cause, Object...args) { - if (!CoreGlobalProperty.ENABLE_ELABORATION) { - return; - } - - // start to generate elaboration... - try { - findElaborationFromCoreError(cause, result); - - // if the elaboration is not found, try to generate it - if (result.getElaboration() == null && cause == null) { - long start = System.currentTimeMillis(); - generateElaboration(errCode, result, fmt, args); - result.setCost((System.currentTimeMillis() - start) + "ms"); - } - } catch (Throwable e) { - logger.warn("exception happened when found elaboration"); - logger.warn(e.getMessage()); - } - } - - private static String addTwoCosts(String origin, String increase) { - long c1 = Long.parseLong(origin.substring(0, origin.length() - 2).trim()); - long c2 = Long.parseLong(increase.substring(0, increase.length() - 2).trim()); - return (c1 + c2) + "ms"; - } - private static ErrorCode getCoreError(ErrorCode result) { if (result.getCause() == null) { return result; @@ -1173,14 +1031,6 @@ public static ErrorCode operr(String fmt, Object...args) { return err(SysErrors.OPERATION_ERROR, fmt, args); } - /** - * use operr(fmt, args...).withCause(cause) - */ - @Deprecated - public static ErrorCode operr(ErrorCode cause, String fmt, Object...args) { - return err(SysErrors.OPERATION_ERROR, cause, fmt, args); - } - public static ErrorCode canerr(String fmt, Object...args) { return err(SysErrors.CANCEL_ERROR, fmt, args); } @@ -1193,14 +1043,6 @@ public static ErrorCode touterr(String fmt, Object...args) { return err(SysErrors.TIMEOUT, fmt, args); } - /** - * use touterr(fmt, args...).withCause(cause) - */ - @Deprecated - public static ErrorCode touterr(ErrorCode cause, String fmt, Object...args) { - return err(SysErrors.TIMEOUT, cause, fmt, args); - } - public static ErrorCode ioerr(String fmt, Object...args) { return err(SysErrors.IO_ERROR, fmt, args); } diff --git a/plugin/nfsPrimaryStorage/src/main/java/org/zstack/storage/primary/nfs/NfsPrimaryStorageKVMBackend.java b/plugin/nfsPrimaryStorage/src/main/java/org/zstack/storage/primary/nfs/NfsPrimaryStorageKVMBackend.java index aacb14766b..93d3d7aab9 100755 --- a/plugin/nfsPrimaryStorage/src/main/java/org/zstack/storage/primary/nfs/NfsPrimaryStorageKVMBackend.java +++ b/plugin/nfsPrimaryStorage/src/main/java/org/zstack/storage/primary/nfs/NfsPrimaryStorageKVMBackend.java @@ -157,8 +157,8 @@ public void success(MountAgentResponse returnValue) { public void fail(ErrorCode errorCode) { if (errorCode.getDetails().contains("java.net.SocketTimeoutException: Read timed out")) { // socket read timeout is caused by timeout of mounting a wrong URL - errorCode = touterr(errorCode, "mount timeout. Please the check if the URL[%s] is" + - " valid to mount", inv.getUrl()); + errorCode = touterr("mount timeout. Please check if the URL[%s] is" + + " valid to mount", inv.getUrl()).withCause(errorCode); } completion.fail(errorCode); }