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..903ab43 100644 --- a/src/main/java/de/tlongo/roscoe/core/Roscoe.java +++ b/src/main/java/de/tlongo/roscoe/core/Roscoe.java @@ -4,59 +4,99 @@ * 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; import java.io.File; +import java.nio.file.Path; import static spark.Spark.*; 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(); + } - /** - * Start the web app by reading the config and creating the routes. - */ + /** + * Initialises Roscoe and fires up the web server + */ + private void go() throws IllegalAccessException, InstantiationException, ClassNotFoundException { + Path roscoeRoot = new File(".").toPath().toAbsolutePath(); + logger.debug("Roscoe Root at: {}", roscoeRoot.toString()); + System.setProperty("roscoe.root", roscoeRoot.toString()); - // 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. + 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(); - } + String routeUrl = jsonRoute.get("route").getAsString(); + String routeImplementation = jsonRoute.get("implementation").getAsString(); + String routeMethod = jsonRoute.get("method").getAsString(); - private void go() { - File file = new File("."); - logger.debug("Roscoe Root at: {}", file.toPath().toAbsolutePath().toString()); - System.setProperty("roscoe.root", file.toPath().toAbsolutePath().toString()); + try { + Class routeClass = Class.forName(routeImplementation); - ConfigManager configManager = new ConfigManager(); + RoscoeRoute roscoeRoute = (RoscoeRoute) routeClass.newInstance(); + roscoeRoute.setMethod(routeMethod); + roscoeRoute.setRouteUrl(routeUrl); + roscoeRoute.setViewHandler(viewHandler); - externalStaticFileLocation(System.getProperty("roscoe.root")); + addRouteToSpark(roscoeRoute); - 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"); + logger.debug("Loaded Route {}", roscoeRoute); + } catch (ClassNotFoundException e) { + handleClassLoadingError(routeUrl, routeImplementation); + } catch (InstantiationException e) { + handleClassLoadingError(routeUrl, routeImplementation); + } catch (IllegalAccessException e) { + handleClassLoadingError(routeUrl, routeImplementation); } + }); } + + /** + * Adds a RoscoeRoute to Spark based on the request method + */ + 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 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); + return (ViewHandler)viewHandlerClass.newInstance(); + } + }