{"id":360,"date":"2006-12-30T00:26:57","date_gmt":"2006-12-30T00:26:57","guid":{"rendered":"http:\/\/www.bartbusschots.ie\/blog\/?p=360"},"modified":"2014-08-04T14:50:41","modified_gmt":"2014-08-04T14:50:41","slug":"simple-java-configuration-files-and-no-xml-in-sight","status":"publish","type":"post","link":"https:\/\/www.bartbusschots.ie\/s\/2006\/12\/30\/simple-java-configuration-files-and-no-xml-in-sight\/","title":{"rendered":"Simple Java Configuration Files and no XML in Sight"},"content":{"rendered":"<p>The last time I discussed Java configuration files it was from the point of locating them on the disk the right way. This time I want to comment on the content of configuration files. There seems to be an obsession with XML in the modern world. Some people seem to think that shoe-horning XML into their applications will somehow magically make them great. I don&#8217;t want to completely put down XML because it most certainly has its uses. In fact I use it quite a bit to store complex and potentially incomplete data sets. However, using XML to store simple configuration information is over-kill and makes the configuration file needlessly complex to edit and needlessly complicates your application. Unless you&#8217;re writing something huge or some thing complex the chances are you&#8217;re configuration file won&#8217;t need to be complicated. The chances are all you really need are some name-value pairs to specify a few parameters. If this is the case Java comes with a wonderfully simple solution right out of the box, <code>.properties<\/code> files.<\/p>\n<p>[tags]Java, XML, Configuration Files[\/tags]<\/p>\n<p><!--more-->The syntax for a <code>.properties<\/code> file could not be simpler; all lines beginning with a <code>#<\/code> are comments and all other lines are in the form:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;key&gt;=&lt;value&gt;\r\n<\/pre>\n<p>Below is a sample configuration for the Virtual Learning environment I&#8217;m working on:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# Basic portal config\r\nBASE_URL=www.eve.nuim.ie\/evePortal\r\n\r\n# Database stuff\r\nDB_JNDI_NAME=jdbc\/eve_portal\r\n\r\n# File locations\r\nDATA_DIR=\/var\/eve-data\r\nPDFLATEX_BIN=\/opt\/local\/bin\/pdflatex\r\nIMAGEMAGICK_BIN_DIR=\/opt\/local\/bin\r\n<\/pre>\n<p>If you choose to use this type of configuration file you can locate and open it all in one step by using functionality built into the java class loader as follows:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nProperties configFile = new Properties();\r\nconfigFile.load(this.getClass().getClassLoader().getResourceAsStream(&quot;\/my_config.properties&quot;));\r\n<\/pre>\n<p>You can they read out any key in the following way:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsome_var = configFile.getProperty(&quot;some_key&quot;);\r\n<\/pre>\n<p>Simple as that, no need to load complex parsing libraries, no big long messy code. Unless you have a good reason to go with a  more complex format you really should be using <code>.properties<\/code> files.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The last time I discussed Java configuration files it was from the point of locating them on the disk the right way. This time I want to comment on the content of configuration files. There seems to be an obsession with XML in the modern world. Some people seem to think that shoe-horning XML into [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[12],"tags":[5],"series":[],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-computers-tech","tag-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7t9xK-5O","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":1,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":7394,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/360\/revisions\/7394"}],"wp:attachment":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/tags?post=360"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/series?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}