Skip to content
This repository has been archived by the owner on Mar 10, 2019. It is now read-only.

Commit

Permalink
Not recreating handler for every request
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Mar 11, 2018
1 parent 28dc4fb commit 01eb7e1
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/main/java/com/gianlu/pyxreloaded/paths/AjaxPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.Map;

public class AjaxPath extends BaseCahHandler {
private final Map<Class<? extends BaseHandler>, BaseHandler> handlers = new HashMap<>();

@Override
protected JsonElement handleRequest(@Nullable String op, @Nullable User user, Parameters params, HttpServerExchange exchange) throws StatusException {
Expand All @@ -25,17 +28,21 @@ protected JsonElement handleRequest(@Nullable String op, @Nullable User user, Pa
BaseHandler handler;
Class<? extends BaseHandler> cls = Handlers.LIST.get(op);
if (cls != null) {
try {
Constructor<?> constructor = cls.getConstructors()[0];
Parameter[] parameters = constructor.getParameters();
Object[] objects = new Object[parameters.length];

for (int i = 0; i < parameters.length; i++)
objects[i] = Providers.get(parameters[i].getAnnotations()[0].annotationType()).get();

handler = (BaseHandler) constructor.newInstance(objects);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
throw new CahException(Consts.ErrorCode.BAD_OP, ex);
handler = handlers.get(cls);
if (handler == null) {
try {
Constructor<?> constructor = cls.getConstructors()[0];
Parameter[] parameters = constructor.getParameters();
Object[] objects = new Object[parameters.length];

for (int i = 0; i < parameters.length; i++)
objects[i] = Providers.get(parameters[i].getAnnotations()[0].annotationType()).get();

handler = (BaseHandler) constructor.newInstance(objects);
handlers.put(cls, handler);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
throw new CahException(Consts.ErrorCode.BAD_OP, ex);
}
}
} else {
throw new CahException(Consts.ErrorCode.BAD_OP);
Expand Down

0 comments on commit 01eb7e1

Please sign in to comment.