走自己的路

路漫漫其修远兮,吾将上下而求索

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  11 随笔 :: 4 文章 :: 36 评论 :: 0 Trackbacks

应用项目大致的体系结构:

    
 

 该异常处理框架满足的要求:

 

  • 完整的异常组织结构
  • 异常的统一处理
  • 可配置,受管式,方便使用

 

完整的异常组织结构:

  • 用户可以方便的定义自己的异常,但所有UncheckedException需要继承BaseAppRuntimeException,所有的checked Exception可以继承BaseAppException,或者需要抛出且不需要check时用WrapperredAppException封装后抛出
  • 合理地使用checked异常
  • Exception有唯一的error code,这样用户报告异常后,可以根据异常号找到相应Exception,把exception直接显示给用户也没有太大的意义,如何纪录exception那就是下文讲到的ExceptionHandler的职责了。
  • 如果是第三方包括jdk中的异常,需要封装成BaseAppException或者BaseAppRuntimeException后抛出

                                     

   

 

统一的异常处理

异常统一在框架中进行处理,不需要在上层应用的代码中去处理抛出的异常。为了尽量捕捉到所有的异常,将异常处理放在了ActionBroker中,这样凡是action以后抛出的异常都可以捕捉到,因为webservice只是简单的调用action类的方法,一般不会出现异常。当我们捕捉到异常后,需要进行异常处理,定义了ExceptionHandler接口,用接口抽象出异常处理类的具体实现。

 


                        
 

USFContextFactory: 创建ExceptionContext的工厂

 1package com.ldd600.exception.context;
 2
 3public class CoreContextFactory {
 4    private static CoreContextFactory instance;
 5
 6    private volatile ExceptionContext exceptionContext;
 7
 8    private Object exceptionContextLock = new Object();
 9
10    private CoreContextFactory() {
11
12    }

13
14    public static synchronized CoreContextFactory getInstance() {
15        if (null == instance) {
16            instance = new CoreContextFactory();
17        }

18        return instance;
19    }

20
21    public ExceptionContext getExceptionContext() {
22        ExceptionContext tempExpContext = exceptionContext;
23        if (tempExpContext == null
24            synchronized (exceptionContextLock) {
25                tempExpContext = exceptionContext;
26                if (tempExpContext == null)
27                    exceptionContext = tempExpContext = new ExceptionContext();
28            }

29        }

30        return tempExpContext;
31    }

32}

33



   

ExceptionContext: 存放全局的exception信息

 

  1package com.ldd600.exception.context;
  2
  3import java.util.ArrayList;
  4import java.util.Collection;
  5import java.util.Collections;
  6import java.util.HashMap;
  7import java.util.List;
  8import java.util.Map;
  9import java.util.Set;
 10
 11import org.springframework.util.Assert;
 12
 13import com.ldd600.exception.base.BaseAppRuntimeException;
 14import com.ldd600.exception.base.ConfigException;
 15import com.ldd600.exception.base.handler.ExceptionHandler;
 16import com.ldd600.exception.config.ExceptionDefinition;
 17
 18public class ExceptionContext {
 19    private Map<Class<?>, ExceptionDefinition> exceptionMap;
 20
 21    private Map<String, ExceptionHandler> handlers = new HashMap<String, ExceptionHandler>();
 22
 23    ExceptionContext() {
 24        exceptionMap = new HashMap<Class<?>, ExceptionDefinition>();
 25    }

 26
 27    public boolean containsException(Class<?> expClazz) {
 28        return (exceptionMap.containsKey(expClazz));
 29    }

 30    
 31    public void addExceptionHander(Class<?> expClazz, Class<? extends ExceptionHandler> handlerClazz) {
 32        try {
 33            ExceptionDefinition definition = getRealExceptionDefinition(expClazz);
 34            if (null == definition) {
 35                throw new IllegalArgumentException(expClazz.getName() + "not in the context, please configure or add it to the context first!!");
 36            }
 
 37            ExceptionHandler handler = handlers.get(handlerClazz.getName());
 38            if (null == handler) {
 39                handler = handlerClazz.newInstance();
 40                handlers.put(handlerClazz.getName(), handler);
 41            }

 42            
 43            definition.getHandlerNames().add(handlerClazz.getName());
 44        }
 catch (Exception ex) {
 45            throw new ConfigException("Add exception handler to context failure!", ex);
 46        }

 47    }

 48    
 49    public void addExceptionHandler(Class<?> expClazz, String errorCode, Class<? extends ExceptionHandler> handlerClazz) {
 50        Assert.hasLength(errorCode, expClazz + " errorCode must not be null or empty string!");
 51        ExceptionDefinition definition = getRealExceptionDefinition(expClazz);
 52        if(null == definition) {
 53            definition = new ExceptionDefinition(errorCode);
 54            exceptionMap.put(expClazz, definition);
 55        }

 56        addExceptionHander(expClazz, handlerClazz);
 57    }

 58    
 59    
 60    
 61    public void addExceptionHandlers(Class<?> expClazz, Class<? extends ExceptionHandler> handlerClazzes) {
 62        for(Class<? extends ExceptionHandler> handlerClazz : handlerClazzes) {
 63            addExceptionHander(expClazz, handlerClazz);
 64        }

 65    }

 66
 67    public void removeExceptionHandler(Class<?> expClazz, Class<? extends ExceptionHandler> handlerClazz) {
 68        Assert.isTrue(containsException(expClazz));
 69        String handlerName = handlerClazz.getName();
 70        getExceptionDefinition(expClazz).getHandlerNames().remove(handlerName);
 71        Collection<ExceptionDefinition> definitons = exceptionMap.values();
 72        boolean isClearHandler = true;
 73        for (ExceptionDefinition expDefinition : definitons) {
 74            if (expDefinition.getHandlerNames().contains(handlerName)) {
 75                isClearHandler = false;
 76                break;
 77            }

 78        }

 79
 80        if (isClearHandler) {
 81            handlers.remove(handlers.get(handlerName));
 82        }

 83    }

 84
 85    public void setExceptionDefinition(Class<?> expClazz, ExceptionDefinition definition) {
 86        exceptionMap.put(expClazz, definition);
 87    }

 88
 89    public ExceptionDefinition getExceptionDefinition(Class<?> expClazz) {
 90        if (containsException(expClazz)) {
 91            return exceptionMap.get(expClazz);  
 92        }
 else if (BaseAppRuntimeException.class.isAssignableFrom(expClazz.getSuperclass())) {
 93            return getExceptionDefinition(expClazz.getSuperclass());
 94        }
 else {
 95            return null;
 96        }

 97    }

 98    
 99    public ExceptionDefinition getRealExceptionDefinition(Class<?> expClazz) {
100        return exceptionMap.get(expClazz);
101    }

102
103    public List<ExceptionHandler> getExceptionHandlers(Class<?> expClazz){
104        ExceptionDefinition definition = getExceptionDefinition(expClazz);
105        if (null != definition) {
106            Set<String> handlerNames = definition.getHandlerNames();
107            List<ExceptionHandler> handlerList = new ArrayList<ExceptionHandler>(handlerNames.size());
108            for (String handlerName : handlerNames) {
109                ExceptionHandler handler = handlers.get(handlerName);
110                handlerList.add(handler);
111            }

112            List<ExceptionHandler> resultHandlerList = new ArrayList<ExceptionHandler>(handlerList);
113            return resultHandlerList;
114        }
 else {
115            return Collections.<ExceptionHandler> emptyList();
116        }

117    }

118    
119    public String getErrorCode(Class<?> expClazz){
120        ExceptionDefinition definition = getExceptionDefinition(expClazz);
121        if (null != definition) {
122            return definition.getErrorCode();
123        }
 else {
124            return "";
125        }

126    }

127    
128    
129}

130
 

ExceptionDefinition: Exception信息单元

 

 1package com.ldd600.exception.config;
 2
 3import java.util.LinkedHashSet;
 4import java.util.Set;
 5
 6public class ExceptionDefinition {
 7    private String errorCode;
 8
 9    private Set<String> handlerNames = new LinkedHashSet<String> ();
10