Design pattern for startup initialisation of static variables before an engine starts serving its request
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I should initialize the startup variables before serving the requests. Few of my variables could be initialized from a local property file (say init.properties) and few would get initialized from a URL, say (/getProperties
).
I should be able to serve requests from my engine only when all my variables gets initialised (both from a local property file and from a remote URL call).
The first request should fail when there is some exception while loading the property file (or) when the remote HTTP call fails and it should be able to retry on consecutive calls.
Here is how I have done the initialisation part:
public class EngineUtils
private static boolean isEnginePropertiesInitialised = false;
private static long keepAliveTime = -1;
private static String serverIp;
private static int port;
private static String mode;
private static String moduleName;
private static String moduleURL;
private static String remoteModuleURL;
private static int moduleType = -1;
private static String token = null;
/**
And this list goes on..
**/
public static synchronised void initEngineProperties() //Made this synchronised to make sure that only one thread is allowed to initialise at a time
if(remoteModuleURL == null) //Checking this to make sure the properties are initialised already
initEngineProps();
initModuleURL();
initConnectingServerIpPort();
public static int initModuleType()
/**
The moduleName used here is initialised from **initEngineProps** and this should have executed before it is initialising
the module type. IâÂÂm just maintaining the order now while writing code consciously but not sure some one else who
read my code understands this order. Is commenting the only way in these situations?
**/
if(moduleName.equals(âÂÂABCâÂÂ)) return 1;
else if(moduleName.equals(âÂÂCDEâÂÂ)) return 2;
else if(moduleName.equals(âÂÂMNOâÂÂ)) return 3;
public static int getModuleType()
if(moduleType == -1)
initModuleType();
private static void populateProperties(JSONObject props) throws Exception
mode = props.optString(âÂÂmodeâÂÂ);
serverIp = props.optString(âÂÂipâÂÂ);
port = props.optString(âÂÂportâÂÂ);
moduleURL = props.optString(âÂÂmodule_urlâÂÂ);
public static void loadPropertiesFromLocalFile() throws Exception
Properties prop = new Properties();
String filepath = âÂÂpath_to_properties_file/abc.propertiesâÂÂ;
InputStream in = new FileInputStream(filepath);
prop.load(in);
token = props.getProperty(âÂÂtokenâÂÂ);
public static void initEngineProps() throws Exception
if(isEnginePropertiesInitialised) return; //To avoid reinitialising the properties again
loadPropertiesFromLocalFile();
/**
This XYZ module has to be initialised only after loading the token value from local property file
Someone else who is reading or looking at this code for the first time may not recognise this.
How could I avoid this?
**/
initXYZModule(token);
int moduleType = getModuleType();
if(moduleType == 1)
/**
1.Do some other stuff
2.Make http request to request uri - this would respond with json object - during success call
3.When there is some network failure (or) server is not reachable this method
**getHttpResponse** throws exception
**/
populateProperties(getHttpResponse(âÂÂfrom_uri_0âÂÂ));
else if(moduleType == 2)
populateProperties(getHttpResponse(âÂÂfrom_uri_1âÂÂ));
else if(moduleType == 3)
populateProperties(getHttpResponse(âÂÂfrom_uri_2âÂÂ));
/**
This method is required everywhere else in the project and the
value has to be initialised before it gets served
**/
public static void getToken()
if(!isEnginePropertiesInitialised)
initEngineProperties();
return token;
public static void initModuleURL()
//Do some stuff here
public static void initConnectingServerIpPort()
//Do some stuff here
After writing this I find it too clumsy to read and it is not easily understandable and I get confused when I try to change some snippet over here. Can someone suggest a better design for the following case so that my code looks easily understandable to everyone?
java object-oriented design-patterns
add a comment |Â
up vote
3
down vote
favorite
I should initialize the startup variables before serving the requests. Few of my variables could be initialized from a local property file (say init.properties) and few would get initialized from a URL, say (/getProperties
).
I should be able to serve requests from my engine only when all my variables gets initialised (both from a local property file and from a remote URL call).
The first request should fail when there is some exception while loading the property file (or) when the remote HTTP call fails and it should be able to retry on consecutive calls.
Here is how I have done the initialisation part:
public class EngineUtils
private static boolean isEnginePropertiesInitialised = false;
private static long keepAliveTime = -1;
private static String serverIp;
private static int port;
private static String mode;
private static String moduleName;
private static String moduleURL;
private static String remoteModuleURL;
private static int moduleType = -1;
private static String token = null;
/**
And this list goes on..
**/
public static synchronised void initEngineProperties() //Made this synchronised to make sure that only one thread is allowed to initialise at a time
if(remoteModuleURL == null) //Checking this to make sure the properties are initialised already
initEngineProps();
initModuleURL();
initConnectingServerIpPort();
public static int initModuleType()
/**
The moduleName used here is initialised from **initEngineProps** and this should have executed before it is initialising
the module type. IâÂÂm just maintaining the order now while writing code consciously but not sure some one else who
read my code understands this order. Is commenting the only way in these situations?
**/
if(moduleName.equals(âÂÂABCâÂÂ)) return 1;
else if(moduleName.equals(âÂÂCDEâÂÂ)) return 2;
else if(moduleName.equals(âÂÂMNOâÂÂ)) return 3;
public static int getModuleType()
if(moduleType == -1)
initModuleType();
private static void populateProperties(JSONObject props) throws Exception
mode = props.optString(âÂÂmodeâÂÂ);
serverIp = props.optString(âÂÂipâÂÂ);
port = props.optString(âÂÂportâÂÂ);
moduleURL = props.optString(âÂÂmodule_urlâÂÂ);
public static void loadPropertiesFromLocalFile() throws Exception
Properties prop = new Properties();
String filepath = âÂÂpath_to_properties_file/abc.propertiesâÂÂ;
InputStream in = new FileInputStream(filepath);
prop.load(in);
token = props.getProperty(âÂÂtokenâÂÂ);
public static void initEngineProps() throws Exception
if(isEnginePropertiesInitialised) return; //To avoid reinitialising the properties again
loadPropertiesFromLocalFile();
/**
This XYZ module has to be initialised only after loading the token value from local property file
Someone else who is reading or looking at this code for the first time may not recognise this.
How could I avoid this?
**/
initXYZModule(token);
int moduleType = getModuleType();
if(moduleType == 1)
/**
1.Do some other stuff
2.Make http request to request uri - this would respond with json object - during success call
3.When there is some network failure (or) server is not reachable this method
**getHttpResponse** throws exception
**/
populateProperties(getHttpResponse(âÂÂfrom_uri_0âÂÂ));
else if(moduleType == 2)
populateProperties(getHttpResponse(âÂÂfrom_uri_1âÂÂ));
else if(moduleType == 3)
populateProperties(getHttpResponse(âÂÂfrom_uri_2âÂÂ));
/**
This method is required everywhere else in the project and the
value has to be initialised before it gets served
**/
public static void getToken()
if(!isEnginePropertiesInitialised)
initEngineProperties();
return token;
public static void initModuleURL()
//Do some stuff here
public static void initConnectingServerIpPort()
//Do some stuff here
After writing this I find it too clumsy to read and it is not easily understandable and I get confused when I try to change some snippet over here. Can someone suggest a better design for the following case so that my code looks easily understandable to everyone?
java object-oriented design-patterns
It might be worth to check AtomicBoolean and CountDownLatch to see how these could help you.
â Thomas
Jan 16 at 19:56
use a static initializer?
â slowy
Jan 17 at 10:20
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I should initialize the startup variables before serving the requests. Few of my variables could be initialized from a local property file (say init.properties) and few would get initialized from a URL, say (/getProperties
).
I should be able to serve requests from my engine only when all my variables gets initialised (both from a local property file and from a remote URL call).
The first request should fail when there is some exception while loading the property file (or) when the remote HTTP call fails and it should be able to retry on consecutive calls.
Here is how I have done the initialisation part:
public class EngineUtils
private static boolean isEnginePropertiesInitialised = false;
private static long keepAliveTime = -1;
private static String serverIp;
private static int port;
private static String mode;
private static String moduleName;
private static String moduleURL;
private static String remoteModuleURL;
private static int moduleType = -1;
private static String token = null;
/**
And this list goes on..
**/
public static synchronised void initEngineProperties() //Made this synchronised to make sure that only one thread is allowed to initialise at a time
if(remoteModuleURL == null) //Checking this to make sure the properties are initialised already
initEngineProps();
initModuleURL();
initConnectingServerIpPort();
public static int initModuleType()
/**
The moduleName used here is initialised from **initEngineProps** and this should have executed before it is initialising
the module type. IâÂÂm just maintaining the order now while writing code consciously but not sure some one else who
read my code understands this order. Is commenting the only way in these situations?
**/
if(moduleName.equals(âÂÂABCâÂÂ)) return 1;
else if(moduleName.equals(âÂÂCDEâÂÂ)) return 2;
else if(moduleName.equals(âÂÂMNOâÂÂ)) return 3;
public static int getModuleType()
if(moduleType == -1)
initModuleType();
private static void populateProperties(JSONObject props) throws Exception
mode = props.optString(âÂÂmodeâÂÂ);
serverIp = props.optString(âÂÂipâÂÂ);
port = props.optString(âÂÂportâÂÂ);
moduleURL = props.optString(âÂÂmodule_urlâÂÂ);
public static void loadPropertiesFromLocalFile() throws Exception
Properties prop = new Properties();
String filepath = âÂÂpath_to_properties_file/abc.propertiesâÂÂ;
InputStream in = new FileInputStream(filepath);
prop.load(in);
token = props.getProperty(âÂÂtokenâÂÂ);
public static void initEngineProps() throws Exception
if(isEnginePropertiesInitialised) return; //To avoid reinitialising the properties again
loadPropertiesFromLocalFile();
/**
This XYZ module has to be initialised only after loading the token value from local property file
Someone else who is reading or looking at this code for the first time may not recognise this.
How could I avoid this?
**/
initXYZModule(token);
int moduleType = getModuleType();
if(moduleType == 1)
/**
1.Do some other stuff
2.Make http request to request uri - this would respond with json object - during success call
3.When there is some network failure (or) server is not reachable this method
**getHttpResponse** throws exception
**/
populateProperties(getHttpResponse(âÂÂfrom_uri_0âÂÂ));
else if(moduleType == 2)
populateProperties(getHttpResponse(âÂÂfrom_uri_1âÂÂ));
else if(moduleType == 3)
populateProperties(getHttpResponse(âÂÂfrom_uri_2âÂÂ));
/**
This method is required everywhere else in the project and the
value has to be initialised before it gets served
**/
public static void getToken()
if(!isEnginePropertiesInitialised)
initEngineProperties();
return token;
public static void initModuleURL()
//Do some stuff here
public static void initConnectingServerIpPort()
//Do some stuff here
After writing this I find it too clumsy to read and it is not easily understandable and I get confused when I try to change some snippet over here. Can someone suggest a better design for the following case so that my code looks easily understandable to everyone?
java object-oriented design-patterns
I should initialize the startup variables before serving the requests. Few of my variables could be initialized from a local property file (say init.properties) and few would get initialized from a URL, say (/getProperties
).
I should be able to serve requests from my engine only when all my variables gets initialised (both from a local property file and from a remote URL call).
The first request should fail when there is some exception while loading the property file (or) when the remote HTTP call fails and it should be able to retry on consecutive calls.
Here is how I have done the initialisation part:
public class EngineUtils
private static boolean isEnginePropertiesInitialised = false;
private static long keepAliveTime = -1;
private static String serverIp;
private static int port;
private static String mode;
private static String moduleName;
private static String moduleURL;
private static String remoteModuleURL;
private static int moduleType = -1;
private static String token = null;
/**
And this list goes on..
**/
public static synchronised void initEngineProperties() //Made this synchronised to make sure that only one thread is allowed to initialise at a time
if(remoteModuleURL == null) //Checking this to make sure the properties are initialised already
initEngineProps();
initModuleURL();
initConnectingServerIpPort();
public static int initModuleType()
/**
The moduleName used here is initialised from **initEngineProps** and this should have executed before it is initialising
the module type. IâÂÂm just maintaining the order now while writing code consciously but not sure some one else who
read my code understands this order. Is commenting the only way in these situations?
**/
if(moduleName.equals(âÂÂABCâÂÂ)) return 1;
else if(moduleName.equals(âÂÂCDEâÂÂ)) return 2;
else if(moduleName.equals(âÂÂMNOâÂÂ)) return 3;
public static int getModuleType()
if(moduleType == -1)
initModuleType();
private static void populateProperties(JSONObject props) throws Exception
mode = props.optString(âÂÂmodeâÂÂ);
serverIp = props.optString(âÂÂipâÂÂ);
port = props.optString(âÂÂportâÂÂ);
moduleURL = props.optString(âÂÂmodule_urlâÂÂ);
public static void loadPropertiesFromLocalFile() throws Exception
Properties prop = new Properties();
String filepath = âÂÂpath_to_properties_file/abc.propertiesâÂÂ;
InputStream in = new FileInputStream(filepath);
prop.load(in);
token = props.getProperty(âÂÂtokenâÂÂ);
public static void initEngineProps() throws Exception
if(isEnginePropertiesInitialised) return; //To avoid reinitialising the properties again
loadPropertiesFromLocalFile();
/**
This XYZ module has to be initialised only after loading the token value from local property file
Someone else who is reading or looking at this code for the first time may not recognise this.
How could I avoid this?
**/
initXYZModule(token);
int moduleType = getModuleType();
if(moduleType == 1)
/**
1.Do some other stuff
2.Make http request to request uri - this would respond with json object - during success call
3.When there is some network failure (or) server is not reachable this method
**getHttpResponse** throws exception
**/
populateProperties(getHttpResponse(âÂÂfrom_uri_0âÂÂ));
else if(moduleType == 2)
populateProperties(getHttpResponse(âÂÂfrom_uri_1âÂÂ));
else if(moduleType == 3)
populateProperties(getHttpResponse(âÂÂfrom_uri_2âÂÂ));
/**
This method is required everywhere else in the project and the
value has to be initialised before it gets served
**/
public static void getToken()
if(!isEnginePropertiesInitialised)
initEngineProperties();
return token;
public static void initModuleURL()
//Do some stuff here
public static void initConnectingServerIpPort()
//Do some stuff here
After writing this I find it too clumsy to read and it is not easily understandable and I get confused when I try to change some snippet over here. Can someone suggest a better design for the following case so that my code looks easily understandable to everyone?
java object-oriented design-patterns
edited Jan 16 at 14:22
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jan 16 at 9:03
Natasha
204
204
It might be worth to check AtomicBoolean and CountDownLatch to see how these could help you.
â Thomas
Jan 16 at 19:56
use a static initializer?
â slowy
Jan 17 at 10:20
add a comment |Â
It might be worth to check AtomicBoolean and CountDownLatch to see how these could help you.
â Thomas
Jan 16 at 19:56
use a static initializer?
â slowy
Jan 17 at 10:20
It might be worth to check AtomicBoolean and CountDownLatch to see how these could help you.
â Thomas
Jan 16 at 19:56
It might be worth to check AtomicBoolean and CountDownLatch to see how these could help you.
â Thomas
Jan 16 at 19:56
use a static initializer?
â slowy
Jan 17 at 10:20
use a static initializer?
â slowy
Jan 17 at 10:20
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Ohh...
you do not need to convert a property to integer then again.
Generaly, if you have if-else or switches statement like that and checking if something is initialized, etc, your design is wrong.
private static Properies props;
static
props.put("ABC","from_uri_0âÂÂ);
props.put("CDE","from_uri_1");
//...
And call just props.get("ABC").
Further, if you would do some actions depending of some key, you could use functional features like:
private static Map<String,Function> funcs;// then static init
Then instead of if-else or switch:
thisFunc = funcs.get(string);
thisFunc.action(do stuff);// of whatever the syntax is
Then to import props from Json i would do
private static String fromJson
= new String âÂÂmodeâÂÂ, "ip", "url";// so you can modify the list in one place
private static void populateProperties(JSONObject jo)
for(String str: fromJson )
props.put(str,jo.optString((str);
Then, you do not need have a boolean value if something is initialized, its just bad code. Instead, the exception must be thrown
public static void initEngineProps() throws IOException
loadPropertiesFromLocalFile();// throws IOException since it is reads some file
public static void getToken()
return token;// token must be there if no exception had been thrown
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Ohh...
you do not need to convert a property to integer then again.
Generaly, if you have if-else or switches statement like that and checking if something is initialized, etc, your design is wrong.
private static Properies props;
static
props.put("ABC","from_uri_0âÂÂ);
props.put("CDE","from_uri_1");
//...
And call just props.get("ABC").
Further, if you would do some actions depending of some key, you could use functional features like:
private static Map<String,Function> funcs;// then static init
Then instead of if-else or switch:
thisFunc = funcs.get(string);
thisFunc.action(do stuff);// of whatever the syntax is
Then to import props from Json i would do
private static String fromJson
= new String âÂÂmodeâÂÂ, "ip", "url";// so you can modify the list in one place
private static void populateProperties(JSONObject jo)
for(String str: fromJson )
props.put(str,jo.optString((str);
Then, you do not need have a boolean value if something is initialized, its just bad code. Instead, the exception must be thrown
public static void initEngineProps() throws IOException
loadPropertiesFromLocalFile();// throws IOException since it is reads some file
public static void getToken()
return token;// token must be there if no exception had been thrown
add a comment |Â
up vote
3
down vote
accepted
Ohh...
you do not need to convert a property to integer then again.
Generaly, if you have if-else or switches statement like that and checking if something is initialized, etc, your design is wrong.
private static Properies props;
static
props.put("ABC","from_uri_0âÂÂ);
props.put("CDE","from_uri_1");
//...
And call just props.get("ABC").
Further, if you would do some actions depending of some key, you could use functional features like:
private static Map<String,Function> funcs;// then static init
Then instead of if-else or switch:
thisFunc = funcs.get(string);
thisFunc.action(do stuff);// of whatever the syntax is
Then to import props from Json i would do
private static String fromJson
= new String âÂÂmodeâÂÂ, "ip", "url";// so you can modify the list in one place
private static void populateProperties(JSONObject jo)
for(String str: fromJson )
props.put(str,jo.optString((str);
Then, you do not need have a boolean value if something is initialized, its just bad code. Instead, the exception must be thrown
public static void initEngineProps() throws IOException
loadPropertiesFromLocalFile();// throws IOException since it is reads some file
public static void getToken()
return token;// token must be there if no exception had been thrown
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Ohh...
you do not need to convert a property to integer then again.
Generaly, if you have if-else or switches statement like that and checking if something is initialized, etc, your design is wrong.
private static Properies props;
static
props.put("ABC","from_uri_0âÂÂ);
props.put("CDE","from_uri_1");
//...
And call just props.get("ABC").
Further, if you would do some actions depending of some key, you could use functional features like:
private static Map<String,Function> funcs;// then static init
Then instead of if-else or switch:
thisFunc = funcs.get(string);
thisFunc.action(do stuff);// of whatever the syntax is
Then to import props from Json i would do
private static String fromJson
= new String âÂÂmodeâÂÂ, "ip", "url";// so you can modify the list in one place
private static void populateProperties(JSONObject jo)
for(String str: fromJson )
props.put(str,jo.optString((str);
Then, you do not need have a boolean value if something is initialized, its just bad code. Instead, the exception must be thrown
public static void initEngineProps() throws IOException
loadPropertiesFromLocalFile();// throws IOException since it is reads some file
public static void getToken()
return token;// token must be there if no exception had been thrown
Ohh...
you do not need to convert a property to integer then again.
Generaly, if you have if-else or switches statement like that and checking if something is initialized, etc, your design is wrong.
private static Properies props;
static
props.put("ABC","from_uri_0âÂÂ);
props.put("CDE","from_uri_1");
//...
And call just props.get("ABC").
Further, if you would do some actions depending of some key, you could use functional features like:
private static Map<String,Function> funcs;// then static init
Then instead of if-else or switch:
thisFunc = funcs.get(string);
thisFunc.action(do stuff);// of whatever the syntax is
Then to import props from Json i would do
private static String fromJson
= new String âÂÂmodeâÂÂ, "ip", "url";// so you can modify the list in one place
private static void populateProperties(JSONObject jo)
for(String str: fromJson )
props.put(str,jo.optString((str);
Then, you do not need have a boolean value if something is initialized, its just bad code. Instead, the exception must be thrown
public static void initEngineProps() throws IOException
loadPropertiesFromLocalFile();// throws IOException since it is reads some file
public static void getToken()
return token;// token must be there if no exception had been thrown
edited Jan 18 at 6:04
Natasha
204
204
answered Jan 16 at 17:43
user8426627
1343
1343
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185202%2fdesign-pattern-for-startup-initialisation-of-static-variables-before-an-engine-s%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It might be worth to check AtomicBoolean and CountDownLatch to see how these could help you.
â Thomas
Jan 16 at 19:56
use a static initializer?
â slowy
Jan 17 at 10:20