<?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>Turnleaf Design &#187; Java</title>
	<atom:link href="http://www.turnleafdesign.com/tag/java/feed" rel="self" type="application/rss+xml" />
	<link>http://www.turnleafdesign.com</link>
	<description>Ramblings of a junior developer</description>
	<lastBuildDate>Thu, 21 Oct 2010 01:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Log4j 4 Enterprise</title>
		<link>http://www.turnleafdesign.com/log4j-4-enterprise</link>
		<comments>http://www.turnleafdesign.com/log4j-4-enterprise#comments</comments>
		<pubDate>Wed, 06 Oct 2010 06:24:39 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Dev Environment]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Logging]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=346</guid>
		<description><![CDATA[Logging is an important part of any application. Logging provides debugging information, the state of the application, and a record of what happened when the application failed. As applications increase in size so does the demands and complexity of logging. Without a proper logging system it can become difficult to determine from where in an [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flog4j-4-enterprise"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flog4j-4-enterprise&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Logging is an important part of any application. Logging provides debugging information, the state of the application, and a record of what happened when the application failed. As applications increase in size so does the demands and complexity of logging. Without a proper logging system it can become difficult to determine from where in an application a log statement is being executed and without an easy way of changing logging behavior it can have a negative effect on an application's performance.</p>
<p>The Apache Foundation maintains the log4j project which is an easy to use system that provides a large array of really cool features for logging. This tutorial, part of my <a href="http://www.turnleafdesign.com/category/dev-environment" target="_blank">dev environment series</a>, is an introduction into using log4j in an enterprise setting. Topics that I will cover include; implementing log4j in a project, customizing the log statement, usage of appenders, creating loggers and logger inheritence,  and setting up log4j to work on an application server. This will cover many of the basic needs of enterprise logging.</p>
<h4>A few simple steps before we begin...</h4>
<p>As with all my tutorials that involve coding you can download the source code <a href="http://turnleafdesign.googlecode.com/svn/trunk/" target="_blank">here</a>. In this example I will be using the project HttpExample, <a href="http://turnleafdesign.googlecode.com/svn/!svn/bc/24/trunk/HttpExample/" target="_blank">starting on revision 24</a>. If you want to work on this code locally you will also need the log4j library which can be found <a href="http://logging.apache.org/log4j/1.2/download.html" target="_blank">here</a>, and then add it to your project's classpath by right clicking on your project &gt; build path &gt; add external libraries and then navigate to the library's location (if you are using eclipse).<span id="more-346"></span></p>
<h4>Log4j, it's better than bad, it's good!</h4>
<p>Implementing log4j in a project is surprisingly simple. I already covered the first step of adding log4j to the project's classpath. The second step is setting up the configuration file. If you look in the project you will see I already created one, log4j.xml, which looks like this:</p>
<pre name="code" class="xml">&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;!DOCTYPE log4j:configuration SYSTEM "/log4j.dtd"&gt;

&lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;

&lt;appender name="simpleAppender" class="org.apache.log4j.ConsoleAppender"&gt;
&lt;layout class="org.apache.log4j.SimpleLayout" /&gt;
&lt;/appender&gt;

&lt;root&gt;
&lt;appender-ref ref="simpleAppender" /&gt;
&lt;/root&gt;

&lt;/log4j:configuration&gt;</pre>
<p>The final step is to add a logger to a class and log statements, which I have also already done so in NewUserFormController:</p>
<pre name="code" class="java">com.tld.form.controller.NewUserFormController

public final class NewUserFormController {
private static Logger LOG = Logger.getLogger(NewUserFormController.class);

...

protected static String createUser(UserFormBean bean) {
LOG.trace("trace");
LOG.debug("debug");
LOG.info("info");
LOG.warn("warn");
LOG.error("error");
LOG.fatal("fatal");

...

}</pre>
<p>In a previous <a href="http://www.turnleafdesign.com/web-developers-should-hate-http" target="_blank">article</a> I created some unit tests for this project, if you try running one of the unit tests under TestNewUserFormController the following should be printed to the console:</p>
<p>DEBUG - debug<br />
INFO - info<br />
WARN - warn<br />
ERROR - error<br />
FATAL - fatal</p>
<p>Not much more advanced than using the standard system.out. There seems to be a problem however, one of the log statments, LOG.trace("trace");, was not printed to the console. This actually is not a problem and brings me to the first feature of log4j I will go over.</p>
<h4>Not all log statements are created equal</h4>
<p>All applications more or less go through the loosely defined stages of; development, qa testing, and finally production. The demands of logging change as much as what occurs in each of these stages. In response to these demands log4j offers six default levels of logging (from lowest to highest); trace, debug, info, warn, error, fatal, these levels give developers relatively fine grained control of which log statements are executed. This aids developers by allowing us to change the amount of logging an application is executing easily and without making a binary change.</p>
<p>To change the logging level of our application we will need to go into the configuration file and add a "level" tag within our "root" tag. Within this tag is the attribute "value" which we set with the minimum level at which to begin logging. So if we set the level to "debug" (the default) all log statements of the level of "debug" or higher will be executed. Here is an example:</p>
<pre name="code" class="xml">&lt;root&gt;
&lt;level value="trace" /&gt;
&lt;appender-ref ref="simpleAppender" /&gt;
&lt;/root&gt;</pre>
<p>With the logging level set to trace, if we run one of the unit tests again all six statements should appear.</p>
<h4>Customizing the logging message</h4>
<p>So log4j has given us control over which statements are printed, currently we are using the SimpleLayout which, though quick, doesn't offer a lot of information about the statements being printed. Log4j offers the PatternLayout class which gives the developer a wide degree of latitude in customizing his log statements. Lets get our feet wet with this system by adding a timestamp and the fully qualified name and line number to our log statements. Open up log4j.xml again and make the following changes to the appender tag:</p>
<pre name="code" class="xml">&lt;appender name="simpleAppender" class="org.apache.log4j.ConsoleAppender"&gt;
&lt;layout class="org.apache.log4j.PatternLayout"&gt;
&lt;param name="ConversionPattern" value="%d{MMM-dd HH:mm:ss} %p %l - %m%n" /&gt;
&lt;/layout&gt;
&lt;/appender&gt;</pre>
<p>Run an unit test and the output should look something like this:</p>
<p>Sep-29 00:39:33 TRACE Sep-29 00:45:57 TRACE com.tld.form.controller.NewUserFormController.createUser(NewUserFormController.java:18) - trace</p>
<p>Definitely a lot more useful as we know exactly where the statement is being executed from and when the statement was executed (not particularly useful here, but absolutely necessary for a deployed application). There is a lot more options for customization, for more information check out <a href="http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/EnhancedPatternLayout.html" target="_blank">this link</a>. It's important to consider the performance impact modifications may have as well, adding the fully qualified name to a log statement has a significant performance impact. The link above notes if an option has a significant impact on performance.</p>
<h4>Appender? I barely knew her!</h4>
<p>Right now all of our log statements are printed out to the console. This is great for when debugging in an IDE, but eventually we will deploy this application to the server and we don't want these log statements to be sent to the standard server log. A lot of information is already dumped to this log and if we deploy multiple applications to a server it would be very difficult to differentiate which application is printing what statement. Again log4j comes through by making it simple to change where a log statement is printed. Again open up log4j.xml and add the following lines (note this is a new appender tag):</p>
<pre name="code" class="xml">&lt;appender name="fileAppender" class="org.apache.log4j.FileAppender"&gt;
&lt;param name="File" value="myLogFile.log" /&gt;
&lt;layout class="org.apache.log4j.PatternLayout"&gt;
&lt;param name="ConversionPattern" value="%d{MMM-dd HH:mm:ss} %p %l - %m%n" /&gt;
&lt;/layout&gt;
&lt;/appender&gt;</pre>
<p>Then add the appender to the root tag like so:</p>
<pre name="code" class="xml">&lt;root&gt;
&lt;level value="trace" /&gt;
&lt;appender-ref ref="simpleAppender" /&gt;
&lt;appender-ref ref="fileAppender" /&gt;
&lt;/root&gt;</pre>
<p>Finally rerun one of the unit tests, refresh your project and you should see a file titled "myLogFile.log" with the log statements contained within.</p>
<p>Clearly this is an unorthadox (and not particularly useful) way of handling logging. Later in this tutorial we will send this output to a more reasonable location, but for now on to loggers.</p>
<h4>Setting up separate logs and logger inheritence</h4>
<p>So far we have been using the root logger (the root tag in the log4j.xml file). For a small application with few dependencies like I am demonstrating, it is difficult to show the issues that arise from only using the root logger. As an application increases in complexity it often pulls in additional libraries, just like a developer may setup step filters in a debugger to ignore portions of an application or libraries he doesn't care about, we can setup loggers to ignore log statements from certain parts of an application or direct log statments to a different location. Lets explore this practice as best we can, given the limitations of this example, once again open up log4j.xml remove the "root" tag and make the following changes:</p>
<pre name="code" class="xml">&lt;logger name="com.tld.form"&gt;
&lt;level value="trace" /&gt;
&lt;/logger&gt;

&lt;root&gt;
&lt;level value="error" /&gt;
&lt;appender-ref ref="simpleAppender" /&gt;
&lt;/root&gt;</pre>
<p>If you run an unit you will see the output was the same as before. Now go add couple of log statements to UserDaoImpl like so:</p>
<pre name="code" class="java">public class UserDaoImpl implements UserDao {

private static final Logger LOG = Logger.getLogger(UserDaoImpl.class);

@Override

public boolean doesUserExist(UserFormBean bean) {

LOG.debug("Debug");
LOG.error("Error");
...
}
...
}</pre>
<p>Again run the unit test and you will see these two log statements added to the original six. Now go back into log4j.xml and add the following:</p>
<pre name="code" class="xml">&lt;logger name="com.tld.form.dao"&gt;
&lt;level value="error" /&gt;
&lt;/logger&gt;</pre>
<p>Rerun the unit test and you will see only the "error" log statement was printed from with in UserDaoImpl.</p>
<p>There are two important and closely related concepts shown in the examples above; logger retrieval and logger inheritance:</p>
<p>1. The way I have been retrieving logger by passing the class is the industry standard. When you call "Logger.getLogger(class)" log4j checks its list of declared loggers and returns the closest ancestor to the passed in name, if none are found than the root logger is returned. Above the logger for the class "NewUserFormController" uses logger "com.tld.form" because its logger name is "<strong>com.tld.form</strong>.controller.NewUserFormController" where as the the class "com.tld.form.dao.UserDaoImpl" uses the logger"com.tld.form.dao" because that is its closest ancestor.</p>
<p>2. Just like Java offers inheritance so a developer can reuse attributes in like ancestor classes, log4j supports inheritance for the same reason. In the above example we didn't have to redefine an appender for loggers "com.tld.form" and "com.tld.form.dao" because both inherited the appender from the root logger (which all loggers inherit from). Also like in Java attributes inherited from a more recent ancestor override those of an older ancestor. For a more in depth explanation of how inheritance works check <a href="http://logging.apache.org/log4j/1.2/manual.html" target="_blank">here</a>.</p>
<h4>Server?! I barel... never mind</h4>
<p>So log4j is working all fine and dandy in Eclipse, but eventually we will move this application to a server and we will still want logging to take place. To do this right we will have to make several changes.</p>
<p>The first change is correcting a mistake I made earlier which involves the location of the log4j config file. One of the strengths of log4j is the ability to make a change to the logging behavior of an application without making a binary change (the change does not involve recompiling and redploying code). While not absolutely necessary we will want to move the log4j out from under the src folder and create a new folder called "resources" and place it in here. This will show other developers that log4j.xml is a separate deployable from the source code.</p>
<p>[Note: you will also want to include a copy of log4j.xml under test, so logging can still take place while running unit tests]</p>
<p>Since log4j is no longer on the classpath of our project we will need to tell our application where to find the configuration file out on the server, however this should only happen once and at application start up. To do this we will need to create a servlet listener. If you look under com.tld.form, you will see I already wrote one:</p>
<pre name="code" class="java">public class LoggerListener implements ServletContextListener {
private static final String LOG4J_CONFIG = "log4jConfig";

@Override
public void contextDestroyed(ServletContextEvent arg0) {
}

@Override
public void contextInitialized(ServletContextEvent arg0) {
DOMConfigurator.configure(arg0.getServletContext()
.getInitParameter(LOG4J_CONFIG));
}
}</pre>
<p>A servlet listener is hook for a developer to run actions during an application start up or shut down ( contextInitialized and contextDestroyed respectively). They are quite handy in scenarios like this where you need to load global properties or configure something. Also I didn't cover this before, but there are two formats to log4j configuration files, in this tutorial we covered using the xml format, but there is also the properties format which is a text file that follows a specific syntax. This information is significant because if you are using the former like we are you will need to use DOMConfigurator, if you are using the latter however you will need to use PropertyConfigurator to configure log4j.</p>
<p>Next we need to update our web.xml to tell it we have a listener and to give the location of the log4j.xml file.</p>
<pre name="code" class="xml">&lt;web-app&gt;
...
&lt;context-param&gt;
&lt;param-name&gt;log4jConfig&lt;/param-name&gt;
&lt;param-value&gt;/etc/glassfishv3/glassfish/domains/domain1/config/log4j.xml&lt;/param-value&gt;
&lt;/context-param&gt;
&lt;listener&gt;
&lt;listener-class&gt;com.tld.form.LoggerListener&lt;/listener-class&gt;
&lt;/listener&gt;
...
&lt;/web-app&gt;</pre>
<p>The "context-param" element tells the listener where the log4j.xml file is located on the server. The listener tag is obviously designating a class in the project as being a servlet listener. Location of elements matter in a web-xml, so put these elements near the top of your web.xml file.</p>
<p>Next we will need to make a change to the build file. Since we are now referencing log4j classes in our application we will need to make sure these classes are available on the classpath of our application when we deploy it to the sever. To do this go into the build.xml file and add the "lib" tag to the war tag like so:</p>
<pre name="code" class="xml">&lt;target name="war" depends="compile"&gt;
&lt;war destfile="${build}/war/${ant.project.name}.war" webxml="webapp/WEB-INF/web.xml"&gt;
&lt;fileset dir="webapp" includes="*.jsp" /&gt;
&lt;classes dir="${build}/classes"/&gt;
&lt;lib dir="${libs}"&gt;
&lt;include name="log4j-1.2.16.jar" /&gt;
&lt;/lib&gt;
&lt;/war&gt;
&lt;/target&gt;</pre>
<p>Now when we build the project the log4j library will be included within the distributable (the ear file).</p>
<p>Finally we will need to update the log4j.xml under the resources folder to get setup handle logging in a server environment. I made the following changes:</p>
<pre name="code" class="xml">&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;!DOCTYPE log4j:configuration SYSTEM "/log4j.dtd"&gt;

&lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;

&lt;appender name="fileAppender" class="org.apache.log4j.FileAppender"&gt;
&lt;param name="File" value="/etc/glassfishv3/glassfish/domains/domain1/logs/HttpExample.log"/&gt;
&lt;layout class="org.apache.log4j.PatternLayout"&gt;
&lt;param name="ConversionPattern" value="%d{MMM-dd HH:mm:ss} %p - %m%n&amp;lt;EOR&amp;gt;%n" /&gt;
&lt;/layout&gt;
&lt;/appender&gt;

&lt;root&gt;
&lt;level value="error" /&gt;
&lt;appender-ref ref="fileAppender" /&gt;
&lt;/root&gt;

&lt;/log4j:configuration&gt;</pre>
<p>We want our application to be writing to its own log for readability and convenience purposes. I also made some changes to the log statement formatting; the fully qualified name and line number has been removed as it is a performance hog and I added a literal "&lt;EOR&gt;" to delineate between log statements.</p>
<p>And that is it! Just make sure you place the log4j.xml file in the correct folder and everything should be working correctly! There are a few features like rolling logs that log4j offers which may be more useful in situation like this, but for brevity of an already long article I will cover that and a few other topics in a future article. If you have any question or comments let me know!</p>
<p><strong>Sources:</strong><br />
<a href="http://logging.apache.org/log4j/1.2/manual.html">http://logging.apache.org/log4j/1.2/manual.html</a><br />
<a href="http://wiki.apache.org/logging-log4j/Log4jXmlFormat">http://wiki.apache.org/logging-log4j/Log4jXmlFormat</a><br />
<a href="http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/EnhancedPatternLayout.html">http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/EnhancedPatternLayout.html</a><br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x60, created 9/14/09 */
google_ad_slot = "1115297999";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a>, <a class='technorati-link' href='http://technorati.com/tag/Logging' rel='tag' target='_blank'>Logging</a>, <a class='technorati-link' href='http://technorati.com/tag/Web+development' rel='tag' target='_blank'>Web development</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/log4j-4-enterprise/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Web developers should hate http</title>
		<link>http://www.turnleafdesign.com/web-developers-should-hate-http</link>
		<comments>http://www.turnleafdesign.com/web-developers-should-hate-http#comments</comments>
		<pubDate>Mon, 23 Aug 2010 13:00:32 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Java don't care]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=324</guid>
		<description><![CDATA[I don't mean that literally, what I do mean is a web developer should hate when Java's http specification is directly accessed in the code. This is a topic I briefly touched on in my 8 signs your code sucks post awhile back. I decided to return to this topic because it touches on a [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fweb-developers-should-hate-http"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fweb-developers-should-hate-http&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I don't mean that literally, what I do mean is a web developer should hate when Java's http specification is directly accessed in the code. This is a topic I briefly touched on in my <a href="http://www.turnleafdesign.com/8-signs-your-code-sucks" target="_blank">8 signs your code sucks</a> post awhile back. I decided to return to this topic because it touches on a very important and fundamental programming practice which is limiting dependencies in code. This article represents the initial entry into a new series I plan on covering called "Java don't care." As aforementioned this series will focus on reducing dependencies in code, as well as making your code less aware of the environment it is in, and how following these practices will help you as a developer. But back to the task at hand; why referencing Java's http specification is bad.<br />
<span id="more-324"></span></p>
<h4>The problems</h4>
<p>A long list likely could be made as to why you should not directly reference http in your code. Here are four of the more important:</p>
<p><strong>Testing</strong></p>
<p>Probably the most readily noticable and debately the most important; referencing http in code greatly increases the difficultly of writing tests. To even begin writing a unit test you will need a tool to mock the http request/session. This increase in the difficulty of writing unit tests leads to two problems; decreased unit test coverage and increased unit test complexity.</p>
<p><strong>Contract</strong></p>
<p>A somewhat overlooked issue, but important none the less, is the lack of a contract between the view and the backend code that supports it. There is no central location to figure out what is contained in the request/session. The HttpServletRequest does not (and could not) provide this information. This lack of a contract can make it difficult to understand what is happening in the application as well as what the developer has access to.</p>
<p><strong>Coupling</strong></p>
<p>Directly accessing the http specification makes the code more aware of a view's internal implementations than it should be. The controller, and certainly the model, should not know the names of the various fields and variables in the view. It creates a mud ball of an application where changes to one level unduly necessitate changes to other levels.</p>
<p>You also introduce an unnecessary requirement into the application. Obviously requiring a web based application to include the httpservlet library in its classpath isn't particularly burdesom, but in other areas it could represent an issue. In general it is a bad programming practice to force requirements on an application.</p>
<p><strong>Maintainability</strong></p>
<p>Fundamentally this is more of a meta-issue, it is the inevitable outcome of the above three issues (as well as others). When you have an application that is difficult to write unit test for, hard to figure out what is accessible where, and has unnecessary requirements to work, you have an application that is difficult to maintain. I would argue that writing a maintainable application is the second most important requirement of an application (the first of course being one that it meets business requirements).</p>
<h4>How to fix it</h4>
<p>There are many differnet ways to fix and/or prevent this issue from occurring. Many frameworks, like Spring, (can) handle the session and request for you. This would also typically be the best way of addressing the issue. Let's assume, for the purpose of this article, that such an option is not available. Below is the controller of a simple application that I wrote that makes extensive use of the http request.</p>
<pre name="code" class="java">public class NewUserFormController extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String nextPage = createUser(req);
req.getRequestDispatcher(nextPage).forward(req, resp);
}

private String createUser(HttpServletRequest req) {
List errorMsg = new ArrayList();
UserDao dao = new UserDaoImpl();
validateUserInput(req,
errorMsg);
if(!dao.doesUserExist(req, errorMsg)){
dao.insertUser(req, errorMsg);
}
if(errorMsg.isEmpty()){
return "Complete.jsp";
} else{
req.setAttribute("errorMsg", errorMsg);
setRequestAttributes(req);
return "Form.jsp";
}
}

private void setRequestAttributes(HttpServletRequest req) {
String fName = req.getParameter("fName");
String lName = req.getParameter("lName");
String eMail = req.getParameter("eMail");
req.setAttribute("fName", fName);
req.setAttribute("lName", lName);
req.setAttribute("eMail", eMail);
}

private void validateUserInput(HttpServletRequest req, List errorMsg) {
String fName = req.getParameter("fName");
String lName = req.getParameter("lName");
String eMail = req.getParameter("eMail");
String password = req.getParameter("password");
String confirmPassword = req.getParameter("confirmPassword");
if (isEmpty(fName)) {
errorMsg.add("Must enter a first name!");
}
if (isEmpty(lName)) {
errorMsg.add("Must enter a last mame!");
}
if (isEmpty(eMail)) {
errorMsg.add("Must enter an email address!");
}
if (isEmpty(password)) {
errorMsg.add("Must enter a password!");
}
if (isEmpty(confirmPassword)) {
errorMsg.add("Must confirm password!");
}
if(!password.equals(confirmPassword)){
errorMsg.add("Passwords must match!");
}
}

private boolean isEmpty(String string) {
return string == null || string.trim().equals("");
}
}</pre>
<p>Entire source of the project can be viewed <a href="http://turnleafdesign.googlecode.com/svn/!svn/bc/10/trunk/HttpExample/" target="_blank">here</a></p>
<p>As you can see the access to the HttpServletRequest permeates near the entireity of the code base. As a result many of the issues I brought up are problems in this code (coincedence?). It would be difficult to write unit tests for this code, it's somewhat difficult to know what information is on the request, and I am dependant on the httpservlet in my code.</p>
<h4>The fix</h4>
<p>So clearly this code needs help. Our refacotring of the code has two major goals:<br />
1. Create a contract between the view and the rest of the application<br />
2. Reduce the depence on the http specification in the code</p>
<p>Typically in this situation I create a bean that holds all the fields in the request/session as well as a few additional meta fields (such as in this example the next page). This creates the previously mentioned contract. A developer can look at the bean class and know what fields are accessible in the application.</p>
<p>The usage of this bean will help accomplish goal number two. Instead of accessing the http request to get the data we need, instead I will dump all the data into the bean and access the bean and then reference it instead.</p>
<p>So here is what the new controller class looks like</p>
<pre name="code" class="java">public final class NewUserFormController {

private NewUserFormController(){

}

protected static String createUser(UserFormBean bean) {
UserDao dao = new UserDaoImpl();
validateUserInput(bean);
if(!dao.doesUserExist(bean)){
dao.insertUser(bean);
}
if(bean.getErrorMsg().isEmpty()){
return FormConstants.COMPLETE_PAGE;
} else{
return FormConstants.FORM_PAGE;
}
}

private static void validateUserInput(UserFormBean bean) {
if (isEmpty(bean.getfName())) {
bean.addErrorMsg("Must enter a first name!");
}
if (isEmpty(bean.getlName())) {
bean.addErrorMsg("Must enter a last mame!");
}
if (isEmpty(bean.geteMail())) {
bean.addErrorMsg("Must enter an email address!");
}
if (isEmpty(bean.getPassword())) {
bean.addErrorMsg("Must enter a password!");
}
if (isEmpty(bean.getConfirmPassword())) {
bean.addErrorMsg("Must confirm password!");
}
if(!bean.getPassword().equals(bean.getConfirmPassword())){
bean.addErrorMsg("Passwords must match!");
}
}

private static boolean isEmpty(String string) {
return string == null || string.trim().equals("");
}
}</pre>
<p>As can be seen the HttpServlet dependency has been completely removed from this code. The code is a bit simpler as I removed pretty much all local variables and use the bean instead. However the best part is I can much more easily write unit tests (which I did).</p>
<h4>So what does this mean to me?</h4>
<p>Well these changes fix much of the four problems I mentioned above. However there is a couple other addtional benefits:</p>
<p><strong>Increased modularity</strong></p>
<p>If a new form was created that had a lot the same fields, but a few additional ones (say city and address), instead of writing a bunch of new code to handle this form, instead these classes; the bean and the controller (removing the final modifier) could be reused. The developer then only needs to write code and test for the new fields.</p>
<p><strong>Separation of responsibilities</strong></p>
<p>With a contract in place there is less of a requirement for the developer writing the backend to be involved in writing the view portion of the application (or vice versa). This allows developers (or designers) to better play to their respective strengths and makes parallel development easier.</p>
<p>You can view all the source code for the entire project <a href="http://turnleafdesign.googlecode.com/svn/trunk/HttpExample/" target="_blank">here</a> (revision 13 is the last revision relevant to this article). The project also requires the <a href="http://www.oracle.com/technetwork/java/download-139443.html" target="_blank">Java servlet package (2.3+) </a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Best+practices' rel='tag' target='_blank'>Best practices</a>, <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a>, <a class='technorati-link' href='http://technorati.com/tag/Unit+testing' rel='tag' target='_blank'>Unit testing</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/web-developers-should-hate-http/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An intro to writing ANT Scripts</title>
		<link>http://www.turnleafdesign.com/an-intro-to-writing-ant-scripts</link>
		<comments>http://www.turnleafdesign.com/an-intro-to-writing-ant-scripts#comments</comments>
		<pubDate>Sat, 14 Aug 2010 19:58:26 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Dev Environment]]></category>
		<category><![CDATA[ANT]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=313</guid>
		<description><![CDATA[What is Ant? A(nother) N(eat) T(ool) is a tool developed by the Apache Foundation for automating tasks in a project. It is primarily, though not exclusively used for Java projects. Most developers interaction with ant is through the xml specification, which is used to define specific operations. The objective of this tutorial Ant was designed [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fan-intro-to-writing-ant-scripts"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fan-intro-to-writing-ant-scripts&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h4>What is Ant?</h4>
<p>A(nother) N(eat) T(ool) is a tool developed by the Apache Foundation for automating tasks in a project. It is primarily, though not exclusively used for Java projects. Most developers interaction with ant is through the xml specification, which is used to define specific operations.</p>
<h4>The objective of this tutorial</h4>
<p>Ant was designed with adaptability and expandability in mind, as a consequence it is used for many tasks and in many systems. As we get deeper in the dev environment series we will rely heavily on ant to make our lives easier and to carry out many important functions. For this tutorial however I will go cover what ant is primarily used for, building a project and creating a deliverable, in this case a jar file. This will provide a good intro into the usage of ant and how it works.<br />
<span id="more-313"></span></p>
<h4>A simple ant script</h4>
<p>So let go ahead and jump in, here is an ant script I wrote for one of my projects:</p>
<pre name="code" class="xml">
&lt;project name="AntProject" default="build-jar" basedir="."&gt;
        &lt;property name="src" location="src" /&gt;
        &lt;property name="build" location="build" /&gt;
        &lt;property name="dist" location="dist" /&gt;

        &lt;target name="init"&gt;
                &lt;mkdir dir="${build}" /&gt;
        &lt;/target&gt;

        &lt;target name="compile" depends="init" description="compile the source "&gt;
                &lt;javac srcdir="${src}" destdir="${build}" /&gt;
        &lt;/target&gt;

        &lt;target name="build-jar" depends="compile" description="generate the distribution"&gt;
                &lt;jar jarfile="${dist}/${ant.project.name}.jar" basedir="${build}"&gt;
                        &lt;manifest&gt;
                                &lt;attribute name="Main-Class" value="com.ant.HelloWorld" /&gt;
                        &lt;/manifest&gt;
                &lt;/jar&gt;
        &lt;/target&gt;
&lt;/project&gt;</pre>
<p>This is a simple build file, for a very (very) simple project which you can view here [http://turnleafdesign.googlecode.com/svn/!svn/bc/7/trunk/Ant%20Project/trunk/Ant%20Project/]. The script is fairly self explanatory but since this is a tutorial...</p>
<p><strong>Project:</strong></p>
<p>The default attribute refers to the target that is run by default when you run the ant build file. So if you were running in command line you would only need to type “ant” in the directory in which the build.xml file resides and the target”build-jar” would be ran. More relevant, from Eclipse (and I assume most other IDEs) you need only click the icon representing the build file to run the default target.</p>
<p>The basedir attribute represents the path from which ant builds off. The path is relative and follows the same  conventions common to navigating file systems through the command line. A period represents the current directory in which the ant scripts resides (which is also the default).</p>
<p><strong>Property:</strong></p>
<p>A tag used to create variables, somewhat similar to Java variables in which you give them a name (though this is not required), a type, and a value. While only the location attribute is shown in the example above, there are many different attributes available.</p>
<p><strong>Target:</strong></p>
<p>Represents an executable operation in the ant script which can be ran from the command line (ant [target name]) or within an IDE. A target can run multiple tasks if needed, for example I could include mkdir, javac, and jar (all of which are considered tasks in ant) within the “init” target.</p>
<p>The depends attribute represents a target that must be run prior to executing the current target, so for the target “compile” to execute the target “init” must be executed. Multiple targets (separated by commas) can be included in the depends attribute with them running in order from left to right.</p>
<p><strong>Mkdir, javac, jar</strong></p>
<p>As briefly mentioned above, these represent tasks in ant. These are predefined operations that ant executes to carry out a task. Unsurprisingly there are a lot of tasks in the ant project. You can create your own as well [http://ant.apache.org/manual/develop.html].</p>
<h4>A bit more about Jar and the jar task</h4>
<p>The main purpose of this script is to build a jar, most with experience with Java know what a jar is but since this is a tutorial...</p>
<p>Jar is a file format specification developed by Sun which is based upon the zip format. The purpose of a jar is to aggregate many files, usually Java classes, into one archive. All jar files must contain a META-INF directory, and a manifest.mf file within that directory. The specification predominantly concerns what can be contained within the META-INF directory and the files there within. Java projects are often built into war and ear files, which when we get to that point I will go over.</p>
<p>Compared to the other tasks in the project there is a bit more going on with the jar task. So to better explain...</p>
<p>Jarfile represents the location where the jar file will be written</p>
<p>basedir represents the root directory of all the files that will be contained within the jar file</p>
<p>The manifest task is used to add additional metadata to the manifest.mf file, which is explained in more detail below.</p>
<h4>The Manifest task and manifest.mf</h4>
<p>Another task that is being executed which I did not touch on is the manifest task. In this example it is designating the main-class which is required in order to allow a jar to be executable (often jars are used as a source library for another project, and don't have (or designate) a main-class so they are not executable).</p>
<p>If you open up the jar file you see two directories; com and META-INF. META-INF as explained earlier is a standard directory in all jars, “com” represents a top level package name. Any number of other folder or files could be in here, depending up how the jar is built. But getting back on track; if you open up the META-INF directory you will see manifest.mf. This file contains metadata about the jar; manifest-version, ant-version, and created-by. All these are standard and are there to provide information as to how the jar was built. Also included is “Main-Class: com.ant.HelloWord,” this is really important because if you attempt to execute the jar the JVM will actually read this file looking for this property.</p>
<p>As we get deeper into this series we will utilize the manifest task more heavily to provide important metadata about the jars, wars, ears we will be building.</p>
<h4>Tip of the iceberg</h4>
<p>This is merely the tip of the proverbial iceberg. As suggested earlier we will increasingly rely on ant as we get deeper into the dev environment series. This tutorial will hopefully provide a good base for explaining what ant is doing for future reference.</p>
<p><strong>Sources:</strong></p>
<p><a href="http://en.wikipedia.org/wiki/Apache_Ant">http://en.wikipedia.org/wiki/Apache_Ant</a></p>
<p><a href="http://en.wikipedia.org/wiki/JAR_%28file_format%29">http://en.wikipedia.org/wiki/JAR_%28file_format%29</a></p>
<p><a href="http://download-llnw.oracle.com/javase/1.5.0/docs/guide/jar/jar.html">http://download-llnw.oracle.com/javase/1.5.0/docs/guide/jar/jar.html</a></p>
<p><a href="http://ant.apache.org/manual/">http://ant.apache.org/manual/</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/ANT' rel='tag' target='_blank'>ANT</a>, <a class='technorati-link' href='http://technorati.com/tag/Automation' rel='tag' target='_blank'>Automation</a>, <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/an-intro-to-writing-ant-scripts/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Intro into Test Driven Development with JUnit4</title>
		<link>http://www.turnleafdesign.com/an-intro-into-test-driven-development-with-junit4</link>
		<comments>http://www.turnleafdesign.com/an-intro-into-test-driven-development-with-junit4#comments</comments>
		<pubDate>Tue, 27 Oct 2009 04:26:16 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Unit]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=260</guid>
		<description><![CDATA[Please read the technical guide before starting this tutorial. This article will mark the first of a long-term series covering professional software development. For the lowdown on this project check out this article. Be sure to give me your feedback as it will be vital in helping me develop better tutorials in the future. Test [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fan-intro-into-test-driven-development-with-junit4"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fan-intro-into-test-driven-development-with-junit4&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Please read the <a href="http://www.turnleafdesign.com/?page_id=263" target="_blank">technical guide </a>before starting this tutorial.</p>
<p>This article will mark the first of a long-term series covering professional software development. For the lowdown on this project check out <a href="http://www.turnleafdesign.com/?p=266" target="_blank">this article</a>. Be sure to give me your feedback as it will be vital in helping me develop better tutorials in the future.</p>
<p>Test driven development seemed like a natural choice as a lead off to my series of tutorials as I had to explain why I am writing all these tests. It is also a very good development methodology that will actually save a lot of time by reducing the amount of time spent debugging. For this tutorial and the entire project, I will be using Junit4. For a synopsis on test driven development you can check out the wikipedia article <a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank">here</a>. If you need a brief refresher on JUnit you can read my tutorial <a href="http://www.turnleafdesign.com/?p=145" target="_blank">here</a> (written in Junit3).<br />
<span id="more-260"></span><br />
The goal of this tutorial, from a project stand point, is to get the connection to our (MySQL) database working. To do this we are going to need several things: a MySQL server, a client to access the server, and a JDBC driver to allow our application to access the server.</p>
<p>The MySQL server:<br />
I will be using MySQL server 5.0 for this project, you can download it <a href="http://dev.mysql.com/downloads/mysql/5.0.html#downloads" target="_blank">here</a>. The tutorial instructions will be based upon the user being “root,” the password “turnleaf,” and the url “http://localhost:3306.”</p>
<p>The MySQL client:<br />
I will be using <a href="http://www.heidisql.com/download.php" target="_blank">HeidiSQL</a> to query and manipulate the database. Once the client is installed import this sql file to setup the database and table.</p>
<p>The JDBC Driver:<br />
You can download the JDBC driver <a href="http://dev.mysql.com/downloads/connector/j/5.0.html" target="_blank">here</a>. Please include it on your projects build path.</p>
<p>To begin this project, please checkout revision 7 from the code repository.</p>
<p>Once you have downloaded the project go ahead and navigate around it a little bit. You will notice I already have several source files; Forecast, ForecastDao, and ForecastDaoImpl. Forecast is a bean for holding all weather information for a specific date (this bean will likely be modified in the future). We also have ForecastDao which is an interface and ForecastDaoImpl which implements ForecastDao. I plan on covering interfaces in more detail in the future, but if you are unfamiliar with the concept you can read a brief description <a href="http://java.sun.com/docs/books/tutorial/java/concepts/interface.html" target="_blank">here</a>.</p>
<p>Update to revision 8 and you will see I added in my first unit test. The unit test is checking to see if I get any returns when I run getAllForecasts(). Here is the code:</p>
<pre name="code" class="java">
@Test

public void testGetAllForecasts(){

List&lt;Forecast&gt; forecasts = dao.getAllForecasts();

Assert.assertTrue(!forecasts.isEmpty());

}
</pre>
<p>If you attempt to run the test you should get a null point error when you attempt to check if forecasts is empty. If you look at the implementation of getAllForecasts() in ForecastDaoImpl it is obvious why, getAllForecasts() is returning null. This is one of the tenants of TDD, write fail first tests.</p>
<p>Go ahead and update to <a href="http://forecastaccuracychecker.googlecode.com/svn/!svn/bc/9/trunk/%20forecastaccuracychecker/forecastchecker/src/com/tld/dao/ForecastDaoImpl.java" target="_blank">revision 9</a>. You will see I have added connection information to getAllForecasts(). Don't worry about it being messy we will refactor it later. Go ahead and run the unit test again. You will still fail, but in the console shows we are successfully connecting to the database.</p>
<p>Update to <a href="http://forecastaccuracychecker.googlecode.com/svn/!svn/bc/10/trunk/%20forecastaccuracychecker/forecastchecker/src/com/tld/dao/ForecastDaoImpl.java" target="_blank">revision 10</a>. I have made several more changes, most noticeably if you run the test it should now pass! (If not check to make sure your database is setup correctly) Before we start celebrating too much, lets actually check the contents of the list to make sure we are getting the right data. Update to <a href="http://forecastaccuracychecker.googlecode.com/svn/!svn/bc/11/trunk/%20forecastaccuracychecker/forecastchecker/src/com/tld/dao/ForecastDaoImpl.java" target="_blank">revision 11</a>, I have added a few more checks, and we are in fact getting the correct data, awesome!</p>
<p><strong>Author note:</strong> For some dumb reason when you create a new Junit4 test class in Eclipse it does not automatically inherit TestClass. Anyways I inherit that class now so you can just do assertEquals(expected, actual) instead of Assert.assertEquals(expected, actual).<br />
If you checked the implementation of getAllForecasts() before updating to revision 10, you will noticed I am only setting the date value of my forecast bean. My new tests initially failed (I originally thought I was setting all fields), thus the importance of not only writing unit tests, but writing good unit tests.</p>
<p>So now it is time to start refactoring. With our unit test we now have a good baseline of how the system should behave. This way when we are making changes we can be confident we are not breaking the system because we will be getting the same output for the same input.</p>
<p>Update to <a href="http://forecastaccuracychecker.googlecode.com/svn/!svn/bc/13/trunk/%20forecastaccuracychecker/forecastchecker/src/com/tld/dao/ForecastDaoImpl.java" target="_blank">revision 13</a>. You will see that I have created the getConnection() method and that contains the database connection logic.</p>
<pre name="code" class="java">
private Connection getConnection() {

Connection conn;

try {

String userName = "root";

String password = "turnleaf";

String url = "jdbc:mysql://localhost/weather";

Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = (Connection) DriverManager.getConnection(url, userName,

password);

} catch (Exception e) {

throw new DataAccessException(e);

}

return conn;

}
</pre>
<p>I pretty much just copied and pasted (one of the VERY few times copying and pasting is ok) the connection logic into this method. The most noticeable change I made is in the catch clause. There is a bunch of different exceptions that could be thrown when attempting to connect to the database, sine they all end in the same scenario, unable to connect to the database, I kept the same generic catch(Exception). However you should never just throw exception so I created my own custom exception called DataAccessException. Inside the super(Throwable) constructor I set it up to add a message stating “Unable to connect to data source,” I also have it extend RunTimeException turning it into an <a href="http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html" target="_blank">unchecked exception</a>.</p>
<p><strong>***Philosophical warning***</strong><br />
This is philosophical decision. As projects become more complex it can become difficult to catch and/or throw an exception at every level. However, you should catch the exception at some point to give the user a reasonable error message and let you know when the application is throwing exceptions. There is another school of thought that all exceptions should be checked as it lets a developer know what exceptions and method could throw, and other various reasons.</p>
<p>Between the message and the name of the exception, it should be pretty clear that when this exception is thrown it means the application failed to connect to the database. Running the unit test we will see everything is still passing so we can be confidant that the refactoring did not break the system.</p>
<p>Update to <a href="http://forecastaccuracychecker.googlecode.com/svn/!svn/bc/14/trunk/%20forecastaccuracychecker/forecastchecker/src/com/tld/dao/ForecastDaoImpl.java" target="_blank">revision 14</a> and you will see I refactored out the closing of the connection. Exceptions should never be ignored, personally I would say being unable to close a database connection could be a major issue. So instead of catching Exception I changed it to only catch SQLException and throw the DataAccessException except I will have my own message in their stating the connection could not be closed. Here is what the closeConnection() method looks like:</p>
<pre name="code" class="java">
private void closeConnection(Connection conn) {

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

throw new DataAccessException("Could not close connection", e);

}

}

}
</pre>
<p>From what it originally looked like, the getAllForecast() method is starting to look a lot better as well as the ForecastDaoImpl class in general. Adding new methods that retrieve data from the database will be easier as all I have to do to get a connection is call getConnection() and to close it closeConnection(Connection). On top of that if I need to change my database connection information I only have to do it in one area.</p>
<p>Despite this refactoring, this is hardly an ideal data access layer. With the test unit already created go ahead and continue to refactor getAllForecast(), a good place to start would be how I create a new Forecast object. I would also suggest adding some new functionality yourself a couple of examples might be; getForecastByDate(Date) or getForecastByCondition(String condition) (Remember create unit tests!). I will continue to refactor and update this work myself. So you can run updates and compare your work to my own.</p>
<p>Addendum and thanks:<br />
Because I am an idiot I couldn't remember how to write a JDBC connection. My initial connection method is predominantly based off of the code in this article: <a href="http://www.kitebird.com/articles/jdbc.html" target="_blank">http://www.kitebird.com/articles/jdbc.html</a><br />
For being able to go to a specific revision with a url:<br />
<a href="http://www.perhammer.com/2008/07/subversion-in-url-revision-browsing.html" target="_blank">http://www.perhammer.com/2008/07/subversion-in-url-revision-browsing.html</a><br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x15, created 9/22/09 */
google_ad_slot = "6237316105";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Best+practices' rel='tag' target='_blank'>Best practices</a>, <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a>, <a class='technorati-link' href='http://technorati.com/tag/Unit' rel='tag' target='_blank'>Unit</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/an-intro-into-test-driven-development-with-junit4/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Link Dump 10/13</title>
		<link>http://www.turnleafdesign.com/link-dump-1013</link>
		<comments>http://www.turnleafdesign.com/link-dump-1013#comments</comments>
		<pubDate>Wed, 14 Oct 2009 01:51:48 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Link Dump]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=224</guid>
		<description><![CDATA[http://www.codeigniter.com – An excellent and lightweight framework for developing PHP applications http://sites.google.com/site/yacoset/ - I may not agree with everything the author says and frankly he probably doesn't care. But there is plenty of good information on his site none the less. http://solitarygeek.com/ - If you like my site then you will probably love this one. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flink-dump-1013"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flink-dump-1013&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.codeigniter.com " target="_blank">http://www.codeigniter.com </a>– An excellent and lightweight framework for developing PHP applications<a href="http://sites.google.com/site/yacoset/" target="_blank"></a></p>
<p><a href="http://sites.google.com/site/yacoset/" target="_blank">http://sites.google.com/site/yacoset/</a> - I may not agree with <a href="http://www.turnleafdesign.com/?p=144" target="_blank">everything the author says</a> and frankly he probably <a href="http://sites.google.com/site/yacoset/Home/how-to-read-this-site" target="_blank">doesn't care</a>. But there is plenty of good information on his site none the less.</p>
<p><a href="http://solitarygeek.com/" target="_blank">http://solitarygeek.com/</a> - If you like my site then you will probably love this one. Similar in writing style and purpose, just a more mature blog written by a more experienced developer.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x60, created 9/14/09 */
google_ad_slot = "1115297999";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Best+practices' rel='tag' target='_blank'>Best practices</a>, <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/link-dump-1013/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Link Dump 10/6</title>
		<link>http://www.turnleafdesign.com/link-dump-106</link>
		<comments>http://www.turnleafdesign.com/link-dump-106#comments</comments>
		<pubDate>Wed, 07 Oct 2009 03:22:07 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Link Dump]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=177</guid>
		<description><![CDATA[http://sourcemaking.com/ - Co-authored by Martin Fowler, a bunch of useful information and best practices on this site. http://www.pbell.com/index.cfm/design-patterns -A wealth of information covering many different areas of programming. Definitely something here for everybody. http://marxsoftware.blogspot.com/ - A well established blog with hundreds of articles covering mostly Java. Technorati Tags: Best practices, Java, Programming]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flink-dump-106"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flink-dump-106&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://sourcemaking.com/" target="_blank">http://sourcemaking.com/</a> - Co-authored by Martin Fowler, a bunch of useful information and best practices on this site.</p>
<p><a href="http://www.pbell.com/index.cfm/design-patterns" target="_blank">http://www.pbell.com/index.cfm/design-patterns </a>-A wealth of information covering many different areas of programming. Definitely something here for everybody.</p>
<p><a href="http://marxsoftware.blogspot.com" target="_blank">http://marxsoftware.blogspot.com/</a> - A well established blog with hundreds of articles covering mostly Java.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x60, created 9/14/09 */
google_ad_slot = "1115297999";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Best+practices' rel='tag' target='_blank'>Best practices</a>, <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/link-dump-106/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Link Dump 9/28</title>
		<link>http://www.turnleafdesign.com/link-dump-928</link>
		<comments>http://www.turnleafdesign.com/link-dump-928#comments</comments>
		<pubDate>Wed, 30 Sep 2009 04:13:59 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Link Dump]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=135</guid>
		<description><![CDATA[http://www.javaworld.com/ - An excellent Java resource http://onjava.com/ - A blog covering java (obviously) supported by O'Reilly Media http://martinfowler.com/ - The website for one of the most respected voices in OO-development Technorati Tags: Java]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flink-dump-928"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flink-dump-928&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.javaworld.com/" target="_blank">http://www.javaworld.com/</a> - An excellent Java resource</p>
<p><a href="http://onjava.com/" target="_blank">http://onjava.com/</a> - A blog covering java (obviously) supported by O'Reilly Media</p>
<p><a href="http://martinfowler.com/" target="_blank">http://martinfowler.com/</a> - The website for one of the most respected voices in OO-development<br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x15, created 9/22/09 */
google_ad_slot = "6237316105";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/link-dump-928/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>If, else and nothing else</title>
		<link>http://www.turnleafdesign.com/if-else-and-nothing-else</link>
		<comments>http://www.turnleafdesign.com/if-else-and-nothing-else#comments</comments>
		<pubDate>Sat, 19 Sep 2009 19:52:03 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Noob Corner]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=81</guid>
		<description><![CDATA[When I was still very new to programming I had a bad habit of writing bloated code. One of my worse areas was when it came to the usage of If statements. Often times I would write a whole if/else block when I could had just as easily gotten the same results in just one [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fif-else-and-nothing-else"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fif-else-and-nothing-else&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>When I was still very new to programming I had a bad habit of writing bloated code. One of my worse areas was when it came to the usage of If statements. Often times I would write a whole if/else block when I could had just as easily gotten the same results in just one line of code.<span id="more-81"></span></p>
<blockquote><p>public class IfExample {</p>
<p>public static void main(String[] args){<br />
boolean getBoolValue;<br />
int a = 1, b = 2;</p>
<p>if(a &gt; b){<br />
getBoolValue = true;<br />
} else{<br />
getBoolValue = false;<br />
}<br />
}</p></blockquote>
<p>Instead of writing the whole if/else statement instead you can just directly take the result of the test condition.</p>
<blockquote><p>getBoolValue = a &gt; b;</p></blockquote>
<p>Some prefer to put parentheses around the comparison, however they are optional. I personally prefer a more minimalist code style.</p>
<h3>Be the ternary</h3>
<p>So what if you need something other than a boolean value? Enter the Java ternary operator. Which looks like this:</p>
<blockquote><p>String isEven = (3 % 2 == 0) ? "Yes" : "No";</p></blockquote>
<p>The element before the “?” is the test condition that is to be performed. The element before the “:” is the value that will be returned if the test condition is true and the element after the “:” is what will be returned if the test condition is false.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x60, created 9/14/09 */
google_ad_slot = "1115297999";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/if-else-and-nothing-else/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Foreach isn&#8217;t a reach in pre-1.5</title>
		<link>http://www.turnleafdesign.com/foreach-isnt-a-reach-pre-1-5</link>
		<comments>http://www.turnleafdesign.com/foreach-isnt-a-reach-pre-1-5#comments</comments>
		<pubDate>Sat, 19 Sep 2009 03:10:47 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=69</guid>
		<description><![CDATA[Developers living in the post 1.5 world are spoiled. With the very sweet foreach loop life is easy when you need to iterate through a list of objects. While us poor developers still living working with 1.4 or lower may not be able to totally match the 1.5 foreach loop we can certainly come close! [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fforeach-isnt-a-reach-pre-1-5"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fforeach-isnt-a-reach-pre-1-5&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Developers living in the post 1.5 world are spoiled. With the very sweet <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html">foreach</a> loop life is easy when you need to iterate through a list of objects. While us poor developers still living working with 1.4 or lower may not be able to totally match the 1.5 foreach loop we can certainly come close! While there are several ways to implement foreach functionality in 1.4 or below I will focus on the way I prefer to do it as I think it is the best way.</p>
<p><span id="more-69"></span></p>
<blockquote><p>public class ForeachExample {</p>
<p>public static void main(String[] args) {<br />
List personList = new ArrayList();</p>
<p>Person joe = new Person();<br />
joe.setFirstName("Joe");<br />
joe.setLastName("Smith");</p>
<p>Person cindy = new Person();<br />
cindy.setFirstName("Cindy");<br />
cindy.setLastName("Doe");</p>
<p>personList.add(joe);<br />
personList.add(cindy);</p>
<p>for(Iterator iter = personList.iterator(); iter.hasNext();){<br />
Person person = (Person)iter.next();<br />
if(person.getFirstName().equals("Joe")){<br />
iter.remove();<br />
continue;<br />
} else if(person.getFirstName().equals("Cindy")){<br />
System.out.println(person.getFirstName() + " wins again!");<br />
}<br />
System.out.println(person.getFirstName());<br />
}<br />
//Prints "Cindy wins again!"<br />
}</p>
<p>}</p></blockquote>
<h3>Iterate? Can you please elaborate...</h3>
<p>Ok so there is quite a bit happening there. First the for loop declaration. In the first section (called the initialization statement) I'm declaring an Iterator object on personList (any class that extends the Collection class can invoke the .iterator() method). An Iterator holds all the values of a collection and gives you tools for moving through and manipulating the list (Note: you can only move forward). I am then setting up the condition for when the for loop should run, in this case when my Iterator object returns false after the last member of my list the program will drop out of the loop. Usually there would be a next statement in a for loop declaration, however it is optional and typically isn't necessary in a foreach loop.<br />
Inside the loop we are assigning a person object to the current iter element, which is of course based on our list we declared earlier. Because an Iterator doesn't know what it holds you must cast the object type when you retrieve it from the Iterator (this issue is solved in 1.5 with parameterization). Next we have have an if statement checking for the name “Joe” in the firstName attribute of a person object, if true the “iter.remove()” method is called. This is a handy method which removes the current element from the Iterator object and also from the underlying list. So if I was to attempt to iterate through the personList again, all person objects with the firstName of “Joe” will have been removed. If you haven't already been exposed to it, the continue statement ends the current iteration of the loop. It's not really necessary here, but I added as it's a common practice.</p>
<h3>That's all nice but why?</h3>
<p>I choose to use the for loop method for imitating a foreach loop because; it keeps the code clean by putting all the loop declaration stuff into one place and the Iterator object is dropped from memory at the same time the for loop goes out of scope. The other way of handling a for each loop in pre-1.5 Java would be the following:</p>
<blockquote><p>Iterator iter = personList.iterator();<br />
while(iter.hasNext()){<br />
Person person = (Person)iter.next();<br />
if(person.getFirstName().equals("Joe")){<br />
iter.remove();<br />
continue;<br />
} else if(person.getFirstName().equals("Cindy")){<br />
System.out.println(person.getFirstName() + " wins again!");<br />
}<br />
System.out.println(person.getFirstName());<br />
}<br />
//Prints "Cindy wins again!"</p></blockquote>
<p>To me the code doesn't look as clean and the program holds on to the Iterator object even though it is no longer useful (remember you cannot re-iterate through an Iterator object). It's a matter of preference, but using the for loop method will always be the better choice from a strictly programmatic perspective.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x60, created 9/14/09 */
google_ad_slot = "1115297999";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/foreach-isnt-a-reach-pre-1-5/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Link Dump 9/17</title>
		<link>http://www.turnleafdesign.com/link-dump-917</link>
		<comments>http://www.turnleafdesign.com/link-dump-917#comments</comments>
		<pubDate>Fri, 18 Sep 2009 03:55:58 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Link Dump]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=65</guid>
		<description><![CDATA[http://www.sixrevisions.com/ - A very active website that focuses on web design. http://www.net.tutsplus.com/ - Has excellent tutorials on web design and development http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html – An absolute must check out for all Java developers. Focuses on best practices for Java developers. Though I wouldn't recommend it for the very new as the concepts may be a bit [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flink-dump-917"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Flink-dump-917&amp;source=TurnleafDesign&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.sixrevisions.com/">http://www.sixrevisions.com/</a> - A very active website that focuses on web design.</p>
<p><a href="http://net.tutsplus.com/">http://www.net.tutsplus.com/</a> - Has excellent tutorials on web design and development</p>
<p><a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html">http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html</a> – An absolute must check out for all Java developers. Focuses on best practices for Java developers. Though I wouldn't recommend it for the very new as the concepts may be a bit advanced.</p>
<p><a href="http://www.ohloh.net/">http://www.ohloh.net/</a> - A very good place to start if you are looking to contribute to an open source project.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x60, created 9/14/09 */
google_ad_slot = "1115297999";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Best+practices' rel='tag' target='_blank'>Best practices</a>, <a class='technorati-link' href='http://technorati.com/tag/Java' rel='tag' target='_blank'>Java</a>, <a class='technorati-link' href='http://technorati.com/tag/Open+source' rel='tag' target='_blank'>Open source</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a>, <a class='technorati-link' href='http://technorati.com/tag/Web+design' rel='tag' target='_blank'>Web design</a>, <a class='technorati-link' href='http://technorati.com/tag/Web+development' rel='tag' target='_blank'>Web development</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/link-dump-917/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

