ResourceProperties

public class ResourceProperties {
    private static HashMap hashInstance = new HashMap();

    private Properties propertiesInstance = new Properties();
    private String sFileName = "";
    private String sFilePath = "";

 public static ResourceProperties getResourceFile(String psResourceFile,Locale locale) throws Exception{
        String lsFileKey = psResourceFile;
        if (null != locale)
            lsFileKey = psResourceFile + "_" + locale.getLanguage();
        if (hashInstance.containsKey(lsFileKey)) {
            return (ResourceProperties) hashInstance.get(lsFileKey);
        } else {
            ResourceProperties lrp = new ResourceProperties();
            lrp.load(lsFileKey);
            hashInstance.put(lsFileKey,lrp);
            return lrp;
        }
    }

public static ResourceProperties reload(String psResourceFile) throws Exception{
        if (hashInstance.containsKey(psResourceFile)) {
            ResourceProperties lrp = (ResourceProperties) hashInstance.get(psResourceFile);
            lrp.load(psResourceFile);
            return lrp;
        } else {
            ResourceProperties lrp = new ResourceProperties();
            lrp.load(psResourceFile);
            hashInstance.put(psResourceFile,lrp);
            return lrp;
        } }
public void load(String psResourceFile) throws Exception {
  try {
            String lsFile       = psResourceFile.replace('.', '/') + ".properties";
            URL urlResource     = this.getClass().getClassLoader().getResource(lsFile);
            sFileName           = urlResource.getFile();
            File file           = new File(sFileName);
            sFilePath           = file.getParentFile().getPath();
            InputStream stream  = urlResource.openStream();
            propertiesInstance.clear();
   propertiesInstance.load(stream);
            stream.close();
  } catch (Exception e) {
            //BaseUtil.printStackTrace(e);
            throw new Exception(e);
  }
    }

/** 获取配置中的一个键值,若没有则用缺省值 */
    public String get(String key, String defaultValue) {
        return propertiesInstance.getProperty(key,defaultValue);
    }

    /** 获取配置中的一个键值,若没有则返回null */
    public String get(String key) {
        return propertiesInstance.getProperty(key);
    }

    /** 设置配置中的一个键值 */
    public void set(String psKey,String psValue) {
        propertiesInstance.setProperty(psKey,psValue);
    }

    /** 保存资源文件 */
    public void save(String psUser)  throws Exception {
        try{
            OutputStream stream = new FileOutputStream(sFileName);
            propertiesInstance.store(stream,"Resource file last update by " + psUser);
            stream.close();
        } catch(Exception e) {
            //BaseUtil.printStackTrace(e);
            throw new  Exception(e);
        }
    }

    public Properties getProperties() {
        return this.propertiesInstance;
    }



package com.bolt.gateway.handler; import com.bolt.gateway.provider.ResponseProvider; import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler; import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.cloud.gateway.support.NotFoundException; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.server.*; import org.springframework.web.server.ResponseStatusException; import java.util.Map; /** * 异常处理 * * @author arch_group */ public class ErrorExceptionHandler extends DefaultErrorWebExceptionHandler { public ErrorExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) { super(errorAttributes, resourceProperties, errorProperties, applicationContext); } /** * 获取异常属性 */ @Override protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) { int code = 500; Throwable error = super.getError(request); if (error instanceof NotFoundException) { code = 404; } if (error instanceof ResponseStatusException) { code = ((ResponseStatusException) error).getStatus().value(); } return ResponseProvider.response(code, this.buildMessage(request, error)); } /** * 指定响应处理方法为JSON处理的方法 * * @param errorAttributes */ @Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); } /** * 根据code获取对应的HttpStatus * * @param errorAttributes */ @Override protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) { int statusCode = (int) errorAttributes.get("code"); return HttpStatus.valueOf(statusCode); } /** * 构建异常信息 * * @param request * @param ex * @return */ private String buildMessage(ServerRequest request, Throwable ex) { StringBuilder message = new StringBuilder("Failed to handle request ["); message.append(request.methodName()); message.append(" "); message.append(request.uri()); message.append("]"); if (ex != null) { message.append(": "); message.append(ex.getMessage()); } return message.toString(); } }
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值