From 8640c13260ee66a1cbfd483a176235d3f6723935 Mon Sep 17 00:00:00 2001 From: Tomas Longo Date: Wed, 21 Jan 2015 23:08:28 +0100 Subject: [PATCH] Refactoring --- .../java/de/tlongo/roscoe/core/Roscoe.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/tlongo/roscoe/core/Roscoe.java b/src/main/java/de/tlongo/roscoe/core/Roscoe.java index 3689836..d88b5e3 100644 --- a/src/main/java/de/tlongo/roscoe/core/Roscoe.java +++ b/src/main/java/de/tlongo/roscoe/core/Roscoe.java @@ -42,6 +42,7 @@ private void go() throws IllegalAccessException, InstantiationException, ClassNo externalStaticFileLocation(System.getProperty("roscoe.root")); ViewHandler viewHandler = instantiateViewHandler(configManager.getConfigItem("core", "viewhandler").asString()); + JsonArray routeArray = configManager.getConfigItem("routes", "routes").jsonElement().getAsJsonArray(); routeArray.forEach(route -> { JsonObject jsonRoute = route.getAsJsonObject(); @@ -53,25 +54,14 @@ private void go() throws IllegalAccessException, InstantiationException, ClassNo try { Class routeClass = Class.forName(routeImplementation); - RoscoeRoute roscoeRoute = (RoscoeRoute)routeClass.newInstance(); + RoscoeRoute roscoeRoute = (RoscoeRoute) routeClass.newInstance(); roscoeRoute.setMethod(routeMethod); roscoeRoute.setRouteUrl(routeUrl); roscoeRoute.setViewHandler(viewHandler); - logger.debug("Loaded Route {}", roscoeRoute); + addRouteToSpark(roscoeRoute); - if (roscoeRoute.getMethod().equals("GET")) { - get(roscoeRoute.getRouteUrl(), roscoeRoute); - } else if (roscoeRoute.getMethod().equals("POST")) { - post(roscoeRoute.getRouteUrl(), roscoeRoute); - } else if (roscoeRoute.getMethod().equals("PUT")) { - put(roscoeRoute.getRouteUrl(), roscoeRoute); - } else if (roscoeRoute.getMethod().equals("DELETE")) { - delete(roscoeRoute.getRouteUrl(), roscoeRoute); - } else { - logger.error("Could not create route for unknown request method '{}'", roscoeRoute.getMethod()); - throw new RuntimeException("Error creating routes"); - } + logger.debug("Loaded Route {}", roscoeRoute); } catch (ClassNotFoundException e) { handleClassLoadingError(routeUrl, routeImplementation); } catch (InstantiationException e) { @@ -82,10 +72,25 @@ private void go() throws IllegalAccessException, InstantiationException, ClassNo }); } + + private void addRouteToSpark(RoscoeRoute route) { + if (route.getMethod().equals("GET")) { + get(route.getRouteUrl(), route); + } else if (route.getMethod().equals("POST")) { + post(route.getRouteUrl(), route); + } else if (route.getMethod().equals("PUT")) { + put(route.getRouteUrl(), route); + } else if (route.getMethod().equals("DELETE")) { + delete(route.getRouteUrl(), route); + } else { + logger.error("Could not create route for unknown request method '{}'", route.getMethod()); + throw new RuntimeException("Error creating route from config."); + } + } private void handleClassLoadingError(String routeName, String routeImplementation) { logger.error("Could not load implementation ({}) for Route {}", routeName, routeImplementation); - throw new RuntimeException("Error loading route from config."); + throw new RuntimeException("Error creating route from config."); } private ViewHandler instantiateViewHandler(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException {