Skip to content

Commit

Permalink
Extracted route creation out of config manager into Roscoe class.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLongo committed Jan 21, 2015
1 parent 7f8176c commit 55ce5cf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 78 deletions.
63 changes: 4 additions & 59 deletions src/main/java/de/tlongo/roscoe/core/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public Integer asInteger() {
return configItem.getAsInt();
}

public JsonElement jsonElement() {
return configItem;
}

public void setItem(JsonElement item) {
configItem = item;
}
Expand All @@ -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<RoscoeRoute> getRoutes() {
final List<RoscoeRoute> 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) {
Expand All @@ -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.");
}
}
73 changes: 54 additions & 19 deletions src/main/java/de/tlongo/roscoe/core/Roscoe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();

/**
Expand All @@ -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();
}

}

0 comments on commit 55ce5cf

Please sign in to comment.