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(); + } + }