mardi 16 janvier 2018

Design pattern which could suit for startup initialization of static variables before an engine starts serving its request

I should initialise the startup variables before serving the requests. Few of my variables could be initialised from local property file (say init.properties) and few would get initialised from an url say (/getProperties).

i) I should be able to serve requests from my engine only when all my variables gets initialised (both from local property file and from remote url call)

ii) The first request should get failed 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 me a better design for the following case so, that my code looks easily understandable to everyone..

Aucun commentaire:

Enregistrer un commentaire