<?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 &#187; Example Code</title>
	<atom:link href="http://byprogrammerforprogrammer.com/category/example-code/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>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>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>
	</channel>
</rss>
