<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://stephenviles.com/</id>
  <title>Stephen Viles</title>
  <updated>2010-05-31T12:00:00Z</updated>
  <link rel="alternate" href="http://stephenviles.com/"/>
  <link rel="self" href="http://stephenviles.com/articles/atom.xml"/>
  <author>
    <name>Stephen Viles</name>
    <uri>http://stephenviles.com/</uri>
  </author>
  <entry>
    <id>tag:stephenviles.com,2010-06-01:/articles/intro-to-java-web-applications/</id>
    <title type="html">Introduction to Java web applications</title>
    <published>2010-05-31T12:00:00Z</published>
    <updated>2010-05-31T12:00:00Z</updated>
    <link rel="alternate" href="http://stephenviles.com/articles/intro-to-java-web-applications/"/>
    <content type="html">&lt;p&gt;I used the following material to give a one-hour overview of Java and web applications to a group of developers
experienced in the Microsoft ecosystem.&lt;/p&gt;

&lt;h4&gt;The basic Hello World: Java source in .java file compiles to JVM bytecode in .class file&lt;/h4&gt;

&lt;p&gt;See the following sections of this useful
    &lt;a href="http://www.vogella.de/articles/JavaIntroduction/article.html"&gt;Introduction to Java Programming&lt;/a&gt;:
&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;1. Introduction&lt;/li&gt;
    &lt;li&gt;2. Classpath&lt;/li&gt;
    &lt;li&gt;3. Your first Java program&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Section 13 provides an overview of JAR (Java ARchive) files. More details at the
    &lt;a href="http://java.sun.com/docs/books/tutorial/deployment/jar/index.html"&gt;Java Tutorial Lesson: Packaging
    Programs in JAR Files&lt;/a&gt;.
&lt;/p&gt;

&lt;h4&gt;Using Java for Financial Applications&lt;/h4&gt;

&lt;h5&gt;Dates and Times&lt;/h5&gt;

&lt;p&gt;The standard Java library has two classes for dates and times:
    &lt;a href="http://java.sun.com/javase/6/docs/api/java/util/Date.html"&gt;java.util.Date&lt;/a&gt; and
    &lt;a href="http://java.sun.com/javase/6/docs/api/java/util/Calendar.html"&gt;java.util.Calendar&lt;/a&gt;. Both are mutable
    and difficult to use for common date arithmetic. A much better replacement is
    &lt;a href="http://joda-time.sourceforge.net/"&gt;Joda-Time&lt;/a&gt;. In particular, the
    &lt;a href="http://joda-time.sourceforge.net/api-release/org/joda/time/LocalDate.html"&gt;LocalDate&lt;/a&gt; class is an
    &amp;ldquo;immutable datetime class representing a date without a time zone&amp;rdquo;. Most of the time, this is exactly
    what you need.
&lt;/p&gt;

&lt;h5&gt;Monetary Values&lt;/h5&gt;

&lt;p&gt;Java does not have a built-in decimal data type. Instead, you need to use the
    &lt;a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html"&gt;BigDecimal&lt;/a&gt; class in the standard
    library for immutable, arbitrary-precision signed decimal numbers with complete control over rounding behavior.
&lt;/p&gt;
&lt;p&gt;Do not use either of the binary floating-point types (float or double). It is impossible to
    represent 0.1 (or any other negative power of ten) as a float or double exactly. The representation errors
    and rounding errors will bite you. See &lt;a href="http://floating-point-gui.de/"&gt;http://floating-point-gui.de/&lt;/a&gt;
    for more details on &amp;ldquo;what every programmer should know about floating-point arithmetic (or why don&amp;rsquo;t my
    numbers add up?)&amp;rdquo;.
&lt;/p&gt;
&lt;p&gt;I recommend carrying the currency with every monetary value, as documented in the
    &lt;a href="http://martinfowler.com/eaaCatalog/money.html"&gt;Money pattern&lt;/a&gt; (Patterns of Enterprise Application
    Architecture, Martin Fowler). The
    &lt;a href="http://java.sun.com/javase/6/docs/api/java/util/Currency.html"&gt;Currency&lt;/a&gt; class in the standard Java
    library represents currencies using their ISO 4217 currency codes.
&lt;/p&gt;

&lt;h4&gt;Programming Guidance: Effective Java and Collections&lt;/h4&gt;

&lt;p&gt;The book &lt;a href="http://java.sun.com/docs/books/effective/"&gt;Effective Java, Second Edition&lt;/a&gt; by Joshua
    Bloch gives much useful programming guidance in bite-sized articles.
&lt;/p&gt;
&lt;p&gt;The Java Collections framework in
    &lt;a href="http://java.sun.com/javase/6/docs/api/java/util/package-summary.html#package_description"&gt;java.util&lt;/a&gt;
    is very useful. See the
    &lt;a href="http://java.sun.com/javase/6/docs/technotes/guides/collections/overview.html"&gt;overview&lt;/a&gt; and
    &lt;a href="http://java.sun.com/javase/6/docs/technotes/guides/collections/reference.html"&gt;annotated outline&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;Google have written an
    &lt;a href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/package-summary.html#package_description"&gt;extension
    collection framework&lt;/a&gt; in the &lt;a href="http://code.google.com/p/guava-libraries/"&gt;Guava Google Core Libraries for Java&lt;/a&gt;.
&lt;/p&gt;

&lt;h4&gt;Structure of Java Web Applications&lt;/h4&gt;

&lt;p&gt;The most basic component of a Java web application is the &lt;dfn&gt;servlet&lt;/dfn&gt;: a Java class that responds to HTTP requests.&lt;/p&gt;
&lt;p&gt;Start with the overview page &lt;a href="http://java.sun.com/j2ee/1.4/docs/tutorial/doc/WebApp.html"&gt;Getting Started
    with Web Applications&lt;/a&gt;, then read the following sections of the
    &lt;a href="http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html"&gt;J2EE 1.4 tutorial&lt;/a&gt;:
&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Chapter 1: Overview&lt;/li&gt;
    &lt;li&gt;Chapter 3: Getting Started with Web Applications&lt;/li&gt;
    &lt;li&gt;Chapter 11: Java Servlet Technology&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After that, you'll find the other 32 chapters describe many other technology components that you can add to Java
    web applications, including Web services.
&lt;/p&gt;

&lt;h4&gt;A Hello World Web Application using Eclipse, Ant and Tomcat&lt;/h4&gt;

&lt;p&gt;The following pages give detailed instructions on how to build a Hello World web application. Along the way, they
    introduce the Eclipse IDE, the Ant build tool, and the Tomcat servlet container.&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;&lt;a href="http://obscuredclarity.blogspot.com/2009/11/hello-world-java-web-application-in.html"&gt;Hello World Java
        Web Application in Eclipse (Part 1)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://obscuredclarity.blogspot.com/2009/11/hello-world-java-web-application-in_12.html"&gt;Hello World
        Java Web Application in Eclipse (Part 2)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://obscuredclarity.blogspot.com/2009/11/hello-world-java-web-application-in_3043.html"&gt;Hello World
        Java Web Application in Eclipse (Part 3)&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The copyright notice on these pages is rather amusing.&lt;/p&gt;

&lt;h4&gt;Web Application Security Issues&lt;/h4&gt;

&lt;p&gt;A public-facing web application has particular security issues. See the
    &lt;a href="http://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project"&gt;Open Web Application Security Project
    (OWASP) Top Ten&lt;/a&gt; for a general overview, and then see the
    &lt;a href="http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API#tab=Java_EE"&gt;Enterprise Security
    API for Java EE&lt;/a&gt;, which provides a security framework for Java web applications.
&lt;/p&gt;</content>
    <summary type="html">I used the following material to give a one-hour overview of Java and web applications to a group of developers experienced in the Microsoft ecosystem.</summary>
  </entry>
  <entry>
    <id>tag:stephenviles.com,2010-05-31:/articles/removing-line-noise-with-a-user-style-sheet/</id>
    <title type="html">Removing line noise with a user style sheet</title>
    <published>2010-05-30T12:00:00Z</published>
    <updated>2010-05-30T12:00:00Z</updated>
    <link rel="alternate" href="http://stephenviles.com/articles/removing-line-noise-with-a-user-style-sheet/"/>
    <content type="html">&lt;p&gt;I find text with underlining or strikethough difficult to read, so I use the following
&lt;a href="http://www.w3.org/TR/CSS21/cascade.html#cascade"&gt;user style sheet&lt;/a&gt; to remove it, except when hovering over links:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
/*
 * This file can be used to apply a style to all web pages you view
 * Rules without !important are overruled by author rules if the
 * author sets any.  Rules with !important overrule author rules.
 */

:link, :visited {
  text-decoration : none !important ;
}
:link:hover, :visited:hover {
  text-decoration : underline !important ;
}
del {
  text-decoration : none !important ;
}
strike {
  text-decoration : none !important ;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Firefox, the user style sheet file is called chrome/userContent.css in the
&lt;a href="http://www.gemal.dk/mozilla/profile.html"&gt;user profile directory&lt;/a&gt;.&lt;/p&gt;</content>
    <summary type="html">I find text with underlining or strikethough difficult to read, so I use the following user style sheet to remove it, except when hovering over links.</summary>
  </entry>
  <entry>
    <id>tag:stephenviles.com,2010-04-25:/articles/a-pattern-for-scripts-that-update-data/</id>
    <title type="html">A pattern for scripts that update data</title>
    <published>2010-04-24T12:00:00Z</published>
    <updated>2010-04-24T12:00:00Z</updated>
    <link rel="alternate" href="http://stephenviles.com/articles/a-pattern-for-scripts-that-update-data/"/>
    <content type="html">&lt;p&gt;The following Bash script is designed to be run from a crontab entry. It performs a database update (using Oracle's
&lt;kbd&gt;sqlplus&lt;/kbd&gt;), checks the output for errors, and adds a success or failure comment to a
&lt;a href="http://www.atlassian.com/software/jira/"&gt;JIRA&lt;/a&gt; ticket by sending an email to the JIRA instance.&lt;/p&gt;

&lt;p&gt;A JIRA instance only accepts email from the addresses of its registered users, so I had to use sendmail with an email
constructed from scratch in order to control the From: address. The issue key is specified in the Subject: line.&lt;/p&gt;

&lt;p&gt;Note the signature block of the email (after the &lt;samp&gt;-- &lt;/samp&gt; line), which identifies where the script is running,
and as which user &amp;mdash; crucial information if you want to modify its behaviour.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Caution:&lt;/em&gt; You don't want to use too many of these out-of-band database updates. Like database triggers, they
have their uses, but do not scale well. A system with more than a few of these scripts will be hard to understand, and
harder to modify. Temporary solutions often become permanent, and permanent solutions should be inside your application,
not outside it.&lt;/p&gt;

&lt;pre&gt;&lt;code class='language-bash'&gt;
#! /bin/bash
set -u
SCRIPT_NAME=$(basename $BASH_SOURCE)
SCRIPT_DIR=$(dirname $(readlink -f $0))
SQL_OUTPUT=/tmp/$SCRIPT_NAME-$$.out
DB_CREDENTIALS=

# Do the update
sqlplus $DB_CREDENTIALS 2&gt;&amp;amp;1 &gt;&gt; $SQL_OUTPUT &amp;lt;&amp;lt; EOF
set echo on
@$SCRIPT_DIR/do-it.sql
EOF

# Look for Oracle errors
ERROR_LIST=/tmp/$SCRIPT_NAME-$$.err
grep 'ORA-' $SQL_OUTPUT &gt;&gt; $ERROR_LIST
grep 'SP2-' $SQL_OUTPUT &gt;&gt; $ERROR_LIST

# Look for success messages
function look_for() {
    grep --silent "$1" $SQL_OUTPUT
    RETURNVAL=$?
    if [[ $RETURNVAL -ne 0 ]] ; then
        echo "Did not find expected output: $1" &gt;&gt; $ERROR_LIST
    fi
}

look_for '1 row updated.'
look_for 'Commit complete.'

# Start the log, which is in email format
SCRIPT_LOG=/tmp/$SCRIPT_NAME-$$.log
cat &gt;&gt; $SCRIPT_LOG &amp;lt;&amp;lt; EOF
To: jira@example.com,me@example.com
From: me@example.com
Subject: PRJ-12345

EOF

# Build body of log
if [ -s "$ERROR_LIST" ] ; then
    echo "Errors found while attempting to do it:" &gt;&gt; $SCRIPT_LOG
    cat $ERROR_LIST &gt;&gt; $SCRIPT_LOG
    echo &gt;&gt; $SCRIPT_LOG
    echo "Full command output:" &gt;&gt; $SCRIPT_LOG
    cat $SQL_OUTPUT &gt;&gt; $SCRIPT_LOG
else
    echo "Done it" &gt;&gt; $SCRIPT_LOG
fi

# Add script detail to log and send it to JIRA ticket
cat &gt;&gt; $SCRIPT_LOG &amp;lt;&amp;lt; EOF

--
Sent by script $SCRIPT_DIR/$SCRIPT_NAME running on host `hostname`.`dnsdomainname` as user `whoami`
EOF
/usr/lib/sendmail -t &amp;lt; $SCRIPT_LOG &gt;&gt; $ERROR_LIST 2&gt;&amp;amp;1

RETURNVAL=$?
if [[ $RETURNVAL -ne 0 ]] ; then
    echo "sendmail had a non-zero exit code, see above for output" &gt;&gt; $ERROR_LIST
else
    rm $SQL_OUTPUT $ERROR_LIST $SCRIPT_LOG
fi
&lt;/code&gt;&lt;/pre&gt;</content>
    <summary type="html">The following Bash script is designed to be run from a crontab entry. It performs a database update (using Oracle's sqlplus), checks the output for errors, and adds a success or failure comment to a JIRA ticket by sending an email to the JIRA instance.</summary>
  </entry>
  <entry>
    <id>tag:stephenviles.com,2010-04-23:/articles/moving-from-blogger-to-nanoc/</id>
    <title type="html">Moving from Blogger to nanoc</title>
    <published>2010-04-22T12:00:00Z</published>
    <updated>2010-04-22T12:00:00Z</updated>
    <link rel="alternate" href="http://stephenviles.com/articles/moving-from-blogger-to-nanoc/"/>
    <content type="html">&lt;p&gt;Blogger are &lt;a href="http://blogger-ftp.blogspot.com/"&gt;shutting down their (S)FTP facility&lt;/a&gt;, so I'm moving to
    &lt;a href="http://nanoc.stoneship.org/"&gt;nanoc&lt;/a&gt;, a "Ruby web publishing system for building small to medium-sized websites".
&lt;/p&gt;
&lt;p&gt;I used &lt;a href="http://anemone.rubyforge.org/"&gt;Anemone&lt;/a&gt; to find all current URLs and set up
    &lt;a href="http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect"&gt;redirects&lt;/a&gt; for each, and I've given
    the site a minor refresh.
&lt;/p&gt;
&lt;p&gt;The feed URL is moving from &lt;a href="http://stephenviles.com/blog/atom.xml"&gt;http://stephenviles.com/blog/atom.xml&lt;/a&gt;
    to &lt;a href="http://stephenviles.com/articles/atom.xml"&gt;http://stephenviles.com/articles/atom.xml&lt;/a&gt;, but you may
    not need to update your feed readers if they understand the permanent redirect.
&lt;/p&gt;
&lt;p&gt;I'm enjoying using nanoc to create a &lt;a href="http://www.artima.com/intv/dry.html"&gt;DRY&lt;/a&gt; and
    &lt;a href="http://www.techterms.com/definition/staticwebsite"&gt;static&lt;/a&gt; site.
&lt;/p&gt;</content>
    <summary type="html">Blogger are shutting down their (S)FTP facility, so I'm moving to nanoc. I've redirected all current URLs and given the site a minor refresh.</summary>
  </entry>
  <entry>
    <id>tag:stephenviles.com,2009-02-09:/articles/whats-problem-were-trying-to-solve/</id>
    <title type="html">What's the problem we're trying to solve?</title>
    <published>2009-02-08T11:00:00Z</published>
    <updated>2009-02-08T11:00:00Z</updated>
    <link rel="alternate" href="http://stephenviles.com/articles/whats-problem-were-trying-to-solve/"/>
    <content type="html">&lt;p&gt;When a discussion starts to get overly general, and seems to be generating more problems than solutions, a simple
    question can help regain some focus:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;What's the problem we're trying to solve?&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Solve that problem first before you try to solve any others.&lt;/em&gt; Sure, note down any other problems you've
    discovered, and make sure that the solution to the original problem does not cause any of those problems (or if it
    does, the other problems are more acceptable than the original).&lt;/p&gt;

&lt;p&gt;The question is also useful when someone has a well-meaning &lt;a
        href="http://97-things.near-time.net/wiki/avoid-good-ideas"&gt;"good idea"&lt;/a&gt; that may be a solution in search of
    a problem. You can also ask:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;What is the simplest method of solving this particular problem?&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;as a gentle guide towards doing the simplest thing that could possibly work.&lt;/p&gt;</content>
    <summary type="html">When a discussion starts to get overly general, and seems to be generating more problems than solutions, a simple question can help regain some focus.</summary>
  </entry>
</feed>
