-
Notifications
You must be signed in to change notification settings - Fork 5
/
WebAppsConfig.java
155 lines (126 loc) · 5.43 KB
/
WebAppsConfig.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.genericsystem.reactor.appserver;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.genericsystem.common.EnginesDeploymentConfig.EngineDeploymentConfig;
import org.genericsystem.common.Root;
import org.genericsystem.common.Statics;
import org.genericsystem.kernel.Engine;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.RootTag;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.DependsOnModel;
import org.genericsystem.reactor.gscomponents.RootTagImpl;
import org.genericsystem.reactor.model.RootContext;
import io.vertx.core.json.JsonObject;
/**
* @author Nicolas Feybesse
*
*/
public class WebAppsConfig extends JsonObject {
public WebAppsConfig() {
this(Statics.DEFAULT_PORT);
}
public WebAppsConfig(String[] mainArgs) {
this(mainArgs.length != 0 ? Integer.parseInt(mainArgs[0]) : Statics.DEFAULT_PORT);
}
public WebAppsConfig(int port) {
put("apps", new JsonObject());
put("host", Statics.DEFAULT_HOST);
put("port", port);
}
public String getHost() {
return getString("host");
}
public int getPort() {
return getInteger("port");
}
public Set<String> getApplicationsPaths() {
return getJsonObject("apps").getMap().keySet();
}
public Class<? extends RootTag> getApplicationClass(String applicationPath) {
return getApplicationDeploymentConfig(applicationPath).getApplicationClass();
}
public Class<Context> getModelClass(String applicationPath) {
return getApplicationDeploymentConfig(applicationPath).getModelClass();
}
private ApplicationDeploymentConfig getApplicationDeploymentConfig(String applicationPath) {
return applicationDeploymentConfig(getJsonObject("apps").getJsonObject(applicationPath));
}
private ApplicationDeploymentConfig applicationDeploymentConfig(JsonObject json) {
return new ApplicationDeploymentConfig(json.getMap());
}
public static Class<?>[] getModelClasses(Class<?> applicationClass) {
DependsOnModel dependOn = applicationClass.getAnnotation(DependsOnModel.class);
return dependOn != null ? dependOn.value() : new Class[] {};
}
public WebAppsConfig addApplication(String path, Class<? extends Tag> htmlAppClass, Class<? extends Context> modelClass, Class<? extends Root> engineClass, String persistentDirectoryPath) {
getJsonObject("apps").put(path, new ApplicationDeploymentConfig(htmlAppClass, modelClass, engineClass, persistentDirectoryPath, getModelClasses(htmlAppClass)));
return this;
}
public void removeApplication(String path) {
getJsonObject("apps").remove(path);
}
public Set<Class<?>> getClasses(String persistentDirectoryPath) {
return getJsonObject("apps").getMap().values().stream().map(json -> applicationDeploymentConfig((JsonObject) json)).filter(conf -> Objects.equals(persistentDirectoryPath, conf.getPersistentDirectoryPath()))
.flatMap(conf -> conf.getClasses().stream()).collect(Collectors.toSet());
}
public Class<? extends Root> getEngineClass(String persistentDirectoryPath) {
Set<Class<? extends Root>> set = getJsonObject("apps").getMap().values().stream().map(json -> applicationDeploymentConfig((JsonObject) json)).filter(conf -> Objects.equals(persistentDirectoryPath, conf.getPersistentDirectoryPath()))
.map(conf -> conf.getEngineClass()).collect(Collectors.toSet());
assert set.size() == 1;
return set.iterator().next();
}
public Set<String> getPersistentDirectoryPaths() {
return getJsonObject("apps").getMap().keySet().stream().map(this::getPersistentDirectoryPath).collect(Collectors.toSet());
}
public String getPersistentDirectoryPath(String applicationPath) {
return getApplicationDeploymentConfig(applicationPath).getPersistentDirectoryPath();
}
public static class ApplicationDeploymentConfig extends EngineDeploymentConfig {
public ApplicationDeploymentConfig(Map<String, Object> map) {
super(map);
assert getString("applicationClass") != null;
}
public ApplicationDeploymentConfig(Class<? extends Tag> applicationClass, Class<? extends Context> modelClass, Class<? extends Root> engineClass, String repositoryPath, Class<?>... classes) {
super(repositoryPath, classes);
put("applicationClass", applicationClass.getName());
put("modelClass", modelClass.getName());
put("engineClass", engineClass.getName());
}
@SuppressWarnings("unchecked")
public Class<? extends RootTag> getApplicationClass() {
try {
return (Class<? extends RootTag>) Class.forName(getString("applicationClass"));
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
@SuppressWarnings("unchecked")
public Class<? extends Root> getEngineClass() {
try {
return (Class<? extends Root>) Class.forName(getString("engineClass"));
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
@SuppressWarnings("unchecked")
public Class<Context> getModelClass() {
try {
return (Class<Context>) Class.forName(getString("modelClass"));
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
}
public static class SimpleWebAppConfig extends WebAppsConfig {
public SimpleWebAppConfig(String[] mainArgs, Class<? extends RootTagImpl> htmlAppClass, String homePersistentDirectoryPath) {
super(mainArgs);
addApplication("/", htmlAppClass, RootContext.class, Engine.class, System.getenv("HOME") + "/genericsystem/" + homePersistentDirectoryPath);
}
}
public String getRootId() {
return "root";
}
}