org.ioc.commons.flowcontrol.exceptionparser.ExceptionParserManagerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ioc-commons-ext Show documentation
Show all versions of ioc-commons-ext Show documentation
Library which contains some extension classes for the library IOC-Commons.
It provides some interface definitions and some tool classes which are non-dependent from the used technology;
i.e. the classes and interfaces from this library do not depend on the choosen ioc-commons-implementation library.
package org.ioc.commons.flowcontrol.exceptionparser;
import java.util.HashMap;
import java.util.Map;
public class ExceptionParserManagerImpl implements ExceptionParserManager {
private final Map, ExceptionParserManager.ExceptionParser>> registered = new HashMap, ExceptionParserManager.ExceptionParser>>();
@Override
public void registerExceptionParser(Class clazz,
ExceptionParserManager.ExceptionParser parser) throws ExceptionParserAlreadyRegistered {
if (!registered.containsKey(clazz)) {
registered.put(clazz, parser);
} else {
throw new ExceptionParserAlreadyRegistered();
}
}
@Override
public void unregisterExceptionParser(Class exceptionClass) {
registered.remove(exceptionClass);
}
@SuppressWarnings("unchecked")
@Override
public EX fromMessage(Class clazz, String message) {
ExceptionParserManager.ExceptionParser> parser = registered.get(clazz);
if (parser != null) {
return (EX) parser.fromMessage(message);
}
return null;
}
public boolean tryHandleException(Throwable e) {
boolean handled = false;
for (ExceptionParserManager.ExceptionParser> parser : this.registered.values()) {
handled |= parser.tryHandleException(e);
if (handled) {
break;
}
}
return handled;
}
}