com.iflytek.edu.cloud.frame.error.MainErrors.java Source code

Java tutorial

Introduction

Here is the source code for com.iflytek.edu.cloud.frame.error.MainErrors.java

Source

/**
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 *
 *     12-2-11
 */
package com.iflytek.edu.cloud.frame.error;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.util.Assert;

import java.util.Locale;

/**
 * <pre>
 * 
 * </pre>
 *
 * @author ?
 * @version 1.0
 */
public class MainErrors {

    protected static Logger logger = LoggerFactory.getLogger(MainErrors.class);

    private static final String ERROR_CODE_PREFIX = "ERROR_";
    private static final String ERROR_SOLUTION_SUBFIX = "_SOLUTION";
    // ??
    private static MessageSourceAccessor errorMessageSourceAccessor;

    public static MainError getError(MainErrorType mainErrorType, Locale locale) {
        String errorMessage = getErrorMessage(ERROR_CODE_PREFIX + mainErrorType.value(), locale);
        String errorSolution = getErrorSolution(ERROR_CODE_PREFIX + mainErrorType.value() + ERROR_SOLUTION_SUBFIX,
                locale);
        return new SimpleMainError(mainErrorType.value(), errorMessage, errorSolution);
    }

    public static void setErrorMessageSourceAccessor(MessageSourceAccessor errorMessageSourceAccessor) {
        MainErrors.errorMessageSourceAccessor = errorMessageSourceAccessor;
    }

    private static String getErrorMessage(String code, Locale locale) {
        try {
            Assert.notNull(errorMessageSourceAccessor, "??");
            return errorMessageSourceAccessor.getMessage(code, new Object[] {}, locale);
        } catch (NoSuchMessageException e) {
            logger.error("?{}?i18n/rop/error?",
                    code);
            throw e;
        }
    }

    private static String getErrorSolution(String code, Locale locale) {
        try {
            Assert.notNull(errorMessageSourceAccessor, "?");
            return errorMessageSourceAccessor.getMessage(code, new Object[] {}, locale);
        } catch (NoSuchMessageException e) {
            logger.error("?{}?i18n/rop/error?",
                    code);
            throw e;
        }
    }

}