-
Notifications
You must be signed in to change notification settings - Fork 5
/
PersistentApplication.java
78 lines (68 loc) · 2.57 KB
/
PersistentApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.genericsystem.reactor.appserver;
import io.vertx.core.http.ServerWebSocket;
import java.lang.reflect.InvocationTargetException;
import org.genericsystem.common.AbstractCache;
import org.genericsystem.common.Root;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.HtmlDomNode.RootHtmlDomNode;
import org.genericsystem.reactor.RootTag;
import org.genericsystem.reactor.annotations.RunScript;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PersistentApplication {
private final Class<? extends RootTag> htmlAppClass;
private RootTag tagTree;
private final Root engine;
private final Class<Context> modelClass;
private final String rootId;
protected static Logger log = LoggerFactory.getLogger(PersistentApplication.class);
public PersistentApplication(Class<? extends RootTag> htmlAppClass, Class<Context> modelClass, Root engine, String rootId) {
this.htmlAppClass = htmlAppClass;
this.modelClass = modelClass;
this.engine = engine;
this.rootId = rootId;
RunScript script = getApplicationClass().getAnnotation(RunScript.class);
if (script != null) {
AbstractCache cache = engine.newCache();
log.info("Script will run ...");
Script runner;
try {
runner = script.value().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
cache.safeConsum(unused -> runner.run(getEngine()));
log.info("Script has run");
}
try {
tagTree = getApplicationClass().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | SecurityException e) {
try {
engine.newCache().start();
tagTree = getApplicationClass().getConstructor(Root.class).newInstance(engine);
engine.getCurrentCache().flush();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | SecurityException | InvocationTargetException | NoSuchMethodException ex) {
throw new IllegalStateException(ex);
}
}
}
public String getRootId() {
return rootId;
}
public Root getEngine() {
return engine;
}
public Class<? extends RootTag> getApplicationClass() {
return htmlAppClass;
}
public void close() {
engine.close();
}
public RootHtmlDomNode init(ServerWebSocket socket) {
try {
return tagTree.init(modelClass.getConstructor(Root.class).newInstance(engine), rootId, socket);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException(e);
}
}
}