<?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>By Programmer For Programmer</title>
	<atom:link href="http://byprogrammerforprogrammer.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://byprogrammerforprogrammer.com</link>
	<description>Here I lay down the useful tips, tricks and utilities for programmers like myself.</description>
	<lastBuildDate>Fri, 02 Apr 2010 16:58:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JProfiler Helping Out With HTML4Java</title>
		<link>http://byprogrammerforprogrammer.com/2010/04/jprofiler-helping-out-with-html4java/</link>
		<comments>http://byprogrammerforprogrammer.com/2010/04/jprofiler-helping-out-with-html4java/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 16:57:00 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=86</guid>
		<description><![CDATA[Just wanted to give props real quick to the awesome profiler JProfiler (http://www.ej-technologies.com/products/jprofiler/overview.html) for providing me with an open-source license to profile my open-source project HTML4Java (https://sourceforge.net/projects/html4java/).  The open-source community is so friendly  .
]]></description>
			<content:encoded><![CDATA[<p>Just wanted to give props real quick to the awesome profiler JProfiler (<a href="http://www.ej-technologies.com/products/jprofiler/overview.html">http://www.ej-technologies.com/products/jprofiler/overview.html</a>) for providing me with an open-source license to profile my open-source project HTML4Java (<a href="https://sourceforge.net/projects/html4java/">https://sourceforge.net/projects/html4java/</a>).  The open-source community is so friendly <img src='http://byprogrammerforprogrammer.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2010/04/jprofiler-helping-out-with-html4java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Nested Loops: break or continue</title>
		<link>http://byprogrammerforprogrammer.com/2010/03/java-nested-loops-break-or-continue/</link>
		<comments>http://byprogrammerforprogrammer.com/2010/03/java-nested-loops-break-or-continue/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 23:58:37 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=82</guid>
		<description><![CDATA[Ever wanted to continue from a doubly-nested for loop?  How about break from a switch statement more than one level deep?
Simple!  First you have to label your loop, and then put that label name after the break or continue keyword.
&#8220;continue&#8221; example:

fooloop: for(foo: foos) {
    ...
    barloop: for(bar [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to <code>continue</code> from a doubly-nested <code>for</code> loop?  How about <code>break</code> from a <code>switch</code> statement more than one level deep?</p>
<p>Simple!  First you have to label your loop, and then put that label name after the <code>break</code> or <code>continue</code> keyword.</p>
<p>&#8220;continue&#8221; example:</p>
<pre>
fooloop: for(foo: foos) {
    ...
    barloop: for(bar : bars) {
        ...
        for (blotto : blottos) {
            ...
            if (next_innermost_loop)
                continue; //normal
            if (next_middle_loop)
                continue barloop; //goes to the next iteration of "barloop"
            if (next_outer_loop)
                continue fooloop; //goes to the next iteration of the outermost loop, "fooloop"
        }
    }
}
</pre>
<p>&#8220;break&#8221; example:</p>
<pre>
fooswitch: switch(foo) {
    case 1:
        ...
        break;
    case 2:
        ...
       switch(bar) {
            ...
            if (break_this_switch)
                break; //normal
            if (break_outer_switch)
                break fooswitch; // breaks out of the out switch, "fooswitch"
        }
}
</pre>
<p>While most might consider this bad practice, when writing some scratch code to do some one-off task that only you will ever use, sometimes it&#8217;s nice to use a shortcut <img src='http://byprogrammerforprogrammer.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2010/03/java-nested-loops-break-or-continue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Generics in Static Methods</title>
		<link>http://byprogrammerforprogrammer.com/2010/01/java-generics-in-static-methods/</link>
		<comments>http://byprogrammerforprogrammer.com/2010/01/java-generics-in-static-methods/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 00:24:44 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Example Code]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=76</guid>
		<description><![CDATA[Ever needed to use generics in a static method but didn't know you could?  Here's how...]]></description>
			<content:encoded><![CDATA[<p>Every time I feel like I know all the Java syntax there is, I learn something new <img src='http://byprogrammerforprogrammer.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Let&#8217;s say you have a class with a static constructor that returns a map, and that map is typed and does fancy things, but you still want to use generics from wherever you are using the Map.</p>
<pre>
public class MyClass {

    public static final &lt;K,V&gt; Map&lt;K,V&gt; createMyMap() {
        ...
        return map;
    }
}
</pre>
<p>If you are like me, you hate hate hate casting to generics, but you are forced to do that or have a big ugly <code>@SuppressWarnings("unchecked")</code> on the method that you access this method.</p>
<pre>
@SuppressWarnings("unchecked")
function Map&lt;String, Object&gt; getMyMap() {
    return MyClass.createMyMap();
}
</pre>
<p><strong>Well not anymore!</strong>.  Today I just learned (by looking through some Java source code) that you can assign generics on your static calls by using the following syntax.</p>
<pre>
function Map&lt;String, Object&gt; getMyMap() {
    return MyClass.&lt;String, Object&gt;createMyMap();
}
</pre>
<p>Just put the generics right after the period and before the method name.  Easy.</p>
<p>I learned this while trying to figure out why <code>java.util.Collections</code> has static fields <code>EMPTY_LIST</code>, <code>EMPTY_SET</code>, and <code>EMPTY_MAP</code>, and also static methods <code>emptyList()</code>, <code>emptySet</code>, and <code>emptyMap</code>.  It says right in the code, &#8220;Unlike this method, the field does not provide type safety&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2010/01/java-generics-in-static-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse/Tomcat publishing lockup: &#8220;Could not delete file&#8230; may be locked by another process&#8221;</title>
		<link>http://byprogrammerforprogrammer.com/2009/12/eclipsetomcat-publishing-lockup-could-not-delete-file-may-be-locked-by-another-process/</link>
		<comments>http://byprogrammerforprogrammer.com/2009/12/eclipsetomcat-publishing-lockup-could-not-delete-file-may-be-locked-by-another-process/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 21:47:28 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=74</guid>
		<description><![CDATA[I was getting the annoying error in Eclipse with Tomcat where it didn&#8217;t think it could publish an application since some files were locked, when they were locked by the javaw.exe process that eclipse.exe started.  The fix for me was to not have &#8220;Use Tomcat installation&#8221; selected in my server configuration.  When I [...]]]></description>
			<content:encoded><![CDATA[<p>I was getting the annoying error in Eclipse with Tomcat where it didn&#8217;t think it could publish an application since some files were locked, when they were locked by the javaw.exe process that eclipse.exe started.  The fix for me was to not have &#8220;Use Tomcat installation&#8221; selected in my server configuration.  When I switched back to the default of &#8220;Use workspace metadata&#8221;, the error stopped happening.</p>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2009/12/eclipsetomcat-publishing-lockup-could-not-delete-file-may-be-locked-by-another-process/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Web Service Annotations in Tomcat</title>
		<link>http://byprogrammerforprogrammer.com/2009/11/java-web-service-annotations-in-tomcat/</link>
		<comments>http://byprogrammerforprogrammer.com/2009/11/java-web-service-annotations-in-tomcat/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 19:56:06 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Example Code]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=43</guid>
		<description><![CDATA[Easy-to-follow instructions on how to go from java files with web service annotations (@WebService, @WebMethod, @WebParam, etc) to automatic generation of a WSDL in Tomcat.]]></description>
			<content:encoded><![CDATA[<p>So recently I had to build a web service, and had a really hard time finding documentation or instructions on how to make web services work in Tomcat using the web service annotations from <a href="http://jcp.org/en/jsr/summary?id=181" target="_blank">JSR 181</a> (@WebService, @WebMethod, @WebParam, etc).  So I created this post as a way to help someone else in the same situation.  I&#8217;m sure there are many ways to do this, but this is one way.</p>
<p>My goal was to create java files annotated with web service annotations and deploy the application to Tomcat, and just have everything magically work.  I didn&#8217;t want to use the wsgen tool.  I wanted it to work like it does in JBoss, Weblogic, and Glassfish, where you just deploy the application and the web service endpoints are created automatically.</p>
<h2>Environment</h2>
<p>Here is the environment that I was working with:</p>
<ul>
<li>Java 1.6.0_16</li>
<li>Tomcat 6.0.18</li>
<li>Eclipse 3.4</li>
<li>Building a WAR and dropping into webapps folder in Tomcat</li>
</ul>
<h2>Example Class</h2>
<p>MyWebService.java:</p>
<pre>
package com.example.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * An example web service implementation
 * @author Sean Adkinson
 */
@WebService(targetNamespace = "http://com.example.webservice", name = "mywebservice")
public class MyWebService
{

    /**
     * Says hello to the given name
     * @param name A name
     * @return Hello, name
     */
    @WebMethod(operationName = "hello")
    @WebResult(name = "return")
    public String hello(@WebParam(name = "name") String name)
    {
        return "Hello, " + name;
    }

}
</pre>
<h2>Instructions</h2>
<p>Follow these instructions to create a web service from our example class above:</p>
<ol>
<li>Download Metro 1.5 binary (or latest point release) at https://metro.dev.java.net/1.5/</li>
<li>Run <code>java -jar metro-1_5.jar</code> to unpack the contents of the jar</li>
<li>Take all the lib/webservices-*.jar files from the expanded Metro installation, and copy them into your web application&#8217;s WEB-INF/lib folder.  Note that I chose to put these in my application&#8217;s library, but you can also put them in the server&#8217;s library at tomcat.home/lib.</li>
<li>Create a <strong>sun-jaxws.xml</strong> file right next to web.xml under WEB-INF.  This is where you specify where to find the classes that have your web service annotations.  Here is a sample file:<br />
<br />
sun-jaxws.xml:</p>
<pre>
&lt;endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'&gt;
    &lt;endpoint
        name='mywebservice'
        implementation='com.example.webservice.MyWebService'
        url-pattern='/mywebservice' /&gt;
&lt;/endpoints&gt;
</pre>
<p>Here, <strong>name</strong> can be anything, <strong>implementation</strong> is the classpath reference to the class with the web service annotations that should have a web service created from it, and <strong>url-pattern</strong> is the path after your web application&#8217;s URL that will hit this web service (so if your application is at http://localhost:8080/MyApplication, this web service will be available at http://localhost:8080/MyApplication/mywebservice).
</li>
<li>Edit your <strong>web.xml</strong> to map the URL pattern above to class <strong>com.sun.xml.ws.transport.http.servlet.WSServlet</strong>, and add a listener for class <strong>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</strong>.  Here is an example:
<pre>
&lt;listener&gt;
    &lt;listener-class&gt;com.sun.xml.ws.transport.http.servlet.WSServletContextListener&lt;/listener-class&gt;
&lt;/listener&gt;
&lt;servlet&gt;
    &lt;servlet-name&gt;myws&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.sun.xml.ws.transport.http.servlet.WSServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;myws&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/mywebservice&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</pre>
</li>
<li>Now deploy your webapp, start Tomcat, and go to http://localhost:8080/MyApplication/mywebservice, and you should see details about the endpoint that was created, with a link to the generated WSDL file.</li>
<li>From here you should be able point whatever client code generating utilities you have at the WSDL and talk with the web service from a client.  I use built-in Eclipse Axis2 code generation for this, but check out wsimport in the Metro /bin folder for another option.</li>
</ol>
<p>That&#8217;s it!  Hope this helps someone who had as hard a time as I did with getting this working in Tomcat.</p>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2009/11/java-web-service-annotations-in-tomcat/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Hibernate: Get Table Name from Mapped Class</title>
		<link>http://byprogrammerforprogrammer.com/2009/10/hibernate-get-table-name-from-mapped-class/</link>
		<comments>http://byprogrammerforprogrammer.com/2009/10/hibernate-get-table-name-from-mapped-class/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 20:44:05 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=29</guid>
		<description><![CDATA[Given a class mapped in Hibernate, use this code to get the table name.]]></description>
			<content:encoded><![CDATA[<p>Given a class that is mapped in Hibernate, you can get the table name that it is mapped to with the following piece of code.</p>
<pre>
import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.Joinable;

public String getTableName(SessionFactory sessionFactory, Class&lt;?&gt; mappedClass)
{
    ClassMetadata cmd = sessionFactory.getClassMetadata(mappedClass);

    //check that the class is mapped to something with a table name
    if (cmd == null || !Joinable.class.isInstance(cmd))
        return null;

    return Joinable.class.cast(cmd).getTableName();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2009/10/hibernate-get-table-name-from-mapped-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SupoSE Bash Update Script (cron)</title>
		<link>http://byprogrammerforprogrammer.com/2009/10/supose-bash-update-script-cron/</link>
		<comments>http://byprogrammerforprogrammer.com/2009/10/supose-bash-update-script-cron/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 03:54:52 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[supose]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=24</guid>
		<description><![CDATA[Script that can be used to update a Subversion index created with SupoSE, designed to be run via cron.]]></description>
			<content:encoded><![CDATA[<p>Hey everyone,</p>
<p>I created a bash script to update an index created by SupoSE, keeping track of what revision was last successfully updated.  I liked this better than their built-in scheduler because it has to continuously be running, and if for some reason our servers had to be restarted, it wouldn&#8217;t automatically start up again.  Thus, I made a script to set up in cron to run hourly.</p>
<p>To use this script:</p>
<ul>
<li>Create an index normally with SupoSE</li>
<li>Create a file to hold the last successfully indexed revision, and put this number in the file as the only text</li>
<li>Set the variables in the script below for your environment</li>
<li>Use bash 3.0 or higher, since I&#8217;m using regular expressions.  If this isn&#8217;t an option, you may just need to rewrite the parsing of &#8220;svn info&#8221; to get the HEAD revision.</li>
</ul>
<p>Enjoy!</p>
<pre>
#!/bin/sh

# Variables
SUPOSE_EXE="supose"
SUPOSE_LOC="/usr/local/supose/bin"
SVN_INDEX="/var/lib/supose/index/myindex"
SVN_URL="http://subversionurl/repo"
SVN_USER="username"
SVN_PASS="password"
LASTREV_FILE="/var/lib/supose/index/last.rev"

# Get the last successfully indexed revision
LASTREV=""
read -r LASTREV &lt; "$LASTREV_FILE"
if [ "$LASTREV" = "" ]; then
        echo "Please specify the last successfully indexed revision in $LASTREV_FILE"
        exit 1;
fi

# Get the "to" revision
TOREV=""
SVNINFO_CMD="svn info $SVN_URL"
SVNINFO=`$SVNINFO_CMD`
if [[ "$SVNINFO" =~ 'Revision: ([0-9]+)' ]]; then
        TOREV=${BASH_REMATCH[1]}
fi
if [ "$TOREV" = "" ]; then
        echo "**** ERROR ****"
        echo "Could not read the HEAD revision number from command: $SVNINFO_CMD"
        echo "Has subversion been updated with a different format?"
        echo "Output from command:"
        echo $SVNINFO
        exit 1;
fi

# Print some output
echo "Updating subversion: $SVN_URL"
echo "Last successful revision: $LASTREV"
echo "HEAD revision: $TOREV"
echo "Index location: $SVN_INDEX"
echo "..."

if [ "$LASTREV" -ge "$TOREV" ]; then
        echo "No revisions to update... skipping process"
        exit 0;
fi

# Add one to to get the FROM revision
let FROMREV=$LASTREV+1
if [ "$FROMREV" = "" ]; then
        echo "Error adding one to last successfully indexed revision: $LASTREV"
        exit 1;
fi

# Run the SupoSE command
SUPOSE_CMD="./$SUPOSE_EXE scan \
                --url $SVN_URL \
                --username $SVN_USER \
                --password $SVN_PASS \
                --fromrev $FROMREV \
                --torev $TOREV \
                --index $SVN_INDEX"
pushd $SUPOSE_LOC
echo "Running $SUPOSE_CMD"
$SUPOSE_CMD
popd

# Was there an error running SupoSE?
SUPOSE_STATUS="$?"
if [ "$SUPOSE_STATUS" -gt "0" ]; then
        echo "**** Error ****"
        echo "$SUPOSE_EXE returned status code: $SUPOSE_STATUS (not successful)"
        echo "File $LASTREV_FILE will not be updated with latest revision"
        echo "Command that was run:"
        echo $SUPOSE_CMD
        exit $SUPOSE_STATUS;
fi

# If no error, update last.rev file
echo "**** Operation completed successfully ****"
echo "Updating $LASTREV_FILE with revision: $TOREV"
echo "$TOREV" > "$LASTREV_FILE"
exit 0
</pre>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2009/10/supose-bash-update-script-cron/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
