<?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>Mark W. Shead &#187; Java</title>
	<atom:link href="http://blog.markwshead.com/category/technology/programming/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.markwshead.com</link>
	<description>Mark's thoughts on being Mark Shead and other random subjects</description>
	<lastBuildDate>Thu, 29 Jul 2010 03:22:33 +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>Changing User Agent in Rome</title>
		<link>http://blog.markwshead.com/104/changing-user-agent-in-rome/</link>
		<comments>http://blog.markwshead.com/104/changing-user-agent-in-rome/#comments</comments>
		<pubDate>Wed, 19 Oct 2005 16:06:28 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/10/19/changing-user-agent-in-rome.html</guid>
		<description><![CDATA[Changing the user agent in Rome FeedFetcher doesn't quite work like you'd expect.  Here is how to get it to work so you can change it so you aren't stuck with "Java/1.5.0_04".  Some sites actually block "Java/1.5.0_04" so this is something worth changing if you want to pull in RSS feeds.]]></description>
			<content:encoded><![CDATA[<p></p><p>If you are trying to use Rome and Rome Feed Fetcher, you may want to change the user agent.  However, the following will not change the default user agent:</p>
<pre><code>
FeedFetcher feedFetcher = new HttpURLFeedFetcher();
feedFetcher.setUserAgent("User Agent 007");
SyndFeed feed = null;
feedURL = new URL(rssUrl);
feed = feedFetcher.retrieveFeed(feedURL);
List entries = feed.getEntries();
</code></pre>
<p>To change the user agent you must use the InfoCache as shown:</p>
<pre><code>
FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
feedFetcher.setUserAgent("User Agent 007");
SyndFeed feed = null;
feedURL = new URL(rssUrl);
feed = feedFetcher.retrieveFeed(feedURL);
List entries = feed.getEntries();
</code></pre>
<p>Otherwise the User agent is set to &#8220;Java/1.5.0_04&#8243;.  This is odd because the default client for Rome is &#8220;Rome Client (http://tinyurl.com/64t5n) Ver: 0.7&#8243;.  It seems like an attempt to change the user agent without having a HashMapFeedInfoCache will result in changing the user agent, but somehow it reverts to &#8220;Java/1.5.0_04&#8243; instead of whatever you set it to.</p>
<h4>People Found This When Looking For:</h4><ul><li>rome client http://tinyurl com/64t5n (1)</li><li>user agent rome (1)</li><li>user agent Rome Client (1)</li></ul><!-- SEO SearchTerms Tagging 2 plugin took 1.254 ms -->]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/104/changing-user-agent-in-rome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cobertura</title>
		<link>http://blog.markwshead.com/92/cobertura/</link>
		<comments>http://blog.markwshead.com/92/cobertura/#comments</comments>
		<pubDate>Sun, 10 Jul 2005 19:43:58 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/07/10/cobertura.html</guid>
		<description><![CDATA[Cobertura is a fork of jCoverage. It runs reports to let you see how much of your code is being tested by unit tests. This is incredibly useful to find areas of your code where a bug would go undetected. It looks like there is a plugin for Maven already, so I&#8217;m going to have [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Cobertura is a fork of jCoverage.  It runs reports to let you see how much of your code is being tested by unit tests.  This is incredibly useful to find areas of your code where a bug would go undetected.</p>
<p>It looks like there is a plugin for Maven already, so I&#8217;m going to have to give it a try sometime.  <a href="http://cobertura.sourceforge.net/">link</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/92/cobertura/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven Runs out of Memory</title>
		<link>http://blog.markwshead.com/89/maven-runs-out-of-memory/</link>
		<comments>http://blog.markwshead.com/89/maven-runs-out-of-memory/#comments</comments>
		<pubDate>Sat, 02 Jul 2005 20:00:15 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/07/02/maven-runs-out-of-memory.html</guid>
		<description><![CDATA[Maven 1.0 has some problems with memory leaks. Most of the time these aren&#8217;t issues, but if you are trying to compile a multiproject you might run into problems. By default Maven tells java to let it have up to 256MB of ram. If you need to increase this you&#8217;ll need to open the maven.bat [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Maven 1.0 has some problems with memory leaks.  Most of the time these aren&#8217;t issues, but if you are trying to compile a multiproject you might run into problems.  By default Maven tells java to let it have up to 256MB of ram.  If you need to increase this you&#8217;ll need to open the maven.bat (windows) or maven.sh (unix) file and change the way that java is called.  Somewhere around line 118 (on the .bat file)you should see the following:</p>
<p><code>if "%MAVEN_OPTS%"=="" SET MAVEN_OPTS="-Xmx256m"</code></p>
<p>Change the line so it reads:</p>
<p><code>if "%MAVEN_OPTS%"=="" SET MAVEN_OPTS="-Xmx1000m"</code></p>
<p>This will give Maven 1GB of memory to work with.  While this doesn&#8217;t really solve the problem of the memory leak, it may give you enough space to keep the problem from crashing Maven.</p>
<p>You can also set MAVEN_OPTS by setting it up as a variable in your environment.  If you do this the environment will override anything you set in the .bat or .sh file.</p>
<h4>People Found This When Looking For:</h4><ul><li>maven OutOfMemory Windows (3)</li><li>maven memory (3)</li><li>maven_opts memory (2)</li><li>maven_opts HOW TO SET (2)</li><li>maven change memory (2)</li><li>maven memory windows (2)</li><li>maven outofmemory (2)</li><li>MAVEN_OPTS memory increasing (2)</li><li>maven memory unix (1)</li><li>maven site outofmemory (1)</li></ul><!-- SEO SearchTerms Tagging 2 plugin took 1.439 ms -->]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/89/maven-runs-out-of-memory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Java Won&#8217;t Get It Right</title>
		<link>http://blog.markwshead.com/53/why-java-wont-get-it-right/</link>
		<comments>http://blog.markwshead.com/53/why-java-wont-get-it-right/#comments</comments>
		<pubDate>Sun, 03 Apr 2005 18:28:04 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[WebObjects]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/04/03/why-java-wont-get-it-right.html</guid>
		<description><![CDATA[Why Java Won&#8217;t Get It Right is an interesting entry about some of the problems with Java technology. The best part is that it is written by someone who actually knows Java. A part that I particularly liked was: They over-architect everything. I’ve actually used a Java framework (I’m not gonna say which) that had [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://www.mattmccray.com/blog/archives/2005/03/19/why-java-wont-get-it-right/">Why Java Won&#8217;t Get It Right</a> is an interesting entry about some of the problems with Java technology.  The best part is that it is written by someone who actually knows Java.   A part that I particularly liked was:</p>
<blockquote><p>
They over-architect everything. I’ve actually used a Java framework (I’m not gonna say which) that had XML config files that configured more XML config files! That’s just silly.
</p></blockquote>
<p>The author makes comparisons to Ruby on Rails and talks about how he doesn&#8217;t think Java will ever have anything like Rails.</p>
<p>I&#8217;ve seen a few demos of Rails and it is impressive, but much of the functionality it gives you has been available in WebObjects for some time.  <strike>In fact I&#8217;ve met several Ruby developers that started with Rails and switched to WebObjects as their application got bigger.</strike> (<strong>Update:</strong> It turns out I was mistaken.  They switched from Ruby to Webobjects, but they were using a different web framework instead of Rails.)</p>
<p>There is an interesting <a href="http://weblog.rubyonrails.com/archives/2005/03/19/bla-bla-list-cloning-a-rails-app-in-rife/">comparison</a> between a Ruby project and a Java project posted on the Ruby on Rails site.  The code comparison is interesting because it shows how much Ruby does for you automatically if you know how to use it.  A lot of what Ruby is doing is giving you automatic setters and getters.</p>
<p>It would be interesting to see a comparison between the amount of code necessary to write a Ruby application and the same app in WebObjects, but when it comes down to actual productivity the language being used is rarely the bottleneck.  The skills of the programmer are by far the most important factor.  The tools available in the language are second and the language ranks third or lower.</p>
<p>Good tools have a huge impact on productivity.  Simple things like auto-complete and real time syntax checking cumulatively make a large difference in productivity.  One of the areas where WebObjects really shines is in giving you the ability to graphically connect your data with the view.  You can still do everything manually in code, but the graphical tools give you the ability to really think about the problem on a  level that is much closer to the user experience.</p>
<ul>
<li><a href="http://www.darcynorman.net/2005/03/31/ruby-on-rails">Some thoughts on how Ruby is Similiar to Webobjects</a></li>
<li><a href="http://raibledesigns.com/page/rd?anchor=facets_of_ruby_by_dave">A look at some of the frameworks available for Ruby</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/53/why-java-wont-get-it-right/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thread.sleep() problem</title>
		<link>http://blog.markwshead.com/39/threadsleep-problem/</link>
		<comments>http://blog.markwshead.com/39/threadsleep-problem/#comments</comments>
		<pubDate>Sun, 03 Apr 2005 04:25:22 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/03/05/threadsleep-problem.html</guid>
		<description><![CDATA[The following is a JUnit test that looks like it should always run without a problem. Mark the current time in a variable called start call Thread.sleep and tell it to sleep for x number of seconds, note the current time again in a variable called end and then assert that end - start is [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The following is a JUnit test that looks like it should always run without a problem.  Mark the current time in a variable called <code>start</code> call Thread.sleep and tell it to sleep for x number of seconds, note the current time again in a variable called <code>end</code> and then assert that <code>end - start</code> is going to be less than or equal to x.</p>
<blockquote><pre><code>
    public void testThreadSleep() throws Exception {
        long start = 0;
        long end = 0;
        long elapsedTime = 0;
        for(int i = 1000; i < 1500 ; i = i + 20){
            start = System.currentTimeMillis();
            Thread.sleep(i);
            end = System.currentTimeMillis();
            assertTrue(i <= end - start);
        }
    }
</code></code></pre>
</blockquote>
<p>However in acutally running this code the assertion is not always true. It appears that when you try to call <code>Thread.sleep(x)</code> it may not sleep for the entire <code>x</code> milliseconds.  Obviously it might take <i>longer</i> than <code>x</code> because a thread isn&#8217;t guaranteed to run.  There might be another thread with a higher priority or the system might be doing garbage collection.  However I would expect that it wouldn&#8217;t run <i>less</i> than the specified amount of time, but that is what appears to be happening.</p>
<p>I believe this has to do with the way that the JVM operates.  Evidently it may wake up a thread a few milliseconds before the appointed time.  It is possible that it may be anticipating garbage collection and waking threads up slightly early</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/39/threadsleep-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entry Level Java Certification (SCJA)</title>
		<link>http://blog.markwshead.com/52/entry-level-java-certification-scja/</link>
		<comments>http://blog.markwshead.com/52/entry-level-java-certification-scja/#comments</comments>
		<pubDate>Wed, 30 Mar 2005 02:06:02 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/03/29/entry-level-java-certification-scja.html</guid>
		<description><![CDATA[According to some posts on Java Ranch, Sun is looking to create a Sun Certified Java Associates exam. The idea is to have an exam that companies can use to certify entry level programmers. I&#8217;m not sure why this is better than the current Sun Certified Java Programmer certification. It sounds like they want a [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>According to some posts on Java Ranch, Sun is looking to create a Sun Certified Java Associates exam.  The idea is to have an exam that companies can use to certify entry level programmers.  I&#8217;m not sure why this is better than the current Sun Certified Java Programmer certification.  It sounds like they want a certification that is easier to pass for someone with less experience.  I didn&#8217;t find the SCJP exam all that difficult.  In fact I didn&#8217;t even bother to put it on my resume, but after reading all the comments at Java Ranch about how difficult it is to pass maybe I should.<br />
<span id="more-52"></span><br />
This seems like an odd move on Sun&#8217;s part.  Generally most companies aren&#8217;t complaining that the programmers they hire are overqualified.  I suppose it may help them find promising junior programmers who they can mentor.  The exam could help separate people who have a good foundational understanding of the language from those that have only played around with a few features.  I suppose it makes sense from a financial stand point.  The tests aren&#8217;t cheap.</p>
<p>I know that many scripting language programmers are trying to learn Java or C# as their next career step.  If you&#8217;ve only worked with a scripting language, the transition to object oriented anything is going to be a big step.  Trying to understand threads, nested classes, etc. when you are still trying to transition away from thinking of everything as a web page with code snippets can be a pretty big challenge.  The new exam could help give these people their first real &#8220;win&#8221; in the Java language.  Sun may see this more as a marketing tactic to help bring people to Java that might be considering C#.  Once someone passes the SCJA they are probably going to want to move up to the SCJP.</p>
<p>At least in my case the Sun Certified Java Developer didn&#8217;t hold a lot of appeal for me once I passed the programmer exam.  I figured that I&#8217;d rather spend my time creating something that I want to use instead of designing an application that is only going to be used as part of the certification process.  It is a lot easier to code something you actually find interesting than something you plan to throw away.</p>
<ul>
<li><a href="http://thedeveloper.blogspot.com/2005/03/new-java-certification-exam-scja.html">Short Comment on new exam</a></li>
<li><a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&#038;f=24&#038;t=026599">Java Ranch Discussion of Exam</a></li>
</ul>
<h4>People Found This When Looking For:</h4><ul><li>scja blogs (2)</li><li>Java Ranchs SCJA certification page (1)</li></ul><!-- SEO SearchTerms Tagging 2 plugin took 0.597 ms -->]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/52/entry-level-java-certification-scja/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparable vs. Equals</title>
		<link>http://blog.markwshead.com/49/comparable-vs-equals/</link>
		<comments>http://blog.markwshead.com/49/comparable-vs-equals/#comments</comments>
		<pubDate>Sun, 27 Mar 2005 22:13:18 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/03/27/comparable-vs-equals.html</guid>
		<description><![CDATA[Agylen: Comparable vs equals has a nice discussion of how compareTo is used in Sets. If you don&#8217;t understand how Java is going to use your compareTo and your equals methods you can run into a problem with Sets. Basically you shouldn&#8217;t have a compareTo() method that returns 0 unless equals() returns true. Since a [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://agylen.com/blojsom/blog/devel/?permalink=Comparable_vs_equals.txt">Agylen: Comparable vs equals</a> has a nice discussion of how compareTo is used in Sets.</p>
<p>If you don&#8217;t understand how Java is going to use your compareTo and your equals methods you can run into a problem with Sets.  Basically you shouldn&#8217;t have a <code>compareTo()</code> method that returns 0 unless <code>equals()</code> returns <code>true</code>.</p>
<p>Since a Set only allows once instance of each object, it will ignore the addition of any objects it already contains.  If your object implements the <code>Comparator</code> interface the Set will check the compareTo method not the equals method.</p>
<p>It is common to write compareTo methods that only look at one field.  For example, you might want to sort Person objects by their last name and then first name.  If you only check the last and first name field, then comparing two John Smiths would return 0 even if they were different objects.  This would cause the Set to evaluate them as the same object and prevent you from adding the second John Smith to the Set.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/49/comparable-vs-equals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ONJava.com: Using JUnit With Eclipse IDE</title>
		<link>http://blog.markwshead.com/38/onjavacom-using-junit-with-eclipse-ide/</link>
		<comments>http://blog.markwshead.com/38/onjavacom-using-junit-with-eclipse-ide/#comments</comments>
		<pubDate>Thu, 24 Feb 2005 01:21:48 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/02/23/onjavacom-using-junit-with-eclipse-ide.html</guid>
		<description><![CDATA[Using JUnit With Eclipse IDE is a great simple tutorial on how to use JUnit in Eclipse. It doesn&#8217;t assume any knowledge of JUnit, but is useful as a quick tutorial for people wanting to make a switch to the Eclipse development environment.]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://www.onjava.com/pub/a/onjava/2004/02/04/juie.html">Using JUnit With Eclipse IDE</a> is a great simple tutorial on how to use JUnit in Eclipse.  It doesn&#8217;t assume any knowledge of JUnit, but is useful as a quick tutorial for people wanting to make a switch to the Eclipse development environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/38/onjavacom-using-junit-with-eclipse-ide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Introduction to Reflection</title>
		<link>http://blog.markwshead.com/37/simple-introduction-to-reflection/</link>
		<comments>http://blog.markwshead.com/37/simple-introduction-to-reflection/#comments</comments>
		<pubDate>Wed, 23 Feb 2005 03:15:46 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/02/22/simple-introduction-to-reflection.html</guid>
		<description><![CDATA[Reflection is a mechanism in java that allows to to get information about a class without needing to know the type of the class. The program below takes a java class name as a command line argument and shows you all of the methods and field names that are in the class. You call the [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Reflection is a mechanism in java that allows to to get information about a class without needing to know the type of the class.  The program below takes a java class name as a command line argument and shows you all of the methods and field names that are in the class.</p>
<p>You call the sample program like this:<br />
<code>java ReflectionTest java.util.Calendar</code></p>
<p>ReflectionTest takes the string you pass in on the command line and attempts to find a class by that name.  If it finds one it gets an array of all the fields and displays their name and their values.  It then gets an array of all the methods and displays their name and their return type.<br />
<span id="more-37"></span><br />
Notice that the Calendar class doesn&#8217;t appear anywhere in the code.   Reflection allows you to look inside a class without needing to know what the class is.  You can use reflection to create objects that can work with classes that they don&#8217;t ever know about directly.</p>
<p>    <code><br />
<font color="#808080">01</font>&nbsp;<font color="#7f0055"><b>package&nbsp;</b></font><font color="#000000">test;</font><br />
<font color="#808080">02</font>&nbsp;<font color="#7f0055"><b>import&nbsp;</b></font><font color="#000000">java.lang.reflect.*;</font><br />
<font color="#808080">03</font>&nbsp;<font color="#ffffff"></font><br />
<font color="#808080">04</font>&nbsp;<font color="#7f0055"><b>public&nbsp;class&nbsp;</b></font><font color="#000000">ReflectionTest</font><font color="#000000">{</font><br />
<font color="#808080">05</font>&nbsp;<font color="#ffffff"></font><br />
<font color="#808080">06</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><b>public&nbsp;static&nbsp;</b></font><font color="#7f0055"><b>void&nbsp;</b></font><font color="#000000">main</font><font color="#000000">(</font><font color="#000000">String</font><font color="#000000">[]&nbsp;</font><font color="#000000">args</font><font color="#000000">)&nbsp;</font><font color="#7f0055"><b>throws&nbsp;</b></font><font color="#000000">Exception&nbsp;</font><font color="#000000">{</font><br />
<font color="#808080">07</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><b>if</b></font><font color="#000000">(</font><font color="#000000">args.length&nbsp;!=&nbsp;</font><font color="#990000">1</font><font color="#000000">)&nbsp;{</font><br />
<font color="#808080">08</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">String&nbsp;usage&nbsp;=&nbsp;</font><font color="#2a00ff">&#34;example&nbsp;usage:&nbsp;ReflectionTest&nbsp;<nobr>java.util.Calendar&#34;</nobr></font><font color="#000000">;</font><br />
<font color="#808080">09</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">usage</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#808080">10</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.exit</font><font color="#000000">(</font><font color="#990000">0</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#808080">11</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br />
<font color="#808080">12</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Class&nbsp;aClass&nbsp;=&nbsp;Class.forName</font><font color="#000000">(</font><font color="#000000">args</font><font color="#000000">[</font><font color="#990000">0</font><font color="#000000">])</font><font color="#000000">;</font><br />
<font color="#808080">13</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Field</font><font color="#000000">[]&nbsp;</font><font color="#000000">fields&nbsp;=&nbsp;aClass.getFields</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#808080">14</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">&#34;Number&nbsp;of&nbsp;Fields&nbsp;=&nbsp;&#34;&nbsp;</font><font color="#000000">+&nbsp;fields.length</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#808080">15</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><b>for</b></font><font color="#000000">(</font><font color="#7f0055"><b>int&nbsp;</b></font><font color="#000000">i&nbsp;=&nbsp;</font><font color="#990000">0</font><font color="#000000">;&nbsp;i&nbsp;&lt;&nbsp;fields.length;&nbsp;i++</font><font color="#000000">){</font><br />
<font color="#808080">16</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Field&nbsp;field&nbsp;=&nbsp;fields</font><font color="#000000">[</font><font color="#000000">i</font><font color="#000000">]</font><font color="#000000">;</font><br />
<font color="#808080">17</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">&#34;[&#34;&nbsp;</font><font color="#000000">+&nbsp;field.getName</font><font color="#000000">()&nbsp;</font><font color="#000000">+&nbsp;</font><font color="#2a00ff">&#34;]&nbsp;&#34;&nbsp;</font><br />
<font color="#808080">18</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">+&nbsp;</font><font color="#2a00ff">&#34;with&nbsp;value&nbsp;[&#34;&nbsp;</font><font color="#000000">+&nbsp;field.get</font><font color="#000000">(</font><font color="#000000">aClass</font><font color="#000000">)&nbsp;</font><font color="#000000">+&nbsp;</font><font color="#2a00ff">&#34;]&#34;</font><font color="#000000">)</font><font color="#000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><br />
<font color="#808080">19</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br />
<font color="#808080">20</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><br />
<font color="#808080">21</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Method</font><font color="#000000">[]&nbsp;</font><font color="#000000">methods&nbsp;=&nbsp;aClass.getMethods</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#808080">22</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">&#34;\nNumber&nbsp;of&nbsp;Methods&nbsp;=&nbsp;&#34;&nbsp;</font><font color="#000000">+&nbsp;methods.length</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#808080">23</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><b>for</b></font><font color="#000000">(</font><font color="#7f0055"><b>int&nbsp;</b></font><font color="#000000">i&nbsp;=&nbsp;</font><font color="#990000">0</font><font color="#000000">;&nbsp;i&nbsp;&lt;&nbsp;methods.length;&nbsp;i++</font><font color="#000000">){</font><br />
<font color="#808080">24</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Method&nbsp;method&nbsp;=&nbsp;methods</font><font color="#000000">[</font><font color="#000000">i</font><font color="#000000">]</font><font color="#000000">;</font><br />
<font color="#808080">25</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">&#34;[&#34;&nbsp;</font><font color="#000000">+&nbsp;method.getName</font><font color="#000000">()&nbsp;</font><font color="#000000">+&nbsp;</font><font color="#2a00ff">&#34;]&nbsp;&#34;&nbsp;</font><br />
<font color="#808080">26</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">+&nbsp;</font><font color="#2a00ff">&#34;returns&nbsp;[&#34;&nbsp;</font><font color="#000000">+&nbsp;method.getReturnType</font><font color="#000000">()&nbsp;</font><font color="#000000">+&nbsp;</font><font color="#2a00ff">&#34;]&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#808080">27</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br />
<font color="#808080">28</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><br />
<font color="#808080">29</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br />
<font color="#808080">30</font>&nbsp;<font color="#000000">}</font></p>
<p>Lines 7 through 10 are just to show the usage and exit if the program is called incorrectly.  In line 12, we get the class object that matches the name passed in on the command line.  If it can't find it an exception is thrown.  For the purposes of this example I just have main throwing the exception.</p>
<p>On line 13 we use the class to get the fields and then print out their name and value in 15 to 19.  Notice the odd reference of </code><code>field.get(aClass)</code> to get the value.  It seems odd in this example because we are just passing it a class.  You could also pass it a real live instance of a Calendar object and it would tell you what the field values are for that instance of Calendar.</p>
<p>The remaining code uses the same process to get the name and return type of each method in the class.</p>
<p>Here are the results of the program being run on java.util.Calendar as an argument:</p>
<p><code><br />
Number of Fields = 41<br />
[ERA] with value [0]<br />
[YEAR] with value [1]<br />
[MONTH] with value [2]<br />
[WEEK_OF_YEAR] with value [3]<br />
[WEEK_OF_MONTH] with value [4]<br />
[DATE] with value [5]<br />
[DAY_OF_MONTH] with value [5]<br />
[DAY_OF_YEAR] with value [6]<br />
[DAY_OF_WEEK] with value [7]<br />
[DAY_OF_WEEK_IN_MONTH] with value [8]<br />
and so on.......<br />
</code><code><br />
Number of Methods = 46<br />
[hashCode] returns [int]<br />
[equals] returns [boolean]<br />
[toString] returns [class java.lang.String]<br />
[get] returns [int]<br />
[clone] returns [class java.lang.Object]<br />
[add] returns [void]<br />
[isSet] returns [boolean]<br />
and so on ...</code></p>
<p>Reflection is on of the tools that frameworks utilize to make things happen automatically.  For example, if you create a Java Bean you can make a .JSP page automatically take form values and populate the bean using the setter methods.  When Tomcat was being written they couldn&#8217;t predict what your particular object would look like, so they used reflection to look inside your object and find the appropriate methods to call.</p>
<p>Reflection is a good way to build future proof functionality.  You can come up with a way of doing something that will work with any class you give it without needing to know about that class ahead of time or force the class to implement an interface.</p>
<p>It is a powerful tool that can help you achieve a great deal of simplicity.  Automatically setting the values of a bean from a form in JSP is a good example of how reflection can simplify your code.</p>
<p>Here are a few links to discussions about reflection.</p>
<ul>
<li><a href=http://www.softwareas.com/index.php?p=53>Discussion of dynamic vs. static in Java</a></li>
<li><a href="http://thisvince.blogspot.com/2005/02/apache-beanutils-commons.html">How Apache BeanUtils use Reflection</a></li>
<li><a href="http://radio.javaranch.com/val/2004/05/18/1084891793000.html">Discussion about how to get access to private fields and methods using reflection</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/37/simple-introduction-to-reflection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dealing with Slow XSLT Transformations</title>
		<link>http://blog.markwshead.com/30/dealing-with-slow-xslt-transformations/</link>
		<comments>http://blog.markwshead.com/30/dealing-with-slow-xslt-transformations/#comments</comments>
		<pubDate>Sun, 30 Jan 2005 21:55:02 +0000</pubDate>
		<dc:creator>Mark Shead</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.markwshead.com/archives/2005/01/30/dealing-with-slow-xslt-transformations.html</guid>
		<description><![CDATA[I have some XSLT stylesheets that I use to integrate information from Amazon with my reading list on my website. This weekend I decided to create another list using Amazon&#8217;s &#8220;People Who Bought this Book also Bought&#8221; feature. After a couple generations of downloading books that were related to books I read, I ended up [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I have some XSLT stylesheets that I use to integrate information from Amazon with my <a href="http://www.markwshead.com/BookList">reading list</a> on my website.  This weekend I decided to create another list using Amazon&#8217;s &#8220;People Who Bought this Book also Bought&#8221; feature.  After a couple generations of downloading books that were related to books I read, I ended up with an XML file with about 1,500 books in it.  I tried running the transform using Xerces and it took 13 hours.</p>
<p>Obviously this wasn&#8217;t going to work, so I refactored parts of the XSLT and was able to cut that time in half.  6 hours still seemed like it was taking far too long.  I considered doing away with the stylesheets and doing the transforms manually using Java to write directly to a file.  After doing some searches I discovered that apache.org has a good solutions for this problem that is part of the <a href="http://xml.apache.org/xalan-j/xsltc_usage.html">Xalan-J</a> project.  Basically it allows you to take an XSLT stylesheet and compile it into java byte code called a &#8220;translet&#8221;.  You can then run the transform using Xalan and the compiled java translet.  This makes things run much quicker.  It appears that this is because the transform doesn&#8217;t need to deal with the XSLT file and because of other optimizations.<br />
<span id="more-30"></span><br />
To do this you must have xercesImpl.jar, xml-api.jar, and xalan.jar in  your classpath.  If you have problems be sure to double check this because the error message it gives you doesn&#8217;t always indicate that it can&#8217;t find a required class.  Use the Xalan command line program to turn your XLST file into a translet.<br />
<code>java org.apache.xalan.xsltc.cmdline.Compile  myXSLT.xsl</code></p>
<p>This will produce several class files:<br />
<code><br />
myXSLT.class<br />
myXSLT$0.class<br />
myXSLT$1.class<br />
myXSLT$2.class<br />
myXSLT$3.class<br />
myXSLT$4.class<br />
</code><br />
To transform xml using this byte code do the following:<br />
<code>java org.apache.xalan.xsltc.cmdline.Transform myFile.xml  myXSLT.xsl</code><br />
This will run the transform using the byte code and output the results to the screen (unless you are redirecting the output from within the XSL file).  To put it in a file:<br />
<code>java org.apache.xalan.xsltc.cmdline.Transform myFile.xml  myXSLT.xsl > myFile.html </code></p>
<p>One thing I discovered was the fact that the redirect:write tag in my xsl file would not compile into byte code.  I switched to using the xsltc:output tag instead and it would work, but the compiler always complained because I was using the <code>select</code> attribute instead of the <code>file</code> attribute.  At first I thought I could just change the attribute name.  However <code>select</code> allows you to use a variable name, but <code>file</code> does not.  So <code>&lt;xsltc:output file="$filename"&gt;</code> will output to a file named &#8220;$filename&#8221; instead of whatever is in the variable $filename.</p>
<p>There is a way around this by using <code>&lt;xsltc:output file="{$filename}"&gt;</code></p>
<p>There are other options for doing the compile including -j which will put the class files into a jar and some options to allow you to compile multiple files at the same time.</p>
<p>When I finished figuring everything out I tried the same transform that had previously taken 13 hours on a 1,500 item xml file.  This time I used a 5,000 item file, but it completed in 2 to 3 hours.  If you use large xml files as input for XSLT transformations I would highly suggest looking into using <a href="http://xml.apache.org/xalan-j/xsltc_usage.html">translets</a>.</p>
<ul>
<li><a href="http://www.throwingbeans.org/tech/transformation_inspiration.html">Another Bloggers take on Translets</a></li>
<li><a href="http://www.jroller.com/comments/bhagvank/Weblog/improving_the_performance_of_struts">How translets relate to struts</a></li>
<li><a href="http://blogs.sun.com/roller/page/shudson310/?anchor=accelerate_your_transforms">More praises and mention of a faster transform for stylesheets</a></li>
</ul>
<h4>People Found This When Looking For:</h4><ul><li>java xslt transform is slow (1)</li><li>xsl transform very slow xalan (1)</li></ul><!-- SEO SearchTerms Tagging 2 plugin took 0.527 ms -->]]></content:encoded>
			<wfw:commentRss>http://blog.markwshead.com/30/dealing-with-slow-xslt-transformations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
