2015-07-24 11:32:22 +03:00
|
|
|
package com.iluwatar.front.controller;
|
2015-07-19 12:44:51 +03:00
|
|
|
|
2015-07-19 13:54:07 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* FrontController is the handler class that takes in all the requests and
|
|
|
|
* renders the correct response.
|
|
|
|
*
|
|
|
|
*/
|
2015-07-19 12:44:51 +03:00
|
|
|
public class FrontController {
|
|
|
|
|
|
|
|
public void handleRequest(String request) {
|
|
|
|
Command command = getCommand(request);
|
|
|
|
command.process();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Command getCommand(String request) {
|
|
|
|
Class commandClass = getCommandClass(request);
|
|
|
|
try {
|
|
|
|
return (Command) commandClass.newInstance();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ApplicationException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Class getCommandClass(String request) {
|
|
|
|
Class result;
|
|
|
|
try {
|
|
|
|
result = Class.forName("com.iluwatar." + request + "Command");
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
result = UnknownCommand.class;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|