<?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>engfer(s) &#187; Java</title>
	<atom:link href="http://www.engfers.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.engfers.com</link>
	<description>» scrumptions blog &#038; code » nothing more, nothing less</description>
	<lastBuildDate>Tue, 06 Oct 2009 21:52:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Equality Testing with Hardcoded Strings</title>
		<link>http://www.engfers.com/2009/10/06/equality-testing-with-hardcoded-strings/</link>
		<comments>http://www.engfers.com/2009/10/06/equality-testing-with-hardcoded-strings/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 21:50:43 +0000</pubDate>
		<dc:creator>engfer</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.engfers.com/?p=1191</guid>
		<description><![CDATA[If you are going to use a String equality comparison that involves a hardcoded string, you might as well save yourself from some NullPointerExceptions and the extra null pointer check, put the hardcoded string as the method call-e instead of the parameter. Instead of: if ( foo.equals( "some static string" ) ){...} or... if ( [...]]]></description>
			<content:encoded><![CDATA[<p>If you are going to use a String equality comparison that involves a hardcoded string, you might as well save yourself from some NullPointerExceptions and the extra null pointer check, put the hardcoded string as the method call-e instead of the parameter.</p>
<p>Instead of:</p>
<pre>if ( foo.equals( "some static string" ) ){...}

or...

if ( foo != null &amp;&amp; foo.equals( "some static string" ) ){...}</pre>
<p>use:<br />
<span id="more-1191"></span></p>
<pre>if ( "some static string".equals( foo ) ){...}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.engfers.com/2009/10/06/equality-testing-with-hardcoded-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exposing/Accessing JBoss JNDI Objects/Datasources From an External JVM</title>
		<link>http://www.engfers.com/2008/08/07/exposing_accessing-jboss-jndi-objects_datasources-from-an-external-jvm/</link>
		<comments>http://www.engfers.com/2008/08/07/exposing_accessing-jboss-jndi-objects_datasources-from-an-external-jvm/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 14:01:17 +0000</pubDate>
		<dc:creator>engfer</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[atg]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[jndi]]></category>

		<guid isPermaLink="false">http://www.engfers.com/?p=161</guid>
		<description><![CDATA[Problem: You have some program that needs to access some data sources that are managed by JBoss and accessed via JNDI, but when you start that program, it can&#8217;t find that JNDI reference or you get a javax.naming.NoInitialContextException with the message &#8220;Need to specify class name in environment or system property, or as an applet [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-177 aligncenter" title="Sad, Broken JNDI Connection to JBoss Data Source" src="http://www.engfers.com/wp-content/uploads/2008/08/external-app-cry.png" alt="" width="410" height="184" /></p>
<h3 class="hrule"><span style="color: #3366ff;">Problem:</span></h3>
<p>You have some program that needs to access some data sources that are managed by JBoss and accessed via JNDI, but when you start that program, it can&#8217;t find that JNDI reference or you get a <strong>javax.naming.NoInitialContextException</strong> with the message &#8220;<em>Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial&#8221;</em></p>
<p><span id="more-161"></span></p>
<h4>Example:</h4>
<p>Our company uses <a href="http://www.atg.com">ATG Portal</a> for our end customer. The ATG Portal + our additions (web aps &#8211; WAR&#8217;s) to the portal get assembled into an <a href="http://en.wikipedia.org/wiki/EAR_(file_format)">EAR</a> file. The EAR file then is deployed onto JBoss, which contains the Tomcat engine to serve up web apps.</p>
<p>You can easily turn at data source into an ATG Component via their JTDataSource pattern by havning your component have a properties file that says&#8230;</p>
<blockquote><p><strong>$class=atg.nucleus.JNDIReference<br />
JNDIName=java:<span style="color: #ff0000;">WhateverDsJndiName</span></strong></p></blockquote>
<p>Now that means that ATG will query JBoss&#8217;s JNDI container to get connections from the JBoss connection pool manager for your datasource. Well how are these defined? Via JBoss <strong>*-ds.xml</strong> files!</p>
<p>You just make a MyNewWhatever<strong>-ds.xml </strong>(&#8220;-ds.xml&#8221; is important&#8230;) that looks like this&#8230;</p>
<p><a title="Example datasource file" href="http://www.engfers.com/wp-content/uploads/2008/08/example-datasource-xml.png"><img class="aligncenter size-full wp-image-164" title="Example datasource file" src="http://www.engfers.com/wp-content/uploads/2008/08/example-datasource-xml.png" alt="" width="500" height="236" /></a></p>
<p><strong><span style="text-decoration: underline;">NOTE</span>: Your &lt;jndi-name&gt; must match the name after the colon for JNDIName property for your component. The &#8220;java:&#8221; part of the JNDIName refers to the scope context for that object. <span style="color: #0000ff;"><em>&#8220;Java&#8221; scoped JNDI components can NOT be seen outside the JVM!!!</em></span></strong></p>
<p>Well ATG has a nice little tool to help you load data into your <a href="http://www.atg.com/repositories/ContentCatalogRepository_en/white_papers/ATG_Data_Anywhere_Architecture_WP.pdf">Repository</a> (or just sanity check your repositories) called <strong>startSQLRepository</strong>. Unfortunately, this script brings up a dumbed down version of the ATG Platform <em>without JBoss</em>, which brings up the repositories, which hits the NewWhateverDsJTDatasource, which queries the JNDI for a connection/transaction manager&#8230;&#8230;</p>
<p><strong>ALARM!!! ERROR!!! EXPLOSIONS!!!</strong> It fails with the <strong>javax.naming.NoInitialContextException</strong>!!!</p>
<p>JBoss is our connection manager and JNDI container but startSQLRepository doesn&#8217;t use JBoss!!!</p>
<p>Oh no we&#8217;re screwed!!! Actually we&#8217;re not&#8230; read on&#8230;</p>
<h3 class="hrule"><span style="color: #3366ff;">Solution:</span></h3>
<p>Well after much reading on JBoss and Redhat&#8217;s sites on <strong>J<a href="http://docs.jboss.org/admin-devel/Chap3.html">Boss JNDI naming service</a></strong>, I found that it&#8217;s not that hard to connect to JBoss&#8217;s JNDI from an external JVM.</p>
<p style="text-align: center;">
<p style="text-align: center;"><a title="JNDI over JNP and HTTP" href="http://www.engfers.com/wp-content/uploads/2008/08/atg-env.png"><img class="size-medium wp-image-166 aligncenter" title="ATG Environment" src="http://www.engfers.com/wp-content/uploads/2008/08/atg-env.png" alt="" width="400" height="277" /></a></p>
<p style="text-align: left;">There are 2 main ways you can connect to JBoss&#8217;s JNDI Context (see above as well)</p>
<ol>
<li><span style="color: #ff0000;"><strong>JNP (Java Network Programming)</strong></span></li>
<li><span style="color: #ff0000;"><strong>HTTP(S)</strong></span></li>
</ol>
<p>The JNDI can now be externalized via these two interfaces. Using JNP, we can create a socket connection to JBoss via the default 1099 port</p>
<p style="text-align: left;">Luckily JBoss has jar&#8217;ed up every-freaking-thing-you-need-in-the-world into a single client jar that your clients must have in their classpath:</p>
<blockquote>
<p style="text-align: left;"># Located in the $JBOSS_HOME/client directory</p>
<p style="text-align: left;"><strong>jbossall-client.jar</strong></p>
</blockquote>
<p style="text-align: left;">You should also include any JDBC drivers for the databases you are connecting to in the client classpath as well.</p>
<p style="text-align: left;">Next, you pass 3 arguments to the client JVM&#8230;</p>
<ul>
<li><span style="text-decoration: underline;">For <strong>JNP</strong> Connection&#8230;</span>
<ul>
<li><span style="color: #3366ff;">-Djava.naming.factory.initial=org.jboss.naming.NamingContextFactory</span></li>
<li><span style="color: #3366ff;">-Djava.naming.provider.url=<strong>jnp://server:1099</strong></span></li>
<li><span style="color: #3366ff;">-Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces</span></li>
</ul>
</li>
<li><span style="text-decoration: underline;">For <strong>HTTP(S)</strong> Connection&#8230;</span>
<ul>
<li><span style="color: #3366ff;">-Djava.naming.factory.initial=org.jboss.naming.<strong>Http</strong>NamingContextFactory</span></li>
<li><span style="color: #3366ff;">-Djava.naming.provider.url=<strong>http://server:8080/invoker/JNDIFactory</strong></span></li>
<li><span style="color: #3366ff;">-Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces</span></li>
</ul>
</li>
</ul>
<p><strong>NOTE</strong>: If you are doing this over SSL (HTTPS), be ready to deal with Secured Socket handshaking and certificates/etc.</p>
<p><strong>NOTE</strong>: Make sure you check your user security information and ports/servlets in the <strong>http-invoker.sar</strong>. You may have to add extra parameters to pass principal (username &amp; password) information onto the inoker servlet.</p>
<p>And we are done right? We run our client and&#8230;</p>
<p><strong>ALARM!!! ERROR!!! EXPLOSIONS!!!</strong> It fails with the <strong>javax.naming.NameNotFoundException</strong></p>
<p>What happened?!?! We forgot that data source in JBoss get added to the &#8220;<em>java</em>&#8221; naming context instead of the global context, and any &#8220;java&#8221; context object can&#8217;t be seen outside of the JVM.</p>
<p>So we need to &#8220;tell&#8221; the data source (via the -ds.xml file) that it shouldn&#8217;t use the java context by default, so just add <strong>&lt;use-java-context&gt;false&lt;/use-java-context&gt;</strong> to your ds file and now your JNDI name will be added to the global context!!</p>
<p><a href="http://www.engfers.com/wp-content/uploads/2008/08/example-datasource-xml_global.png"><img class="alignnone size-medium wp-image-185" title="Example -ds.xml with use-java-context = false" src="http://www.engfers.com/wp-content/uploads/2008/08/example-datasource-xml_global.png" alt="" width="500" height="250" /></a></p>
<p style="text-align: left;">And then they lived happily ever after&#8230;</p>
<p style="text-align: center;"><img class="size-full wp-image-176 aligncenter" title="Happy JNDI Connection to JBoss Data Source" src="http://www.engfers.com/wp-content/uploads/2008/08/external-app-happy.png" alt="" width="410" height="184" /></p>
<p style="text-align: center;"><strong>The End</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.engfers.com/2008/08/07/exposing_accessing-jboss-jndi-objects_datasources-from-an-external-jvm/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
