Design pattern for startup initialisation of static variables before an engine starts serving its request

The name of the pictureThe name of the pictureThe name of the pictureClash 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).



  1. 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).


  2. 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?







share|improve this question





















  • 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
















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).



  1. 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).


  2. 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?







share|improve this question





















  • 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












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).



  1. 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).


  2. 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?







share|improve this question













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).



  1. 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).


  2. 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?









share|improve this question












share|improve this question




share|improve this question








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
















  • 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










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







share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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







    share|improve this answer



























      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







      share|improve this answer

























        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







        share|improve this answer















        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








        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jan 18 at 6:04









        Natasha

        204




        204











        answered Jan 16 at 17:43









        user8426627

        1343




        1343






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?