cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Spring and JPA CCO

xsalgadog
Participant
0 Likes
318

Hi,

Is it possible to use Spring boot and jpa java with plugin customizations, in some version of SCCO?

Accepted Solutions (0)

Answers (1)

Answers (1)

R_Zieschang
Active Contributor
0 Likes

Hi xsalgadog,

this is not possible, because CCO wont scan your packages and components. The spring component scanning happens far far prior to loading your plugins. I once implemented some kind of rough package scanning myself.

The exception handler factory scans for all classes in my namespace that extends my FeignHttpExceptionHandler interface, creates the instance and you could use the getHandler Method to get the specific Exception handler. You can adapt this so it scans e.g. Spring Annotations or your own ones.

But this is very basic, but can be extended to fit your needs.

Regards
Robert

public interface FeignHttpExceptionHandler {

    Logger log = Logger.getLogger(FeignHttpExceptionHandler.class);

    Exception handle(Response response);<br>}
public class ForbiddenException extends Exception {    <br>   private static final Logger log = Logger.getLogger(ForbiddenException.class);    <br>   public ForbiddenException(int status, String reason) {        <br>      log.severe("Error " + status + " : Reason: " + reason);    <br>   }<br>}
public class ExceptionHandlerFactory {

    private static final Logger log = Logger.getLogger(ExceptionHandlerFactory.class);

    private final Map<String, Object> beanMap;

    private static ExceptionHandlerFactory _INSTANCE;

    public static ExceptionHandlerFactory getInstance() {
        if (_INSTANCE == null) {
            _INSTANCE = new ExceptionHandlerFactory();
        }
        return _INSTANCE;
    }

    private ExceptionHandlerFactory() {
        beanMap = new HashMap<>();
        Reflections reflections = new Reflections("de.hokona.cco.plugin", MainClass.class.getClassLoader(), new SubTypesScanner(false));
        Set<Class<? extends FeignHttpExceptionHandler>> handlers = reflections.getSubTypesOf(FeignHttpExceptionHandler.class);
        for (Class<?> handler : handlers) {
            try {
                Object instance = handler.getConstructor().newInstance();
                beanMap.put(handler.getName(), instance);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                log.severe("Error creating instance of " + handler.getName());
            }
        }
    }

    public <T> T getHandler(Class<T> handler) {
        for (Map.Entry<String, Object> entry : beanMap.entrySet()) {
            if (entry.getValue().getClass() == handler) {
                return (T) entry.getValue();
            }
        }
        return null;
    }
}