/* ==================================================================
* Created [2009-4-27 下午11:32:55] by Jon.King
* ==================================================================
* TSS
* ==================================================================
* mailTo:jinpujun@hotmail.com
* Copyright (c) Jon.King, 2009-2012
* ==================================================================
*/
package com.jinhe.tss.core.exception.convert;
import java.util.ArrayList;
import java.util.List;
import com.jinhe.tss.core.Config;
import com.jinhe.tss.core.sso.SSOConstants;
import com.jinhe.tss.core.util.BeanUtil;
/**
* <p> ExceptionConvertorFactory.java </p>
* <p>
* 异常转换器工厂类
* </p>
*/
public class ExceptionConvertorFactory {
private static IExceptionConvertor convertor = null;
public static IExceptionConvertor getConvertor() {
if (convertor == null) {
String className = Config.getAttribute(SSOConstants.EXCEPTION_CONVERTOR);
if (className != null) {
String[] classNames = className.split(",");
if (classNames.length > 1) {
convertor = new ArrayExceptionConvertor(classNames);
} else {
convertor = (IExceptionConvertor) BeanUtil.newInstanceByName(classNames[0]);
}
}
if (convertor == null) {
convertor = new FaineantExceptionConvertor();
}
}
return convertor;
}
}
/**
* <p> FaineantExceptionConvertor </p>
*
* <p>
* 默认异常转换器:不做任何转换处理
* </p>
*/
class FaineantExceptionConvertor implements IExceptionConvertor {
public Exception execute(Exception exception) {
return exception;
}
}
/**
* <p> ArrayExceptionConvertor </p>
* <p>
* 列表异常转换器:处理多异常转换器
* </p>
*/
class ArrayExceptionConvertor implements IExceptionConvertor {
/**
* 转换器列表
*/
private List<Object> convertors;
/**
* 转换器器类名数组
*/
private String[] classNames;
/**
* 构造函数
* @param classNames
*/
public ArrayExceptionConvertor(String[] classNames) {
this.classNames = classNames;
}
public Exception execute(Exception exception) {
if (convertors == null) {
init();
}
for (Object item : convertors) {
if (item != null && item instanceof IExceptionConvertor) {
IExceptionConvertor convertor = (IExceptionConvertor) item;
Exception e = convertor.execute(exception);
if (e != exception) {
return e;
}
}
}
return exception;
}
/**
* <p>
* 初始化异常转换器列表
* </p>
*/
private void init() {
if (classNames == null) return;
convertors = new ArrayList<Object>();
for (int i = 0; i < classNames.length; i++) {
String className = classNames[i];
if (className != null && !"".equals(className)) {
Object convertor = BeanUtil.newInstanceByName(className);
if (convertor != null) {
convertors.add(convertor);
}
}
}
}
}