Skip to content

Commit 1af724f

Browse files
committedFeb 24, 2016
Merge pull request #2829 from elpaso/server-static-initializers-construct-on-firts-use
Moved static vars to functions
2 parents 395bae6 + d266582 commit 1af724f

File tree

5 files changed

+108
-79
lines changed

5 files changed

+108
-79
lines changed
 

‎src/server/qgis_map_serv.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ int fcgi_accept()
3636

3737
int main( int argc, char * argv[] )
3838
{
39-
QgsServer server;
40-
server.init( argc, argv );
39+
QgsServer server( argc, argv );
4140
// Starts FCGI loop
4241
while ( fcgi_accept() >= 0 )
4342
{

‎src/server/qgsserver.cpp

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,36 @@
5454
#include <stdlib.h>
5555

5656

57-
// Static initialisers, default values for fcgi server
58-
QgsApplication* QgsServer::mQgsApplication = nullptr;
59-
bool QgsServer::mInitialised = false;
60-
QString QgsServer::mServerName( "qgis_server" );
61-
bool QgsServer::mCaptureOutput = false;
62-
char* QgsServer::mArgv[1];
63-
int QgsServer::mArgc = 1;
64-
QString QgsServer::mConfigFilePath;
65-
QgsMapRenderer* QgsServer::mMapRenderer = nullptr;
66-
QgsCapabilitiesCache* QgsServer::mCapabilitiesCache;
6757

58+
// Server status static initialisers.
59+
// Default values are for C++, SIP bindings will override their
60+
// options in in init()
61+
62+
QString QgsServer::sConfigFilePath = QString();
63+
QgsCapabilitiesCache* QgsServer::sCapabilitiesCache = nullptr;
64+
QgsMapRenderer* QgsServer::sMapRenderer = nullptr;
6865
#ifdef HAVE_SERVER_PYTHON_PLUGINS
69-
bool QgsServer::mInitPython = true;
70-
QgsServerInterfaceImpl* QgsServer::mServerInterface = nullptr;
66+
QgsServerInterfaceImpl*QgsServer::sServerInterface = nullptr;
67+
bool QgsServer::sInitPython = true;
7168
#endif
69+
// Initialization must run once for all servers
70+
bool QgsServer::sInitialised = false;
71+
char* QgsServer::sArgv[1];
72+
int QgsServer::sArgc;
73+
QgsApplication* QgsServer::sQgsApplication = nullptr;
74+
bool QgsServer::sCaptureOutput = false;
75+
76+
77+
78+
QgsServer::QgsServer( int &argc, char **argv )
79+
{
80+
init( argc, argv );
81+
}
7282

7383

7484
QgsServer::QgsServer()
7585
{
86+
init();
7687
}
7788

7889

@@ -81,6 +92,13 @@ QgsServer::~QgsServer()
8192
}
8293

8394

95+
QString& QgsServer::serverName()
96+
{
97+
static QString* name = new QString( "qgis_server" );
98+
return *name;
99+
}
100+
101+
84102
QFileInfo QgsServer::defaultAdminSLD()
85103
{
86104
return QFileInfo( "admin.sld" );
@@ -292,17 +310,18 @@ QString QgsServer::configPath( const QString& defaultConfigPath, const QMap<QStr
292310
*/
293311
bool QgsServer::init()
294312
{
295-
if ( mInitialised )
313+
if ( sInitialised )
296314
{
297315
return false;
298316
}
299-
mArgv[0] = mServerName.toUtf8().data();
300-
mArgc = 1;
301-
mCaptureOutput = true;
317+
318+
sArgv[0] = serverName().toUtf8().data();
319+
sArgc = 1;
320+
sCaptureOutput = true;
302321
#ifdef HAVE_SERVER_PYTHON_PLUGINS
303-
mInitPython = false;
322+
sInitPython = false;
304323
#endif
305-
return init( mArgc , mArgv );
324+
return init( sArgc , sArgv );
306325
}
307326

308327

@@ -311,7 +330,7 @@ bool QgsServer::init()
311330
*/
312331
bool QgsServer::init( int & argc, char ** argv )
313332
{
314-
if ( mInitialised )
333+
if ( sInitialised )
315334
{
316335
return false;
317336
}
@@ -330,7 +349,7 @@ bool QgsServer::init( int & argc, char ** argv )
330349
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, optionsPath );
331350
}
332351

333-
mQgsApplication = new QgsApplication( argc, argv, getenv( "DISPLAY" ), QString(), "server" );
352+
sQgsApplication = new QgsApplication( argc, argv, getenv( "DISPLAY" ), QString(), "server" );
334353

335354
QCoreApplication::setOrganizationName( QgsApplication::QGIS_ORGANIZATION_NAME );
336355
QCoreApplication::setOrganizationDomain( QgsApplication::QGIS_ORGANIZATION_DOMAIN );
@@ -386,24 +405,24 @@ bool QgsServer::init( int & argc, char ** argv )
386405
}
387406
if ( !defaultConfigFilePath.isEmpty() )
388407
{
389-
mConfigFilePath = defaultConfigFilePath;
408+
sConfigFilePath = defaultConfigFilePath;
390409
}
391410

392411
//create cache for capabilities XML
393-
mCapabilitiesCache = new QgsCapabilitiesCache();
394-
mMapRenderer = new QgsMapRenderer;
395-
mMapRenderer->setLabelingEngine( new QgsPalLabeling() );
412+
sCapabilitiesCache = new QgsCapabilitiesCache();
413+
sMapRenderer = new QgsMapRenderer;
414+
sMapRenderer->setLabelingEngine( new QgsPalLabeling() );
396415

397416
#ifdef ENABLE_MS_TESTS
398417
QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" );
399418
#endif
400419

401420
#ifdef HAVE_SERVER_PYTHON_PLUGINS
402-
mServerInterface = new QgsServerInterfaceImpl( mCapabilitiesCache );
403-
if ( mInitPython )
421+
sServerInterface = new QgsServerInterfaceImpl( sCapabilitiesCache );
422+
if ( sInitPython )
404423
{
405424
// Init plugins
406-
if ( ! QgsServerPlugins::initPlugins( mServerInterface ) )
425+
if ( ! QgsServerPlugins::initPlugins( sServerInterface ) )
407426
{
408427
QgsMessageLog::logMessage( "No server python plugins are available", "Server", QgsMessageLog::INFO );
409428
}
@@ -415,7 +434,7 @@ bool QgsServer::init( int & argc, char ** argv )
415434
#endif
416435

417436
QgsEditorWidgetRegistry::initEditors();
418-
mInitialised = true;
437+
sInitialised = true;
419438
QgsMessageLog::logMessage( "Server initialized", "Server", QgsMessageLog::INFO );
420439
return true;
421440
}
@@ -436,12 +455,6 @@ void QgsServer::putenv( const QString &var, const QString &val )
436455
*/
437456
QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryString )
438457
{
439-
// Run init if handleRequest was called without previously initialising
440-
// the server
441-
if ( ! mInitialised )
442-
{
443-
init();
444-
}
445458

446459
/*
447460
* This is mainly for python bindings, passing QUERY_STRING
@@ -453,15 +466,15 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
453466
int logLevel = QgsServerLogger::instance()->logLevel();
454467
QTime time; //used for measuring request time if loglevel < 1
455468
QgsMapLayerRegistry::instance()->removeAllMapLayers();
456-
mQgsApplication->processEvents();
469+
sQgsApplication->processEvents();
457470
if ( logLevel < 1 )
458471
{
459472
time.start();
460473
printRequestInfos();
461474
}
462475

463476
//Request handler
464-
QScopedPointer<QgsRequestHandler> theRequestHandler( createRequestHandler( mCaptureOutput ) );
477+
QScopedPointer<QgsRequestHandler> theRequestHandler( createRequestHandler( sCaptureOutput ) );
465478

466479
try
467480
{
@@ -476,10 +489,10 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
476489

477490
#ifdef HAVE_SERVER_PYTHON_PLUGINS
478491
// Set the request handler into the interface for plugins to manipulate it
479-
mServerInterface->setRequestHandler( theRequestHandler.data() );
492+
sServerInterface->setRequestHandler( theRequestHandler.data() );
480493
// Iterate filters and call their requestReady() method
481494
QgsServerFiltersMap::const_iterator filtersIterator;
482-
QgsServerFiltersMap filters = mServerInterface->filters();
495+
QgsServerFiltersMap filters = sServerInterface->filters();
483496
for ( filtersIterator = filters.constBegin(); filtersIterator != filters.constEnd(); ++filtersIterator )
484497
{
485498
filtersIterator.value()->requestReady();
@@ -488,22 +501,22 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
488501
//Pass the filters to the requestHandler, this is needed for the following reasons:
489502
// 1. allow core services to access plugin filters and implement thir own plugin hooks
490503
// 2. allow requestHandler to call sendResponse plugin hook
491-
theRequestHandler->setPluginFilters( mServerInterface->filters() );
504+
theRequestHandler->setPluginFilters( sServerInterface->filters() );
492505
#endif
493506

494507
// Copy the parameters map
495508
QMap<QString, QString> parameterMap( theRequestHandler->parameterMap() );
496509
#ifdef HAVE_SERVER_PYTHON_PLUGINS
497510
const QgsAccessControl* accessControl = nullptr;
498-
accessControl = mServerInterface->accessControls();
511+
accessControl = sServerInterface->accessControls();
499512
#endif
500513

501514
printRequestParameters( parameterMap, logLevel );
502515
QMap<QString, QString>::const_iterator paramIt;
503516
//Config file path
504-
QString configFilePath = configPath( mConfigFilePath, parameterMap );
517+
QString configFilePath = configPath( sConfigFilePath, parameterMap );
505518
#ifdef HAVE_SERVER_PYTHON_PLUGINS
506-
mServerInterface->setConfigFilePath( configFilePath );
519+
sServerInterface->setConfigFilePath( configFilePath );
507520
#endif
508521
//Service parameter
509522
QString serviceString = theRequestHandler->parameter( "SERVICE" );
@@ -600,8 +613,8 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
600613
, parameterMap
601614
, p
602615
, theRequestHandler.data()
603-
, mMapRenderer
604-
, mCapabilitiesCache
616+
, sMapRenderer
617+
, sCapabilitiesCache
605618
#ifdef HAVE_SERVER_PYTHON_PLUGINS
606619
, accessControl
607620
#endif
@@ -617,14 +630,14 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
617630

618631
#ifdef HAVE_SERVER_PYTHON_PLUGINS
619632
// Iterate filters and call their responseComplete() method
620-
filters = mServerInterface->filters();
633+
filters = sServerInterface->filters();
621634
for ( filtersIterator = filters.constBegin(); filtersIterator != filters.constEnd(); ++filtersIterator )
622635
{
623636
filtersIterator.value()->responseComplete();
624637
}
625638
// We are done using theRequestHandler in plugins, make sure we don't access
626639
// to a deleted request handler from Python bindings
627-
mServerInterface->clearRequestHandler();
640+
sServerInterface->clearRequestHandler();
628641
#endif
629642

630643
theRequestHandler->sendResponse();

‎src/server/qgsserver.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@
4747
class SERVER_EXPORT QgsServer
4848
{
4949
public:
50+
/**
51+
* Standard ctor for CGI/FCGI
52+
* @note Not available in Python bindings
53+
*/
54+
QgsServer( int & argc, char ** argv );
55+
//! The following is mainly for python bindings, that do not pass argc/argv
5056
QgsServer();
5157
~QgsServer();
58+
5259
/** Server initialization: intialise QGIS ang QT core application.
53-
* This method is automatically called by handleRequest if it wasn't
54-
* explicitly called before
5560
* @note Not available in Python bindings
5661
*/
5762
static bool init( int & argc, char ** argv );
@@ -84,7 +89,7 @@ class SERVER_EXPORT QgsServer
8489

8590
/** Returns a pointer to the server interface */
8691
#ifdef HAVE_SERVER_PYTHON_PLUGINS
87-
QgsServerInterfaceImpl* serverInterface() { return mServerInterface; }
92+
QgsServerInterfaceImpl* serverInterface() { return sServerInterface; }
8893
#endif
8994

9095
private:
@@ -106,20 +111,23 @@ class SERVER_EXPORT QgsServer
106111
//! Create and return a request handler instance
107112
static QgsRequestHandler* createRequestHandler( const bool captureOutput = false );
108113

109-
// Server status
110-
static QString mConfigFilePath;
111-
static QgsCapabilitiesCache* mCapabilitiesCache;
112-
static QgsMapRenderer* mMapRenderer;
114+
// Return the server name
115+
static QString &serverName();
116+
117+
// Status
118+
static QString sConfigFilePath;
119+
static QgsCapabilitiesCache* sCapabilitiesCache;
120+
static QgsMapRenderer* sMapRenderer;
113121
#ifdef HAVE_SERVER_PYTHON_PLUGINS
114-
static QgsServerInterfaceImpl* mServerInterface;
115-
static bool mInitPython;
122+
static QgsServerInterfaceImpl* sServerInterface;
123+
static bool sInitPython;
116124
#endif
117-
static bool mInitialised;
118-
static QString mServerName;
119-
static char* mArgv[1];
120-
static int mArgc;
121-
static QgsApplication* mQgsApplication;
122-
static bool mCaptureOutput;
125+
//! Initialization must run once for all servers
126+
static bool sInitialised;
127+
static char* sArgv[1];
128+
static int sArgc;
129+
static QgsApplication* sQgsApplication;
130+
static bool sCaptureOutput;
123131
};
124132
#endif // QGSSERVER_H
125133

‎src/server/qgsserverplugins.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@
2626

2727
#include <QLibrary>
2828

29+
30+
// Initialize static members
31+
QgsPythonUtils* QgsServerPlugins::sPythonUtils;
32+
33+
2934
QgsServerPlugins::QgsServerPlugins()
3035
{
3136
}
3237

33-
// Initialize static members
34-
QgsPythonUtils* QgsServerPlugins::mPythonUtils;
35-
// Initialize static members
36-
QStringList QgsServerPlugins::mServerPlugins;
38+
// Construct on first use
39+
QStringList &QgsServerPlugins::serverPlugins()
40+
{
41+
static QStringList* pluginList = new QStringList();
42+
return *pluginList;
43+
}
44+
3745

3846
// This code is mainly borrowed from QGIS desktop Python plugin initialization
3947
bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
@@ -73,10 +81,10 @@ bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
7381
}
7482

7583
QgsDebugMsg( "Python support library's instance() symbol resolved." );
76-
mPythonUtils = pythonlib_inst();
77-
mPythonUtils->initServerPython( interface );
84+
sPythonUtils = pythonlib_inst();
85+
sPythonUtils->initServerPython( interface );
7886

79-
if ( mPythonUtils && mPythonUtils->isEnabled() )
87+
if ( sPythonUtils && sPythonUtils->isEnabled() )
8088
{
8189
QgsDebugMsg( "Python support ENABLED :-)" );
8290
}
@@ -88,20 +96,20 @@ bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
8896

8997
//Init plugins: loads a list of installed plugins and filter them
9098
//for "server" metadata
91-
QListIterator<QString> plugins( mPythonUtils->pluginList() );
99+
QListIterator<QString> plugins( sPythonUtils->pluginList() );
92100
bool atLeastOneEnabled = false;
93101
while ( plugins.hasNext() )
94102
{
95103
QString pluginName = plugins.next();
96-
QString pluginService = mPythonUtils->getPluginMetadata( pluginName, "server" );
104+
QString pluginService = sPythonUtils->getPluginMetadata( pluginName, "server" );
97105
if ( pluginService == "True" )
98106
{
99-
if ( mPythonUtils->loadPlugin( pluginName ) )
107+
if ( sPythonUtils->loadPlugin( pluginName ) )
100108
{
101-
if ( mPythonUtils->startServerPlugin( pluginName ) )
109+
if ( sPythonUtils->startServerPlugin( pluginName ) )
102110
{
103111
atLeastOneEnabled = true;
104-
mServerPlugins.append( pluginName );
112+
serverPlugins().append( pluginName );
105113
QgsMessageLog::logMessage( QString( "Server plugin %1 loaded!" ).arg( pluginName ), "Server", QgsMessageLog::INFO );
106114
}
107115
else
@@ -115,6 +123,7 @@ bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
115123
}
116124
}
117125
}
118-
return mPythonUtils && mPythonUtils->isEnabled() && atLeastOneEnabled;
126+
return sPythonUtils && sPythonUtils->isEnabled() && atLeastOneEnabled;
119127
}
120128

129+

‎src/server/qgsserverplugins.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class SERVER_EXPORT QgsServerPlugins
3737
* @return bool true on success
3838
*/
3939
static bool initPlugins( QgsServerInterface* interface );
40-
//! Pointer to QgsPythonUtils
41-
static QgsPythonUtils* mPythonUtils;
4240
//! List of available server plugin names
43-
static QStringList mServerPlugins;
41+
static QStringList& serverPlugins();
42+
//! Pointer to QgsPythonUtils
43+
static QgsPythonUtils* sPythonUtils;
4444
};
4545

4646
#endif // QGSSERVERPLUGINS_H

0 commit comments

Comments
 (0)
Please sign in to comment.