-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathServerInitUtil.cpp
More file actions
511 lines (441 loc) · 15.6 KB
/
ServerInitUtil.cpp
File metadata and controls
511 lines (441 loc) · 15.6 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
Copyright 2009-2020, Sumeet Chhetri
Licensed under the Apache License, Version 2.0 (const the& "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* ServerInitUtil.cpp
*
* Created on: 28-Apr-2020
* Author: sumeetc
*/
#include "ServerInitUtil.h"
Logger ServerInitUtil::loggerIB;
cb_into_pv PicoVWriter::cb;
cb_into_pv_for_date PicoVWriter::cdt;
moodycamel::ConcurrentQueue<PicoVWriter*> PicoVWriter::toBeClosedConns;
void ServerInitUtil::closeConnection(void* pcwr) {
PicoVWriter* si = (PicoVWriter*)pcwr;
si->cqat = Timer::getTimestamp();
PicoVWriter::toBeClosedConns.enqueue(si);
}
void ServerInitUtil::closeConnections() {
PicoVWriter* si;
Timer t;
t.start();
while(PicoVWriter::toBeClosedConns.try_dequeue(si)) {
if((Timer::getTimestamp()-si->cqat)>10 && si->useCounter==0) {
delete si;
} else {
PicoVWriter::toBeClosedConns.enqueue(si);
}
if(t.elapsedMilliSeconds()>900) {
PicoVWriter::cdt();
t.start();
}
}
}
void ServerInitUtil::bootstrapIB(std::string serverRootDirectory, SERVER_BACKEND type) {
if(type==V_PICO) {
Writer::isPicoEvAsyncBackendMode = true;
}
bootstrap(serverRootDirectory, loggerIB, type);
}
void ServerInitUtil::bootstrap(std::string serverRootDirectory, Logger& logger, SERVER_BACKEND type) {
serverRootDirectory += "/";
RegexUtil::replace(serverRootDirectory,"[/]+","/");
std::string incpath = serverRootDirectory + "include/";
std::string rtdcfpath = serverRootDirectory + "rtdcf/";
std::string pubpath = serverRootDirectory + "public/";
std::string respath = serverRootDirectory + "resources/";
std::string webpath = serverRootDirectory + "web/";
std::string logpath = serverRootDirectory + "logs/";
std::string resourcePath = respath;
PropFileReader pread;
propMap srprps = pread.getProperties(respath+"server.prop");
std::string servd = serverRootDirectory;
std::string logp = respath+"/logging.xml";
LoggerFactory::init(logp, serverRootDirectory, "", StringUtil::toLowerCopy(srprps["LOGGING_ENABLED"])=="true");
logger = LoggerFactory::getLogger("MOD_FFEADCPP");
logger << "FFEAD in bootstrap " << serverRootDirectory << std::endl;
bool isCompileEnabled = false;
std::string compileEnabled = srprps["DEV_MODE"];
if(compileEnabled=="true" || compileEnabled=="TRUE")
isCompileEnabled = true;
/*if(srprps["SCRIPT_ERRS"]=="true" || srprps["SCRIPT_ERRS"]=="TRUE")
{
SCRIPT_EXEC_SHOW_ERRS = true;
}*/
bool sessatserv = true;
if(srprps["SESS_STATE"]=="server")
sessatserv = true;
long sessionTimeout = 3600;
if(srprps["SESS_TIME_OUT"]!="")
{
try {
sessionTimeout = CastUtil::toLong(srprps["SESS_TIME_OUT"]);
} catch(const std::exception& e) {
logger << "Invalid session timeout value defined, defaulting to 1hour/3600sec" << std::endl;
}
}
ConfigurationData::getInstance();
#ifdef HAVE_SSLINC
SSLHandler::setIsSSL(false);
#endif
strVec webdirs,webdirs1,pubfiles;
//ConfigurationHandler::listi(webpath,"/",true,webdirs,false);
CommonUtils::listFiles(webdirs, webpath, "/");
//ConfigurationHandler::listi(webpath,"/",false,webdirs1,false);
CommonUtils::listFiles(webdirs1, webpath, "/", false);
CommonUtils::loadMimeTypes(respath+"mime-types.prop");
CommonUtils::loadLocales(respath+"locale.prop");
RegexUtil::replace(serverRootDirectory,"[/]+","/");
RegexUtil::replace(webpath,"[/]+","/");
CoreServerProperties csp(serverRootDirectory, respath, webpath, srprps, sessionTimeout, sessatserv);
ConfigurationData::getInstance()->setCoreServerProperties(csp);
bool enableCors = StringUtil::toLowerCopy(srprps["ENABLE_CRS"])=="true";
bool enableSecurity = StringUtil::toLowerCopy(srprps["ENABLE_SEC"])=="true";
bool enableFilters = StringUtil::toLowerCopy(srprps["ENABLE_FLT"])=="true";
bool enableControllers = StringUtil::toLowerCopy(srprps["ENABLE_CNT"])=="true";
bool enableContMpg = StringUtil::toLowerCopy(srprps["ENABLE_CNT_MPG"])=="true";
bool enableContPath = StringUtil::toLowerCopy(srprps["ENABLE_CNT_PTH"])=="true";
bool enableContExt = StringUtil::toLowerCopy(srprps["ENABLE_CNT_EXT"])=="true";
bool enableContRst = StringUtil::toLowerCopy(srprps["ENABLE_CNT_RST"])=="true";
bool enableExtra = StringUtil::toLowerCopy(srprps["ENABLE_EXT"])=="true";
bool enableScripts = StringUtil::toLowerCopy(srprps["ENABLE_SCR"])=="true";
bool enableSoap = StringUtil::toLowerCopy(srprps["ENABLE_SWS"])=="true";
bool enableLogging = StringUtil::toLowerCopy(srprps["LOGGING_ENABLED"])=="true";
bool enableExtControllers = StringUtil::toLowerCopy(srprps["ENABLE_EXT_CNT"])=="true";
bool enableJobs = StringUtil::toLowerCopy(srprps["ENABLE_JOBS"])=="true";
bool enableStaticResponses = StringUtil::toLowerCopy(srprps["ENABLE_STATIC_RESP"])=="true";
ConfigurationData::enableFeatures(enableCors, enableSecurity, enableFilters, enableControllers,
enableContMpg, enableContPath, enableContExt,enableContRst, enableExtra, enableScripts,
enableSoap, enableLogging, enableExtControllers, enableJobs, enableStaticResponses);
strVec cmpnames;
try
{
ConfigurationHandler::handle(webdirs, webdirs1, incpath, rtdcfpath, serverRootDirectory, respath);
}
catch(const XmlParseException& p)
{
logger << p.getMessage() << std::endl;
}
catch(const std::exception& msg)
{
logger << msg.what() << std::endl;
}
logger << INTER_LIB_FILE << std::endl;
bool libpresent = true;
void *dlibtemp = dlopen(INTER_LIB_FILE, RTLD_NOW);
//logger << std::endl <<dlibtemp << std::endl;
if(dlibtemp==NULL)
{
libpresent = false;
logger << dlerror() << std::endl;
logger.info("Could not load Library");
}
else
dlclose(dlibtemp);
//Generate library if dev mode = true or the library is not found in prod mode
if(isCompileEnabled || !libpresent)
libpresent = false;
#ifdef BUILD_AUTOCONF
if(!libpresent)
{
std::string configureFilePath = rtdcfpath+"/autotools/configure";
if (access( configureFilePath.c_str(), F_OK ) == -1 )
{
std::string compres = rtdcfpath+"/autotools/autogen.sh "+serverRootDirectory;
std::string output = ScriptHandler::execute(compres, true);
logger << "Set up configure for intermediate libraries\n\n" << std::endl;
}
if (access( configureFilePath.c_str(), F_OK ) != -1 )
{
std::string compres = respath+"rundyn-configure.sh "+serverRootDirectory;
#ifdef DEBUG
compres += " --enable-debug=yes";
#endif
std::string output = ScriptHandler::execute(compres, true);
logger << "Set up makefiles for intermediate libraries\n\n" << std::endl;
logger << output << std::endl;
compres = respath+"rundyn-automake_autoconf.sh "+serverRootDirectory;
output = ScriptHandler::execute(compres, true);
logger << "Intermediate code generation task\n\n" << std::endl;
logger << output << std::endl;
}
}
void* checkdlib = dlopen(INTER_LIB_FILE, RTLD_NOW);
void* checkddlib = dlopen(DINTER_LIB_FILE, RTLD_NOW);
if(checkdlib==NULL || checkddlib==NULL)
{
std::string compres = rtdcfpath+"/autotools/autogen-noreconf.sh "+serverRootDirectory;
std::string output = ScriptHandler::execute(compres, true);
logger << "Set up configure for intermediate libraries\n\n" << std::endl;
compres = respath+"rundyn-configure.sh "+serverRootDirectory;
#ifdef DEBUG
compres += " --enable-debug=yes";
#endif
output = ScriptHandler::execute(compres, true);
logger << "Set up makefiles for intermediate libraries\n\n" << std::endl;
logger << output << std::endl;
compres = respath+"rundyn-automake_autoconf.sh "+serverRootDirectory;
if(!libpresent)
{
std::string output = ScriptHandler::execute(compres, true);
logger << "Rerunning Intermediate code generation task\n\n" << std::endl;
logger << output << std::endl;
}
checkdlib = dlopen(INTER_LIB_FILE, RTLD_NOW);
checkddlib = dlopen(DINTER_LIB_FILE, RTLD_NOW);
}
if(checkdlib==NULL || checkddlib==NULL)
{
logger << dlerror() << std::endl;
logger.info("Could not load Library");
exit(0);
}
else
{
dlclose(checkdlib);
dlclose(checkddlib);
logger.info("Library generated successfully");
}
#else
if(!libpresent)
{
#if defined(BUILD_CMAKE)
std::string compres = respath+"rundyn-automake.sh "+serverRootDirectory + " cmake";
#elif defined(BUILD_XMAKE)
std::string compres = respath+"rundyn-automake.sh "+serverRootDirectory + " xmake";
#elif defined(BUILD_MESON)
std::string compres = respath+"rundyn-automake.sh "+serverRootDirectory + " meson";
#elif defined(BUILD_SCONS)
std::string compres = respath+"rundyn-automake.sh "+serverRootDirectory + " scons";
#elif defined(BUILD_BAZEL)
std::string compres = respath+"rundyn-automake.sh "+serverRootDirectory + " bazel";
#elif defined(BUILD_BUCK2)
std::string compres = respath+"rundyn-automake.sh "+serverRootDirectory + " buck2";
#elif defined(BUILD_SHELLB)
std::string compres = respath+"rundyn-automake.sh "+serverRootDirectory + " shellb";
#else
logger << "Invalid Build Type specified, only autotools, cmake, xmake, meson, scons, shellb, bazel and buck2 supported...\n" << std::endl;
#endif
std::string output = ScriptHandler::execute(compres, true);
logger << "Intermediate code generation task\n\n" << std::endl;
logger << output << std::endl;
}
void* checkdlib = dlopen(INTER_LIB_FILE, RTLD_NOW);
void* checkddlib = dlopen(DINTER_LIB_FILE, RTLD_NOW);
if(checkdlib==NULL || checkddlib==NULL)
{
logger << dlerror() << std::endl;
logger.info("Could not load Library");
exit(0);
}
else
{
dlclose(checkdlib);
dlclose(checkddlib);
logger.info("Library generated successfully");
}
#endif
#ifdef INC_COMP
for (unsigned int var1 = 0;var1<ConfigurationData::getInstance()->componentNames.size();var1++)
{
std::string name = ConfigurationData::getInstance()->componentNames.at(var1);
StringUtil::replaceFirst(name,"Component_","");
ComponentHandler::registerComponent(name);
AppContext::registerComponent(name);
}
#endif
/*#ifdef INC_DSTC
bool distocache = false;
int distocachepoolsize = 20;
try {
if(srprps["DISTOCACHE_POOL_SIZE"]!="")
{
distocachepoolsize = CastUtil::toInt(srprps["DISTOCACHE_POOL_SIZE"]);
}
} catch(const std::exception& e) {
logger << ("Invalid poolsize specified for distocache") << std::endl;
}
try {
if(srprps["DISTOCACHE_PORT_NO"]!="")
{
CastUtil::toInt(srprps["DISTOCACHE_PORT_NO"]);
DistoCacheHandler::trigger(srprps["DISTOCACHE_PORT_NO"], distocachepoolsize);
logger << ("Session store is set to distocache store") << std::endl;
distocache = true;
}
} catch(const std::exception& e) {
logger << ("Invalid port specified for distocache") << std::endl;
}
if(!distocache) {
logger << ("Session store is set to file store") << std::endl;
}
#endif*/
logger << ("Initializing WSDL files....") << std::endl;
ConfigurationHandler::initializeWsdls();
logger << ("Initializing WSDL files done....") << std::endl;
void* dlib = dlopen(INTER_LIB_FILE, RTLD_NOW);
//logger << std::endl <<dlib << std::endl;
if(dlib==NULL)
{
logger << dlerror() << std::endl;
logger.info("Could not load Library");
exit(0);
}
else
{
logger.info("Library loaded successfully");
dlclose(dlib);
}
void* ddlib = dlopen(DINTER_LIB_FILE, RTLD_NOW);
//logger << std::endl <<dlib << std::endl;
if(ddlib==NULL)
{
logger << dlerror() << std::endl;
logger.info("Could not load dynamic Library");
exit(0);
}
else
{
logger.info("Dynamic Library loaded successfully");
dlclose(ddlib);
}
ddlib = dlopen(DINTER_LIB_FILE, RTLD_NOW);
//logger << std::endl <<dlib << std::endl;
if(ddlib==NULL)
{
logger << dlerror() << std::endl;
logger.info("Could not load dynamic Library");
exit(0);
}
else
{
logger.info("Second Dynamic Library loaded successfully");
dlclose(ddlib);
}
ConfigurationData::getInstance()->serverType = type;
}
void ServerInitUtil::initIB() {
init(loggerIB);
}
void ServerInitUtil::initIB(cb_reg_ext_fd_pv pvregfd, cb_into_pv cb, cb_into_pv_for_date cdt) {
Writer::pvregfd = pvregfd;
PicoVWriter::cb = cb;
PicoVWriter::cdt = cdt;
init(loggerIB);
}
bool ServerInitUtil::isInited() {
return CacheManager::isInitCompleted() && DataSourceManager::isInitCompleted() && SearchEngineManager::isInitCompleted();
}
void ServerInitUtil::init(Logger& logger) {
std::cout << "FFEAD in init" << std::endl;
std::cout << "Initializing ffead-cpp....." << std::endl;
#ifdef INC_COMP
try {
if(srprps["CMP_PORT"]!="")
{
int port = CastUtil::toInt(srprps["CMP_PORT"]);
if(port>0)
{
ComponentHandler::trigger(srprps["CMP_PORT"]);
}
}
} catch(const std::exception& e) {
logger << ("Component Handler Services are disabled") << std::endl;
}
#endif
#ifdef INC_MSGH
try {
if(srprps["MESS_PORT"]!="")
{
int port = CastUtil::toInt(srprps["MESS_PORT"]);
if(port>0)
{
MessageHandler::trigger(srprps["MESS_PORT"],resourcePath);
}
}
} catch(const std::exception& e) {
logger << ("Messaging Handler Services are disabled") << std::endl;
}
#endif
#ifdef INC_MI
try {
if(srprps["MI_PORT"]!="")
{
int port = CastUtil::toInt(srprps["MI_PORT"]);
if(port>0)
{
MethodInvoc::trigger(srprps["MI_PORT"]);
}
}
} catch(const std::exception& e) {
logger << ("Method Invoker Services are disabled") << std::endl;
}
#endif
//Load all the FFEADContext beans so that the same copy is shared by all process
//We need singleton beans so only initialize singletons(controllers,authhandlers,formhandlers..)
std::cout << ("Initializing ffeadContext....") << std::endl;
ConfigurationData::getInstance()->initializeAllSingletonBeans();
GenericObject::init(ConfigurationData::getReflector());
std::cout << ("Initializing ffeadContext done....") << std::endl;
#ifdef INC_SDORM
std::cout << ("Initializing DataSources....") << std::endl;
ConfigurationHandler::initializeDataSources();
std::cout << ("Initializing DataSources done....") << std::endl;
#endif
std::cout << ("Initializing Caches....") << std::endl;
ConfigurationHandler::initializeCaches();
std::cout << ("Initializing Caches done....") << std::endl;
std::cout << ("Initializing Searches....") << std::endl;
ConfigurationHandler::initializeSearches();
std::cout << ("Initializing Searches done....") << std::endl;
#ifdef INC_JOBS
if(ConfigurationData::getInstance()->isJobsEnabled()) {
JobScheduler::start();
}
#endif
bool isLazyHeaderParsing = StringUtil::toLowerCopy(ConfigurationData::getInstance()->coreServerProperties.sprops["LAZY_HEADER_PARSE"])=="true";
HTTPResponseStatus::init();
HttpRequest::init(isLazyHeaderParsing);
HttpResponse::init();
MultipartContent::init();
int counter = 0;
while(!isInited()) {
Thread::sSleep(1);
if(counter++==60) {
std::cout << "Cannot wait more than 60 seconds for cache/database to initialize, will forcefully start server now...." << std::endl;
break;
}
}
//Sleep for some time so as to make sure all the new child processes are set correctly
//and all init is complete...
std::cout << "All initializations are now complete...." << std::endl;
//sleep(10);
}
void ServerInitUtil::cleanUp() {
#ifdef INC_SDORM
ConfigurationHandler::destroyDataSources();
#endif
ConfigurationHandler::destroyCaches();
ConfigurationHandler::destroySearches();
ConfigurationData::getInstance()->clearAllSingletonBeans();
#ifdef INC_JOBS
if(ConfigurationData::getInstance()->isJobsEnabled()) {
JobScheduler::stop();
}
#endif
RegexUtil::flushCache();
HttpClient::cleanup();
LoggerFactory::clear();
CommonUtils::clearInstance();
}