<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:36:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Java &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Groovy: Handling Byte Order Marks When Reading a File</title>
		<link>https://myshittycode.com/2018/08/28/groovy-java-handling-byte-order-marks-when-reading-a-file/</link>
					<comments>https://myshittycode.com/2018/08/28/groovy-java-handling-byte-order-marks-when-reading-a-file/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 29 Aug 2018 02:11:37 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1087</guid>

					<description><![CDATA[<p>PROBLEM Given a file with the following content:- When reading the file:- &#8230; the following values are printed:- Even though the value is trimmed, there is still a leading space in front of text. A further inspection reveals the leading space is not a regular space:- SOLUTION Some editors prepend a special Unicode character called [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2018/08/28/groovy-java-handling-byte-order-marks-when-reading-a-file/">Groovy: Handling Byte Order Marks When Reading a File</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Given a file with the following content:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
10,20
</pre></div>


<p>When reading the file:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
def inputStream = new FileInputStream(&#039;test.csv&#039;)
def value = inputStream.text.trim()

println &quot;|${value}|&quot;
</pre></div>


<p>&#8230; the following values are printed:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
| 10,20|
</pre></div>


<p>Even though the value is trimmed, there is still a leading space in front of text.</p>



<p>A further inspection reveals the leading space is not a regular space:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
// first character is not a space
assert value.charAt(0) != (char) &#039; &#039;

// ASCII value: 65279 vs 32
assert (int) value.charAt(0) != (int) ((char) &#039; &#039;).charValue()
</pre></div>


<h2 class="wp-block-heading">SOLUTION</h2>



<p>Some editors prepend a special Unicode character called a byte order mark (BOM) to the file.</p>



<p>The simplest way to remove this special character is to leverage Apache Commons IO&#8217;s <strong>BOMInputStream</strong>:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [1]; title: ; notranslate">
def inputStream = new BOMInputStream(new FileInputStream(&#039;test.csv&#039;))
def value = inputStream.text.trim()

println &quot;|${value}|&quot;
</pre></div>


<p>&#8230; and now, the values are printed correctly:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
|10,20|
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2018/08/28/groovy-java-handling-byte-order-marks-when-reading-a-file/">Groovy: Handling Byte Order Marks When Reading a File</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2018/08/28/groovy-java-handling-byte-order-marks-when-reading-a-file/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1087</post-id>	</item>
		<item>
		<title>JEE Security: Preventing Clickjacking Attacks</title>
		<link>https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/</link>
					<comments>https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 31 Aug 2017 15:03:05 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Spring Security]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1066</guid>

					<description><![CDATA[<p>PROBLEM Clickjacking is an attack that tricks the users to perform unintended actions&#8230; see OWASP&#8217;s Testing for Clickjacking (OTG-CLIENT-009) SOLUTION To prevent clickjacking attacks, the app must set X-FRAME-OPTIONS header with an appropriate value:- If set correctly, the HTTPS response should show X-FRAME-OPTIONS header:- There are several ways to set this header. Solution 1: Using [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/">JEE Security: Preventing Clickjacking Attacks</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Clickjacking is an attack that tricks the users to perform unintended actions&#8230; see <a href="https://www.owasp.org/index.php/Testing_for_Clickjacking_(OTG-CLIENT-009)" target="_blank" rel="noopener">OWASP&#8217;s Testing for Clickjacking (OTG-CLIENT-009)</a></p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>To prevent clickjacking attacks, the app must set X-FRAME-OPTIONS header with an appropriate value:-</p>



<ul class="wp-block-list">
<li><b>DENY</b>: this denies any domain using the page as an iFrame source. This is the best option.</li>



<li><b>SAMEORIGIN</b>: this allows pages within the same domain to use other application pages as iFrame sources.</li>



<li><b>ALLOW-FROM [whitelisted domains]</b>: this declares a list of domains that are allowed to include the pages as iFrame sources.</li>
</ul>



<p>If set correctly, the HTTPS response should show X-FRAME-OPTIONS header:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [9]; title: ; notranslate">
➜  ~ curl -i -k https://localhost:8443/
HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
X-Application-Context: application:local:8443
Set-Cookie: JSESSIONID=04ADDAF886A20AA561021E869E980BCC; Path=/; Secure; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Content-Length: 631
Date: Thu, 31 Aug 2017 14:56:57 GMT
</pre></div>


<p>There are several ways to set this header.</p>



<h3 class="wp-block-heading">Solution 1: Using a servlet filter</h3>



<p>You may create a servlet filter that sets X-FRAME-OPTIONS in the response header.</p>



<p>Here&#8217;s an example using web.xml-less Spring Boot:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35]; title: ; notranslate">
@SpringBootApplication
class Application extends SpringBootServletInitializer {
    static void main(String&#x5B;] args) {
        SpringApplication.run(Application, args)
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application)
    }

    @Bean
    FilterRegistrationBean clickjackingPreventionFilter() {
        return new FilterRegistrationBean(
                urlPatterns: &#x5B;&#039;/**&#039;],
                filter: new Filter() {
                    @Override
                    void init(final FilterConfig filterConfig) throws ServletException {
                    }

                    @Override
                    void doFilter(final ServletRequest servletRequest,
                                  final ServletResponse servletResponse,
                                  final FilterChain filterChain) throws IOException, ServletException {
                        final HttpServletResponse response = (HttpServletResponse) servletResponse
                        response.addHeader(&#039;X-FRAME-OPTIONS&#039;, &#039;DENY&#039;)
                        filterChain.doFilter(servletRequest, servletResponse)
                    }

                    @Override
                    void destroy() {
                    }
                }
        )
    }
}
</pre></div>


<h3 class="wp-block-heading">Solution 2: Using Spring Security</h3>



<p>Spring Security provides a very easy way to set the X-FRAME-OPTIONS header:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [7]; title: ; notranslate">
@Configuration
@EnableWebSecurity
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.
                headers().frameOptions().deny().
                and().
                authorizeRequests().
                antMatchers(&#039;/**&#039;).permitAll()
    }
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/">JEE Security: Preventing Clickjacking Attacks</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1066</post-id>	</item>
		<item>
		<title>JEE Security: Disabling HTTP OPTIONS method</title>
		<link>https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/</link>
					<comments>https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 31 Aug 2017 14:37:04 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1062</guid>

					<description><![CDATA[<p>PROBLEM HTTP OPTIONS method is used to provide a list of methods that are supported by the web server. For example, the following shows both GET and HEAD are allowed on the given link:- Enabling OPTIONS may increase the risk of cross-site tracing (XST)&#8230; see OWASP&#8217;s Test HTTP Methods (OTG-CONFIG-006). SOLUTION There are several ways [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/">JEE Security: Disabling HTTP OPTIONS method</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>HTTP OPTIONS method is used to provide a list of methods that are supported by the web server.</p>



<p>For example, the following shows both GET and HEAD are allowed on the given link:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [11]; title: ; notranslate">
➜  ~ curl -i -k -X OPTIONS https://localhost:8443/
HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
X-Application-Context: application:local:8443
Allow: GET,HEAD
Content-Length: 0
Date: Thu, 31 Aug 2017 14:07:21 GMT
</pre></div>


<p>Enabling OPTIONS may increase the risk of cross-site tracing (XST)&#8230; see <a href="https://www.owasp.org/index.php/Testing_for_HTTP_Methods_and_XST_(OWASP-CM-008)" target="_blank" rel="noopener">OWASP&#8217;s Test HTTP Methods (OTG-CONFIG-006)</a>.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>There are several ways to disable OPTIONS method.</p>



<h3 class="wp-block-heading">Solution 1: Using web.xml</h3>



<p>If your app has <b>web.xml</b>, you may add the following snippet:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [5,6,7,8,9,10,11,12,13]; title: ; notranslate">
&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?--&gt;
&lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemalocation=&quot;http://xmlns.jcp.org/xml/ns/javaee
		 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot; metadata-complete=&quot;true&quot; version=&quot;3.1&quot;&gt;

    &lt;security-constraint&gt;
        &lt;web-resource-collection&gt;
            &lt;web-resource-name&gt;restricted methods&lt;/web-resource-name&gt;
            &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
            &lt;http-method&gt;OPTIONS&lt;/http-method&gt;
        &lt;/web-resource-collection&gt;
        &lt;auth-constraint&gt;
        &lt;/auth-constraint&gt;
    &lt;/security-constraint&gt;

    &lt;!-- Other configurations --&gt;
&lt;/web-app&gt;
</pre></div>


<h3 class="wp-block-heading">Solution 2: Using Spring Boot</h3>



<p>If you are using Spring Boot, there isn&#8217;t any option to mimic the above configuration programmatically.</p>



<p>However, you still can use <b>web.xml</b> in conjunction with Spring Boot by setting <b>metadata-complete</b> to <b>false</b> and use servlet version 3.0 or higher:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [3]; title: ; notranslate">
&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?--&gt;
&lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemalocation=&quot;http://xmlns.jcp.org/xml/ns/javaee
		 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot; metadata-complete=&quot;false&quot; version=&quot;3.1&quot;&gt;

    &lt;security-constraint&gt;
        &lt;web-resource-collection&gt;
            &lt;web-resource-name&gt;restricted methods&lt;/web-resource-name&gt;
            &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
            &lt;http-method&gt;OPTIONS&lt;/http-method&gt;
        &lt;/web-resource-collection&gt;
        &lt;auth-constraint&gt;
    &lt;/auth-constraint&gt;&lt;/security-constraint&gt;
&lt;/web-app&gt;
</pre></div>


<h3 class="wp-block-heading">Solution 3: Using Spring Security</h3>



<p>If you don&#8217;t want to use <b>web.xml</b>, you may configure Spring Security to disable OPTIONS method on all URIs:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [7]; title: ; notranslate">
@Configuration
@EnableWebSecurity
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests().
                antMatchers(HttpMethod.OPTIONS, &#039;/**&#039;).denyAll().
                antMatchers(&#039;/**&#039;).permitAll()
    }
}
</pre></div>


<p>Now, when trying to hit the same link with OPTIONS method, the app will return 403 Forbidden:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [2]; title: ; notranslate">
➜  ~ curl -i -k -X OPTIONS https://localhost:8443/
HTTP/1.1 403
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 31 Aug 2017 14:26:51 GMT
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/">JEE Security: Disabling HTTP OPTIONS method</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1062</post-id>	</item>
		<item>
		<title>Java: Exploring Preferences API</title>
		<link>https://myshittycode.com/2017/04/20/java-exploring-preferences-api/</link>
					<comments>https://myshittycode.com/2017/04/20/java-exploring-preferences-api/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 20 Apr 2017 16:07:47 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=964</guid>

					<description><![CDATA[<p>BACKGROUND In any written scripts or rich client apps, there is almost a need to persist the user preferences or app configurations. Most of the time, we, the proud developers, handle that situation in very ad-hoc manner. When storing in a file, we use different formats: from old-boring XML, to cool-kid JSON, to even cooler-kid [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/04/20/java-exploring-preferences-api/">Java: Exploring Preferences API</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">BACKGROUND</h2>



<p>In any written scripts or rich client apps, there is almost a need to persist the user preferences or app configurations.</p>



<p>Most of the time, we, the proud developers, handle that situation in very ad-hoc manner. When storing in a file, we use different formats: from old-boring XML, to cool-kid JSON, to even cooler-kid YAML or the kindergarten-kid key=value property. Then, we have to decide where to write the file to, whether to use C:\ and screw your non-windows users, whether to use backslashes to construct the file path or forward slashes because we are sick and tired escaping the effing backslashes.</p>



<p>The long story short is&#8230; yes, we, the proud developers, can do all that&#8230; or, as one of my current project peers like to say, &#8220;make it configurable&#8221; on literally everything to the point it&#8217;s pretty close of becoming a drinking game now.</p>



<p>But, the point I want to make here is&#8230; we are consistent on being inconsistent.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>Java provides the Preferences API as an attempt to solve this mess. Using this API, the developers do not need to know where or how to store the user preferences or app configurations. Rather, it relies on the native API to store the data: registry for Windows, <b>.plist</b> for Mac and XML for Unix/Linux.</p>



<p>The most interesting part is&#8230; the Preferences API has been around since JDK 1.4.</p>



<p>Code wise, it doesn&#8217;t get any simpler than this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
// create new configuration or reference existing configuration
Preferences preferences = Preferences.userNodeForPackage(WuTangClan)

// insert/update 3 key/value pairs
preferences.put(&#039;key1&#039;, &#039;value1&#039;)
preferences.put(&#039;key2&#039;, &#039;value2&#039;)
preferences.put(&#039;key3&#039;, &#039;value3&#039;)

// returns &#039;value2&#039;
println preferences.get(&#039;key2&#039;, &#039;-&#039;)

// returns &#039;-&#039;
println preferences.get(&#039;invalid&#039;, &#039;-&#039;)

// remove by key
preferences.remove(&#039;key3&#039;)

// delete everything
preferences.removeNode()
</pre></div>


<p>But, where and how exactly do Mac and Windows store this data?</p>



<p>There are several ways to get an instance of <b>Preferences</b>.</p>



<h2 class="wp-block-heading">Preferences.userNodeForPackage(WuTangClan)</h2>



<h3 class="wp-block-heading">Mac</h3>



<p>If <b>WuTangClan</b> class file is located under <b>wu.tang.clan.config</b> package, the configuration file is created at <b>~/Library/Preferences/wu.tang.clan.plist</b> with the following content:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
{    &quot;/wu/tang/clan/&quot; = {
        &quot;config/&quot; = {
            &quot;key1&quot; = &quot;value1&quot;;
            &quot;key2&quot; = &quot;value2&quot;;
        };
    };
}
</pre></div>


<h3 class="wp-block-heading">64-bit Windows + 64-bit JVM</h3>



<p>Configuration is stored in the registry with the following key:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
HKEY_CURRENT_USER\SOFTWARE\JavaSoft\Prefs\wu\tang\clan\config
</pre></div>


<h2 class="wp-block-heading">Preferences.userRoot().node(&#8216;path&#8217;)</h2>



<h3 class="wp-block-heading">Example 1</h3>



<p>Let&#8217;s assume we have this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
Preferences.userRoot().node(&#039;wu&#039;)
</pre></div>


<h3 class="wp-block-heading">Mac</h3>



<p>The configuration is created at <b>~/Library/Preferences/com.apple.java.util.prefs.plist</b> with the following content:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
{    &quot;/&quot; = {
        ...

        &quot;wu/&quot; = {
            &quot;key1&quot; = &quot;value1&quot;;
            &quot;key2&quot; = &quot;value2&quot;;
        };

        ...
    };
}
</pre></div>


<p>This file also contains configurations from other installed software.</p>



<h3 class="wp-block-heading">64-bit Windows + 64-bit JVM</h3>



<p>Configuration is stored in the registry with the following key:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
HKEY_CURRENT_USER\SOFTWARE\JavaSoft\Prefs\wu
</pre></div>


<h3 class="wp-block-heading">Example 2</h3>



<p>How about this?</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
Preferences.userRoot().node(&#039;wu/tang&#039;)

// ... OR ...

Preferences.userRoot().node(&#039;wu&#039;).node(&#039;tang&#039;)
</pre></div>


<h3 class="wp-block-heading">Mac</h3>



<p>The configuration still resides under <b>~/Library/Preferences/com.apple.java.util.prefs.plist</b> with the following content:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
{    &quot;/&quot; = {
        ...
        &quot;wu/&quot; = {
            &quot;tang/&quot; = {
                &quot;key1&quot; = &quot;value1&quot;;
                &quot;key2&quot; = &quot;value2&quot;;
            };
        };
        ...
    };
}
</pre></div>


<h3 class="wp-block-heading">64-bit Windows + 64-bit JVM</h3>



<p>Configuration is stored in the registry with the following key:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
HKEY_CURRENT_USER\SOFTWARE\JavaSoft\Prefs\wu\tang
</pre></div>


<h3 class="wp-block-heading">Example 3</h3>



<p>How about this?</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
Preferences.userRoot().node(&#039;wu/tang/clan&#039;)

// ... OR ...

Preferences.userRoot().node(&#039;wu&#039;).node(&#039;tang&#039;).node(&#039;clan&#039;)
</pre></div>


<h3 class="wp-block-heading">Mac</h3>



<p>Now, the shit is about to get real here.</p>



<p>Mac, for some reason, creates a stub under <b>~/Library/Preferences/com.apple.java.util.prefs.plist</b> with the following content:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
{    &quot;/&quot; = {
        ...
        &quot;wu/&quot; = { &quot;tang/&quot; = { &quot;clan/&quot; = { }; }; };
        ...
    };
}
</pre></div>


<p>The actual configuration now resides under <b>~/Library/Preferences/wu.tang.clan.plist</b>:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
{    &quot;/wu/tang/clan/&quot; = {
        &quot;key1&quot; = &quot;value1&quot;;
        &quot;key2&quot; = &quot;value2&quot;;
    };
}
</pre></div>


<p>It appears when the path reaches certain depth, Mac will create the separate configuration file for it.</p>



<h3 class="wp-block-heading">64-bit Windows + 64-bit JVM</h3>



<p>Configuration is stored in the registry with the following key:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
HKEY_CURRENT_USER\SOFTWARE\JavaSoft\Prefs\wu\tang\clan
</pre></div>


<h2 class="wp-block-heading">Preferences.systemNodeForPackage(WuTangClan) or Preferences.systemRoot().node(&#8216;path&#8217;)</h2>



<p>Instead of storing the configuration at user level, we may also store the configuration at system level.</p>



<h3 class="wp-block-heading">Mac</h3>



<p>Instead of storing under <b>~/Library/Preferences</b>, the configuration is stored under <b>/Library/Preferences</b>.</p>



<p>On top of that, based on <a href="https://developer.apple.com/library/content/documentation/Java/Conceptual/Java14Development/05-CoreJavaAPIs/CoreJavaAPIs.html" target="_blank" rel="noopener">Java Development Guide for Mac</a>, the configuration is only persisted if the user is an administrator.</p>



<p>The really weird part is the code will not throw any exceptions due to insufficient permission.</p>



<h3 class="wp-block-heading">64-bit Windows + 64-bit JVM</h3>



<p>Instead of storing under <b>HKEY_CURRENT_USER\[path]</b>, the configuration is stored under <b>HKEY_LOCAL_MACHINE\[path]</b>.</p>



<h2 class="wp-block-heading">Best Practices</h2>



<p>I&#8217;m not sure if this is a best practice, but my personal preference is to specify my own string path through <b>Preferences.userRoot().node(..)</b>.</p>



<p><b>Preferences.userNodeForPackage(..)</b> worries me because if I refactor my code by moving the class files around, it may not find the existing configuration due to changed path.</p>



<p>When specifying the string path, do make sure the path value is rather unique to prevent reading an existing configuration from other installed software.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/04/20/java-exploring-preferences-api/">Java: Exploring Preferences API</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/04/20/java-exploring-preferences-api/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">964</post-id>	</item>
		<item>
		<title>JAXB2: Adding toString() to Generated Java Classes</title>
		<link>https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/</link>
					<comments>https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 07 Mar 2017 16:09:09 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JAXB 2]]></category>
		<category><![CDATA[JAXB-2 Maven Plugin]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=915</guid>

					<description><![CDATA[<p>PROBLEM By default, the generated Java class prints the memory address when toString() is invoked. However, sometimes it is helpful to have a more meaningful toString() for debugging purposes. SOLUTION To fix this, configure maven-jaxb2-plugin to generate toString() based on the fields in the class:-</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/">JAXB2: Adding toString() to Generated Java Classes</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>By default, the generated Java class prints the memory address when <b>toString()</b> is invoked.</p>



<p>However, sometimes it is helpful to have a more meaningful <b>toString()</b> for debugging purposes.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>To fix this, configure <b>maven-jaxb2-plugin</b> to generate <b>toString()</b> based on the fields in the class:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [5,9,10,11,12,13,46,47,48,49,50,51,52,53,54,55]; title: ; notranslate">
&lt;project ...=&quot;&quot;&gt;
  ...

  &lt;properties&gt;
    &lt;org.jvnet.jaxb2_commons.version&gt;0.11.1&lt;/org.jvnet.jaxb2_commons.version&gt;
  &lt;/properties&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupid&gt;org.jvnet.jaxb2_commons&lt;/groupid&gt;
      &lt;artifactid&gt;jaxb2-basics-runtime&lt;/artifactid&gt;
      &lt;version&gt;${org.jvnet.jaxb2_commons.version}&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupid&gt;org.jvnet.jaxb2.maven2&lt;/groupid&gt;
        &lt;artifactid&gt;maven-jaxb2-plugin&lt;/artifactid&gt;
        &lt;version&gt;0.13.1&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;goals&gt;
              &lt;goal&gt;generate&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        &lt;configuration&gt;
          &lt;schemalanguage&gt;WSDL&lt;/schemalanguage&gt;
          &lt;generatepackage&gt;my.package.wsdl&lt;/generatepackage&gt;
          &lt;bindingdirectory&gt;${project.basedir}/src/main/resources&lt;/bindingdirectory&gt;
          &lt;bindingincludes&gt;
            &lt;include&gt;jaxb-binding.xjb&lt;/include&gt;
          &lt;/bindingincludes&gt;
          &lt;schemas&gt;
            &lt;schema&gt;
              &lt;fileset&gt;
                &lt;directory&gt;${project.basedir}/src/main/resources&lt;/directory&gt;
                &lt;includes&gt;
                  &lt;include&gt;web-service.wsdl&lt;/include&gt;
                &lt;/includes&gt;
              &lt;/fileset&gt;
            &lt;/schema&gt;
          &lt;/schemas&gt;
          &lt;args&gt;
            &lt;arg&gt;-XtoString&lt;/arg&gt;
          &lt;/args&gt;
          &lt;plugins&gt;
            &lt;plugin&gt;
              &lt;groupid&gt;org.jvnet.jaxb2_commons&lt;/groupid&gt;
              &lt;artifactid&gt;jaxb2-basics&lt;/artifactid&gt;
              &lt;version&gt;${org.jvnet.jaxb2_commons.version}&lt;/version&gt;
            &lt;/plugin&gt;
          &lt;/plugins&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
&lt;/project&gt;
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/">JAXB2: Adding toString() to Generated Java Classes</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">915</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-19 08:57:38 by W3 Total Cache
-->