<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Johnmunsch's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 03:32:14 GMT</pubDate>
    <description>DZone Snippets: Johnmunsch's Code Snippets</description>
    <item>
      <title>Log4j 101</title>
      <link>http://snippets.dzone.com/posts/show/3857</link>
      <description>// Once you have included the Log4j jar into the classpath of your&lt;br /&gt;// application, you can use the following code within each class to&lt;br /&gt;// get a logger for that class. That is, all log messages issued from&lt;br /&gt;// within that class will be tagged with the classname to make it&lt;br /&gt;// easier to locate a problem. See my tutorial on Log4j,&lt;br /&gt;// Log4j in 30 Minutes or Less (http://www.johnmunsch.com/projects/Presentations/)&lt;br /&gt;// for a better introduction.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;private static Logger log = Logger.getLogger(&lt;class name&gt;.class);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// When you need to specify the location (or a different name) for &lt;br /&gt;// the log.properties file, you can set a specific System variable&lt;br /&gt;// which Log4j will be guaranteed to read as soon as any part of&lt;br /&gt;// your code tries to use it. You do that by passing the following&lt;br /&gt;// to the JVM as you start your Java application.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;-Dlog4j.configuration=file:&lt;path to log.properties file&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Apr 2007 14:43:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3857</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>Proper Use Of String and StringBuffer In Java</title>
      <link>http://snippets.dzone.com/posts/show/2266</link>
      <description>// This isn't really code at all, this is a comment that I use &lt;br /&gt;// over and over again as I go through code written by people &lt;br /&gt;// who obviously don't understand when to use a String and when&lt;br /&gt;// to use a StringBuffer.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// I put this block of text wherever I find problems with Strings&lt;br /&gt;// and StringBuffers to improve understanding of how each one works&lt;br /&gt;// and when to use one or the other.&lt;br /&gt;//&lt;br /&gt;// Rule #1: Use a StringBuffer if you intend to build a string out of&lt;br /&gt;// dynamic elements. That is, if you are going to include the results&lt;br /&gt;// of function calls or variables as part of the string then you use&lt;br /&gt;// a StringBuffer and do append() calls. StringBuffer's append is&lt;br /&gt;// much faster than using "+" to stick together String objects.&lt;br /&gt;//&lt;br /&gt;// Rule #2: But, if you are just building a string out of static pieces&lt;br /&gt;// of text, it's better to use Strings and "+" than creating a&lt;br /&gt;// StringBuffer and making append calls. Instead just use a String&lt;br /&gt;// and a bunch of "+" signs between the sections. For example:&lt;br /&gt;// String test = "this " + "is " + "a " + "test " + "string";&lt;br /&gt;// is _not_ expensive. Why? Because all the pieces are static text and&lt;br /&gt;// the compiler can make it into this _as it compiles the code_:&lt;br /&gt;// String test = "this is a test string";&lt;br /&gt;//&lt;br /&gt;// Rule #3: For goodness sake, do this:&lt;br /&gt;// StringBuffer test = new StringBuffer();&lt;br /&gt;// test.append("static string ");&lt;br /&gt;// test.append(dynamicCall());&lt;br /&gt;// test.append(" another static string ");&lt;br /&gt;// test.append(someVariable);&lt;br /&gt;// DON'T DO THIS:&lt;br /&gt;// StringBuffer test = new StringBuffer();&lt;br /&gt;// test.append("static string " + dynamicCall());&lt;br /&gt;// test.append(" another static string " + someVariable);&lt;br /&gt;// This is officially the worst of all worlds. You need to use a&lt;br /&gt;// StringBuffer in this case because you've got some dynamic parts&lt;br /&gt;// but there is still addition going on between dynamic parts (the&lt;br /&gt;// function call and variable) and static strings. Ack!&lt;br /&gt;//&lt;br /&gt;// If you can't remember anything else, remember this:&lt;br /&gt;// static strings = String and "+"&lt;br /&gt;// dynamic strings = StringBuffer and append() &lt;/code&gt;</description>
      <pubDate>Mon, 10 Jul 2006 20:06:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2266</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>Returning A File As An Attachment From JSP</title>
      <link>http://snippets.dzone.com/posts/show/2096</link>
      <description>// This is truly a snippet. It's a small piece of code you use&lt;br /&gt;// within a JSP file to make sure that the file you are&lt;br /&gt;// returning is not interpreted by the browser. You use this&lt;br /&gt;// when you want to return a TXT file, JPG or other file that&lt;br /&gt;// the browser might open on its own rather than offer a &lt;br /&gt;// dialog to the user so it can be saved.&lt;br /&gt;//&lt;br /&gt;// Note: In this example I'm returning a text file so I set &lt;br /&gt;// the MIME type of the response to text/plain, but you need &lt;br /&gt;// to make sure it matches the type of data you are returning.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;response.setContentType("text/plain");&lt;br /&gt;&lt;br /&gt;response.setHeader("Content-Disposition", "attachment; filename=\"test.txt\"");&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 25 May 2006 01:04:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2096</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>JDBC 101</title>
      <link>http://snippets.dzone.com/posts/show/2095</link>
      <description>// Note the use of a prepared statement. You can gain a couple of different benefits&lt;br /&gt;// from using a prepared statement versus a conventional statement. One is that the&lt;br /&gt;// database can compile the statement once and simply insert different values into the&lt;br /&gt;// variables each time you call it. For frequently used functions this can result in&lt;br /&gt;// improved performance. The second advantage is where the values to be passed in might&lt;br /&gt;// be changable by hostile users (e.g. a search term taken from a webpage). In those&lt;br /&gt;// cases assembled SQL strings are suseptible to attack by cleverly phrased inputs but&lt;br /&gt;// prepared statements are not because they are _never_ interpreted as actual SQL, only&lt;br /&gt;// as values for variables.&lt;br /&gt;//&lt;br /&gt;// See my other code snippets "Getting A Data Source From Tomcat" and "Data Source 101"&lt;br /&gt;// for more information on how to get the data source you pass into a routine like this.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    /**&lt;br /&gt;     * In this case the Record object is something we are saving to the database.&lt;br /&gt;     */&lt;br /&gt;    public void persistRecord(DataSource dataSource, &lt;br /&gt;        Record record) throws Exception {&lt;br /&gt;&lt;br /&gt;        Connection conn = null;&lt;br /&gt;        PreparedStatement st = null;&lt;br /&gt;        &lt;br /&gt;        int retVal = 0;&lt;br /&gt;        &lt;br /&gt;        try {&lt;br /&gt;            conn = dataSource.getConnection();&lt;br /&gt;            &lt;br /&gt;            st = conn.prepareStatement("insert into test(a, b) values(?, ?)");&lt;br /&gt;&lt;br /&gt;            st.setInt(1, record.getA());&lt;br /&gt;            st.setString(2, record.getB());&lt;br /&gt;            &lt;br /&gt;            int records = st.executeUpdate();&lt;br /&gt;        } catch (SQLException se) {&lt;br /&gt;            log.error(se, se);&lt;br /&gt;            throw se;&lt;br /&gt;        } finally {&lt;br /&gt;            UtilityJDBC.closeSQLClasses(conn, st, null);   &lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * NOTE!! if you close a statement the associated ResultSet&lt;br /&gt;     * is closed too so you should copy the contents to some other&lt;br /&gt;     * object. The result set is invalidated also if you recycle a&lt;br /&gt;     * Statement and try to execute some other query before the result&lt;br /&gt;     * set has been completely examined.&lt;br /&gt;     * &lt;br /&gt;     * @param conn&lt;br /&gt;     * @param st&lt;br /&gt;     * @param rs&lt;br /&gt;     * @throws SQLException&lt;br /&gt;     */&lt;br /&gt;    public static void closeSQLClasses(Connection conn, Statement st,&lt;br /&gt;        ResultSet rs) {&lt;br /&gt;&lt;br /&gt;        try {&lt;br /&gt;            if (rs != null) {&lt;br /&gt;                rs.close();&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            if (st != null) {&lt;br /&gt;                st.close();&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            if (conn != null) {&lt;br /&gt;                conn.close();&lt;br /&gt;            }&lt;br /&gt;        } catch (SQLException se) {&lt;br /&gt;            log.error(se, se);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 24 May 2006 23:54:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2095</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>A Client For the XML-RPC Servlet</title>
      <link>http://snippets.dzone.com/posts/show/1636</link>
      <description>// When combined with the Apache XML-RPC library this code&lt;br /&gt;// will let you call the servlet in the snippet "An XML-RPC&lt;br /&gt;// Servlet". Of course, since XML-RPC is pretty ubiquitous&lt;br /&gt;// you can also use this code to call servers in dozens of&lt;br /&gt;// other languages as well.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.util.Vector;&lt;br /&gt;&lt;br /&gt;import org.apache.commons.logging.Log;&lt;br /&gt;import org.apache.commons.logging.LogFactory;&lt;br /&gt;import org.apache.xmlrpc.XmlRpcClient;&lt;br /&gt;&lt;br /&gt;public class XMLRPCTestClient {&lt;br /&gt;    private static final String serverAddress = &lt;br /&gt;	"http://localhost:8080/lol/remoteapi";&lt;br /&gt;    private static Log log = LogFactory.getLog(XMLRPCTestClient.class);&lt;br /&gt;    &lt;br /&gt;    /** Creates a new instance of XMLRPCTestClient */&lt;br /&gt;    public XMLRPCTestClient(String address) {&lt;br /&gt;        try {&lt;br /&gt;            XmlRpcClient xmlrpc = new XmlRpcClient(address);&lt;br /&gt;&lt;br /&gt;            Vector params = new Vector();&lt;br /&gt;            params.addElement("Hello World! Hello!");&lt;br /&gt;&lt;br /&gt;            try {&lt;br /&gt;                // this method returns a string&lt;br /&gt;                String result = (String) xmlrpc.execute("echo.echo", params);&lt;br /&gt;                System.out.println(result);&lt;br /&gt;            } catch (Exception e) {&lt;br /&gt;                log.error("The remote procedure call failed.", e);&lt;br /&gt;            }&lt;br /&gt;        } catch (java.net.MalformedURLException mue) {&lt;br /&gt;            log.error(&lt;br /&gt;                "The address given for the XML-RPC interface is bad: " + address);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param args the command line arguments&lt;br /&gt;     */&lt;br /&gt;    public static void main(String[] args) {&lt;br /&gt;        XMLRPCTestClient xmlRPCTestClient = new XMLRPCTestClient(&lt;br /&gt;            serverAddress);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 06 Mar 2006 01:50:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1636</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>An XML-RPC Servlet</title>
      <link>http://snippets.dzone.com/posts/show/1635</link>
      <description>// This depends upon the Apache XML-RPC library.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.OutputStream;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletConfig;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServlet;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import org.apache.commons.logging.Log;&lt;br /&gt;import org.apache.commons.logging.LogFactory;&lt;br /&gt;import org.apache.xmlrpc.XmlRpcServer;&lt;br /&gt;&lt;br /&gt;public class XmlRpcServlet extends HttpServlet {&lt;br /&gt;    public class EchoHandler {&lt;br /&gt;        public String echo(String input) {&lt;br /&gt;            return input;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;	&lt;br /&gt;    private XmlRpcServer server = new XmlRpcServer();&lt;br /&gt;&lt;br /&gt;    private static Log log = LogFactory.getLog(XmlRpcServlet.class);&lt;br /&gt;&lt;br /&gt;	@Override&lt;br /&gt;	public void init(ServletConfig config) throws ServletException {&lt;br /&gt;        server.addHandler("echo", new EchoHandler());&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	@Override&lt;br /&gt;	protected void doGet(HttpServletRequest request, &lt;br /&gt;			HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;        doPost(request, response);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	@Override&lt;br /&gt;	protected void doPost(HttpServletRequest request, &lt;br /&gt;			HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;        byte[] result = server.execute(request.getInputStream());&lt;br /&gt;        &lt;br /&gt;        response.setContentType("text/xml");&lt;br /&gt;        response.setContentLength(result.length);&lt;br /&gt;        &lt;br /&gt;        OutputStream output = response.getOutputStream();&lt;br /&gt;        output.write(result);&lt;br /&gt;        output.flush();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// The following needs to be added to your web.xml file to&lt;br /&gt;// expose the servlet so it may be called remotely.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  &lt;servlet&gt;&lt;br /&gt;    &lt;servlet-name&gt;XmlRpcServlet&lt;/servlet-name&gt;&lt;br /&gt;    &lt;servlet-class&gt;XmlRpcServlet&lt;/servlet-class&gt;&lt;br /&gt;  &lt;/servlet&gt;&lt;br /&gt;&lt;br /&gt;  &lt;servlet-mapping&gt;&lt;br /&gt;    &lt;servlet-name&gt;XmlRpcServlet&lt;/servlet-name&gt;&lt;br /&gt;    &lt;url-pattern&gt;/remoteapi&lt;/url-pattern&gt;        &lt;br /&gt;  &lt;/servlet-mapping&gt;    &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 06 Mar 2006 01:47:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1635</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>Ant 101</title>
      <link>http://snippets.dzone.com/posts/show/1600</link>
      <description>// Ah Ant. How I love thee, how much better a build tool&lt;br /&gt;// than my ex build tool Make.&lt;br /&gt;//&lt;br /&gt;// This is my current starting build.xml Ant script for any&lt;br /&gt;// Java project. It specifies my usual directories, it will&lt;br /&gt;// build source code, make a Jar, build a War, clean up build&lt;br /&gt;// products, create Javadoc, and even run unit tests.&lt;br /&gt;//&lt;br /&gt;// Notice that I use the JReleaseInfo library to automatically&lt;br /&gt;// generate a Java class that contains version numbers, build&lt;br /&gt;// numbers, etc. Be sure to put the Jar from that library in&lt;br /&gt;// the library directory to get that feature:&lt;br /&gt;// http://jreleaseinfo.sourceforge.net/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0"?&gt;&lt;br /&gt;&lt;project name="New Project" basedir="." default="all"&gt;&lt;br /&gt;    &lt;target name="init"&gt;&lt;br /&gt;        &lt;property name="appName" value="newProject"/&gt;&lt;br /&gt;    &lt;br /&gt;        &lt;property name="buildDir" value="${basedir}/build"/&gt;&lt;br /&gt;        &lt;property name="libDir" value="${basedir}/lib"/&gt;&lt;br /&gt;        &lt;property name="javaSourceDir" value="${basedir}/src"/&gt;&lt;br /&gt;        &lt;property name="testDir" value="${basedir}/test"/&gt;&lt;br /&gt;        &lt;property name="webSourceDir" value="${basedir}/web"/&gt;&lt;br /&gt;        &lt;property name="etcDir" value="${basedir}/etc"/&gt;&lt;br /&gt;        &lt;property name="tempDir" value="${basedir}/tmp"/&gt;&lt;br /&gt;        &lt;property name="classDir" value="${buildDir}/classes"/&gt;&lt;br /&gt;&lt;br /&gt;        &lt;!-- This may get overridden when we are called from Anthill. --&gt;&lt;br /&gt;        &lt;property name="version" value="Local Build"/&gt;&lt;br /&gt;        &lt;property name="deployDir" value="${buildDir}"/&gt;&lt;br /&gt;&lt;br /&gt;        &lt;property name="distDir" value="${deployDir}/dist"/&gt;&lt;br /&gt;        &lt;property name="junitDir" value="${deployDir}/JUnit"/&gt;&lt;br /&gt;        &lt;property name="javadocDir" value="${deployDir}/apidoc"/&gt;&lt;br /&gt;&lt;br /&gt;        &lt;!-- Create the directories where we put all the build products. --&gt;    &lt;br /&gt;        &lt;mkdir dir="${buildDir}"/&gt;&lt;br /&gt;        &lt;mkdir dir="${classDir}"/&gt;&lt;br /&gt;        &lt;mkdir dir="${distDir}"/&gt;&lt;br /&gt;        &lt;mkdir dir="${junitDir}"/&gt;&lt;br /&gt;        &lt;mkdir dir="${javadocDir}"/&gt;&lt;br /&gt;&lt;br /&gt;        &lt;path id="compileClasspath"&gt;&lt;br /&gt;            &lt;fileset dir="${libDir}"/&gt;&lt;br /&gt;        &lt;/path&gt;&lt;br /&gt;&lt;br /&gt;        &lt;!-- Make available build info in such a way that we can display it to the user. --&gt;&lt;br /&gt;        &lt;taskdef name="jreleaseinfo" classname="ch.oscg.jreleaseinfo.anttask.JReleaseInfoAntTask"&gt;&lt;br /&gt;            &lt;classpath refid="compileClasspath"/&gt;&lt;br /&gt;        &lt;/taskdef&gt;&lt;br /&gt;&lt;br /&gt;        &lt;jreleaseinfo className="MyReleaseInfo" packageName="com.johnmunsch.${appName}"&lt;br /&gt;                targetDir="${javaSourceDir}" project="${appName}" version="${version}" &lt;br /&gt;                buildNumFile="buildnum.properties" buildNumProperty="buildnum"&gt;&lt;br /&gt;            &lt;parameter name="VersionNum" type="int" value="1" /&gt;&lt;br /&gt;            &lt;parameter name="RevisionNum" type="Integer" value="0" /&gt;&lt;br /&gt;        &lt;/jreleaseinfo&gt;&lt;br /&gt;    &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;    &lt;target name="compile" depends="init"&gt;&lt;br /&gt;        &lt;javac srcdir="${javaSourceDir}" destdir="${classDir}" debug="true" deprecation="true"&gt;&lt;br /&gt;            &lt;classpath refid="compileClasspath"/&gt;&lt;br /&gt;        &lt;/javac&gt;&lt;br /&gt;&lt;br /&gt;        &lt;!-- Copy files needed to run the software to destinations in the &lt;br /&gt;         build directory. I do this because I usually pull all binary files like&lt;br /&gt;         this from inside the Jar files that make up my application rather than&lt;br /&gt;         having them loose. So they need to be copied to the class dir so they&lt;br /&gt;         get included in the Jar file for the application. --&gt;&lt;br /&gt;        &lt;copy todir="${classDir}" &gt;&lt;br /&gt;            &lt;fileset dir="${javaSourceDir}"&gt;&lt;br /&gt;                &lt;include name="**/*.gif"/&gt;&lt;br /&gt;                &lt;include name="**/*.jpg"/&gt;&lt;br /&gt;                &lt;include name="**/*.png"/&gt;&lt;br /&gt;                &lt;include name="**/*.wav"/&gt;&lt;br /&gt;                &lt;include name="**/*.dtd"/&gt;&lt;br /&gt;                &lt;include name="**/*.properties"/&gt;&lt;br /&gt;            &lt;/fileset&gt;&lt;br /&gt;        &lt;/copy&gt;&lt;br /&gt;    &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;    &lt;target name="jar" depends="init,compile"&gt;&lt;br /&gt;        &lt;jar jarfile="${distDir}/${appName}.jar" compress="true" &lt;br /&gt;            basedir="${classDir}"/&gt;&lt;br /&gt;    &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;    &lt;target name="war" depends="jar"&gt;&lt;br /&gt;        &lt;war destfile="${distDir}/${appName}.war" webxml="${etcDir}/WEB-INF/web.xml"&gt;&lt;br /&gt;            &lt;fileset dir="web"/&gt;&lt;br /&gt;            &lt;lib dir="${libDir}"&gt;&lt;br /&gt;                &lt;include name="jstl.jar"/&gt;&lt;br /&gt;                &lt;include name="standard.jar"/&gt;&lt;br /&gt;            &lt;/lib&gt;&lt;br /&gt;            &lt;lib dir="${libDir}"&gt;&lt;br /&gt;                &lt;include name="*.jar"/&gt;&lt;br /&gt;            &lt;/lib&gt;&lt;br /&gt;            &lt;lib dir="${distDir}"&gt;&lt;br /&gt;                &lt;include name="${appName}.jar"/&gt;&lt;br /&gt;            &lt;/lib&gt;&lt;br /&gt;        &lt;/war&gt;&lt;br /&gt;    &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;    &lt;target name="all" depends="war" description="Build everything."&gt;&lt;br /&gt;        &lt;echo message="Application built."/&gt;&lt;br /&gt;    &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;    &lt;target name="javadoc" depends="init" description="Javadoc for the code."&gt;&lt;br /&gt;        &lt;javadoc packagenames="*" sourcepath="${javaSourceDir}" &lt;br /&gt;	        destdir="${javadocDir}"/&gt;&lt;br /&gt;    &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;    &lt;target name="clean" depends="init" description="Clean all build products."&gt;&lt;br /&gt;        &lt;delete dir="${tempDir}"/&gt;&lt;br /&gt;        &lt;delete dir="${javadocDir}"/&gt;&lt;br /&gt;        &lt;delete dir="${junitDir}"/&gt;&lt;br /&gt;        &lt;delete dir="${distDir}"/&gt;&lt;br /&gt;        &lt;delete dir="${classDir}"/&gt;&lt;br /&gt;        &lt;delete dir="${buildDir}"/&gt;&lt;br /&gt;    &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;    &lt;target name="junit" depends="jar" description="Performs unit tests."&gt;&lt;br /&gt;        &lt;javac srcdir="test" destdir="${classDir}" debug="true" deprecation="true"&gt;&lt;br /&gt;            &lt;classpath refid="compileClasspath"/&gt;&lt;br /&gt;        &lt;/javac&gt;&lt;br /&gt;&lt;br /&gt;        &lt;mkdir dir="${tempDir}"/&gt;&lt;br /&gt;&lt;br /&gt;        &lt;junit failureproperty="junit.failed"&gt;&lt;br /&gt;            &lt;formatter type="xml"/&gt;&lt;br /&gt;            &lt;batchtest todir="${tempDir}"&gt;&lt;br /&gt;                &lt;fileset dir="${classDir}"&gt;&lt;br /&gt;                   &lt;include name="**/*Test.class"/&gt;&lt;br /&gt;                   &lt;include name="**/Test*.class"/&gt;&lt;br /&gt;                &lt;/fileset&gt;&lt;br /&gt;            &lt;/batchtest&gt;&lt;br /&gt;            &lt;classpath&gt;&lt;br /&gt;                &lt;path refid="compileClasspath"/&gt;&lt;br /&gt;                &lt;pathelement location="${classDir}"/&gt;&lt;br /&gt;            &lt;/classpath&gt;&lt;br /&gt;        &lt;/junit&gt;&lt;br /&gt;&lt;br /&gt;        &lt;mkdir dir="${buildDir}/JUnit"/&gt;&lt;br /&gt;&lt;br /&gt;        &lt;junitreport tofile="TESTS-TestSuites.xml" todir="${tempDir}"&gt;&lt;br /&gt;            &lt;fileset dir="${tempDir}"&gt;&lt;br /&gt;                &lt;include name="TEST-*.xml"/&gt;&lt;br /&gt;            &lt;/fileset&gt;&lt;br /&gt;            &lt;report todir="${junitDir}"/&gt;&lt;br /&gt;        &lt;/junitreport&gt;&lt;br /&gt;  	&lt;br /&gt;        &lt;fail message="JUnit test failure." if="junit.failed"/&gt;  	&lt;br /&gt;    &lt;/target&gt;&lt;br /&gt;&lt;/project&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Feb 2006 20:31:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1600</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>Getting A Data Source From Tomcat</title>
      <link>http://snippets.dzone.com/posts/show/1595</link>
      <description>// You can setup a data source in tomcat using a context file&lt;br /&gt;// or you can set one up using the Administration web pages&lt;br /&gt;// as well. Either way you do it, here is the simple code to&lt;br /&gt;// get the data source from Tomcat so you can start pulling&lt;br /&gt;// out database connections.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Obtain our environment naming context&lt;br /&gt;Context initCtx = new InitialContext();&lt;br /&gt;Context envCtx = (Context) initCtx.lookup("java:comp/env");&lt;br /&gt;&lt;br /&gt;// Look up our data source by the name we gave it when we created it. &lt;br /&gt;// In this case that's "jdbc/EmployeeDB".&lt;br /&gt;DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Feb 2006 04:13:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1595</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>Data Source 101</title>
      <link>http://snippets.dzone.com/posts/show/1594</link>
      <description>// Way too many people hard code their JDBC connections to &lt;br /&gt;// the database server(s). Use data sources to connect to&lt;br /&gt;// your databases. They can be setup easily in any major&lt;br /&gt;// Java application server as well as JSP/servlet engines&lt;br /&gt;// like Tomcat and Spring.&lt;br /&gt;//&lt;br /&gt;// If you aren't running inside a container that provides&lt;br /&gt;// you with easy access to a data source though, you can&lt;br /&gt;// include the Jakarta Commons DBCP library and create a&lt;br /&gt;// data source as shown in the code below. The result is&lt;br /&gt;// a data source complete with pooling, caching, etc. that&lt;br /&gt;// you can pass to other code that just expects to receive&lt;br /&gt;// a data source.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;BasicDataSource dataSource = new BasicDataSource();&lt;br /&gt;&lt;br /&gt;dataSource.setDriverClassName(System.getProperty("driverClassName"));&lt;br /&gt;dataSource.setUsername(System.getProperty("username"));&lt;br /&gt;dataSource.setPassword(System.getProperty("password"));&lt;br /&gt;dataSource.setUrl(System.getProperty("url"));&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Feb 2006 04:10:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1594</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>Velocity 101 Unit Tests</title>
      <link>http://snippets.dzone.com/posts/show/1592</link>
      <description>// Unit tests for the Velocity 101 code. Note that the second test is&lt;br /&gt;// actually a test to confirm that the function returns a failure&lt;br /&gt;// correctly when the Velocity template file is not found.&lt;br /&gt;//&lt;br /&gt;// Be sure to get the test.vm file that goes with these unit tests.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package com.johnmunsch.util;&lt;br /&gt;&lt;br /&gt;import org.apache.velocity.VelocityContext;&lt;br /&gt;&lt;br /&gt;import junit.framework.TestCase;&lt;br /&gt;&lt;br /&gt;public class BoilerplateTest extends TestCase {&lt;br /&gt;    private VelocityContext context = null;&lt;br /&gt;    private String testTemplate = null;&lt;br /&gt;&lt;br /&gt;    protected void setUp() throws Exception {&lt;br /&gt;        context = new VelocityContext();&lt;br /&gt;        testTemplate = System.getProperty("testTemplate");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * A very basic test to see if we can get a word inserted into a template&lt;br /&gt;     * with the Boilerplate class.&lt;br /&gt;     * @throws Exception &lt;br /&gt;     */&lt;br /&gt;    public void testApply() throws Exception {&lt;br /&gt;        context.put("foo", "Velocity");&lt;br /&gt;        &lt;br /&gt;        String output = Boilerplate.apply(context, testTemplate);&lt;br /&gt;        &lt;br /&gt;        assertEquals(output, "&lt;html&gt;&lt;body&gt;Hello Velocity World!&lt;/body&gt;&lt;html&gt;");&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Same sort of test as above except this time we specify a bogus name for&lt;br /&gt;     * the template file and we expect to get an exception. The failure occurs&lt;br /&gt;     * only if we don't have an exception thrown.&lt;br /&gt;     */&lt;br /&gt;    public void testApply2() {&lt;br /&gt;        context.put("foo", "Velocity");&lt;br /&gt;&lt;br /&gt;        try {&lt;br /&gt;            Boilerplate.apply(context, "heyheypaula.vm");&lt;br /&gt;            &lt;br /&gt;            fail();&lt;br /&gt;        } catch (Exception e) {&lt;br /&gt;            // We expect an exception. Failure is when we don't see one.&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// This is a required file for the unit tests in "Velocity 101 Unit Tests". &lt;br /&gt;// Put it into a file and make sure that the filename is passed into the unit&lt;br /&gt;// test above as a System property.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;html&gt;&lt;body&gt;Hello $foo World!&lt;/body&gt;&lt;html&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Feb 2006 04:00:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1592</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
  </channel>
</rss>
