-
Notifications
You must be signed in to change notification settings - Fork 5
/
PersistentApplication.java
67 lines (57 loc) · 2.2 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
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.AbstractRoot;
import org.genericsystem.reactor.Model;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.RunScript;
import org.genericsystem.reactor.html.HtmlApp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PersistentApplication {
private final Class<? extends HtmlApp<?>> htmlAppClass;
private final AbstractRoot engine;
private final Class<? extends Model> modelClass;
private final String rootId;
protected static Logger log = LoggerFactory.getLogger(PersistentApplication.class);
public PersistentApplication(Class<? extends HtmlApp<?>> htmlAppClass, Class<? extends Model> modelClass, AbstractRoot 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");
}
}
public String getRootId() {
return rootId;
}
public AbstractRoot getEngine() {
return engine;
}
public Class<? extends HtmlApp<?>> getApplicationClass() {
return htmlAppClass;
}
public void close() {
engine.close();
}
@SuppressWarnings("unchecked")
public Tag<?> newHtmlApp(ServerWebSocket socket) {
try {
return ((HtmlApp<Model>) getApplicationClass().getConstructor(AbstractRoot.class, ServerWebSocket.class).newInstance(getEngine(), socket)).init(modelClass.getConstructor(AbstractRoot.class).newInstance(engine), rootId);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException(e);
}
}
}