<?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>CB1, INC. &#187; java</title>
	<atom:link href="http://www.cb1inc.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cb1inc.com</link>
	<description></description>
	<lastBuildDate>Wed, 28 Sep 2011 17:54:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>MethodSelectionException When Invoking Java From ColdFusion MX</title>
		<link>http://www.cb1inc.com/2007/06/14/methodselectionexception-when-invoking-java-from-coldfusion-mx/</link>
		<comments>http://www.cb1inc.com/2007/06/14/methodselectionexception-when-invoking-java-from-coldfusion-mx/#comments</comments>
		<pubDate>Thu, 14 Jun 2007 17:50:16 +0000</pubDate>
		<dc:creator>Chris Barber</dc:creator>
				<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[In ColdFusion MX, you can create a Java object by calling the handy <a href="http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functi42.htm#wp1102241" target="_blank"><code>CreateObject()</code></a> function.

<pre>&#60;CFSET myObj = CreateObject("java", "java.util.Hashtable")&#62;</pre>

Suppose you have the following Java class:

<div class="cb1-docs-code"><pre>class Person {
    private String name;

    public void setName(String n) {
       this.name = n;
    }
}</pre></div>

Now, let's create an instance in ColdFusion and set a name:

<div class="cb1-docs-code"><pre>&#60;CFSET somebodySpecial = CreateObject("java", "Person")&#62;
&#60;CFSET somebodySpecial.setName("Chuck")&#62;</pre></div>

Cool, but what happens if you pass an argument that isn't a string:

<div class="cb1-docs-code"><pre>&#60;CFSET somebodySpecial.setName(123)&#62;

coldfusion.runtime.java.MethodSelectionException:
    The selected method setName was not found.</pre></div>

Whoops!  ColdFusion wasn't able to find a method of <code>Person</code> with a signature of "<code>public void setName(int)</code>".  What you need to do is cast the arguments to the correct datatype.  For this example, you could do something like this:

<div class="cb1-docs-code"><pre>&#60;CFSET somebodySpecial.setName(ToString(123))&#62;</pre></div>

The problem is what if you need to call methods that use other data types such as int, double, or boolean.  <code>ToString()</code> won't cut it.  Instead use ColdFusion's <a href="http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funct147.htm" target="_blank"><code>JavaCast()</code></a> function:

<div class="cb1-docs-code"><pre>JavaCast(type, variable)

type        "boolean"
            "int"
            "long"
            "float"
            "double"
            "String"

variable    A ColdFusion variable that holds a scalar or string type
</pre></div>

So, the correct way is:

<div class="cb1-docs-code"><pre>&#60;CFSET somebodySpecial.setName(JavaCast("String", 123))&#62;
&#60;CFSET somebodySpecial.setName(JavaCast("String", "Chuck"))&#62;</pre></div>

No more MethodSelectionExceptions!]]></description>
			<content:encoded><![CDATA[<p>In ColdFusion MX, you can create a Java object by calling the handy <a href="http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functi42.htm#wp1102241" target="_blank"><code>CreateObject()</code></a> function.</p>
<pre class="brush: xml; title: ;">
&lt;CFSET myObj = CreateObject(&quot;java&quot;, &quot;java.util.Hashtable&quot;)&gt;
</pre>
<p>Suppose you have the following Java class:</p>
<pre class="brush: java; title: ;">
class Person {
    private String name;

    public void setName(String n) {
       this.name = n;
    }
}
</pre>
<p>Now, let&#8217;s create an instance in ColdFusion and set a name:</p>
<pre class="brush: xml; title: ;">
&lt;CFSET somebodySpecial = CreateObject(&quot;java&quot;, &quot;Person&quot;)&gt;
&lt;CFSET somebodySpecial.setName(&quot;Chuck&quot;)&gt;
</pre>
<p>Cool, but what happens if you pass an argument that isn&#8217;t a string:</p>
<pre class="brush: xml; title: ;">
&lt;CFSET somebodySpecial.setName(123)&gt;
</pre>
<pre class="brush: plain; title: ;">
coldfusion.runtime.java.MethodSelectionException:
    The selected method setName was not found.
</pre>
<p>Whoops!  ColdFusion wasn&#8217;t able to find a method of <code>Person</code> with a signature of &#8220;<code>public void setName(int)</code>&#8220;.  What you need to do is cast the arguments to the correct datatype.  For this example, you could do something like this:</p>
<pre class="brush: xml; title: ;">
&lt;CFSET somebodySpecial.setName(ToString(123))&gt;
</pre>
<p>The problem is what if you need to call methods that use other data types such as int, double, or boolean.  <code>ToString()</code> won&#8217;t cut it.  Instead use ColdFusion&#8217;s <a href="http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funct147.htm" target="_blank"><code>JavaCast()</code></a> function:</p>
<pre class="brush: plain; title: ;">
JavaCast(type, variable)

type        &quot;boolean&quot;
            &quot;int&quot;
            &quot;long&quot;
            &quot;float&quot;
            &quot;double&quot;
            &quot;String&quot;

variable    A ColdFusion variable that holds a scalar or string type
</pre>
<p>So, the correct way is:</p>
<pre class="brush: xml; title: ;">
&lt;CFSET somebodySpecial.setName(JavaCast(&quot;String&quot;, 123))&gt;
&lt;CFSET somebodySpecial.setName(JavaCast(&quot;String&quot;, &quot;Chuck&quot;))&gt;
</pre>
<p>No more MethodSelectionExceptions!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cb1inc.com/2007/06/14/methodselectionexception-when-invoking-java-from-coldfusion-mx/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Setting the Default JVM on Ubuntu</title>
		<link>http://www.cb1inc.com/2007/05/30/setting-the-default-jvm-on-ubuntu/</link>
		<comments>http://www.cb1inc.com/2007/05/30/setting-the-default-jvm-on-ubuntu/#comments</comments>
		<pubDate>Wed, 30 May 2007 18:13:17 +0000</pubDate>
		<dc:creator>Chris Barber</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[There are circumstances where you may need to have more than one version of Java installed, but how do you switch between them?

Execute the following command to list the installed JVMs:

<div class="cb1-docs-code"><pre>$ <b>sudo update-alternatives --config java</b>

There are 3 alternatives which provide `java'.

  Selection    Alternative
-----------------------------------------------
          1    /usr/bin/gij-wrapper-4.1
*         2    /usr/lib/jvm/java-1.5.0-sun/jre/bin/java
 +        3    /usr/lib/j2se/1.4/bin/java

Press enter to keep the default[*], or type selection number: 
</pre></div>

Simply enter the number for the JVM to use.  The * shows the current selected JVM.]]></description>
			<content:encoded><![CDATA[<p>There are circumstances where you may need to have more than one version of Java installed, but how do you switch between them?</p>
<p>Execute the following command to list the installed JVMs:</p>
<pre class="brush: plain; title: ;">
sudo update-alternatives --config java
</pre>
<p>Which will output something similar to the following:</p>
<pre class="brush: plain; title: ;">
There are 3 alternatives which provide `java'.

  Selection    Alternative
-----------------------------------------------
          1    /usr/bin/gij-wrapper-4.1
*         2    /usr/lib/jvm/java-1.5.0-sun/jre/bin/java
 +        3    /usr/lib/j2se/1.4/bin/java

Press enter to keep the default[*], or type selection number:
</pre>
<p>Simply enter the number for the JVM to use. The * shows the current selected JVM.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cb1inc.com/2007/05/30/setting-the-default-jvm-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java Anonymous Inner Classes</title>
		<link>http://www.cb1inc.com/2007/05/26/java-anonymous-inner-classes/</link>
		<comments>http://www.cb1inc.com/2007/05/26/java-anonymous-inner-classes/#comments</comments>
		<pubDate>Sat, 26 May 2007 16:36:29 +0000</pubDate>
		<dc:creator>Chris Barber</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Just the other day I stumbled across some weird syntax in some Java code that I've never seen or read about.  I started to research it and discovered it's called an anonymous inner class.  Basically, it's defining a one-time use object.

The code I was looking at is used to establish a connection to an SSL website.  It just so happens that in my scenario, the website was using a self-signed certificate and in order for the <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/net/ssl/HttpsURLConnection.html" target="_blank">HttpsURLConnection</a> to connect, I needed to define a <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/net/ssl/HostnameVerifier.html" target="_blank">HostnameVerifier</a> to force the hostname to be valid.  Here's the static function:

<div class="cb1-docs-code"><pre>public static void setDefaultHostnameVerifier(HostnameVerifier v)</pre></div>

Now I need to create an object that implements the HostnameVerifier interface who's definition looks like this:

<div class="cb1-docs-code"><pre>public interface HostnameVerifier {
    boolean verify(String hostname, SSLSession session)
};</pre></div>

So, if I were to do this the old way, I would need to do this:

<div class="cb1-docs-code"><pre>public class MyHostnameVerifier implements HostnameVerifier {
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};</pre></div>

And then in my code call:

<div class="cb1-docs-code"><pre>MyHostnameVerifier mhv = new MyHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(mhv);
</pre></div>

Yuck! Why would I want to create MyHostnameVerifier if I'm only going to be using it once? That's where anonymous inner classes come in.

<div class="cb1-docs-code"><pre>HttpsURLConnection.setDefaultHostnameVerifier(
    new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    }
);</pre></div>

This is much cleaner. What if we wanted our verifier to check a Hashtable to see if a specific host is blocked. In order for our anonymous inner class to be able to access variables outside its scope, the variables must be final.

<div class="cb1-docs-code"><pre>final Hashtable<String,String> blockedHosts = new Hashtable<String,String>();
blockedHosts.put("10.0.0.1", "10.0.0.1");
blockedHosts.put("10.0.0.2", "10.0.0.2");
blockedHosts.put("10.0.0.3", "10.0.0.3");

HttpsURLConnection.setDefaultHostnameVerifier(
    new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            return !blockedHosts.containsKey(urlHostName);
        }
    }
);</pre></div>

You need to be careful when you final your outer variables.  The classic example is a outer variable used to keep track of a count.  If you simple do <code>final int count = 0;</code>, you will not be able to update the count.  However, if count was an array, the array is final, but the elements within are not:

<div class="cb1-docs-code"><pre>final int[] count = new int[1];</pre></div>

Then you would just access <code>count[0]</code> every time you wanted to change its value.

As you can see, Java anonymous inner classes are fun!]]></description>
			<content:encoded><![CDATA[<p>Just the other day I stumbled across some weird syntax in some Java code that I&#8217;ve never seen or read about.  I started to research it and discovered it&#8217;s called an anonymous inner class.  Basically, it&#8217;s defining a one-time use object.</p>
<p>The code I was looking at is used to establish a connection to an SSL website.  It just so happens that in my scenario, the website was using a self-signed certificate and in order for the <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/net/ssl/HttpsURLConnection.html" target="_blank">HttpsURLConnection</a> to connect, I needed to define a <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/net/ssl/HostnameVerifier.html" target="_blank">HostnameVerifier</a> to force the hostname to be valid.  Here&#8217;s the static function:</p>
<pre class="brush: java; title: ;">
public static void setDefaultHostnameVerifier(HostnameVerifier v)
</pre>
<p>Now I need to create an object that implements the HostnameVerifier interface who&#8217;s definition looks like this:</p>
<pre class="brush: java; title: ;">
public interface HostnameVerifier {
    boolean verify(String hostname, SSLSession session)
};
</pre>
<p>So, if I were to do this the old way, I would need to do this:</p>
<pre class="brush: java; title: ;">
public class MyHostnameVerifier implements HostnameVerifier {
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};
</pre>
<p>And then in my code call:</p>
<pre class="brush: java; title: ;">
MyHostnameVerifier mhv = new MyHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(mhv);
</pre>
<p>Yuck! Why would I want to create MyHostnameVerifier if I&#8217;m only going to be using it once? That&#8217;s where anonymous inner classes come in.</p>
<pre class="brush: java; title: ;">
HttpsURLConnection.setDefaultHostnameVerifier(
    new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    }
);
</pre>
<p>This is much cleaner. What if we wanted our verifier to check a Hashtable to see if a specific host is blocked. In order for our anonymous inner class to be able to access variables outside its scope, the variables must be final.</p>
<pre class="brush: java; title: ;">
final Hashtable&lt;String,String&gt; blockedHosts = new Hashtable&lt;String,String&gt;();
blockedHosts.put(&quot;10.0.0.1&quot;, &quot;10.0.0.1&quot;);
blockedHosts.put(&quot;10.0.0.2&quot;, &quot;10.0.0.2&quot;);
blockedHosts.put(&quot;10.0.0.3&quot;, &quot;10.0.0.3&quot;);

HttpsURLConnection.setDefaultHostnameVerifier(
    new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            return !blockedHosts.containsKey(urlHostName);
        }
    }
);
</pre>
<p>You need to be careful when you final your outer variables.  The classic example is a outer variable used to keep track of a count.  If you simple do <code>final int count = 0;</code>, you will not be able to update the count.  However, if count was an array, the array is final, but the elements within are not:</p>
<pre class="brush: java; title: ;">
final int[] count = new int[1];
</pre>
<p>Then you would just access <code>count[0]</code> every time you wanted to change its value.</p>
<p>As you can see, Java anonymous inner classes are fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cb1inc.com/2007/05/26/java-anonymous-inner-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting PFX Certificates to Java Keystores</title>
		<link>http://www.cb1inc.com/2007/04/30/converting-pfx-certificates-to-java-keystores/</link>
		<comments>http://www.cb1inc.com/2007/04/30/converting-pfx-certificates-to-java-keystores/#comments</comments>
		<pubDate>Mon, 30 Apr 2007 09:24:43 +0000</pubDate>
		<dc:creator>Chris Barber</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[A PFX (Personal inFormation eXchange) file is a PKCS #12 certificate that can contain both public and private keys. Windows loves this format, but if we want to use them from a Java program, we will need to convert them to a Java keystore.

Before we begin, make sure you have the Java runtime installed. I'm using Java 1.5.0_07, but 1.6 may work. I doubt 1.4 or below will work.

To make this easy, we are going to leverage a utility class that is bundled with <a href="http://www.mortbay.org/" target="_blank">Jetty</a>, a free Java web server. Download the latest stable version from their site, which at the time is version <a href="http://dist.codehaus.org/jetty/jetty-6.1.x/jetty-6.1.1.zip">6.1.1</a>.

Extract the zip file to a folder, then open a terminal and change into that folder and execute the following:

<div class="cb1-docs-code"><pre>&#62; java -classpath lib/jetty-6.1.1.jar org.mortbay.jetty.security.PKCS12Import</pre></div>

You should get a message that looks like this:

<div class="cb1-docs-code"><pre>usage: java PKCS12Import {pkcs12file} [newjksfile]</pre></div>

If you don't see this exact message, then make sure you are using a valid Java and Jetty version and make sure you are in the Jetty folder. Moving on.

Now, that things work, you can actually pass the path of the PFX file and the keystore to create. You will be prompted for the passwords for both files.

<div class="cb1-docs-code"><pre>&#62; java -classpath lib/jetty-6.1.1.jar org.mortbay.jetty.security.PKCS12Import 
         MyCert.pfx MyCert.jks
Enter input keystore passphrase: ******
Enter output keystore passphrase: ******</pre></div>

When it is all said and done, you should be looking at a shiny new Java keystore file. Verify the keystore by executing the following and entering the password you entered above:

<div class="cb1-docs-code"><pre>&#62; keytool -list -keystore MyCert.jks -v

Enter keystore password:  ******

Keystore type: jks
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: 29d1a9e13ca529ef1a32b1ea135b713_5a537e12-9c8e-833f-bb76-30ab870dd21
Creation date: Jan 1, 2007
Entry type: keyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=XXXX, OU=IT, O=XXXX, L=XXXX, ST=XXXX, C=XXXX, EMAILADDRESS=XXXX
Issuer: CN=XXXX, O=IT, L=XXXX, ST=XXXX, C=XXXX, EMAILADDRESS=XXXX
Serial number: a0032c317ba3200000e1
Valid from: Mon Jan 1 00:00:00 CDT 2007 until: Tue Jan 1 00:00:00 CDT 2008
Certificate fingerprints:
         MD5:  B1:63:6A:2C:2E:97:A4:33:E9:61:98:01:CA:0B:74:91
         SHA1: 61:98:01:04:7D:33:6C:2E:97:A4:D2:C7:61:61:B1:63:6A:2C:2E:97</pre></div>

Your output may vary, but you should have a valid Java keystore in the end. You can use the keytool tool to merge this keystore into another existing keystore, but I'll leave that for another day.]]></description>
			<content:encoded><![CDATA[<p>A PFX (Personal inFormation eXchange) file is a PKCS #12 certificate that can contain both public and private keys. Windows loves this format, but if we want to use them from a Java program, we will need to convert them to a Java keystore.</p>
<p>Before we begin, make sure you have the Java runtime installed. I&#8217;m using Java 1.5.0_07, but 1.6 may work. I doubt 1.4 or below will work.</p>
<p>To make this easy, we are going to leverage a utility class that is bundled with <a href="http://www.mortbay.org/" target="_blank">Jetty</a>, a free Java web server. Download the latest stable version from their site, which at the time is version <a href="http://dist.codehaus.org/jetty/jetty-6.1.x/jetty-6.1.1.zip">6.1.1</a>.</p>
<p>Extract the zip file to a folder, then open a terminal and change into that folder and execute the following:</p>
<pre class="brush: plain; title: ;">
$ java -classpath lib/jetty-6.1.1.jar org.mortbay.jetty.security.PKCS12Import
</pre>
<p>You should get a message that looks like this:</p>
<pre class="brush: plain; title: ;">
usage: java PKCS12Import {pkcs12file} [newjksfile]
</pre>
<p>If you don&#8217;t see this exact message, then make sure you are using a valid Java and Jetty version and make sure you are in the Jetty folder. Moving on.</p>
<p>Now, that things work, you can actually pass the path of the PFX file and the keystore to create. You will be prompted for the passwords for both files.</p>
<pre class="brush: plain; title: ;">
$ java -classpath lib/jetty-6.1.1.jar org.mortbay.jetty.security.PKCS12Import
         MyCert.pfx MyCert.jks
Enter input keystore passphrase: ******
Enter output keystore passphrase: ******
</pre>
<p>When it is all said and done, you should be looking at a shiny new Java keystore file. Verify the keystore by executing the following and entering the password you entered above:</p>
<pre class="brush: plain; title: ;">
$ keytool -list -keystore MyCert.jks -v

Enter keystore password:  ******

Keystore type: jks
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: 29d1a9e13ca529ef1a32b1ea135b713_5a537e12-9c8e-833f-bb76-30ab870dd21
Creation date: Jan 1, 2007
Entry type: keyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=XXXX, OU=IT, O=XXXX, L=XXXX, ST=XXXX, C=XXXX, EMAILADDRESS=XXXX
Issuer: CN=XXXX, O=IT, L=XXXX, ST=XXXX, C=XXXX, EMAILADDRESS=XXXX
Serial number: a0032c317ba3200000e1
Valid from: Mon Jan 1 00:00:00 CDT 2007 until: Tue Jan 1 00:00:00 CDT 2008
Certificate fingerprints:
         MD5:  B1:63:6A:2C:2E:97:A4:33:E9:61:98:01:CA:0B:74:91
         SHA1: 61:98:01:04:7D:33:6C:2E:97:A4:D2:C7:61:61:B1:63:6A:2C:2E:97
</pre>
<p>Your output may vary, but you should have a valid Java keystore in the end. You can use the keytool tool to merge this keystore into another existing keystore, but I&#8217;ll leave that for another day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cb1inc.com/2007/04/30/converting-pfx-certificates-to-java-keystores/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

