From 55ce5cfe634c11ad9e3de5365a4e8a53d9060b1a Mon Sep 17 00:00:00 2001 From: Tomas Longo Date: Wed, 21 Jan 2015 22:59:44 +0100 Subject: [PATCH 1/3] Extracted route creation out of config manager into Roscoe class. --- .../de/tlongo/roscoe/core/ConfigManager.java | 63 +--------------- .../java/de/tlongo/roscoe/core/Roscoe.java | 73 ++++++++++++++----- 2 files changed, 58 insertions(+), 78 deletions(-) diff --git a/src/main/java/de/tlongo/roscoe/core/ConfigManager.java b/src/main/java/de/tlongo/roscoe/core/ConfigManager.java index 35acf37..a6f3a16 100644 --- a/src/main/java/de/tlongo/roscoe/core/ConfigManager.java +++ b/src/main/java/de/tlongo/roscoe/core/ConfigManager.java @@ -30,6 +30,10 @@ public Integer asInteger() { return configItem.getAsInt(); } + public JsonElement jsonElement() { + return configItem; + } + public void setItem(JsonElement item) { configItem = item; } @@ -50,66 +54,12 @@ public ConfigManager() { try { configs.put("routes", new JsonParser().parse(new FileReader(configDir + "/routes.json")).getAsJsonObject()); configs.put("core", new JsonParser().parse(new FileReader(configDir + "/core.json")).getAsJsonObject()); - - viewHandler = instantiateViewHandler(getConfigItem("core", "viewhandler").asString()); } catch (FileNotFoundException e) { logger.error("Could not load routes config {}", configDir + ("/routes.json")); throw new RuntimeException("Could not load routes config."); - } catch (ClassNotFoundException e) { - logger.error("Could not find ViewHandler implemenation.", e); - throw new RuntimeException("Could not load viewhandler"); - } catch (InstantiationException e) { - logger.error("Could not instantiate ViewHandler implemenation.", e); - throw new RuntimeException("Could not load viewhandler"); - } catch (IllegalAccessException e) { - logger.error("Could not instantiate ViewHandler implemenation.", e); - throw new RuntimeException("Could not load viewhandler"); } } - private ViewHandler instantiateViewHandler(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { - logger.debug("loading viewhandler {}", className); - Class viewHandlerClass = Class.forName(className); - return (ViewHandler)viewHandlerClass.newInstance(); - } - - public List getRoutes() { - final List routes = new ArrayList<>(); - JsonArray jsonRoutesArray = configs.get("routes").getAsJsonArray("routes"); - - logger.debug("Loading Routes from config..."); - jsonRoutesArray.forEach(route -> { - JsonObject jsonRoute = route.getAsJsonObject(); - - String routeUrl = jsonRoute.get("route").getAsString(); - String routeImplementation = jsonRoute.get("implementation").getAsString(); - String routeMethod = jsonRoute.get("method").getAsString(); - - try { - Class routeClass = Class.forName(routeImplementation); - - RoscoeRoute roscoeRoute = (RoscoeRoute)routeClass.newInstance(); - roscoeRoute.setMethod(routeMethod); - roscoeRoute.setRouteUrl(routeUrl); - roscoeRoute.setViewHandler(viewHandler); - - logger.debug("Loaded Route {}", roscoeRoute); - - routes.add(roscoeRoute); - } catch (ClassNotFoundException e) { - handleClassLoadingError(routeUrl, routeImplementation); - } catch (InstantiationException e) { - handleClassLoadingError(routeUrl, routeImplementation); - } catch (IllegalAccessException e) { - handleClassLoadingError(routeUrl, routeImplementation); - } - - }); - logger.debug("Found {} routes in config", routes.size()); - - return routes; - } - public ConfigItem getConfigItem(String configName, String key) { JsonObject config = configs.getOrDefault(configName, null); if (config == null) { @@ -121,9 +71,4 @@ public ConfigItem getConfigItem(String configName, String key) { return configItem; } - - 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."); - } } diff --git a/src/main/java/de/tlongo/roscoe/core/Roscoe.java b/src/main/java/de/tlongo/roscoe/core/Roscoe.java index bc76c80..3689836 100644 --- a/src/main/java/de/tlongo/roscoe/core/Roscoe.java +++ b/src/main/java/de/tlongo/roscoe/core/Roscoe.java @@ -4,6 +4,8 @@ * Created by tomas on 02.01.15. */ +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,7 +15,7 @@ public class Roscoe { Logger logger = LoggerFactory.getLogger(Roscoe.class); - public static void main(String[] args) { + public static void main(String[] args) throws IllegalAccessException, ClassNotFoundException, InstantiationException { new Roscoe().go(); /** @@ -29,34 +31,67 @@ public static void main(String[] args) { // // 2. Create the routes // a. For every route in the config, create a spark-route and register it. - - - - } - private void go() { + private void go() throws IllegalAccessException, InstantiationException, ClassNotFoundException { File file = new File("."); logger.debug("Roscoe Root at: {}", file.toPath().toAbsolutePath().toString()); System.setProperty("roscoe.root", file.toPath().toAbsolutePath().toString()); ConfigManager configManager = new ConfigManager(); - externalStaticFileLocation(System.getProperty("roscoe.root")); - configManager.getRoutes().forEach(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 routes"); + ViewHandler viewHandler = instantiateViewHandler(configManager.getConfigItem("core", "viewhandler").asString()); + JsonArray routeArray = configManager.getConfigItem("routes", "routes").jsonElement().getAsJsonArray(); + routeArray.forEach(route -> { + JsonObject jsonRoute = route.getAsJsonObject(); + + String routeUrl = jsonRoute.get("route").getAsString(); + String routeImplementation = jsonRoute.get("implementation").getAsString(); + String routeMethod = jsonRoute.get("method").getAsString(); + + try { + Class routeClass = Class.forName(routeImplementation); + + RoscoeRoute roscoeRoute = (RoscoeRoute)routeClass.newInstance(); + roscoeRoute.setMethod(routeMethod); + roscoeRoute.setRouteUrl(routeUrl); + roscoeRoute.setViewHandler(viewHandler); + + logger.debug("Loaded Route {}", 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"); + } + } catch (ClassNotFoundException e) { + handleClassLoadingError(routeUrl, routeImplementation); + } catch (InstantiationException e) { + handleClassLoadingError(routeUrl, routeImplementation); + } catch (IllegalAccessException e) { + handleClassLoadingError(routeUrl, routeImplementation); } + }); } + + 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."); + } + + private ViewHandler instantiateViewHandler(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { + logger.debug("loading viewhandler {}", className); + Class viewHandlerClass = Class.forName(className); + return (ViewHandler)viewHandlerClass.newInstance(); + } + } From 8640c13260ee66a1cbfd483a176235d3f6723935 Mon Sep 17 00:00:00 2001 From: Tomas Longo Date: Wed, 21 Jan 2015 23:08:28 +0100 Subject: [PATCH 2/3] 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 { From 1fbc46ea1bc84480536c9d04af0e95003ba94947 Mon Sep 17 00:00:00 2001 From: Tomas Longo Date: Wed, 21 Jan 2015 23:14:40 +0100 Subject: [PATCH 3/3] Documentation --- .../java/de/tlongo/roscoe/core/Roscoe.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/tlongo/roscoe/core/Roscoe.java b/src/main/java/de/tlongo/roscoe/core/Roscoe.java index d88b5e3..903ab43 100644 --- a/src/main/java/de/tlongo/roscoe/core/Roscoe.java +++ b/src/main/java/de/tlongo/roscoe/core/Roscoe.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.nio.file.Path; import static spark.Spark.*; public class Roscoe { @@ -17,32 +18,24 @@ public class Roscoe { public static void main(String[] args) throws IllegalAccessException, ClassNotFoundException, InstantiationException { new Roscoe().go(); - - /** - * Start the web app by reading the config and creating the routes. - */ - - // 1. Determine the framework route and export to a property - // a. if none is specified in the config, determine it by yourself - // b. create and store paths to folders under the fw root - // - templates - // - views - // - assets - // - // 2. Create the routes - // a. For every route in the config, create a spark-route and register it. } + /** + * Initialises Roscoe and fires up the web server + */ private void go() throws IllegalAccessException, InstantiationException, ClassNotFoundException { - File file = new File("."); - logger.debug("Roscoe Root at: {}", file.toPath().toAbsolutePath().toString()); - System.setProperty("roscoe.root", file.toPath().toAbsolutePath().toString()); + Path roscoeRoot = new File(".").toPath().toAbsolutePath(); + logger.debug("Roscoe Root at: {}", roscoeRoot.toString()); + System.setProperty("roscoe.root", roscoeRoot.toString()); ConfigManager configManager = new ConfigManager(); + + // NOTE: According to the Spark doc this method has to be called before any other + // Spark method 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(); @@ -72,7 +65,10 @@ private void go() throws IllegalAccessException, InstantiationException, ClassNo }); } - + + /** + * Adds a RoscoeRoute to Spark based on the request method + */ private void addRouteToSpark(RoscoeRoute route) { if (route.getMethod().equals("GET")) { get(route.getRouteUrl(), route); @@ -93,6 +89,10 @@ private void handleClassLoadingError(String routeName, String routeImplementatio throw new RuntimeException("Error creating route from config."); } + /** + * Instantiates a view handler denoted by a classname + * @param className The qualified name of the class to instantiate + */ private ViewHandler instantiateViewHandler(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { logger.debug("loading viewhandler {}", className); Class viewHandlerClass = Class.forName(className);