-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
436 lines (335 loc) · 15.1 KB
/
Server.java
File metadata and controls
436 lines (335 loc) · 15.1 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package javaxt.express;
import javaxt.express.cms.Content;
import javaxt.http.servlet.HttpServlet;
import javaxt.http.servlet.HttpServletRequest;
import javaxt.http.servlet.HttpServletResponse;
import javaxt.http.servlet.ServletException;
import javaxt.sql.*;
import javaxt.json.*;
import javaxt.io.Jar;
import static javaxt.utils.Console.*;
import java.util.*;
import java.io.IOException;
import javaxt.express.utils.DbUtils;
//******************************************************************************
//** Express Server
//******************************************************************************
/**
* Command line application used to start a web server and serve content.
*
******************************************************************************/
public class Server {
//**************************************************************************
//** main
//**************************************************************************
/** Command line interface used to start the server.
* @param arguments Command line arguments
*/
public static void main(String[] arguments) throws Exception {
HashMap<String, String> args = parseArgs(arguments);
Jar jar = new Jar(Server.class);
//Process command line args
if (args.containsKey("-version")){
String version = jar.getVersion();
if (version==null) version = "Unknown";
System.out.println(version);
}
else if (args.containsKey("-deploy")){
Deploy.main(arguments);
}
else if (args.containsKey("-start")){
javaxt.utils.Value start = getValue(args, "-start");
Config config = getConfig(args, jar);
try {
//Get servlet
HttpServlet servlet;
if (start.equals("website") || start.equals("cms")){
servlet = new WebSite(config, args);
}
else if (start.equals("webservice") || start.equals("webservices")){
servlet = new WebServices(config, args);
}
else{
servlet = new WebServer(config, args);
}
//Start server
int port = config.get("webserver").get("port").toInteger();
int numThreads = config.get("webserver").get("numThreads").toInteger();
javaxt.http.Server server = new javaxt.http.Server(port, numThreads, servlet);
server.start();
}
catch (Exception e) {
System.out.println("Server could not start because of an " +
e.getClass() + ": " + e.getMessage());
e.printStackTrace();
}
}
else{
System.out.println("Unsupported command");
}
}
//**************************************************************************
//** WebSite
//**************************************************************************
/** Returns an HTTP servlet used serve content using the Express Content
* Management System (CMS).
*/
private static class WebSite extends javaxt.express.cms.WebSite {
private WebSite(Config config, HashMap<String, String> args){
super(getDirectory(config));
}
private static javaxt.io.Directory getDirectory(Config config){
javaxt.io.Directory web = null;
try{
web = (javaxt.io.Directory)
config.get("webserver").get("webDir").toObject();
}
catch(Exception e){}
if (web==null) throw new IllegalArgumentException(
"Invalid directory. Use the -dir argument to specify a path to a CMS folder.");
return web;
}
/* Override the native getContent method to generate custom HTML for wiki
* pages. Performs custom keyword substitution for the "<%=index%>" tag.
*/
public Content getContent(HttpServletRequest request, javaxt.io.File file){
//Get path from url
String path = request.getURL().getPath().toLowerCase();
//Remove leading and trailing "/" characters
if (path.startsWith("/")) path = path.substring(1);
if (path.endsWith("/")) path = path.substring(0, path.length()-1);
//Return content
if (path.equals("wiki")){
String html = file.getText();
Date date = file.getDate();
if (file.getName(false).equals("index")){
Content content = getIndex(file);
javaxt.utils.Date d = new javaxt.utils.Date(content.getDate());
if (d.isAfter(new javaxt.utils.Date(date))){
date = d.getDate();
}
html = html.replace("<%=index%>", content.getHTML());
}
return new Content(html, date);
}
else{
return super.getContent(request, file);
}
}
}
//**************************************************************************
//** WebServer
//**************************************************************************
/** Returns an HTTP servlet used serve static files using the FileManager.
* The FileManager ensures that files are properly cached by the browser.
*/
private static class WebServer extends HttpServlet {
private FileManager fileManager;
public WebServer(Config config, HashMap<String, String> args){
//Get directory
javaxt.io.Directory web = null;
try{
web = (javaxt.io.Directory)
config.get("webserver").get("webDir").toObject();
}
catch(Exception e){}
if (web==null) throw new IllegalArgumentException(
"Invalid directory. Use the -dir argument to specify a path to a web folder.");
//If we're still here, instantiate the file manager
fileManager = new FileManager(web);
}
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
fileManager.sendFile(request, response);
}
}
//**************************************************************************
//** WebServices
//**************************************************************************
/** Returns an HTTP servlet used execute CRUD operations (create, read,
* update, delete) using a given set of models.
*/
private static class WebServices extends HttpServlet {
private WebService ws;
private Database database;
public WebServices(Config config, HashMap<String, String> args) throws Exception {
//Parse inputs
String input = getValue(args, "-model", "-models").toString();
javaxt.io.File inputFile;
try {
inputFile = new javaxt.io.File(input);
if (!inputFile.exists()) throw new Exception();
}
catch(Exception e){
throw new IllegalArgumentException("Models are required");
}
database = config.getDatabase();
if (database==null) throw new IllegalArgumentException("Invalid or missing database");
//Get models
javaxt.orm.Model[] models = new javaxt.orm.Parser(inputFile.getText()).getModels();
//Initialize schema (create tables, indexes, etc)
String schema = new javaxt.orm.Schema(models).getSQLScript();
if (database.getDriver().equals("H2")){
//Set H2 to PostgreSQL mode
Properties properties = database.getProperties();
if (properties==null){
properties = new java.util.Properties();
database.setProperties(properties);
}
properties.setProperty("MODE", "PostgreSQL");
properties.setProperty("DATABASE_TO_LOWER", "TRUE");
properties.setProperty("DEFAULT_NULL_ORDERING", "HIGH");
//Update list of non-reserved keywords (e.g. "KEY", "VALUE", "YEAR")
HashSet<String> nonKeyWords = new HashSet<>();
Set<String> resevedKeyWords = new HashSet<>(
Arrays.asList(database.getReservedKeywords())
);
for (javaxt.orm.Model model : models){
String str = model.getTableName().toUpperCase();
if (resevedKeyWords.contains(str)) nonKeyWords.add(str);
for (javaxt.orm.Field field : model.getFields()){
str = field.getColumnName().toUpperCase();
if (resevedKeyWords.contains(str)) nonKeyWords.add(str);
}
}
if (!nonKeyWords.isEmpty()){
properties.setProperty("NON_KEYWORDS", String.join(",", nonKeyWords));
}
}
DbUtils.initSchema(database, schema, null);
//Enable metadata caching
database.enableMetadataCache(true);
//Inititalize connection pool
database.initConnectionPool();
//Compile models into classes
Class[] classes = new javaxt.orm.Compiler(models).getClasses();
//Instantiate web services
ws = new Demo();
//Initialize classes and register them with the web service
for (Class c : classes){
Model.init(c, database.getConnectionPool());
ws.addModel(c);
}
}
/** Used to process HTTP requests */
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Add CORS support
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Headers","*");
response.addHeader("Access-Control-Allow-Methods", "*");
//Generate response
ServiceRequest req = new ServiceRequest(request);
ServiceResponse rsp = ws.getServiceResponse(req, database);
rsp.send(response);
}
private class Demo extends WebService {}
}
//**************************************************************************
//** getConfig
//**************************************************************************
private static Config getConfig(HashMap<String, String> args, Jar jar){
javaxt.io.File jarFile = new javaxt.io.File(jar.getFile());
//Get config file
javaxt.io.File configFile = (args.containsKey("-config")) ?
getFile(args.get("-config"), jarFile) :
new javaxt.io.File(jar.getFile().getParentFile(), "config.json");
//Initialize config
Config config = new Config();
if (configFile.exists()){
try{
config.init(configFile.getJSONObject());
}
catch(Exception e){}
}
//Get web config
JSONObject webConfig = config.get("webserver").toJSONObject();
if (webConfig==null){
webConfig = new JSONObject();
config.set("webserver", webConfig);
}
//Set directory for the web server
try{
String dir = getValue(args, "-d", "-dir", "-directory", "-web", "-share").toString();
if (dir.endsWith("\"")) dir = dir.substring(0, dir.length()-1);
java.io.File f = new java.io.File(dir);
if (f.isFile()) f = f.getParentFile();
javaxt.io.Directory web = new javaxt.io.Directory(f);
webConfig.set("webDir", web);
}
catch(Exception e){}
//Set port for the web server
Integer port = getValue(args, "-p", "-port").toInteger();
if (port==null && !webConfig.has("port")) webConfig.set("port", 8080);
else webConfig.set("port", port);
//Set number of threads for the web server
Integer numThreads = getValue(args, "-t", "-threads").toInteger();
if (numThreads==null && !webConfig.has("numThreads")) webConfig.set("numThreads", 250);
else webConfig.set("numThreads", numThreads);
//Get database config
Database database = config.getDatabase();
if (database==null){
String db = getValue(args, "-database", "-data").toString();
if (db!=null){
try{
//Check if the database arg represents a path to a directory
String path = db;
javaxt.io.Directory dir = new javaxt.io.Directory(path);
if (dir.exists()){
try{
java.io.File f = new java.io.File(path);
javaxt.io.Directory d = new javaxt.io.Directory(f.getCanonicalFile());
if (!dir.toString().equals(d.toString())){
dir = d;
}
}
catch(Exception e){
}
}
else{
path = (configFile.exists()) ? configFile.MapPath(path)
: jarFile.MapPath(path);
dir = new javaxt.io.Directory(new java.io.File(path));
}
//If the directory doesn't exist, throw an error. Do not try
//to create a new directory because the path may be invalid.
if (!dir.exists()) throw new Exception();
//Create a new database using H2
database = new Database();
database.setDriver("H2");
database.setHost(dir.toString().replace("\\", "/") + "database");
}
catch(Exception e){
//Try parsing connection string
if (args.containsKey("-database")){
try{
database = new Database(db);
}
catch(Exception ex){}
}
}
//Update database config
if (database!=null){
int maxConnections = Math.min(25, webConfig.get("numThreads").toInteger());
database.setConnectionPoolSize(maxConnections);
config.setDatabase(database);
}
}
}
return config;
}
//**************************************************************************
//** getFile
//**************************************************************************
/** Returns a File for a given path
* @param path Full canonical path to a file or a relative path (relative
* to the jarFile)
*/
public static javaxt.io.File getFile(String path, javaxt.io.File jarFile){
javaxt.io.File file = new javaxt.io.File(path);
if (!file.exists()){
file = new javaxt.io.File(jarFile.MapPath(path));
}
return file;
}
}