{"id":2904,"date":"2018-10-07T09:30:37","date_gmt":"2018-10-07T09:30:37","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2904"},"modified":"2018-10-07T10:08:41","modified_gmt":"2018-10-07T10:08:41","slug":"abstract-factory-pattern-reading-config-from-json","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/abstract-factory-pattern-reading-config-from-json\/","title":{"rendered":"Abstract Factory Pattern &#8211; Reading Config from JSON"},"content":{"rendered":"<p><strong>How it Works<\/strong><\/p>\n<ol>\n<li>We have JSON file with Location and Name of Factory Class<\/li>\n<li>Using Configuration.java we read the JSON File and read the Configuration<\/li>\n<li>We run TasteFoodFromMenu.java by supplying the JSON file Location as Input<\/li>\n<li>Now by the above method the JAR&#8217;s could be built independently and by changing the JSON file we could make changes and deploy the code without restarting the Server<\/li>\n<\/ol>\n<p>For more detail refer <a href=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2018\/10\/AbstractFactPattReadingConfig.zip\">here<\/a><\/p>\n<p><strong>Configuration.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.IOException;\r\nimport java.nio.charset.StandardCharsets;\r\nimport java.nio.file.FileSystems;\r\nimport java.nio.file.Files;\r\nimport java.nio.file.Path;\r\n\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\npublic class Configuration {\r\n\r\n    public static Configuration loadConfiguration(String fileName) throws IOException {    \t\r\n    \t\/\/Read Name of File\r\n        Path path = FileSystems.getDefault().getPath(fileName);\r\n        \r\n        \/\/Read Contents of File\r\n        String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);\r\n        \r\n        ObjectMapper mapper = new ObjectMapper();\r\n        \r\n        \/\/Map Location and FactoryType\r\n        Configuration config = mapper.readValue(contents, Configuration.class);\r\n        return config;\r\n    }\r\n\r\n    private String factoryType;\r\n    private String location;\r\n\r\n\r\n    public String getLocation() {\r\n        return location;\r\n    }\r\n\r\n    public void setLocation(String location) {\r\n        this.location = location;\r\n    }\r\n\r\n    public String getFactoryType() {\r\n        return factoryType;\r\n    }\r\n\r\n    public void setFactoryType(String factoryType) {\r\n        this.factoryType = factoryType;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>TasteFoodFromMenu.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class TasteFoodFromMenu {\r\n\t  public static void main(String&#x5B;] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {\r\n\t        Configuration configuration = Configuration.loadConfiguration(args&#x5B;0]);\r\n\t        String location = configuration.getLocation();\r\n\t        URL url = new URL(location);\r\n\t        URLClassLoader ucl = new URLClassLoader(new URL&#x5B;]{url});\r\n\t        Class&lt;IFoodFactory&gt; cls = (Class&lt;IFoodFactory&gt;) Class.forName(configuration.getFactoryType(), true, ucl);\r\n\t        IFoodFactory cameraFactory = cls.newInstance();\r\n\t        IFood camera = cameraFactory.prepareFood(); \r\n\t        camera.serveFood();\r\n\t    }\r\n}\r\n<\/pre>\n<p><strong>config.json<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;factoryType&quot;: &quot;com.mugil.food.ChineseFoodFactory&quot;,\r\n  &quot;location&quot;: &quot;file:\/\/\/D:\/java\/ReadConfFromJSON\/lib\/FoodMenu.jar&quot;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How it Works We have JSON file with Location and Name of Factory Class Using Configuration.java we read the JSON File and read the Configuration We run TasteFoodFromMenu.java by supplying the JSON file Location as Input Now by the above method the JAR&#8217;s could be built independently and by changing the JSON file we could&hellip; <a href=\"https:\/\/codethataint.com\/blog\/abstract-factory-pattern-reading-config-from-json\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[228],"tags":[],"class_list":["post-2904","post","type-post","status-publish","format-standard","hentry","category-factory-pattern"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2904"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2904\/revisions"}],"predecessor-version":[{"id":2910,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2904\/revisions\/2910"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}