<?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>Tech Juice</title>
	<atom:link href="http://www.tech-juice.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tech-juice.org</link>
	<description>Programming, Networking, Reversing and More</description>
	<lastBuildDate>Sun, 01 Jul 2012 21:38:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Using @DataSourceDefinition in Java EE 6 With Postgresql</title>
		<link>http://www.tech-juice.org/2012/05/30/datasourcedefinition-postgresql-example-in-java-ee/</link>
		<comments>http://www.tech-juice.org/2012/05/30/datasourcedefinition-postgresql-example-in-java-ee/#comments</comments>
		<pubDate>Wed, 30 May 2012 20:20:40 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[datasourcedefinition]]></category>
		<category><![CDATA[entitymanager]]></category>
		<category><![CDATA[jpa]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=2135</guid>
		<description><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#1_Install_Postgresql_Driver_in_Application_Server">2 1. Install Postgresql Driver in Application Server</a></li><li><a href="#2_Define_Datasource_With_DataSourceDefinition_Annotation">3 2. Define Datasource With @DataSourceDefinition Annotation</a></li><li><a href="#3_Add_Datasource_to_persistencexml">4 3. Add Datasource to persistence.xml</a></li><li><a href="#Using_the_Datasource">5 Using the Datasource</a></li><li><a href="#Common_Glassfish_Problems">6 Common Glassfish Problems</a></li><li><a href="#Resources">7 Resources</a></li></ul></div>
<span id="Introduction">Introduction</span>
<p>Java EE applications use <code>DataSource</code> objects when they access relational databases through the JDBC API. Typically the <code>Datasource</code> that should be used is referenced by its Java Naming and Directory Interface (JNDI) name in the <code>persistence.xml</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;jta-data-source<span style="color: #000000; font-weight: bold;">&#62;</span></span></span>java:app/env/myDatasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;/jta-data-source<span style="color: #000000; font-weight: bold;">&#62;</span></span></span></pre></div></div>

<p>The <code>Datasource</code> details (host, username, password, database type) for this JNDI name need to be declared somewhere. Prior to Java EE 6, you could create a DataSource object using vendor-specific mechanisms. This could be a deployment descriptor for the application server in the WAR, e.g. <code>glassfish-web.xml</code> or XML configuration files in the application server directory.</p>
<p>In Java EE 6 the <a href="https://blogs.oracle.com/enterprisetechtips/entry/datasource_resource_definition_in_java">@DataSourceDefinition</a> annotation was introduced. With this annotation you can declare &#8216;application server agnostic&#8217; datasources directly in your code. While this is usually a bad idea for&#8230;</p>]]></description>
			<content:encoded><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#1_Install_Postgresql_Driver_in_Application_Server">2 1. Install Postgresql Driver in Application Server</a></li><li><a href="#2_Define_Datasource_With_DataSourceDefinition_Annotation">3 2. Define Datasource With @DataSourceDefinition Annotation</a></li><li><a href="#3_Add_Datasource_to_persistencexml">4 3. Add Datasource to persistence.xml</a></li><li><a href="#Using_the_Datasource">5 Using the Datasource</a></li><li><a href="#Common_Glassfish_Problems">6 Common Glassfish Problems</a></li><li><a href="#Resources">7 Resources</a></li></ul></div>
<h2><span id="Introduction">Introduction</span></h2>
<p>Java EE applications use <code>DataSource</code> objects when they access relational databases through the JDBC API. Typically the <code>Datasource</code> that should be used is referenced by its Java Naming and Directory Interface (JNDI) name in the <code>persistence.xml</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java:app/env/myDatasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The <code>Datasource</code> details (host, username, password, database type) for this JNDI name need to be declared somewhere. Prior to Java EE 6, you could create a DataSource object using vendor-specific mechanisms. This could be a deployment descriptor for the application server in the WAR, e.g. <code>glassfish-web.xml</code> or XML configuration files in the application server directory.</p>
<p>In Java EE 6 the <a href="https://blogs.oracle.com/enterprisetechtips/entry/datasource_resource_definition_in_java">@DataSourceDefinition</a> annotation was introduced. With this annotation you can declare &#8216;application server agnostic&#8217; datasources directly in your code. While this is usually a bad idea for production code, it can be very useful for testing, since you don&#8217;t have to change the configuration of the application server. You can just deploy the WAR in any application server and it will run.</p>
<p>In this example we run through the steps for setting up a <a href="http://www.tech-juice.org/2012/05/28/adding-jpa-to-a-maveneclipsejsf2-project/">JPA project</a> with a Postgresql database connection using the <code>@DataSourceDefinition</code> annotation.</p>
<h2><span id="1_Install_Postgresql_Driver_in_Application_Server">1. Install Postgresql Driver in Application Server</span></h2>
<p>First, make sure that the application server is set up to support Postgresql connections. For Glassfish 3 we need to install the Postgresql JDBC drivers.</p>
<ol>
<li>Download the latest JDBC drivers at the <a href="http://jdbc.postgresql.org/">Postgresql website</a>.</li>
<li>Download the driver file <code>postgresql-<version>.jdbc4.jar</code> and copy it to <code>&lt;glassfish_home>/glassfish/lib/</code>.</li>
</ol>
<p>For other application servers refer to the documentation.</p>
<h2><span id="2_Define_Datasource_With_DataSourceDefinition_Annotation">2. Define Datasource With @DataSourceDefinition Annotation</span></h2>
<p>Next we define the details of the datasource named <code>java:app/env/myDatasource</code> using the <code>@DataSourceDefinition</code> annotation in the Java code. We create a special class for this purpose, in <strong>DbConfiguration.java</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.sql.DataSourceDefinition</span><span style="color: #339933;">;</span>
&nbsp;
@DataSourceDefinition<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;java:app/env/myDatasource&quot;</span>,
		minPoolSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,
		initialPoolSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,
		className <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;org.postgresql.ds.PGSimpleDataSource&quot;</span>,
		serverName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;localhost&quot;</span>,
		user <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;admin&quot;</span>,
		password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;admin&quot;</span>,
		databaseName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test&quot;</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DbConfiguration <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When the application server deploys the application, all <code>@DataSourceDefinition</code> annotations are processed. The application server will look in all classes, so it doesn&#8217;t matter where you put the definition.</p>
<p><strong>Note: </strong> you can also define a datasource in the <code>web.xml</code> file that is included in the WAR file.:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java:app/env/myDatasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.postgresql.ds.PGSimpleDataSource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;server-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>localhost<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/server-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;port-number<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5432<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/port-number<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;database-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/database-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>admin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/user<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>admin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2><span id="3_Add_Datasource_to_persistencexml">3. Add Datasource to persistence.xml</span></h2>
<p>Our <strong>persistence.xml</strong> is quite simple, referencing the data source named <code>java:app/env/myDatasource</code> with the JTA transaction type.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/persistence&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence-unit</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myUnit&quot;</span> <span style="color: #000066;">transaction-type</span>=<span style="color: #ff0000;">&quot;JTA&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java:app/env/myDatasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence-unit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2><span id="Using_the_Datasource">Using the Datasource</span></h2>
<p>Now we can make a simple JSF 2.0 web application that uses the database. Let&#8217;s make a simple entity with a &#8216;name&#8217; field.</p>
<p><strong>NamedEntity.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.NamedQuery</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Table</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>
@NamedQuery<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;getAll&quot;</span>, query<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT c from NamedEntity c&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NamedEntity <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
        @Id
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And we add NamedEntity to our persistence unit:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence-unit</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myUnit&quot;</span> <span style="color: #000066;">transaction-type</span>=<span style="color: #ff0000;">&quot;JTA&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java:app/env/myDatasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NamedEntity<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence-unit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We also need a bean that retrieves the names, and makes them available in the JSF page. Since we have declared <code>transaction-type="JTA"</code> in <code>persistence.xml</code>, we can use the <code>@PersistenceContext</code> annotation. This tells the application server to inject a reference to the appropriate EntityManager instance at run-time. In this case it means we will get an EntityManger for the <code>myUnit</code> persistence unit.</p>
<p><strong>TestBean.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.sql.DataSourceDefinition</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.ApplicationScoped</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.inject.Named</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.PersistenceContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.TypedQuery</span><span style="color: #339933;">;</span>
&nbsp;
@Named<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span>
@ApplicationScoped
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestBean <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// EntityManager is injected here by application server</span>
        @PersistenceContext
        <span style="color: #000000; font-weight: bold;">private</span> EntityManager em<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getNames<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		TypedQuery<span style="color: #339933;">&lt;</span>NamedEntity<span style="color: #339933;">&gt;</span> query <span style="color: #339933;">=</span> em.<span style="color: #006633;">createNamedQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getAll&quot;</span>, NamedEntity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span>NamedEntity<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> query.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                StringBuilder result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The name are: [&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003399;">String</span> separator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>NamedEntity entity<span style="color: #339933;">:</span> results<span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                        result.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>separator<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>entity.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        separator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                result.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> result.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then in our JSF page we can reference the TestBean class through an EL expression:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{test.getNames()}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>This code should work without problems, printing all rows from the database, e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">The names are: [Jack, John, Dave]</pre></div></div>

<h2><span id="Common_Glassfish_Problems">Common Glassfish Problems</span></h2>
<p>Some errors you may run into when using Glassfish 3 are:</p>
<p><strong>WebappClassLoader unable to load resource</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">javax.servlet.ServletException: WEB9031: WebappClassLoader unable to load resource 
[org.postgresql.jdbc4.Jdbc4PreparedStatement], because it has not yet been started, or was already stopped</pre></div></div>

<p>This means the Postgresql JDBC driver cannot be found. Copy the JDBC JAR file to <code>&lt;glassfish_home>/glassfish/lib/</code> and restart the server.</p>
<p><strong>No PasswordCredential found</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">WARNING: RAR5038:Unexpected exception while creating resource for pool 
__SYSTEM/pools/__datasource_definition/testApp/java:app/env/myDatasource. 
Exception : javax.resource.spi.SecurityException: No PasswordCredential found
WARNING: RAR5117 : Failed to obtain/create connection from connection pool 
[ __SYSTEM/pools/__datasource_definition/testApp/java:app/env/myDatasource]. 
Reason : com.sun.appserv.connectors.internal.api.PoolingException: No PasswordCredential found
WARNING: RAR5114 : Error allocating connection : 
[Error in allocating a connection. Cause: No PasswordCredential found]</pre></div></div>

<p>This error indicates the password field for a datasource was left blank. Even though it&#8217;s perfectly valid to connect to a database without a password, Glassfish does not like this &#8212; it will completely ignore empty properties. Thankfully there is a special meta-string that can be used instead property values to represent a zero length string: <code>()</code>.</p>
<p><strong>ERROR: relation &#8216;namedentity&#8217; does not exist</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Internal Exception: org.postgresql.util.PSQLException: ERROR: relation &quot;namedentity&quot; does not exist
Error Code: 0
Call: INSERT INTO NAMEDENTITY (NAME) VALUES (?)
	bind =&gt; [1 parameter bound]
Query: InsertObjectQuery(my.package.namedentity@7f96dfa4)</pre></div></div>

<p>This means the table associated with an entity does not exist in the database yet. You can tell the JPA implementation to automatically create the tables, but since this is outside the spec there is no fixed syntax. For EclipseLink (the JPA implementation inside Glassfish 3) you have to add two properties to your <code>persistence.xml</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence-unit</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myUnit&quot;</span> <span style="color: #000066;">transaction-type</span>=<span style="color: #ff0000;">&quot;JTA&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- other stuf.. --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;eclipselink.ddl-generation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;create-tables&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;eclipselink.ddl-generation.output-mode&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;database&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence-unit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2><span id="Resources">Resources</span></h2>
<ul>
<li><a href="http://javahowto.blogspot.com/2010/04/datasourcedefinition-examples-in-javaee.html">http://javahowto.blogspot.com/2010/04/datasourcedefinition-examples-in-javaee.html</a></li>
<li><a href="https://blogs.oracle.com/enterprisetechtips/entry/datasource_resource_definition_in_java">https://blogs.oracle.com/enterprisetechtips/entry/datasource_resource_definition_in_java</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/05/30/datasourcedefinition-postgresql-example-in-java-ee/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding JPA Support to a Maven/Eclipse/JSF2 Project</title>
		<link>http://www.tech-juice.org/2012/05/28/adding-jpa-to-a-maveneclipsejsf2-project/</link>
		<comments>http://www.tech-juice.org/2012/05/28/adding-jpa-to-a-maveneclipsejsf2-project/#comments</comments>
		<pubDate>Mon, 28 May 2012 17:09:55 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[eclipselink]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[java ee]]></category>
		<category><![CDATA[javax]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=2112</guid>
		<description><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#Add_JPA_Dependencies_to_pomxml">2 Add JPA Dependencies to pom.xml</a></li><li><a href="#Add_JPA_Facet_to_Eclipse_Project">3 Add JPA Facet to Eclipse Project</a></li><li><a href="#Configure_a_Persistence_Unit">4 Configure a Persistence Unit</a></li></ul></div>
<span id="Introduction">Introduction</span>
<p>the Java Persistence API (JPA) allows for easy managing of relational data in Java applications. It is a replacement of the much criticized EJB 2.0 and EJB 2.1 entity beans. In this post we show how to add JPA support to an <a href="http://www.tech-juice.org/2012/05/13/hello-world-with-jsf-2-0-glassfish-3-maven-svn-and-eclipse/">existing</a> Maven/Eclipse/JSF2 project with Java EE 6.</p>
<p>With JPA you can do create an entity that is backed by a table in the database. For example you can create a persisted entity (which is just a POJO with some annotations):</p>
<p><strong>Name.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Table</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Column</span><span style="color: #339933;">;</span>
&#160;
@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&#34;CUSTOMER_INFORMATION&#34;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&#160;
    @Id
    @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&#34;FULL_NAME&#34;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&#160;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And a client class that fetches all customer names with and prints them:<br />
<strong>Client.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Persistence</span></pre></div>&#8230;</div>]]></description>
			<content:encoded><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#Add_JPA_Dependencies_to_pomxml">2 Add JPA Dependencies to pom.xml</a></li><li><a href="#Add_JPA_Facet_to_Eclipse_Project">3 Add JPA Facet to Eclipse Project</a></li><li><a href="#Configure_a_Persistence_Unit">4 Configure a Persistence Unit</a></li></ul></div>
<h2><span id="Introduction">Introduction</span></h2>
<p>the Java Persistence API (JPA) allows for easy managing of relational data in Java applications. It is a replacement of the much criticized EJB 2.0 and EJB 2.1 entity beans. In this post we show how to add JPA support to an <a href="http://www.tech-juice.org/2012/05/13/hello-world-with-jsf-2-0-glassfish-3-maven-svn-and-eclipse/">existing</a> Maven/Eclipse/JSF2 project with Java EE 6.</p>
<p>With JPA you can do create an entity that is backed by a table in the database. For example you can create a persisted entity (which is just a POJO with some annotations):</p>
<p><strong>Name.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Table</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Column</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;CUSTOMER_INFORMATION&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
    @Id
    @Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;FULL_NAME&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And a client class that fetches all customer names with and prints them:<br />
<strong>Client.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Persistence</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManagerFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManager</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> printNames<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The customer names are:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        EntityManagerFactory emf <span style="color: #339933;">=</span> Persistence.<span style="color: #006633;">createEntityManagerFactory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;examplePersistenceUnit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        EntityManager em <span style="color: #339933;">=</span> emf.<span style="color: #006633;">createEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>Customer<span style="color: #339933;">&gt;</span> customers <span style="color: #339933;">=</span> em.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select c from Customer c&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Customer c<span style="color: #339933;">:</span> customers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Example output:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">The customer names are:
John
Jack
Dave</pre></div></div>

<h2><span id="Add_JPA_Dependencies_to_pomxml">Add JPA Dependencies to pom.xml</span></h2>
<p>JPA is part of the <a href="http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition">Java EE</a> specification, more specifically the <code>javax.persistence.*</code> package. Let&#8217;s think about which dependencies we need to add to our pom.xml to make JPA work.</p>
<ol>
<li>Application servers such as Glassfish and JBoss are fully Java EE compliant, and will provide an implementation of the <code>javax.persistence.*</code> classes at runtime. You can just deploy classes that using <code>javax.persistence.Persist</code> to Glassfish, without having to include any JAR files. So no need to modify the pom.xml for that situation.</li>
<li>Compiling Java EE code requires that the Java EE API is available on the classpath. We <a href="http://www.tech-juice.org/2012/05/13/hello-world-with-jsf-2-0-glassfish-3-maven-svn-and-eclipse/#Adding_Dependencies_To_POM">solved</a> this by adding the following dependency:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javaee-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>provided<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note that this artifact <strong>only provides the Java EE API, not the method bodies</strong>. For compilation only this works fine, but you can&#8217;t run the code locally with it! I&#8217;m not sure why the Java API does not provide implementations, most likely it is to keep the JAR size down and be implementation-independent. But it sure is a headache for <a href="http://www.adam-bien.com/roller/abien/entry/trouble_with_crippled_java_ee">many developers</a>.
</li>
<li>When <strong>unit testing</strong> the application, e.g. with JUnit, it needs to run locally instead of in an application server such as Glassfish. To run outside of an application server we need provide implementations for the <code>javax.persistence.*</code> classes ourselves. Glassfish uses <a href="http://www.eclipse.org/eclipselink/">EclipseLink</a> for its JPA implementation, so let&#8217;s change our test-time dependencies to include EclipseLink.
<p>First, add the EclipseLink repository to the pom:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>EclipseLink Repo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://download.eclipse.org/rt/eclipselink/maven.repo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;snapshots<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;enabled<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/enabled<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/snapshots<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Next we include the EclipseLink libraries with the &#8216;test&#8217; scope <strong>above</strong> the Java EE API. This way, the <code>javax.persistence</code> classes will be resolved to the EclipseLink artifact instead of the <code>javaee-api</code> artifact. We also need a database driver, in this case PostgreSQL. Together it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>eclipselink<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.persistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javaee-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>provided<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
</ol>
<h2><span id="Add_JPA_Facet_to_Eclipse_Project">Add JPA Facet to Eclipse Project</span></h2>
<p>Eclipse provides excellent JPA support out of the box, but it requires some work to use it with a M2Eclipse/Maven project. Here is how I did it:</p>
<ol>
<li>Re-open the Project Properties go to Project Facets and check &#8216;JPA&#8217;, version 2.0.</li>
<li>In the bottom, click on &#8216;Further configuration available&#8230;&#8217;.</li>
<li>For JPA Implementation choose &#8216;Disable Library Configuration&#8217;, since this is handled by Maven.</li>
<li>Set the connection details under &#8216;Connection&#8217;, and click OK. This will create a META-INF directory under <code>src/main/java</code>.</li>
<li>Drag the META-INF directory from <code>src/main/java</code> to <code>src/main/resources</code>. Make sure <code>src/main/resources</code> is on the Build Path (rightclick, Build Path, Add to Build Path). You will get an error message and the &#8216;JPA Content&#8217; node will stop working.</li>
<li>Now go into the Project Properties again, and uncheck &#8216;JPA&#8217;, apply the change, and re-enable JPA. Now it should correctly use the <code>persistence.xml</code> file in <code>src/main/resources/META-INF</code>.
</ol>
<p>Now when building a WAR file, the <code>persistence.xml</code> file will be copied to <code>/WEB-INF/classes/META-INF/persistence.xml</code>, which is one of the <a href="http://javahowto.blogspot.com/2007/06/where-to-put-persistencexml-in-web-app.html">valid locations</a>.</p>
<h2><span id="Configure_a_Persistence_Unit">Configure a Persistence Unit</span></h2>
<p>Before you can use all the sweet JPA features, you need to create a persistence unit and assign a data source to it (a database connection). Some good sources to learn about the configuration of JPA are:</p>
<ul>
<li><a href="http://java.boot.by/scbcd5-guide/ch07.html">Persistence Units and Persistence Contexts</a></li>
<li><a href="http://openejb.apache.org/jpa-concepts.html">JPA Concepts &#8211; openejb.apache.org</a></li>
<li><a href="http://www.vogella.com/articles/JavaPersistenceAPI/article.html">JPA 2.0 with EclipseLink &#8211; Tutorial</a></li>
</ul>
<p>You may also be interested in another post here: <a href="http://www.tech-juice.org/2012/05/30/datasourcedefinition-postgresql-example-in-java-ee/">Using @DataSourceDefinition in Java EE 6 With Postgresql</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/05/28/adding-jpa-to-a-maveneclipsejsf2-project/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to play UStream channels in VLC</title>
		<link>http://www.tech-juice.org/2012/05/20/how-to-play-ustream-channels-in-vlc/</link>
		<comments>http://www.tech-juice.org/2012/05/20/how-to-play-ustream-channels-in-vlc/#comments</comments>
		<pubDate>Sun, 20 May 2012 18:31:33 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[flv]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rtmp]]></category>
		<category><![CDATA[rtmpdump]]></category>
		<category><![CDATA[stream]]></category>
		<category><![CDATA[ustream]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=2107</guid>
		<description><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#1_Install_rtmpdump">2 1: Install rtmpdump</a></li><li><a href="#2_Find_the_RTMPDump_Parameters">3 2: Find the RTMPDump Parameters</a></li><li><a href="#3_Record_or_Play_the_Stream">4 3: Record or Play the Stream </a></li></ul></div>
<span id="Introduction">Introduction</span>
<p>Ustream.tv doesn&#8217;t support opening streams in VLC by default, but it is possible with some effort. Most video streaming sites use the <a href="http://en.wikipedia.org/wiki/Real_Time_Messaging_Protocol">Real Time Messaging Protocol</a> (RTMP) for transmitting video data to your flash player. Using <a href="http://rtmpdump.mplayerhq.hu/">rtmpdump</a> we can connect to RTMP streams, and record them or play them in VLC.</p>
<span id="1_Install_rtmpdump">1: Install rtmpdump</span>
<p>On Ubuntu:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> rtmpdump</pre></div></div>

<p>Binaries for all platforms: <a href="http://rtmpdump.mplayerhq.hu/download/">here</a>.</p>
<span id="2_Find_the_RTMPDump_Parameters">2: Find the RTMPDump Parameters</span>
<p>It&#8217;s not trivial to find out  the correct parameters for <code>rtmpdump</code>, so use this Python script:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># This script finds the rtmpdump command syntax for opening a UStream stream.</span>
&#160;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&#160;
&#160;
<span style="color: #ff7700;font-weight:bold;">def</span> getVideoData<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Get the HTML contents</span>
    req = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
    response = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>req<span style="color: black;">&#41;</span>
    html = response.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&#160;
    <span style="color: #808080; font-style: italic;"># Extract the channel ID from the HTML</span>
    channelId = <span style="color: #008000;">None</span>
    m = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&#34;Channel<span style="color: #000099; font-weight: bold;">\s</span>ID<span style="color: #000099; font-weight: bold;">\:</span><span style="color: #000099; font-weight: bold;">\s</span>+(<span style="color: #000099; font-weight: bold;">\d</span>+)&#34;</span>, html<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>m<span style="color: black;">&#41;</span>:
        channelId = m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&#160;
    <span style="color: #808080; font-style: italic;"># Extract the</span></pre></div>&#8230;</div>]]></description>
			<content:encoded><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#1_Install_rtmpdump">2 1: Install rtmpdump</a></li><li><a href="#2_Find_the_RTMPDump_Parameters">3 2: Find the RTMPDump Parameters</a></li><li><a href="#3_Record_or_Play_the_Stream">4 3: Record or Play the Stream </a></li></ul></div>
<h2><span id="Introduction">Introduction</span></h2>
<p>Ustream.tv doesn&#8217;t support opening streams in VLC by default, but it is possible with some effort. Most video streaming sites use the <a href="http://en.wikipedia.org/wiki/Real_Time_Messaging_Protocol">Real Time Messaging Protocol</a> (RTMP) for transmitting video data to your flash player. Using <a href="http://rtmpdump.mplayerhq.hu/">rtmpdump</a> we can connect to RTMP streams, and record them or play them in VLC.</p>
<h2><span id="1_Install_rtmpdump">1: Install rtmpdump</span></h2>
<p>On Ubuntu:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> rtmpdump</pre></div></div>

<p>Binaries for all platforms: <a href="http://rtmpdump.mplayerhq.hu/download/">here</a>.</p>
<h2><span id="2_Find_the_RTMPDump_Parameters">2: Find the RTMPDump Parameters</span></h2>
<p>It&#8217;s not trivial to find out  the correct parameters for <code>rtmpdump</code>, so use this Python script:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># This script finds the rtmpdump command syntax for opening a UStream stream.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> getVideoData<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Get the HTML contents</span>
    req = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
    response = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>req<span style="color: black;">&#41;</span>
    html = response.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Extract the channel ID from the HTML</span>
    channelId = <span style="color: #008000;">None</span>
    m = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Channel<span style="color: #000099; font-weight: bold;">\s</span>ID<span style="color: #000099; font-weight: bold;">\:</span><span style="color: #000099; font-weight: bold;">\s</span>+(<span style="color: #000099; font-weight: bold;">\d</span>+)&quot;</span>, html<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>m<span style="color: black;">&#41;</span>:
        channelId = m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Extract the channel title from the HTML</span>
    channelTitle = <span style="color: #008000;">None</span>
    m = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;property<span style="color: #000099; font-weight: bold;">\=</span><span style="color: #000099; font-weight: bold;">\&quot;</span>og<span style="color: #000099; font-weight: bold;">\:</span>url<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\s</span>+content<span style="color: #000099; font-weight: bold;">\=</span><span style="color: #000099; font-weight: bold;">\&quot;</span>http<span style="color: #000099; font-weight: bold;">\:</span><span style="color: #000099; font-weight: bold;">\/</span><span style="color: #000099; font-weight: bold;">\/</span>www.&quot;</span> +
            <span style="color: #483d8b;">&quot;ustream<span style="color: #000099; font-weight: bold;">\.</span>tv<span style="color: #000099; font-weight: bold;">\/</span>(?:channel<span style="color: #000099; font-weight: bold;">\/</span>)?([^<span style="color: #000099; font-weight: bold;">\&quot;</span>]+)<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>, html<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>m<span style="color: black;">&#41;</span>:
        channelTitle = m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    amfContent = <span style="color: #008000;">None</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>channelId<span style="color: black;">&#41;</span>:
        amfUrl = <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://cdngw.ustream.tv/Viewer/getStream/1/&quot;</span>
        + channelId + <span style="color: #483d8b;">&quot;.amf&quot;</span><span style="color: black;">&#41;</span>
        response = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>amfUrl<span style="color: black;">&#41;</span>
        amfContent = response.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        rtmpUrl = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;(rtmp<span style="color: #000099; font-weight: bold;">\:</span><span style="color: #000099; font-weight: bold;">\/</span><span style="color: #000099; font-weight: bold;">\/</span>[^<span style="color: #000099; font-weight: bold;">\x</span>00]+)&quot;</span>, amfContent<span style="color: black;">&#41;</span>.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'tmp.txt'</span>, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
        f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>amfContent<span style="color: black;">&#41;</span>
        streamName = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;streamName(?:<span style="color: #000099; font-weight: bold;">\W</span>+)([^<span style="color: #000099; font-weight: bold;">\x</span>00]+)&quot;</span>,
                amfContent<span style="color: black;">&#41;</span>.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>channelId, channelTitle, rtmpUrl, streamName<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> getRtmpCommand<span style="color: black;">&#40;</span>rtmpUrl, streamName<span style="color: black;">&#41;</span>:
    result = <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;rtmpdump -v -r <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> + rtmpUrl + <span style="color: #483d8b;">&quot;/&quot;</span> + streamName + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>
              <span style="color: #483d8b;">&quot; -W <span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.ustream.tv/flash/viewer.swf<span style="color: #000099; font-weight: bold;">\&quot;</span> --live&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span>argv=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Process arguments</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> argv <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
        argv = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>
&nbsp;
    usage = <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Usage: ustreamRTMPDump.py &lt;ustream channel url&gt; [filename]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            <span style="color: #483d8b;">&quot;e.g. <span style="color: #000099; font-weight: bold;">\&quot;</span>ustreamRTMPDump.py 'http://www.ustream.tv/ffrc'<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>argv<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> usage
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Get RTMP information and print it</span>
    url = argv<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Opening url: &quot;</span> + url + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
    <span style="color: black;">&#40;</span>channelId, channelTitle, rtmpUrl, streamName<span style="color: black;">&#41;</span> = getVideoData<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Channel ID: &quot;</span> + channelId
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Channel Title: &quot;</span> + channelTitle
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;RTMP URL: &quot;</span> + rtmpUrl
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;RTMP Streamname: &quot;</span> + streamName + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
    rtmpCmd = getRtmpCommand<span style="color: black;">&#40;</span>rtmpUrl, streamName<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;RTMP Command:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + rtmpCmd
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>To use this script to get the <code>rtmpdump</code> command for a UStream channel, simply call it with the channel URL:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python ustreamRTMPDump.py <span style="color: #000000; font-weight: bold;">&lt;</span>url<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ python ustreamRTMPDump.py <span style="color: #ff0000;">&quot;http://www.ustream.tv/decoraheagles&quot;</span>
Opening url: http:<span style="color: #000000; font-weight: bold;">//</span>www.ustream.tv<span style="color: #000000; font-weight: bold;">/</span>decoraheagles
&nbsp;
Channel ID: <span style="color: #000000;">3064708</span>
Channel Title: decoraheagles
RTMP URL: rtmp:<span style="color: #000000; font-weight: bold;">//</span>ustreamlivefs.fplive.net<span style="color: #000000; font-weight: bold;">/</span>ustream3live-live<span style="color: #000000; font-weight: bold;">/</span>
RTMP Streamname: stream_live_1_1_3064708
&nbsp;
RTMP Command:
rtmpdump <span style="color: #660033;">-v</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;rtmp://ustreamlivefs.fplive.net/ustream3live-live//stream_live_1_1_3064708&quot;</span> <span style="color: #660033;">-W</span> <span style="color: #ff0000;">&quot;http://www.ustream.tv/flash/viewer.swf&quot;</span> <span style="color: #660033;">--live</span></pre></div></div>

<p>Running this command will connect to the stream and write the binary data to the terminal as characters. Not exactly what we want. But we can easily write the stream data to a file, or play it in VLC!</p>
<h2><span id="3_Record_or_Play_the_Stream">3: Record or Play the Stream </span></h2>
<p>To <strong>play</strong> the stream in VLC, simply pipe the command into VLC by adding <code>| vlc -</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ rtmpdump <span style="color: #660033;">-v</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;rtmp://ustreamlivefs.fplive.net/ustream3live-live//stream_live_1_1_3064708&quot;</span> 
<span style="color: #660033;">-W</span> <span style="color: #ff0000;">&quot;http://www.ustream.tv/flash/viewer.swf&quot;</span> <span style="color: #660033;">--live</span> <span style="color: #000000; font-weight: bold;">|</span> vlc -</pre></div></div>

<p>To <strong>record</strong> the stream to a file <code>video.flv</code> simply add <code>-o video.flv</code> to the command.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ rtmpdump <span style="color: #660033;">-v</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;rtmp://ustreamlivefs.fplive.net/ustream3live-live//stream_live_1_1_3064708&quot;</span> 
<span style="color: #660033;">-W</span> <span style="color: #ff0000;">&quot;http://www.ustream.tv/flash/viewer.swf&quot;</span> <span style="color: #660033;">--live</span> <span style="color: #660033;">-o</span> video.flv</pre></div></div>

<p>You can then play this file in VLC, even while it is being recorded:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ vlc video.flv</pre></div></div>

<h3>Example Output</h2>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ rtmpdump <span style="color: #660033;">-v</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;rtmp://ustreamlivefs.fplive.net/ustream3live-live//stream_live_1_1_3064708&quot;</span> <span style="color: #660033;">-W</span> <span style="color: #ff0000;">&quot;http://www.ustream.tv/flash/viewer.swf&quot;</span> <span style="color: #660033;">--live</span> <span style="color: #000000; font-weight: bold;">|</span> vlc -
RTMPDump v2.4
<span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2010</span> Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL
VLC media player 2.0.1 Twoflower <span style="color: #7a0874; font-weight: bold;">&#40;</span>revision 2.0.1-<span style="color: #000000;">0</span>-gf432547<span style="color: #7a0874; font-weight: bold;">&#41;</span>
WARNING: You haven<span style="color: #ff0000;">'t specified an output file (-o filename), using stdout
[0x8d07908] main libvlc: Running vlc with the default interface. Use '</span>cvlc<span style="color: #ff0000;">' to use vlc without interface.
ERROR: RTMP_HashSWF: couldn'</span>t contact swfurl http:<span style="color: #000000; font-weight: bold;">//</span>www.ustream.tv<span style="color: #000000; font-weight: bold;">/</span>flash<span style="color: #000000; font-weight: bold;">/</span>viewer.swf <span style="color: #7a0874; font-weight: bold;">&#40;</span>HTTP error <span style="color: #000000;">302</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
Connecting ...
INFO: Connected...
Starting Live Stream
INFO: Metadata:
INFO:   author                
INFO:   copyright             
INFO:   description           
INFO:   keywords              
INFO:   rating                
INFO:   title                 
INFO:   presetname            Custom
INFO:   creationdate          Sat May <span style="color: #000000;">19</span> <span style="color: #000000;">23</span>:01:07 <span style="color: #000000;">2012</span>
INFO:   videodevice           ATI AVStream Analog Capture
INFO:   framerate             <span style="color: #000000;">30.00</span>
INFO:   width                 <span style="color: #000000;">720.00</span>
INFO:   height                <span style="color: #000000;">480.00</span>
INFO:   videocodecid          avc1
INFO:   videodatarate         <span style="color: #000000;">500.00</span>
INFO:   avclevel              <span style="color: #000000;">50.00</span>
INFO:   avcprofile            <span style="color: #000000;">77.00</span>
INFO:   videokeyframe_frequency1.00
INFO:   audiodevice           Microphone <span style="color: #7a0874; font-weight: bold;">&#40;</span>Realtek High Defini
INFO:   audiosamplerate       <span style="color: #000000;">22050.00</span>
INFO:   audiochannels         <span style="color: #000000;">1.00</span>
INFO:   audioinputvolume      <span style="color: #000000;">100.00</span>
INFO:   audiocodecid          .mp3
INFO:   audiodatarate         <span style="color: #000000;">48.00</span>
<span style="color: #000000;">0.634</span> kB <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">0.00</span> sec<span style="color: #ff0000;">&quot;sni-qt/24365&quot;</span> WARN  <span style="color: #000000;">20</span>:<span style="color: #000000;">30</span>:<span style="color: #000000;">09.944</span> void StatusNotifierItemFactory::connectToSnw<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> Invalid interface to SNW_SERVICE 
<span style="color: #000000;">36.430</span> kB <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#91;</span>flv <span style="color: #000000; font-weight: bold;">@</span> 0xb5211aa0<span style="color: #7a0874; font-weight: bold;">&#93;</span> Estimating duration from bitrate, this may be inaccurate
<span style="color: #000000;">214.671</span> kB <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">1.13</span> sec</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/05/20/how-to-play-ustream-channels-in-vlc/feed/</wfw:commentRss>
		<slash:comments>38</slash:comments>
		</item>
		<item>
		<title>Hello World with JSF 2.0, Glassfish 3, Maven, SVN and Eclipse</title>
		<link>http://www.tech-juice.org/2012/05/13/hello-world-with-jsf-2-0-glassfish-3-maven-svn-and-eclipse/</link>
		<comments>http://www.tech-juice.org/2012/05/13/hello-world-with-jsf-2-0-glassfish-3-maven-svn-and-eclipse/#comments</comments>
		<pubDate>Sun, 13 May 2012 02:18:19 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[java ee]]></category>
		<category><![CDATA[java ee 6]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=1955</guid>
		<description><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#What_Are_We_Building">2 What Are We Building?</a></li><li><a href="#Project_Setup_with_Maven_Archetypes">3 Project Setup with Maven Archetypes</a></li><li><a href="#Adding_Dependencies_To_POM">4 Adding Dependencies To POM</a></li><li><a href="#Configuring_the_Deployment_Descriptor">5 Configuring the Deployment Descriptor</a></li><li><a href="#Adding_a_Bean">6 Adding a Bean</a></li><li><a href="#Adding_a_JSF_Page">7 Adding a JSF Page</a></li><li><a href="#Deploying_Manually_In_Glassfish_3">8 Deploying Manually In Glassfish 3</a></li><li><a href="#Importing_Into_Eclipse">9 Importing Into Eclipse</a><ul><li><a href="#Set_Java_version_in_POM_to_16">9.1 Set Java version in POM to 1.6</a></li><li><a href="#Install_Eclipse_Plugins">9.2 Install Eclipse Plugins</a></li><li><a href="#Import_the_Project">9.3 Import the Project</a></li><li><a href="#Installing_Glassfish_Support">9.4 Installing Glassfish Support</a></li><li><a href="#Deploying_to_Glassfish">9.5 Deploying to Glassfish</a></li><li><a href="#Add_Project_to_SVN">9.6 Add Project to SVN</a></li></ul></li></ul></div>
<span id="Introduction">Introduction</span>
<p>Serious Java Web application development requires a lot of different applications. In addition to a JDK, an IDE and an application server you may need a Web application framework, a build system and some form of version control. Unsurprisingly, getting set up for development can be quite the task.</p>
<p>In this post we&#8217;ll take a look at setting up a Web application project from start to finish. The software we will use is:</p>
<span id="What_Are_We_Building">What Are We Building?</span>
<p>Different applications come in different forms. </p>
<ul>
<li>A Windows application written in C++ may be compiled to a .exe file.</li>
<li>Runnable Java GUI applications often come in a JAR (Java ARchive).</li>
<li>Java Web applications are packaged in <a href="http://www.tech-juice.org/2012/05/10/an-introduction-to-java-web-application-development/#How_Is_a_Java_Web_Application_Distributed">WAR and EAR files</a>.</li>
</ul>
<p>We are building a Web application, so the product of building our project will be&#8230;</p>]]></description>
			<content:encoded><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#What_Are_We_Building">2 What Are We Building?</a></li><li><a href="#Project_Setup_with_Maven_Archetypes">3 Project Setup with Maven Archetypes</a></li><li><a href="#Adding_Dependencies_To_POM">4 Adding Dependencies To POM</a></li><li><a href="#Configuring_the_Deployment_Descriptor">5 Configuring the Deployment Descriptor</a></li><li><a href="#Adding_a_Bean">6 Adding a Bean</a></li><li><a href="#Adding_a_JSF_Page">7 Adding a JSF Page</a></li><li><a href="#Deploying_Manually_In_Glassfish_3">8 Deploying Manually In Glassfish 3</a></li><li><a href="#Importing_Into_Eclipse">9 Importing Into Eclipse</a><ul><li><a href="#Set_Java_version_in_POM_to_16">9.1 Set Java version in POM to 1.6</a></li><li><a href="#Install_Eclipse_Plugins">9.2 Install Eclipse Plugins</a></li><li><a href="#Import_the_Project">9.3 Import the Project</a></li><li><a href="#Installing_Glassfish_Support">9.4 Installing Glassfish Support</a></li><li><a href="#Deploying_to_Glassfish">9.5 Deploying to Glassfish</a></li><li><a href="#Add_Project_to_SVN">9.6 Add Project to SVN</a></li></ul></li></ul></div>
<h2><span id="Introduction">Introduction</span></h2>
<p>Serious Java Web application development requires a lot of different applications. In addition to a JDK, an IDE and an application server you may need a Web application framework, a build system and some form of version control. Unsurprisingly, getting set up for development can be quite the task.</p>
<p>In this post we&#8217;ll take a look at setting up a Web application project from start to finish. The software we will use is:</p>

<table id="wp-table-reloaded-id-14-no-1" class="wp-table-reloaded wp-table-reloaded-id-14">
<thead>
	<tr class="row-1 odd">
		<th class="column-1">Application</th><th class="column-2">Download</th>
	</tr>
</thead>
<tbody>
	<tr class="row-2 even">
		<td class="column-1"><strong>Java EE 6 SDK with JDK 7 U3</strong></td><td class="column-2"><a href="http://www.oracle.com/technetwork/java/javaee/downloads/index.html">www.oracle.com</a></td>
	</tr>
	<tr class="row-3 odd">
		<td class="column-1"><strong>Maven 3.0.4</strong></td><td class="column-2"><a href="http://maven.apache.org/download.html">maven.apache.org</a></td>
	</tr>
	<tr class="row-4 even">
		<td class="column-1"><strong>Glassfish Server Open Source Edition 3.1.2</strong></td><td class="column-2"><a href="http://glassfish.java.net/public/downloadsindex.html">glassfish.java.net</strong></td>
	</tr>
	<tr class="row-5 odd">
		<td class="column-1"><strong>JSF 2.0</strong></td><td class="column-2">Libraries included with Glassfish.</td>
	</tr>
	<tr class="row-6 even">
		<td class="column-1"><strong>Eclipse 3.7</strong></td><td class="column-2"><a href="http://www.eclipse.org/downloads/">www.eclipse.org</a></td>
	</tr>
</tbody>
</table>

<h2><span id="What_Are_We_Building">What Are We Building?</span></h2>
<p>Different applications come in different forms. </p>
<ul>
<li>A Windows application written in C++ may be compiled to a .exe file.</li>
<li>Runnable Java GUI applications often come in a JAR (Java ARchive).</li>
<li>Java Web applications are packaged in <a href="http://www.tech-juice.org/2012/05/10/an-introduction-to-java-web-application-development/#How_Is_a_Java_Web_Application_Distributed">WAR and EAR files</a>.</li>
</ul>
<p>We are building a Web application, so the product of building our project will be a WAR file. The Glassfish application server extracts a WAR file and runs the application inside it. The WAR file also contains a deployment descriptor, <code>web.xml</code> that describes <em>how</em> the application server should run the WAR.</p>
<p><strong>The structure of a WAR file looks like this</strong> (<a href="http://docstore.mik.ua/orelly/xml/jxslt/ch06_02.htm">source</a>):</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/warfile.gif"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/warfile.gif" alt="" title="warfile" width="485" height="305" class="alignnone size-full wp-image-2006" /></a></p>
<h2><span id="Project_Setup_with_Maven_Archetypes">Project Setup with Maven Archetypes</span></h2>
<p>Now that we know what we are building, it is time to start setting up the project. The directory/file structure of the project we are creating is very <em>different</em> from the structure of the WAR file. Our build tool, Maven, makes the &#8216;translation&#8217; between the project structure and the WAR structure. It takes care of compiling and packaging up the files in the project into a valid WAR file that is ready for deployment.</p>
<p>We don&#8217;t have to start completely from scratch with the project. Maven can download pre-made, barebones projects called <a href="http://maven.apache.org/archetype/maven-archetype-plugin/">archetypes</a>. Archetypes are nothing more than a bunch of files and folders that are packaged up and given a name. </p>
<p>There are currently 588 archetypes, 21 of which contain &#8220;jsf&#8221; in their name. Most of these archetypes are quite heavyweight: they come with pretty big <em>pom.xml</em> files. Instead of using these, we will start off with a very basic non-jsf archetype, and add only the things we really need.</p>
<p>First we will download the archetype <code>maven-archetype-webapp</code> with the <code>mvn archetype:generate</code> command. Be be sure to choose a fitting groupId and artifactId:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt;mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] &gt;&gt;&gt; maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom &gt;&gt;&gt;
...
[INFO] Generating project in Interactive mode
Define value for property 'groupId': : mygroup.com
Define value for property 'artifactId': : myprojectname
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  mygroup.com: :
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------</pre></div></div>

<p>This creates a very simple Mavenized Web application project in a directory named <em>myprojectname</em>. The contents of this folder are:</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/files_archetype_webap.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/files_archetype_webap.png" alt="" title="files_archetype_webap" width="291" height="196" class="alignnone size-full wp-image-2008" /></a></p>
<div id='stb-box-5400' class='stb-warning_box' ><strong>Note</strong><br />
Usually, archetypes contain a pom.xml file, meaning they are Maven projects. But this does not <em>have</em> to be the case. In theory, an archetype can be any set of files and folders.</div>
<p>The default pom.xml file is very simple, containing only one dependency. The packaging type is set to <code>war</code>, which is what we need.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mygroup.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myprojectname<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myprojectname Maven Webapp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://maven.apache.org<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.8.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myprojectname<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2><span id="Adding_Dependencies_To_POM">Adding Dependencies To POM</span></h2>
<p>Our project will use JSF 2.0 web framework, and run on the Glassfish application server. JSF is part of the standard Java EE platform, which means it&#8217;s built-in to any compliant Java EE application server, such as Oracle&#8217;s WebLogic, GlassFish Open Source Edition, or JBoss AS. But it can also be used as a standalone library in servlet containers such as Tomcat and Jetty.</p>
<p>In order to be able use JSF (the <code>javax.faces</code> package) in our source code we must add a dependency for it in the pom.xml. Add this dependency for the entire Java EE 6 API (including JSF and CDI):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javaee-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>provided<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The <code>provided</code> scope means that Maven assumes the dependency is provided by the application server at run-time. Maven will only add the JAR to the classpath for compilation and testing phases, but it will not be included in the WAR file. This is what we want, because Glassfish 3 contains a full Java EE implementation including JSF 2.0.</p>
<h2><span id="Configuring_the_Deployment_Descriptor">Configuring the Deployment Descriptor</span></h2>
<p>A <a href="http://en.wikipedia.org/wiki/Deployment_descriptor">deployment descriptor</a> describes how a Web application should be deployed. In the past, using a web.xml file in the WEB-INF directory was required. However, with the <a href="http://www.servletworld.com/servlet-tutorials/servlet3/new-features.html">new features</a> in the Servlet 3.0 specification in Java EE 6 it has become possible to use <a href="http://en.wikipedia.org/wiki/Java_annotation">annotations</a> instead of XML configuration files such as web.xml. It doesn’t mean the web.xml is now obsolete, but its size can be reduced. Information specified in the deployment descriptor takes precedence over the information specified through Annotations.</p>
<p>Part of the deployment descriptor are <em>servlet mappings</em> that describe which URLs are passed to the Faces Servlet. Some application servers (including GlassFish) automatically provide a servlet mapping for the <code>/faces/*</code>, <code>*.faces</code>, and <code>*.jsf</code> patterns. The <code>*.jsf</code> pattern means that a request to <code>http://localhost/myprojectname/index.jsf</code> is passed to the Faces Servlet. By default, the Faces Servlet then looks for a file but with a <code>.jsp</code extension on the filesystem. We can change these defaults through the web.xml file.</p>
<p>Discard the outdated <strong>web.xml</strong> that came with the archetype, and replace it with this web.xml:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;3.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Faces Servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.faces.webapp.FacesServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Faces Servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.faces.DEFAULT_SUFFIX<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This instructs the application server to pass <strong>.xhtml</strong> requests to the Faces Servlet, and to let the Faces Servlet look for <strong>.xhtml</strong> files on the filesystem.</p>
<h2><span id="Adding_a_Bean">Adding a Bean</span></h2>
<p>A bean is a Java class that is managed by the JSF framework, and can be referenced from JSF pages using the bean name. It is a historical accident that there are two separate mechanisms, CDI beans and JSF managed beans, for beans that can be used in JSF pages. The <a href="http://stackoverflow.com/questions/4347374/jsf-backing-beans-managedbean-or-cdi-beans-named">consensus</a> for Java EE 6 seems to be CDI beans, so we will use that method.</p>
<p>In previous JSF versions, each bean had to be declared in a <code>faces-config.xml</code> file. Thankfully we now have annotations, which makes this file obsolete in most cases. Create a new source file at <code>/src/main/java/com/mygroup/Hello.java</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.mygroup</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.inject.Named</span><span style="color: #339933;">;</span>
&nbsp;
@Named
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloBean
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getGreeting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <code>@Named</code> annotation means the class is a CDI bean. One thing we need to do now is tell the server that this project is a module containing CDI beans. To do this, we add an empty file called beans.xml to the project at <code>/src/main/webapp/WEB-INF/beans.xml</code>.</p>
<div id='stb-box-9861' class='stb-warning_box' ><strong>Note</strong><br />
Make sure you have created the beans.xml file, or the project will <strong>not</strong> work. It may seem silly that an empty file is so important, but there are convincing reasons for this behavior. They are described <a href="http://seamframework.org/Documentation/WhatIsBeansxmlAndWhyDoINeedIt">here</a>.</div>
<h2><span id="Adding_a_JSF_Page">Adding a JSF Page</span></h2>
<p>We have defined in the web.xml file that our views use the <code>.xhtml</code> extension. So we remove the <code>/src/main/webapp/index.jsp</code> file that came with the archetype, and create a new file <code>/src/main/webapp/index.xhtml</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span style="color: #00bbdd;">        &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">xmlns:ui</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/facelets&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">xmlns:f</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/core&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My JSF Page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Bean Test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;HelloBean says: #{helloBean.greeting}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<ul>
<li>Since this is a JSF page, we can use <code>h:</code>, <code>f:</code> and <code>ui:</code> tags. These tags will be processed by the Faces Servlet.</li>
<li>The <a href="http://courses.coreservlets.com/Course-Materials/pdf/jsf/jsf2/JSF2-Expression-Language.pdf">EL statement</a> <code>#{helloBean.greeting}</code> is evaluated when the page is requested. This should call the <code>getGreeting()</code> method of the <code>HelloBean</code> class.</li>
</ul>
<p>The completed project should look like this:</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/completedProject.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/completedProject.png" alt="" title="completedProject" width="347" height="299" class="alignnone size-full wp-image-2062" /></a></p>
<h2><span id="Deploying_Manually_In_Glassfish_3">Deploying Manually In Glassfish 3</span></h2>
<p>OK, now we should have a working JSF/Maven project. Let's try to deploy and run it manually before we import it into Eclipse.</p>
<p>Package the project into a WAR file with the <code>mvn package</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="cmd" style="font-family:monospace;">&gt;mvn package
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------</pre></div></div>

<p>This builds the project into a WAR file at <code>/target/myprojectname.war</code>. </p>
<p>Next, we start up a Glassfish 3 server with the <code>asadmin start-domain</code> command.</p>

<div class="wp_syntax"><div class="code"><pre class="cmd" style="font-family:monospace;">asadmin start-domain --verbose domain1</pre></div></div>

<p>Now copy the WAR file to the <strong>autodeploy</strong> directory of your domain directory, e.g. <code>glassfish/domains/domain1/autodeploy</code>. You will see some output from the <code>asadmin</code> process, which can help identify any problems with the project. Let's assume that things didn't blow up in your face and the deployment was succesful, and you see something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cmd" style="font-family:monospace;">[#|2012-05-12T23:40:30.620+0200|INFO|glassfish3.1.2
|[AutoDeploy] Successfully autodeployed : C:\glassfish3\glassfish\domains\domain1\autodeploy\myprojectname.war.|#]</pre></div></div>

<p>Now open a browser and open the page, e.g. at <code>http://localhost:8080/myprojectname/index.xhtml</code>. You should see "HelloBean says: Hello World".</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/beantest.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/beantest.png" alt="" title="beantest" width="517" height="218" class="alignnone size-full wp-image-2059" /></a></p>
<h2><span id="Importing_Into_Eclipse">Importing Into Eclipse</span></h2>
<p>Now that we have a working JSF/Maven project, it is time to import the project into Eclipse.</p>
<div id='stb-box-3110' class='stb-warning_box' ><strong>Note</strong><br />
Although many guides on the internet (wrongly) recommend it, <strong>you should not use the mvn eclipse:eclipse command</strong> to create the Eclipse project. This plugin can only generate WTP projects for <a href="http://wiki.eclipse.org/WTP_FAQ#What_version_of_Eclipse_does_WTP_work_with.3F">very old</a> Eclipse versions (WTP 2.0 is the maximum). Instead, use the m2eclipse plugin as described in this section.</div>
<h3><span id="Set_Java_version_in_POM_to_16">Set Java version in POM to 1.6</span></h3>
<p>It is a good idea to tie the project to a specific JDK version, to ensure the build stays the same when a newer JDK version is used. We do this by adding some <a href="http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html">parameters</a> for the <code>maven-compiler-plugin</code> to our pom. Since we are developing for Java EE 6, we will specify version 1.6. Change the <code>&lt;build></code> section to this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myprojectname<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3><span id="Install_Eclipse_Plugins">Install Eclipse Plugins</span></h3>
<p>For JSF development you need the Java EE version of Eclipse, for this guide Eclipse Indigo SR2 was used. After installing Eclipse we need to add three plugins through <strong>Help</strong> -> <strong>Eclipse Marketplace</strong>.</p>
<ul>
<li><strong>Subclipse</strong>: This plugin provides SVN <a href="http://www.tech-juice.org/2012/05/01/how-to-add-a-new-eclipse-project-to-an-svn-repository/">integration</a>.</li>
<li><strong>Maven Integration for Eclipse</strong> (a.k.a. m2eclipse): This plugin adds basic Maven support to Eclipse. Obviously.</li>
<li><strong>Maven Integration for Eclipse WTP</strong> (a.k.a. m2e-wtp): This plugin integrates m2eclipse with the Web Tools Project, which handles Web application specific stuff in Eclipse.</li>
</ul>
<h3><span id="Import_the_Project">Import the Project</span></h3>
<p>Go to <strong>File</strong> -> <strong>Import</strong>, and choose <strong>Existing Maven Projects</strong>. Browse to the directory where the pom.xml is located, and click Finish. The project should now be imported, and initialized.</p>
<p>If we look at the Project Facets (under Project -> Properties) we see that the Java version has been correctly set to 1.6, and the Dynamic Web Module is set to support Servlet 3.0. </p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/ProjectFacets.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/ProjectFacets.png" alt="" title="ProjectFacets" width="962" height="495" class="alignnone size-full wp-image-2071" /></a></p>
<h3><span id="Installing_Glassfish_Support">Installing Glassfish Support</span></h3>
<p>Eclipse does not come with pre-installed Glassfish server support, but fortunately this can be added with a few clicks.</p>
<ol>
<li>Open the Servers view through <strong>Window</strong> -> <strong>Show View</strong> -> <strong>Others</strong>.</li>
<li>Right-click in the Servers view and select <strong>New</strong> -> <strong>Server</strong>.</li>
<li>Click "Download additional server adapters" in the top-right and install the "Oracle Glassfish Server Tools" module.</li>
</ol>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/GlassfishServerTools.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/GlassfishServerTools.png" alt="" title="GlassfishServerTools" width="542" height="494" class="alignnone size-full wp-image-2072" /></a></p>
<h3><span id="Deploying_to_Glassfish">Deploying to Glassfish</span></h3>
<p>Create a new Glassfish server instance through the Servers view. We choose the latest available Glassfish edition, in my case it was Glassfish 3.1.2. Then we are prompted for the installation directory of the server. We can either select the path of an existing Glassfish installation, or choose an empty directory and click "Install Server" to make Eclipse download a fresh installation for us.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/NewGlassfish.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/NewGlassfish.png" alt="" title="NewGlassfish" width="525" height="588" class="alignnone size-full wp-image-2073" /></a></p>
<p>Now we can add a project to the Glassfish server by right-clicking it and choosing <strong>Add and Remove...</strong>:</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/AddRemove.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/AddRemove.png" alt="" title="AddRemove" width="408" height="143" class="alignnone size-full wp-image-2074" /></a></p>
<p>Start the server, and your project will be served by Glassfish!</p>
<h3><span id="Add_Project_to_SVN">Add Project to SVN</span></h3>
<p>As a final step we can upload the project to an SVN repository, so we can work on it with others and keep a history of the code. A detailed description of the process can be found in <a href="http://www.tech-juice.org/2012/05/01/how-to-add-a-new-eclipse-project-to-an-svn-repository/">another post</a> on this blog. Be sure to the add <code>target</code> directory to <code>svn:ignore</code> by rightclicking it and selecting <strong>Team</strong> -> <strong>Add to svn ignore...</strong></p>
<p>In order for other developers to immediately have a working project, we will need to upload <strong>all</strong> other project files, including <code>.project</code>, <code>.classpath</code> and the <code>.settings</code> directory. Of course the other developers will also need to have the same plugins installed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/05/13/hello-world-with-jsf-2-0-glassfish-3-maven-svn-and-eclipse/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How Maven Builds a WAR File</title>
		<link>http://www.tech-juice.org/2012/05/12/how-maven-builds-a-war-file/</link>
		<comments>http://www.tech-juice.org/2012/05/12/how-maven-builds-a-war-file/#comments</comments>
		<pubDate>Sat, 12 May 2012 12:48:18 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[goal]]></category>
		<category><![CDATA[lifecycle]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven 3]]></category>
		<category><![CDATA[phase]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[war]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=2031</guid>
		<description><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#Example_Project">2 Example Project</a></li><li><a href="#Maven_Configuration_Is_Mostly_Implicit">3 Maven Configuration Is Mostly Implicit</a></li><li><a href="#Executed_Lifecycle_Phases">4 Executed Lifecycle Phases</a><ul><li><a href="#1_resourcesresources">4.1 1. resources:resources</a></li><li><a href="#2_compilercompile">4.2 2. compiler:compile</a></li><li><a href="#3_resourcestestResources">4.3 3. resources:testResources</a></li><li><a href="#4_compilertestCompile">4.4 4. compiler:testCompile</a></li><li><a href="#5_surefiretest">4.5 5. surefire:test</a></li><li><a href="#7_warwar">4.6 7. war:war</a></li></ul></li><li><a href="#Dependencies">5 Dependencies</a></li></ul></div>
<span id="Introduction">Introduction</span>
<p>When dealing with a Java Web applications, the completed application is commonly delivered as a <a href="http://www.tech-juice.org/2012/05/10/an-introduction-to-java-web-application-development/#How_Is_a_Java_Web_Application_Distributed">WAR file</a>. The <a href="http://maven.apache.org/">Maven</a> build system can easily be set up to create WAR files. In this post we take an in-depth look at how Maven goes from a source project to the final WAR product.</p>
<p><em>Used software: Apache Maven 3.0.4.</em></p>
<p>The structure of a WAR file looks like this (<a href="http://docstore.mik.ua/orelly/xml/jxslt/ch06_02.htm">source</a>):</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/warfile.gif"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/warfile.gif" alt="" title="warfile" width="485" height="305" class="alignnone size-full wp-image-2006" /></a></p>
<span id="Example_Project">Example Project</span>
<p>Let&#8217;s look at a very simple Mavenized Web application project in a directory named <em>myprojectname</em>. The contents of this folder are:</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/SampleSourceProject.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/SampleSourceProject.png" alt="" title="SampleSourceProject" width="301" height="459" class="alignnone size-full wp-image-2032" /></a></p>
<p>The pom.xml file looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&#34;http://maven.apache.org/POM/4.0.0&#34;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&#34;http://www.w3.org/2001/XMLSchema-instance&#34;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&#34;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&#34;</span><span style="color: #000000; font-weight: bold;">&#62;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;modelVersion<span style="color: #000000; font-weight: bold;">&#62;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;/modelVersion<span style="color: #000000; font-weight: bold;">&#62;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;groupId<span style="color: #000000; font-weight: bold;">&#62;</span></span></span>mygroup.com</pre></div>&#8230;</div>]]></description>
			<content:encoded><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#Example_Project">2 Example Project</a></li><li><a href="#Maven_Configuration_Is_Mostly_Implicit">3 Maven Configuration Is Mostly Implicit</a></li><li><a href="#Executed_Lifecycle_Phases">4 Executed Lifecycle Phases</a><ul><li><a href="#1_resourcesresources">4.1 1. resources:resources</a></li><li><a href="#2_compilercompile">4.2 2. compiler:compile</a></li><li><a href="#3_resourcestestResources">4.3 3. resources:testResources</a></li><li><a href="#4_compilertestCompile">4.4 4. compiler:testCompile</a></li><li><a href="#5_surefiretest">4.5 5. surefire:test</a></li><li><a href="#7_warwar">4.6 7. war:war</a></li></ul></li><li><a href="#Dependencies">5 Dependencies</a></li></ul></div>
<h2><span id="Introduction">Introduction</span></h2>
<p>When dealing with a Java Web applications, the completed application is commonly delivered as a <a href="http://www.tech-juice.org/2012/05/10/an-introduction-to-java-web-application-development/#How_Is_a_Java_Web_Application_Distributed">WAR file</a>. The <a href="http://maven.apache.org/">Maven</a> build system can easily be set up to create WAR files. In this post we take an in-depth look at how Maven goes from a source project to the final WAR product.</p>
<p><em>Used software: Apache Maven 3.0.4.</em></p>
<p>The structure of a WAR file looks like this (<a href="http://docstore.mik.ua/orelly/xml/jxslt/ch06_02.htm">source</a>):</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/warfile.gif"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/warfile.gif" alt="" title="warfile" width="485" height="305" class="alignnone size-full wp-image-2006" /></a></p>
<h2><span id="Example_Project">Example Project</span></h2>
<p>Let&#8217;s look at a very simple Mavenized Web application project in a directory named <em>myprojectname</em>. The contents of this folder are:</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/SampleSourceProject.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/SampleSourceProject.png" alt="" title="SampleSourceProject" width="301" height="459" class="alignnone size-full wp-image-2032" /></a></p>
<p>The pom.xml file looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mygroup.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myprojectname<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myprojectname Maven Webapp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://maven.apache.org<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.8.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myprojectname<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>To package this bare-bones project into a WAR file we use the <code>mvn package</code> command.</p>

<div class="wp_syntax"><div class="code"><pre class="cmd" style="font-family:monospace;">C:\Projects\myprojectname&gt;mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myprojectname Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
...
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ myprojectname ---
[INFO] Packaging webapp
[INFO] Assembling webapp [myprojectname] in [C:\Projects\myprojectname\target\myprojectname]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Projects\myprojectname\src\main\webapp]
[INFO] Webapp assembled in [18 msecs]
[INFO] Building war: C:\Projects\target\myprojectname.war
...</pre></div></div>

<p>This creates a WAR file at <code>/target/myprojectname.war</code>. The execution of this <code>mvn package</code> command is the scenario we will take a closer look at. An overview of the operation is displayed in the image below.</p>
<div id="attachment_2036" class="wp-caption alignnone" style="width: 1003px"><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/MavenBuildSimple1.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/MavenBuildSimple1.png" alt="" title="MavenBuildSimple" width="993" height="211" class="size-full wp-image-2036" /></a><p class="wp-caption-text">Maven is used to build a WAR file from the source project, through the &quot;mvn package&quot; command.</p></div>
<h2><span id="Maven_Configuration_Is_Mostly_Implicit">Maven Configuration Is Mostly Implicit</span></h2>
<p>We know that Maven can succesfully create a WAR file from the source project. But our pom.xml file is almost empty, so how does Maven know things like where the <em>.java</em> files for compilation are, or what the exact structure of the WAR file should be? The answer is that these settings are implicit. Maven uses a &#8220;convention over configuration&#8221; approach, and provides default values for the project&#8217;s configuration.</p>
<p>First, some things are implicit because of the <a href="http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings">default way</a> Maven plugins are bound to the lifecycle phases. For example, the <em>compile</em> phase has the goal <em>compiler:compile</em> assigned to it by default. This means that when executing the <em>compile</em> phase, the <em>compile</em> <a href="http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html">goal</a> of the <em>compiler</em> <a href="http://maven.apache.org/plugins/maven-compiler-plugin/">plugin</a> is called. When you select WAR packaging in your pom.xml, the <a href="http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html">war:war</a> goal (which creates the WAR) is bound to the <em>package</em> phase.</p>
<p>Second, plugins define default values for their parameters that are used when no explicit value is passed. For example, the <em>compiler:compile</em> goal has a parameter called <em>compilerId</em>. This parameter has the value <code>javac</code> <a href="http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerId">by default</a>, meaning the regular JDK compiler will be used. A project&#8217;s pom.xml file can override this default to use a different compiler.</p>
<p>Finally, some settings are contained in the <a href="http://kb.sonatype.org/entries/20900166-what-is-the-maven-super-pom">Super POM</a> that every pom.xml implicitly inherits. For Maven 3 the Super POM is located in <code>maven_dir/lib/maven-model-builder-3.0.3.jar:org/apache/maven/model/pom-4.0.0.xml</code>. In this file we find the default directory locations for source files, resources, test source files, etc:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.basedir}/target<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.build.directory}/classes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.artifactId}-${project.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testOutputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.build.directory}/test-classes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testOutputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.basedir}/src/main/java<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scriptSourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>src/main/scripts<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scriptSourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testSourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.basedir}/src/test/java<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testSourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.basedir}/src/main/resources<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testResources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testResource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.basedir}/src/test/resources<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testResource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testResources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>These mappings define Maven&#8217;s <a href="http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html">Standard Directory Layout</a>, and it is encouraged to keep this default structure in all your Maven projects.</p>
<h2><span id="Executed_Lifecycle_Phases">Executed Lifecycle Phases</span></h2>
<p><em>If you are having trouble understanding this section, go read about Maven lifecycles, phases, goals and plugins <a href="http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html">here</a>.</em></p>
<p>For our project, when <code>mvn package</code> is entered this will execute all <a href="http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html">lifecycle phases</a> that have a goal bound to them in sequence, up to and including the <em>package</em> phase. In our case this means six phases are executed: <code>process-resources</code>, <code>compile</code>, <code>process-test-resources</code>, <code>test-compile</code>, <code>test</code> and <code>package</code>.</p>
<p>Each phase consists of one or more <em>goals</em>. Goals are provided by Maven plugins: a plugin may have one or more goals wherein each goal represents a capability of that plugin. For example, the Compiler plugin has two goals: <code>compiler:compile</code> and <code>compiler:testCompile</code>. </p>
<p>We can use the <code>mvn help:describe -Dcmd=phasename</code> command to list the lifecycle phases along with bound goals for a project. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="cmd" style="font-family:monospace;">C:\Project\myprojectname&gt;mvn help:describe -Dcmd=package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myprojectname Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.1.1:describe (default-cli) @ myprojectname ---
[INFO] 'package' is a phase corresponding to this plugin:
org.apache.maven.plugins:maven-war-plugin:2.1.1:war
&nbsp;
It is a part of the lifecycle for the POM packaging 'war'. This lifecycle includes the following phases:
* validate: Not defined
* initialize: Not defined
* generate-sources: Not defined
* process-sources: Not defined
* generate-resources: Not defined
* process-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:resources
* compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
* process-classes: Not defined
* generate-test-sources: Not defined
* process-test-sources: Not defined
* generate-test-resources: Not defined
* process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:testResources
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile
* process-test-classes: Not defined
* test: org.apache.maven.plugins:maven-surefire-plugin:2.10:test
* prepare-package: Not defined
* package: org.apache.maven.plugins:maven-war-plugin:2.1.1:war
* pre-integration-test: Not defined
* integration-test: Not defined
* post-integration-test: Not defined
* verify: Not defined
* install: org.apache.maven.plugins:maven-install-plugin:2.3.1:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
&nbsp;
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.496s
[INFO] Finished at: Sat May 12 04:30:35 CEST 2012
[INFO] Final Memory: 5M/121M
[INFO] ------------------------------------------------------------------------</pre></div></div>

<p>Next, we look at each goal that is goals executed in the WAR build in more detail.</p>
<h3><span id="1_resourcesresources">1. resources:resources</span></h3>
<p>The <a href="http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html">resources:resources</a> goal copies resources for the main source code to the main output directory.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/ResourcesGoal.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/ResourcesGoal.png" alt="" title="ResourcesGoal" width="800" height="352" class="alignnone size-full wp-image-2029" /></a></p>
<h3><span id="2_compilercompile">2. compiler:compile</span></h3>
<p>The <a href="http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html">compiler:compile</a> goal compiles the application source files.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/CompileGoal.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/CompileGoal.png" alt="" title="CompileGoal" width="800" height="405" class="alignnone size-full wp-image-2039" /></a></p>
<h3><span id="3_resourcestestResources">3. resources:testResources</span></h3>
<p>The <a href="http://maven.apache.org/plugins/maven-resources-plugin/testResources-mojo.html">resources:testResources</a> goal copies resources for the unit test source code to the test output directory.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/TestResourcesGoal.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/TestResourcesGoal.png" alt="" title="TestResourcesGoal" width="800" height="366" class="alignnone size-full wp-image-2040" /></a></p>
<h3><span id="4_compilertestCompile">4. compiler:testCompile</span></h3>
<p>The <a href="http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html">compiler:compile</a> goal compiles the application unit test sources.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/TestCompileGoal.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/TestCompileGoal.png" alt="" title="TestCompileGoal" width="800" height="405" class="alignnone size-full wp-image-2041" /></a></p>
<h3><span id="5_surefiretest">5. surefire:test</span></h3>
<p>The <a href="http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html">surefire:test</a> goal executes the unit tests of an application, on the classes in the <code>/target/test-classes</code> directory. The build stops if a unit test fails. When there are no test classes, the goal is always succesful.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/SurefireTestGoal1.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/SurefireTestGoal1.png" alt="" title="SurefireTestGoal" width="680" height="360" class="alignnone size-full wp-image-2043" /></a></p>
<h3><span id="7_warwar">7. war:war</span></h3>
<p>The <a href="http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html">war:war</a> goal builds a WAR file. It gathers all needed files in the <code>/target/myprojectname/</code> directory, then packages them up into <code>myprojectname.war</code>. One of these steps is copying the contents of <code>/src/main/webapp/</code> to <code>/target/myprojectname/</code>.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/WarGoal1.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/WarGoal1.png" alt="" title="WarGoal1" width="800" height="446" class="alignnone size-full wp-image-2045" /></a></p>
<p>Other important tasks performed by the war plugin are copying the compiled classes are copied to <code>WEB-INF/classes/</code>, and runtime dependencies to <code>WEB-INF/lib/</code>. By default the WAR builder also includes two Maven descriptor files (<a href="http://maven.apache.org/shared/maven-archiver/">details</a>):</p>
<ul>
<li>The pom file, located in the archive in <code>META-INF/maven/${groupId}/${artifactId}/pom.xml</code></li>
<li>A pom.properties file, located in the archive in <code>META-INF/maven/${groupId}/${artifactId}/pom.properties</code>. During the build this file is generated in the <code>/target/maven-archiver/pom.properties</code> directory.<br />
The file looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">#Generated by Maven
#Sat May 12 00:50:42 CEST 2012
version=1.0-SNAPSHOT
groupId=mygroup.com
artifactId=myprojectname</pre></div></div>

</li>
</ul>
<p>Finally, the <code>/target/myprojectname</code> directory is packaged up into a WAR file. In addition the pom file and the pom.properties file are put in the <code>META-INF/maven</code> directory of the archive, and a manifest file is generated at <code>META-INF/MANIFEST.MF</code>.</p>
<div id="attachment_2046" class="wp-caption alignnone" style="width: 810px"><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/WarGoal2.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/WarGoal2.png" alt="" title="WarGoal2" width="800" height="669" class="size-full wp-image-2046" /></a><p class="wp-caption-text">The final step is packaging everything into a WAR file. The bulk of the files comes from the target/myprojectname directory (red), and some additional files are included (green).</p></div>
<p>The final WAR is placed in <code>/target/myprojectname.war</code>. This WAR can now be deployed with a servlet container (e.g., Tomcat) or application server (e.g., Glassfish).</p>
<h2><span id="Dependencies">Dependencies</span></h2>
<p>Currently, our pom.xml only has one dependency (JUnit). Let&#8217;s add another dependency and see what happens when the WAR is built.</p>
<p>We add this dependency to the pom:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>log4j<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>log4j<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.2.16<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When no scope is specified, a dependency has the <strong>compile</strong> scope (<a href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope">read more</a>). The compile scope means that the dependency is available during compilation, testing, and at runtime. </p>
<p>Every dependency that is needed at runtime must be distributed with the application. So when we run <code>mvn package</code> with this pom, Maven downloads the log4j JAR and includes it the <code>/WEB-INF/lib</code> directory in the WAR file.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/DependencyInclude.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/DependencyInclude.png" alt="" title="DependencyInclude" width="332" height="196" class="alignnone size-full wp-image-2051" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/05/12/how-maven-builds-a-war-file/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>An Introduction To Java Web Applications</title>
		<link>http://www.tech-juice.org/2012/05/10/an-introduction-to-java-web-application-development/</link>
		<comments>http://www.tech-juice.org/2012/05/10/an-introduction-to-java-web-application-development/#comments</comments>
		<pubDate>Thu, 10 May 2012 13:12:26 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ear]]></category>
		<category><![CDATA[enterprise edition]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java ee]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[standard edition]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[war]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=1950</guid>
		<description><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#Building_Blocks">2 Building Blocks</a><ul><li><a href="#Servlets">2.1 Servlets</a></li><li><a href="#JSP">2.2 JSP</a></li><li><a href="#Web_Frameworks">2.3 Web Frameworks</a></li></ul></li><li><a href="#Which_Technology_Should_I_Use">3 Which Technology Should I Use?</a></li><li><a href="#What_is_Java_EE">4 What is Java EE?</a><ul><li><a href="#Language_vs_Platform">4.1 Language vs Platform</a></li><li><a href="#Java_SE_vs_Java_EE">4.2 Java SE vs Java EE</a></li></ul></li><li><a href="#Java_Web_Application_Servers">5 Java Web Application Servers</a><ul><li><a href="#Web_Server">5.1 Web Server</a></li><li><a href="#Web_Container">5.2 Web Container</a></li><li><a href="#Application_Server">5.3 Application Server</a></li></ul></li><li><a href="#How_Is_a_Java_Web_Application_Distributed">6 How Is a Java Web Application Distributed?</a><ul><li><a href="#JAR_Java_ARchive">6.1 JAR: Java ARchive</a></li><li><a href="#WAR_Web_application_ARchive">6.2 WAR: Web application ARchive</a></li><li><a href="#EAR_Enterprise_ARchive">6.3 EAR: Enterprise ARchive</a></li></ul></li><li><a href="#Sources">7 Sources</a></li></ul></div>
<span id="Introduction">Introduction</span>
<p>A Web application is an application that is accessed over a network such as the Internet or an intranet. While the earliest websites served only static web pages, dynamic response generation quickly became possible via CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, PHP, or some other server-side technology.</p>
<p>Java has become a popular language for creating dynamic Web applications over the last 15 years, due to the introduction of servlets, JSP, and frameworks such as JSF and Spring. In this post we give an overview of these technologies, and explain the the major differences between them.</p>
<span id="Building_Blocks">Building Blocks</span>
<p>For the sake of simplicity we distinguish three types of Web application building blocks: <strong>servlets</strong>, <strong>JSPs</strong> and&#8230;</p>]]></description>
			<content:encoded><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#Building_Blocks">2 Building Blocks</a><ul><li><a href="#Servlets">2.1 Servlets</a></li><li><a href="#JSP">2.2 JSP</a></li><li><a href="#Web_Frameworks">2.3 Web Frameworks</a></li></ul></li><li><a href="#Which_Technology_Should_I_Use">3 Which Technology Should I Use?</a></li><li><a href="#What_is_Java_EE">4 What is Java EE?</a><ul><li><a href="#Language_vs_Platform">4.1 Language vs Platform</a></li><li><a href="#Java_SE_vs_Java_EE">4.2 Java SE vs Java EE</a></li></ul></li><li><a href="#Java_Web_Application_Servers">5 Java Web Application Servers</a><ul><li><a href="#Web_Server">5.1 Web Server</a></li><li><a href="#Web_Container">5.2 Web Container</a></li><li><a href="#Application_Server">5.3 Application Server</a></li></ul></li><li><a href="#How_Is_a_Java_Web_Application_Distributed">6 How Is a Java Web Application Distributed?</a><ul><li><a href="#JAR_Java_ARchive">6.1 JAR: Java ARchive</a></li><li><a href="#WAR_Web_application_ARchive">6.2 WAR: Web application ARchive</a></li><li><a href="#EAR_Enterprise_ARchive">6.3 EAR: Enterprise ARchive</a></li></ul></li><li><a href="#Sources">7 Sources</a></li></ul></div>
<h2><span id="Introduction">Introduction</span></h2>
<p>A Web application is an application that is accessed over a network such as the Internet or an intranet. While the earliest websites served only static web pages, dynamic response generation quickly became possible via CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, PHP, or some other server-side technology.</p>
<p>Java has become a popular language for creating dynamic Web applications over the last 15 years, due to the introduction of servlets, JSP, and frameworks such as JSF and Spring. In this post we give an overview of these technologies, and explain the the major differences between them.</p>
<h2><span id="Building_Blocks">Building Blocks</span></h2>
<p>For the sake of simplicity we distinguish three types of Web application building blocks: <strong>servlets</strong>, <strong>JSPs</strong> and <strong>frameworks</strong></p>
<h3><span id="Servlets">Servlets</span></h3>
<p>In Java, Web applications consist of <em>servlets</em>. A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP. An example of a servlet that takes a request and returns a page with the numbers 1 to 10 is given below.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet <span style="color: #009900;">&#40;</span>HttpServletRequest req,
                     HttpServletResponse res<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">PrintWriter</span> out <span style="color: #339933;">=</span> res.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;html&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;head&gt;&lt;title&gt;Greeting Page&lt;/title&gt;&lt;/head&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;body&gt;The numbers are:&lt;ul&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
      out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;li&gt;&quot;</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/ul&gt;&lt;/body&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/html&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    out.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3><span id="JSP">JSP</span></h3>
<p>Developing a Web application in plain servlets was a pain: imagine the horror of having hundreds of <code>println()</code> calls with HTML tags or Javascript code spread out over all your classes. This problem was alleviated in 1999 with the release of JavaServer Pages (JSP). With JSP you could write HTML in a JSP file, and the server would automatically create a servlet from the page. JSP also allows you to write blocks of Java code inside the JSP.</p>
<p>An example of a simple JSP page that prints a bullet list with the numbers 1 to 10 is given below.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
The numbers are<span style="color: #339933;">:</span>
<span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">%&gt;</span>
        <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;%=</span> i <span style="color: #339933;">%&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;%</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Unfortunately, having Java inside HTML proved almost as awkward as having HTML inside Java. It could easily create a hard to read mess. It was only years later when the introduction of <a href="http://en.wikipedia.org/wiki/JavaServer_Pages_Standard_Tag_Library">JSTL</a> and <a href="http://en.wikipedia.org/wiki/Unified_Expression_Language">EL</a> alleviated this problem somewhat by providing tags and expressions that could be used in JSP&#8217;s, instead of Java code.</p>
<h3><span id="Web_Frameworks">Web Frameworks</span></h3>
<p>Even with the improvements to JSP, developing with &#8220;plain vanilla&#8221; JSP/servlets can be quite laborious: you&#8217;ll have to bring in a lot of code to control, preprocess, postprocess, gather data, validate, convert, listen, etc the HTTP request and response. The shortcomings of plain servlets and JSP sparked the creation of many <em>Web frameworks</em> that intended to make Web application development easier. These frameworks are built on top of JSP/servlet technology: most frameworks use JSP in some shape or form, and <strong>all frameworks eventually create servlets</strong>.</p>
<p>A key selling point of most frameworks is the separation between the presentation of a page and the underlying logic. This means there is no intermingling of HTML and Java! In addition, many frameworks provide libraries for database access, templating frameworks and session management, and they often promote code reuse. A timeline with the release dates of some well-known Web frameworks is displayed below <sup>1</sup>.</p>
<div id="attachment_1951" class="wp-caption alignnone" style="width: 760px"><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/frameworks-e1336599424615.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/frameworks-e1336599424615.png" alt="" title="frameworks" width="750" height="350" class="size-full wp-image-1951" /></a><p class="wp-caption-text">A timeline with the release dates of some Web frameworks. Most of them are Java frameworks, but some other ones are included.</p></div>
<p>Over the last 10 years plain servlets and JSP have gradually been replaced by Web frameworks as the technology of choice. In 2012, using frameworks has become the standard; using JSP is only an option for very small Web applications. For larger projects frameworks are a must, since they remove the need to reinvent the wheel over and over during implementation. </p>
<p>The interesting parts of a Hello World application for the JSF 2.0 framework look something like this:</p>
<p><strong>Hello.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">hello</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.ManagedBean</span><span style="color: #339933;">;</span>
&nbsp;
@ManagedBean
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hello <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> world <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getworld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> world<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>hello.jsf</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;en&quot;</span>
      xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/1999/xhtml&quot;</span>
      xmlns<span style="color: #339933;">:</span>h<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://java.sun.com/jsf/html&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h<span style="color: #339933;">:</span>head<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Facelets Hello World<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>h<span style="color: #339933;">:</span>head<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h<span style="color: #339933;">:</span>body<span style="color: #339933;">&gt;</span>
        #<span style="color: #009900;">&#123;</span>hello.<span style="color: #006633;">world</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">&lt;/</span>h<span style="color: #339933;">:</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<h2><span id="Which_Technology_Should_I_Use">Which Technology Should I Use?</span></h2>
<p>That&#8217;s a good question. As with all things software, there is no unified answer (another ten posts could be dedicated to <em>trying</em> to give a complete answer). Servlets and JSPs are lightweight, but there are other lightweight technologies to consider, e.g. PHP. Frameworks are more powerful, but if you are building a very small Web application using a full-blown framework may be overkill. It all depends on your demands and personal preference. </p>
<p>If you are considering a framework, you have to choose between an uncountable number of web-frameworks that exists in the Java environment. The bottom line is that most frameworks will <em>do the job</em>, and you can&#8217;t really judge a framework until you have actually <em>tried</em> it. Here are some links to get you started.</p>
<ul>
<li><a href="http://www.slideshare.net/mraible/comparing-jvm-web-frameworks-jfokus-2012">Comparing JVM Web Frameworks</a> (2012)</li>
<li><a href="http://www.kai-waehner.de/blog/2010/12/30/categorization-of-web-frameworks-in-the-java-environment/">Categorization of Web Frameworks in the Java Environment</a> (2012)</li>
<li><a href="http://zeroturnaround.com/java-ee-productivity-report-2011/">Java Productivity Report</a> (2011)</li>
</ul>
<div id="attachment_1980" class="wp-caption alignnone" style="width: 620px"><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/Java-EE.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/Java-EE.png" alt="" title="Java-EE" width="610" height="458" class="size-full wp-image-1980" /></a><p class="wp-caption-text">An overview of which Java EE standards are used the most (source: ZeroTurnaround).</p></div>
<h2><span id="What_is_Java_EE">What is Java EE?</span></h2>
<p>In many documents about Java Web applications you will run into the term Java EE, but what does it mean exactly?</p>
<p>The short answer is that Java EE refers to <a href="http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition">Java Platform Enterprise Edition</a>. The long answer is, well, longer.</p>
<h3><span id="Language_vs_Platform">Language vs Platform</span></h3>
<p>Java technology is both a programming language and a platform. The Java programming language is a high-level object-oriented language that has a particular syntax and style. A Java platform is a particular environment in which Java programming language applications run. Every platform has an <a href="http://en.wikipedia.org/wiki/Application_programming_interface">API</a> that defines things like the classes that are available to the programmer, and which functions are available in each class.</p>
<p>When most people think of the Java programming language, they think of the <a href="http://en.wikipedia.org/wiki/Java_Platform,_Standard_Edition">Java SE</a> API. Java SE&#8217;s API provides the core functionality of the Java programming language. It defines everything from the basic types and objects of the Java programming language to high-level classes that are used for networking, security, database access, graphical user interface (GUI) development, and XML parsing.</p>
<p>So, the Java language defines that you can use an <code>if</code> statement, and that single line comments start with <code>//</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">==</span> y<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// A comment</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the Java SE <a href="http://docs.oracle.com/javase/7/docs/api/">API</a> defines the existence of the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/List.html">List</a>, <a href="http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html">ArrayList</a> and <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html">String</a> objects:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The platform APIs are <strong>just APIs</strong; a program that uses parts of an API must have an actual implementation of the API at runtime. Oracle provides a Java Runtime Environment (JRE) and Java Development Kit (JDK) for every platform. These are the files that are downloaded and installed on a computer in order to run or develop Java programs, respectively. They contain the actual <strong>implementation</strong> of classes defined in the API. There are various different implementations by other parties, e.g. the open source Java SE implementation <a href="http://openjdk.java.net/">OpenJDK</a>.</p>
<h3><span id="Java_SE_vs_Java_EE">Java SE vs Java EE</span></h3>
<p>The editions of the Java platform range from lightweight to heavyweight. For &#8216;normal&#8217; computers two versions are available: <a href="http://en.wikipedia.org/wiki/Java_Platform,_Standard_Edition">Java SE</a> and <a href="http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition">Java EE</a>. The image below gives an overview (<a href="http://www.calsoftlabs.com/whitepapers/wireless-device-programming.html">source</a>).</p>
<div id="attachment_1964" class="wp-caption alignnone" style="width: 427px"><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/java_editions.gif"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/java_editions.gif" alt="" title="java_editions" width="417" height="289" class="size-full wp-image-1964" /></a><p class="wp-caption-text">An overview of Java Platform editions. The Java EE edition is the &#039;largest&#039; of all java versions.</p></div>
<p>Java Platform Enterprise Edition or Java EE is the enterprise Java computing platform. The platform was known as Java 2 Platform, Enterprise Edition or <strong>J2EE</strong> until the name was changed to <strong>Java EE</strong> in version 5. The current version is called Java EE 6. Java EE extends Java SE, providing an API for fault-tolerance, object-relational mapping, distributed and multi-tier architectures, and web services.</p>
<p>Some of the top-level packages (there are 11 in total) defined by the Java EE <a href="http://docs.oracle.com/javaee/6/api/">API</a> are:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">javax.faces.*          This package defines the root of the JavaServer Faces (JSF) API.
javax.servlet.*        The servlet specification defines a set of APIs to service mainly HTTP requests. It includes the JavaServer Pages specification.
javax.ejb.*            The Enterprise JavaBean (EJB) specification defines a set of lightweight APIs that an object container (the EJB container) will support in order to provide transactions (using JTA), remote procedure calls (using RMI or RMI-IIOP), concurrency control, dependency injection and access control for business objects.
javax.persistence.*    This package contains the classes and interfaces that define the contracts between a persistence provider and the managed classes and the clients of the Java Persistence API (JPA).</pre></div></div>

<p>The <em>reference implementation</em> of the Java EE platform is called <a href="http://glassfish.java.net/">Glassfish</a>. This implements the new packages defined by Java EE. There are several <a href="http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition#Certified_application_servers">other implementations</a> that provide a runtime environment for Java EE programs, such as JBoss and WebSphere. Since Java EE runs &#8216;on top&#8217; of Java SE, Java EE programs can use all features of Java SE.</p>
<p>Note that just because Java EE stands for Enterprise Edition, it does not mean that it is used only by enterprises. Java EE is the name given to a set of specifications that are bundled together to form a platform. The moment you need to write a web application, you need to use the Servlet + JSP specifications at a bare minimum, which are both contained in the EE specification. So you can simply <strong>do more stuff</strong> with a Java EE application because it can use the <strong>additional APIs</strong> provided by the Java EE specification.</p>
<h2><span id="Java_Web_Application_Servers">Java Web Application Servers</span></h2>
<p>Understanding the differences between a web server, a servlet container and an application server is important when learning how a Java Web application is presented to its <em>end users</em>. The servlet/application/EJB container terms are a bit ambiguous, but we will try to come to a workable definition. Let&#8217;s take a look. </p>
<h3><span id="Web_Server">Web Server</span></h3>
<p>A Web server handles the HTTP protocol. When the Web server receives an HTTP request, it responds with an HTTP response, such as sending back an HTML page. To process a request, a Web server may respond with a static HTML page or image, send a redirect, or delegate the dynamic response generation to some other program such as CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, or some other server-side technology. Whatever their purpose, such server-side programs generate a response, most often in HTML, for viewing in a Web browser <sup>2</sup>. The Web server itself does not dynamically generate responses, it just handles receiving the request and sending the response.</p>
<p>The most commonly used Web servers are <a href="http://en.wikipedia.org/wiki/Apache_HTTP_Server">Apache</a>, <a href="http://en.wikipedia.org/wiki/Internet_Information_Services">Microsoft IIS</a> and <a href="http://en.wikipedia.org/wiki/Nginx">nginx</a>.</p>
<h3><span id="Web_Container">Web Container</span></h3>
<p>A Web container (or servlet container) is a program that provides an environment for the servlets and JSP to run. Putting it differently we can say that a Web container is combination of a Servlet Engine and a JSP Engine. Most Web containers also include a Web server, so they can run stand-alone.</p>
<p>The Web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights. If an HTTP Request refers to a Web Component (typically a servlet or a JSP) then the request is forwarded to the Web Container and the result of the request is sent back to Web Server, which uses that result to prepare the HTTP Response for the particular HTTP Request. </p>
<p>So a Web container provides a runtime environment for a <strong>subset</strong> of the Java EE specification, usually only the JSP and Servlet parts. This means a programmer could write the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.*</span><span style="color: #339933;">;</span></pre></div></div>

<p>This would work just fine, because the Web container contains an implementation of <code>javax.servlet.*</code>. The use of JSP is also allowed, e.g. using <code>&lt;jsp></code> tags.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>include page<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hello.jsp&quot;</span><span style="color: #339933;">/&gt;</span></pre></div></div>

<p>But you can&#8217;t use other Java EE features such as Enterprise Java Beans (EJBs):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.Remote</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Remote</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Cart <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addItem<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeItem<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Collection</span> getItems<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This <strong>will not work</strong> because the Web container does not know the <code>javax.ejb.*</code> package. When you do need EJBs it may be possible to extend a Web container with additional Java EE implementation parts, but it is a much better idea to use a full-fledged Java EE application server.</p>
<p>The most common Web container are <a href="http://tomcat.apache.org/">Tomcat</a> and <a href="http://jetty.codehaus.org/jetty/">Jetty</a>. Tomcat 7 implements <a href="http://wiki.apache.org/tomcat/Specifications">two specifications</a>: JSP 2.2 and Servlet 3.0.</p>
<h3><span id="Application_Server">Application Server</span></h3>
<p>An application server (a.ka. <em>Java EE server</em> or <em>application container</em>) is a server that provides support for the <strong>entire Java EE</strong> specification. It can run applications that use EJBs, provides transaction support through the <code>javax.transaction.*</code> package, etc. In addition it provides the capabilities of a Web Container as well as of a Web Server. The image below shows a full application server (<a href="http://docs.oracle.com/javaee/5/tutorial/doc/bnabo.html">source</a>).</p>
<div id="attachment_1983" class="wp-caption alignnone" style="width: 571px"><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/overview-serverAndContainers.png"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/overview-serverAndContainers.png" alt="" title="overview-serverAndContainers" width="561" height="376" class="size-full wp-image-1983" /></a><p class="wp-caption-text">An application server provides support for the full Java EE specification.</p></div>
<p>In an application server, programs can happily use the full Java EE specification, e.g. declaring Entity Beans:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.GeneratedValue</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Table</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>  
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bookbank&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Book</span> <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> 
<span style="color: #009900;">&#123;</span>   
  @Id 
  <span style="color: #003399;">Long</span> empid<span style="color: #339933;">;</span>
&nbsp;
  @GeneratedValue 
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
   <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>  
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span>  
  <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>  
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><span id="How_Is_a_Java_Web_Application_Distributed">How Is a Java Web Application Distributed?</span></h2>
<p>As we just learned Java Web applications are run in Web containers or application servers. But how do you present a Web application to the server? The short answer is this:</p>
<p><b>Web applications are packaged in WAR and EAR files. Web containers (e.g. Tomcat) accept WAR files. Application servers (e.g. Glassfish) accept WAR files and EAR files.</b></p>
<p>For a more detailed explanation we have to dig deeper&#8230; </p>
<p>A Java EE application is delivered in a Java Archive (JAR) file, a Web Archive (WAR) file, or an Enterprise Archive (EAR) file. A WAR or EAR file is a standard JAR file with a .war or .ear extension. Using JAR, WAR, and EAR files and modules makes it possible to assemble a number of different Java EE applications using some of the same components<sup>4</sup>. Note that a Java Web application is always a Java EE application, but the opposite is not true. Only Java EE applications packages in WAR and EAR files are web applications.</p>
<h3><span id="JAR_Java_ARchive">JAR: Java ARchive</span></h3>
<p>A JAR (Java ARchive) is an archive file format typically used to aggregate many Java class files and associated metadata and resources (text, images and so on) into one file to distribute application software or libraries on the Java platform.</p>
<ul>
<li>Based on the ZIP format: it can aggregate many files and directories into one aggregate file, possibly compressing them.</li>
<li>A <strong>manifest file</strong> is a specific file contained within a JAR archive. It is used to define information about the JAR.</li>
<li>The manifest file is named <code>MANIFEST.MF</code> and must be located at <code>META-INF/MANIFEST.MF</code> in the JAR.</li>
<li>One of the most common uses of JAR files is to bundle all the .class files, and other required component files of a library into a JAR file and include that JAR file into the CLASSPATH of another Java application which requires the services of that library.</li>
<li>EJB modules contain class files for enterprise beans and an EJB deployment descriptor. EJB modules are packaged as JAR files with a .jar extension.</li>
<li>Application client modules, which contain class files and an application client deployment descriptor. Application client modules are packaged as JAR files with a .jar extension.</li>
</ul>
<h3><span id="WAR_Web_application_ARchive">WAR: Web application ARchive</span></h3>
<p>A WAR file (or Web application ARchive) is a JAR file used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries and static Web pages (HTML and related files) that together constitute a <strong>Web module</strong>.</p>
<ul>
<li>A WAR file contains what is known as a <a href="http://docs.oracle.com/javaee/6/tutorial/doc/bnadx.html">web module</a>. It is the smallest deployable and usable unit of web resources.</li>
<li>Web containers (Tomcat) and application servers (Glassfish) can <strong>both</strong> <deploy and run WAR files.</li>
<li>The top-level directory of a web module is the <em>document root</em> of the application. The document root is where JSP pages, client-side classes and archives, and static web resources, such as images, are stored.</li>
<li>The document root contains a subdirectory named <code>WEB-INF</code>, which can contain the following files and directories:
<ul>
<li><code>classes</code>: A directory that contains server-side classes: servlets, enterprise bean class files, utility classes, and JavaBeans components</li>
<li><code>tags</code>: A directory that contains tag files, which are implementations of tag libraries</li>
<li><code>lib</code>: A directory that contains JAR files that contain enterprise beans, and JAR archives of libraries called by server-side classes</li>
<li><strong>Deployment descriptors</strong>, such as <code>web.xml</code> (the web application deployment descriptor) and <code>ejb-jar.xml</code> (an EJB deployment descriptor)</li>
</ul>
</li>
</ul>
<p>The structure of a WAR file looks like this:<br />
<div id="attachment_1989" class="wp-caption alignnone" style="width: 432px"><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/web-module.gif"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/web-module.gif" alt="" title="web-module" width="422" height="303" class="size-full wp-image-1989" /></a><p class="wp-caption-text">The structure of a WAR file<sup>4</sup>.</p></div></p>
<p>Most real Web applications will use a <strong>web.xml</strong> file. It instructs the servlet container (e.g. Tomcat) on how to deploy and run the Web application. An example web.xml file is:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
 <span style="color: #00bbdd;">&lt;!DOCTYPE web-app</span>
<span style="color: #00bbdd;">     PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN&quot;</span>
<span style="color: #00bbdd;">     &quot;http://java.sun.com/j2ee/dtds/web-app_2_2.dtd&quot;&gt;</span>
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>HelloServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mypackage.HelloServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>HelloServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/HelloServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             Resource reference to a factory for javax.mail.Session
             instances that may be used for sending electronic mail messages,
             preconfigured to connect to the appropriate SMTP server.
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mail/Session<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.mail.Session<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-auth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Container<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-auth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3><span id="EAR_Enterprise_ARchive">EAR: Enterprise ARchive</span></h3>
<p>An EAR (Enterprise ARchive) file contains Java EE modules and, optionally, deployment descriptors. When a large Web application is deployed, all its .jar and .war files are packaged as JAR file with .ear extension and deployed into an application server.</p>
<ul>
<li>In addition to Web modules (WAR files), an EAR file can contain EJB modules (JAR), Application client modules (JAR) and Resource adapter modules (RAR).</li>
<li>Deploying EAR files requires an application server such as Glassfish.</li>
<li>Web containers such as Tomcat <strong>can not</strong> deploy EAR files, only WAR files.</li>
</ul>
<div id="attachment_1994" class="wp-caption alignnone" style="width: 449px"><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/overview-ear.gif"><img src="http://www.tech-juice.org/wp-content/uploads/2012/05/overview-ear.gif" alt="" title="overview-ear" width="439" height="264" class="size-full wp-image-1994" /></a><p class="wp-caption-text">EAR file structure<sup>5</sup>.</p></div>
<h2><span id="Sources">Sources</span></h2>
<ol>
<li><a href="http://www.flickr.com/photos/mraible/4378559350/">History of Web Frameworks</a></li>
<li><a href="http://www.javaworld.com/javaworld/javaqa/2002-08/01-qa-0823-appvswebserver.html">App server, Web server: What&#8217;s the difference?</a></li>
<li><a href="http://docs.oracle.com/javaee/5/tutorial/doc/bnabo.html">Java EE Containers &#8211; The Java EE 5 Tutorial</a></li>
<li><a href="http://docs.oracle.com/javaee/6/tutorial/doc/bnadx.html">Web Modules &#8211; The Java EE 6 Tutorial</a></li>
<li><a href="http://docs.oracle.com/javaee/6/tutorial/doc/bnaby.html">Packaging Applications &#8211; The Java EE 6 Tutorial</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/05/10/an-introduction-to-java-web-application-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Setting Up Git/Github on Ubuntu In Five Minutes</title>
		<link>http://www.tech-juice.org/2012/05/07/setting-up-gitgithub-on-ubuntu-in-five-minutes/</link>
		<comments>http://www.tech-juice.org/2012/05/07/setting-up-gitgithub-on-ubuntu-in-five-minutes/#comments</comments>
		<pubDate>Mon, 07 May 2012 02:05:59 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[ssh-agent]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=1936</guid>
		<description><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#1_Install_Git">1 1: Install Git</a></li><li><a href="#2_Configure_Git">2 2: Configure Git</a></li><li><a href="#3_Generate_Cryptographic_Keys">3 3: Generate Cryptographic Keys</a></li><li><a href="#4_Add_Public_Key_To_Github">4 4: Add Public Key To Github</a></li><li><a href="#5_Tell_SSH_to_Use_The_New_Key">5 5: Tell SSH to Use The New Key</a></li><li><a href="#6_Test_Connection_To_Github">6 6: Test Connection To Github</a></li><li><a href="#7_Clone_a_Repository_And_Start_Coding">7 7: Clone a Repository And Start Coding</a></li></ul></div>
<span id="1_Install_Git">1: Install Git</span>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> git-core git-doc</pre></div></div>

<span id="2_Configure_Git">2: Configure Git</span>
<p>Bare minimum:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> config <span style="color: #660033;">--global</span> user.name <span style="color: #ff0000;">&#34;Firstname Lastname&#34;</span>
<span style="color: #c20cb9; font-weight: bold;">git</span> config <span style="color: #660033;">--global</span> user.email <span style="color: #ff0000;">&#34;myemail@gmail.com&#34;</span></pre></div></div>

<p>Enable colors when outputting to terminal:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> config <span style="color: #660033;">--global</span> color.ui <span style="color: #c20cb9; font-weight: bold;">true</span></pre></div></div>

<p>These settings are saved in <code>~/.gitconfig</code>.</p>
<span id="3_Generate_Cryptographic_Keys">3: Generate Cryptographic Keys</span>
<p>Create an RSA public/private key pair in the <code>~/.ssh/</code> directory. Be sure to use a passphrase.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa <span style="color: #660033;">-C</span> <span style="color: #ff0000;">&#34;myemail@gmail.com&#34;</span> <span style="color: #660033;">-f</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>git_rsa</pre></div></div>

<p>This generates a private key in <code>~/.ssh/git_rsa</code> and the corresponding public key in <code>~/.ssh/git_rsa.pub</code></p>
<span id="4_Add_Public_Key_To_Github">4: Add Public Key To Github</span>
<p>Copy all contents of <code>~/.ssh/git_rsa.pub</code> and <a href="https://github.com/settings/ssh">add</a> it to your Github profile.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cat</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>git_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJCqSW4MyOESvnNL6Xjc9jQ3Vvj+yZSqD+HB5Dcbewc3vKpUISaC4mnUetN2B0Xn5QUOVTwdfDti+N+uMHod5VTeDPN7jNdbA<span style="color: #000000; font-weight: bold;">/</span>b<span style="color: #000000; font-weight: bold;">/</span>Vjo+C+hdJ8tissJDaCSQ29Wluhlogoe<span style="color: #000000; font-weight: bold;">/</span>4H3uSWmwKvMkgnK6DK3rXBKdy</pre></div>&#8230;</div>]]></description>
			<content:encoded><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#1_Install_Git">1 1: Install Git</a></li><li><a href="#2_Configure_Git">2 2: Configure Git</a></li><li><a href="#3_Generate_Cryptographic_Keys">3 3: Generate Cryptographic Keys</a></li><li><a href="#4_Add_Public_Key_To_Github">4 4: Add Public Key To Github</a></li><li><a href="#5_Tell_SSH_to_Use_The_New_Key">5 5: Tell SSH to Use The New Key</a></li><li><a href="#6_Test_Connection_To_Github">6 6: Test Connection To Github</a></li><li><a href="#7_Clone_a_Repository_And_Start_Coding">7 7: Clone a Repository And Start Coding</a></li></ul></div>
<h2><span id="1_Install_Git">1: Install Git</span></h2>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> git-core git-doc</pre></div></div>

<h2><span id="2_Configure_Git">2: Configure Git</span></h2>
<p>Bare minimum:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> config <span style="color: #660033;">--global</span> user.name <span style="color: #ff0000;">&quot;Firstname Lastname&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">git</span> config <span style="color: #660033;">--global</span> user.email <span style="color: #ff0000;">&quot;myemail@gmail.com&quot;</span></pre></div></div>

<p>Enable colors when outputting to terminal:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> config <span style="color: #660033;">--global</span> color.ui <span style="color: #c20cb9; font-weight: bold;">true</span></pre></div></div>

<p>These settings are saved in <code>~/.gitconfig</code>.</p>
<h2><span id="3_Generate_Cryptographic_Keys">3: Generate Cryptographic Keys</span></h2>
<p>Create an RSA public/private key pair in the <code>~/.ssh/</code> directory. Be sure to use a passphrase.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa <span style="color: #660033;">-C</span> <span style="color: #ff0000;">&quot;myemail@gmail.com&quot;</span> <span style="color: #660033;">-f</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>git_rsa</pre></div></div>

<p>This generates a private key in <code>~/.ssh/git_rsa</code> and the corresponding public key in <code>~/.ssh/git_rsa.pub</code></p>
<h2><span id="4_Add_Public_Key_To_Github">4: Add Public Key To Github</span></h2>
<p>Copy all contents of <code>~/.ssh/git_rsa.pub</code> and <a href="https://github.com/settings/ssh">add</a> it to your Github profile.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cat</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>git_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJCqSW4MyOESvnNL6Xjc9jQ3Vvj+yZSqD+HB5Dcbewc3vKpUISaC4mnUetN2B0Xn5QUOVTwdfDti+N+uMHod5VTeDPN7jNdbA<span style="color: #000000; font-weight: bold;">/</span>b<span style="color: #000000; font-weight: bold;">/</span>Vjo+C+hdJ8tissJDaCSQ29Wluhlogoe<span style="color: #000000; font-weight: bold;">/</span>4H3uSWmwKvMkgnK6DK3rXBKdy<span style="color: #000000; font-weight: bold;">/</span>T6Xeb8iFlxK5LLUt1B5nv9wxOGlhLd6ul5VeVGZKYxdwRLRkWE1w+ffEnfhJualOOZrW71up2EYl<span style="color: #000000; font-weight: bold;">/</span>FnmOMCMA7oKRyje+uQ2XSai220MlMwFH<span style="color: #000000; font-weight: bold;">/</span>VSOFskEM9dmilRxtFv27cq1vasiAIloKwFP8uYh42P4m3FPbWMulNXZvA3F4YsDfTBfDzQ3SUt myemail<span style="color: #000000; font-weight: bold;">@</span>gmail.com</pre></div></div>

<h2><span id="5_Tell_SSH_to_Use_The_New_Key">5: Tell SSH to Use The New Key</span></h2>
<p>Git uses the <em>ssh</em> application to connect to Github. By default, <em>ssh</em> only tries to use the private key in <code>~/.ssh/id_rsa</code>. We must tell it to also consider <code>~/.ssh/git_rsa</code> for SSH authentication.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh-add</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>git_rsa</pre></div></div>

<p>This command requires that the <em>ssh-agent</em> daemon is running. If you can&#8217;t use <em>ssh-agent</em>, create the file <code>~/.ssh/config</code> and add:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Host github.com
        User            <span style="color: #c20cb9; font-weight: bold;">git</span>
        IdentityFile    ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>git_rsa</pre></div></div>

<h2><span id="6_Test_Connection_To_Github">6: Test Connection To Github</span></h2>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-T</span> <span style="color: #c20cb9; font-weight: bold;">git</span><span style="color: #000000; font-weight: bold;">@</span>github.com
Hi <span style="color: #000000; font-weight: bold;">&lt;</span>user<span style="color: #000000; font-weight: bold;">&gt;!</span> You<span style="color: #ff0000;">'ve successfully authenticated, but GitHub does not provide shell access.</span></pre></div></div>

<h2><span id="7_Clone_a_Repository_And_Start_Coding">7: Clone a Repository And Start Coding</span></h2>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span><span style="color: #000000; font-weight: bold;">@</span>github.com:username<span style="color: #000000; font-weight: bold;">/</span>myrepo.git</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/05/07/setting-up-gitgithub-on-ubuntu-in-five-minutes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to Add a New Eclipse Project to an SVN Repository</title>
		<link>http://www.tech-juice.org/2012/05/01/how-to-add-a-new-eclipse-project-to-an-svn-repository/</link>
		<comments>http://www.tech-juice.org/2012/05/01/how-to-add-a-new-eclipse-project-to-an-svn-repository/#comments</comments>
		<pubDate>Tue, 01 May 2012 21:11:53 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[commit]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[ignore]]></category>
		<category><![CDATA[subclipse]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[svn:ignore]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=1452</guid>
		<description><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#Installing_Subclipse">2 Installing Subclipse</a></li><li><a href="#Connecting_to_Your_Repository">3 Connecting to Your Repository</a></li><li><a href="#Uploading_a_New_Project_to_the_Repository">4 Uploading a New Project to the Repository</a></li><li><a href="#Ignoring_Files">5 Ignoring Files</a></li><li><a href="#Checking_Out_a_Project_From_the_Repository">6 Checking Out a Project From the Repository</a></li></ul></div>
<span id="Introduction">Introduction</span>
<p>Any serious development effort requires some type of <a href="http://en.wikipedia.org/wiki/Revision_control">version control software</a> such as <a href="http://en.wikipedia.org/wiki/Apache_Subversion">Subversion </a>(SVN). Eclipse has excellent SVN support through the Subclipse plugin. In this document we will see how a new Eclipse project can be checked into an SVN repository, and give some tips for organizing your repository.</p>
<p>If you are to learn one thing from this guide it is that you should think carefully about commiting IDE-specific files.</p>
<span id="Installing_Subclipse">Installing Subclipse</span>
<p>First we install the Subclipse plugin into Eclipse to enable SVN support. This plugin includes the Subversion client, and integrates SVN into the Eclipse interface.</p>
<p><strong>1)</strong> Open the Eclipse Marketplace through Help -Eclipse Marketplace.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/marketplace.png"><img class="alignnone size-full wp-image-1465" title="marketplace" src="http://www.tech-juice.org/wp-content/uploads/2012/05/marketplace.png" alt="" height="361" width="653" /></a></p>
<p><strong>2)</strong> Search for Subclipse, and click Install.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/subclipse.png"><img class="alignnone size-full wp-image-1471" title="subclipse" src="http://www.tech-juice.org/wp-content/uploads/2012/05/subclipse.png" alt="" height="824" width="569" /></a></p>
<p><strong>3)</strong> Select all available Subclipse modules for installation, and accept the license agreement. Choose to restart Eclipse when prompted.</p>
<span id="Connecting_to_Your_Repository">Connecting to Your Repository</span>
<p>With Subclipse installed we can now connect to an SVN repository, check out code, commit changes,&#8230;</p>]]></description>
			<content:encoded><![CDATA[<div id="toc_container" class="no_bullets"><p class="toc_title">Contents</p><ul class="toc_list"><li><a href="#Introduction">1 Introduction</a></li><li><a href="#Installing_Subclipse">2 Installing Subclipse</a></li><li><a href="#Connecting_to_Your_Repository">3 Connecting to Your Repository</a></li><li><a href="#Uploading_a_New_Project_to_the_Repository">4 Uploading a New Project to the Repository</a></li><li><a href="#Ignoring_Files">5 Ignoring Files</a></li><li><a href="#Checking_Out_a_Project_From_the_Repository">6 Checking Out a Project From the Repository</a></li></ul></div>
<h2><span id="Introduction">Introduction</span></h2>
<p>Any serious development effort requires some type of <a href="http://en.wikipedia.org/wiki/Revision_control">version control software</a> such as <a href="http://en.wikipedia.org/wiki/Apache_Subversion">Subversion </a>(SVN). Eclipse has excellent SVN support through the Subclipse plugin. In this document we will see how a new Eclipse project can be checked into an SVN repository, and give some tips for organizing your repository.</p>
<p>If you are to learn one thing from this guide it is that you should think carefully about commiting IDE-specific files.</p>
<h2><span id="Installing_Subclipse">Installing Subclipse</span></h2>
<p>First we install the Subclipse plugin into Eclipse to enable SVN support. This plugin includes the Subversion client, and integrates SVN into the Eclipse interface.</p>
<p><strong>1)</strong> Open the Eclipse Marketplace through Help -> Eclipse Marketplace.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/marketplace.png"><img class="alignnone size-full wp-image-1465" title="marketplace" src="http://www.tech-juice.org/wp-content/uploads/2012/05/marketplace.png" alt="" height="361" width="653" /></a></p>
<p><strong>2)</strong> Search for Subclipse, and click Install.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/subclipse.png"><img class="alignnone size-full wp-image-1471" title="subclipse" src="http://www.tech-juice.org/wp-content/uploads/2012/05/subclipse.png" alt="" height="824" width="569" /></a></p>
<p><strong>3)</strong> Select all available Subclipse modules for installation, and accept the license agreement. Choose to restart Eclipse when prompted.</p>
<h2><span id="Connecting_to_Your_Repository">Connecting to Your Repository</span></h2>
<p>With Subclipse installed we can now connect to an SVN repository, check out code, commit changes, and more of that good stuff. Subclipse provides several new views that will help us with these SVN tasks.</p>
<p><strong>1)</strong> Go to Windows -&gt; Views -&gt; Other to open a list of all available views.  Notice that there is a new  &#8220;SVN&#8221; category. Open the <em>SVN Repositories</em> view from the SVN category.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/showview.png"><img class="alignnone size-full wp-image-1470" title="showview" src="http://www.tech-juice.org/wp-content/uploads/2012/05/showview.png" alt="" height="438" width="311" /></a></p>
<p><strong>2)</strong> Right-click in the <em>SVN Repositories</em> view, and select New -&gt; Repository Location.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/svnrepositories.png"><img class="alignnone size-full wp-image-1462" title="svnrepositories" src="http://www.tech-juice.org/wp-content/uploads/2012/05/svnrepositories.png" alt="" height="201" width="752" /></a></p>
<p><strong>3)</strong> Enter the URL of your repository and click Finish.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/addrepository.png"><img class="alignnone size-full wp-image-1463" title="addrepository" src="http://www.tech-juice.org/wp-content/uploads/2012/05/addrepository.png" alt="" height="433" width="525" /></a></p>
<p>Note that SVN supports multiple protocols for repository access, so the URL can have a <em>http://</em>, <em>https:// </em>or <em>svn://</em> prefix. It&#8217;s a good idea to set up your SVN server to allow only <em>https</em>, since it is the most secure.</p>
<p><strong>4)</strong> Now the repository should be available in the <em>SVN Repositories</em> view, and you can browse its contents. Our repository uses the <a href="http://svnbook.red-bean.com/en/1.7/svn.branchmerge.maint.html#svn.branchmerge.maint.layout">standard organization</a> with three top-level directories.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/browse_repository.png"><img class="alignnone size-full wp-image-1464" title="browse_repository" src="http://www.tech-juice.org/wp-content/uploads/2012/05/browse_repository.png" alt="" height="195" width="1111" /></a></p>
<h2><span id="Uploading_a_New_Project_to_the_Repository">Uploading a New Project to the Repository</span></h2>
<p>We have a project <em>SimpleProject</em> that we want to add to the repository.</p>
<p><strong>1)</strong> Right-click the project and select Team -&gt; Share Project.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/share.png"><img class="alignnone size-full wp-image-1469" title="share" src="http://www.tech-juice.org/wp-content/uploads/2012/05/share.png" alt="" height="743" width="657" /></a></p>
<p><strong>2)</strong> Select <em>SVN</em> as the repository type.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/repo_type.png"><img class="alignnone size-full wp-image-1468" title="repo_type" src="http://www.tech-juice.org/wp-content/uploads/2012/05/repo_type.png" alt="" height="331" width="525" /></a></p>
<p><strong>3)</strong> Select the repository this project should be stored in.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/repo_location.png"><img class="alignnone size-full wp-image-1467" title="repo_location" src="http://www.tech-juice.org/wp-content/uploads/2012/05/repo_location.png" alt="" height="439" width="525" /></a></p>
<p><strong>4)</strong> Select the repository directory where the project should be stored, and click Finish.</p>
<p>The files in a repository are usually organized by project. A common convention is to store each project in a separate subdirectory of the <em>/trunk </em>directory in the repository. We will follow that convention, so the target location of <em>SimpleProject</em> is /trunk/SimpleProject.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/repo_dir.png"><img class="alignnone size-full wp-image-1466" title="repo_dir" src="http://www.tech-juice.org/wp-content/uploads/2012/05/repo_dir.png" alt="" height="439" width="525" /></a></p>
<p><strong>5)</strong> Now the <em>/trunk/SimpleProject</em> directory is created in the repository, and the project is associated with this repository location in Eclipse. You can now commit (upload) your changes by right-clicking and selecting Team -> Commit.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/commit_property.png"><img class="alignnone size-full wp-image-1477" title="commit_property" src="http://www.tech-juice.org/wp-content/uploads/2012/05/commit_property.png" alt="" height="833" width="747" /></a></p>
<h2><span id="Ignoring_Files">Ignoring Files</span></h2>
<p>When committing our simple project we see several Eclipse-specific files:</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/commit_wrong.png"><img class="alignnone size-full wp-image-1478" title="commit_wrong" src="http://www.tech-juice.org/wp-content/uploads/2012/05/commit_wrong.png" alt="" height="300" width="466" /></a></p>
<p>It is good practice to be conservative with uploading IDE-specific files to the repository. You have to <a href="http://stackoverflow.com/questions/445187/to-check-in-or-not-check-in-the-entire-eclipse-project">think carefully</a> about which files to check in. Using the <em>svn:ignore</em> property we can prevent files from being committed.</p>
<p>A common approach is to check in the <code>.classpath</code> and <code>.project</code> files, and to decide about files in the <code>.settings</code> on a per-file basis. For this example, however, we will use <code>svn:ignore</code> property to simply make <em>all</em> Eclipse files invisible to SVN.</p>
<p><strong>1) </strong>Right-click your project and select Team -&gt; Show Properties.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/show_properties.png"><img class="alignnone size-full wp-image-1483" title="show_properties" src="http://www.tech-juice.org/wp-content/uploads/2012/05/show_properties.png" alt="" height="128" width="317" /></a></p>
<p><strong>2) </strong>Right-click in the <em>SVN Properties</em> view and select <em>Add a property</em>.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/add_property.png"><img class="alignnone size-full wp-image-1474" title="add_property" src="http://www.tech-juice.org/wp-content/uploads/2012/05/add_property.png" alt="" height="222" width="567" /></a></p>
<p><strong>3) </strong>Enter <em>svn:ignore </em>as the property name, and enter the names of the files you want to exclude from SVN. For this example we enter<em> .project</em>, <em>.classpath</em> and <em>.settings</em>. Click OK.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/set_property.png"><img class="alignnone size-full wp-image-1482" title="set_property" src="http://www.tech-juice.org/wp-content/uploads/2012/05/set_property.png" alt="" height="520" width="525" /></a></p>
<p><strong>4)</strong> Commit your project using Team -&gt; Commit. The IDE-specific files are no longer listed, so we can safely commit the rest of the project!</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/commit_good.png"><img class="alignnone size-full wp-image-1476" title="commit_good" src="http://www.tech-juice.org/wp-content/uploads/2012/05/commit_good.png" alt="" height="583" width="466" /></a></p>
<p>Two things are important to know:</p>
<ul>
<li>Properties in SVN are attributes of <em>directories</em>.</li>
<li>Properties can be committed and checked out.</li>
</ul>
<p>The property we just created is committed to the repository as an attribute of the <code>/trunk/SimpleProject</code> directory. If someone does a checkout of this project (I will show you how), it will automatically have the correct <code>svn:ignore</code> property.</p>
<p>A fast way of adding files to the <code>svn:ignore</code> property is by right-clicking and selecting Team -> Add to svn:ignore&#8230; This is only possible if the file is not in the repository yet&#8211;the option is greyed out otherwise. In that case you should first delete the file from the repository through the <em>SVN Repositories</em> view.</p>
<h2><span id="Checking_Out_a_Project_From_the_Repository">Checking Out a Project From the Repository</span></h2>
<p>The downside of not storing IDE-specific files is that you need to do some extra work to start working on a project from the repository. Non-default settings, e.g. library classpaths, have to be configured manually after checking out the project.</p>
<p>When your projects start growing big, with lots of dependencies, you should really consider using a build tool such as <a href="http://maven.apache.org/">Maven</a>. You can safely store Maven&#8217;s pom.xml in the repository.</p>
<p>To check out a simple project from an SVN repository you can simple do this:</p>
<p><strong>1) </strong>Go to <em>SVN Repositories</em> view and right-click the project directory. Select Checkout.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/check_project.png"><img class="alignnone size-full wp-image-1475" title="check_project" src="http://www.tech-juice.org/wp-content/uploads/2012/05/check_project.png" alt="" height="441" width="663" /></a></p>
<p><strong>2) </strong>Select the option to check out using the New Project Wizard.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/new_project_wizard.png"><img class="alignnone size-full wp-image-1479" title="new_project_wizard" src="http://www.tech-juice.org/wp-content/uploads/2012/05/new_project_wizard.png" alt="" height="64" width="406" /></a></p>
<p><strong>3) </strong>Select the type of project you want to create, in this case it is just a Java Project.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/project_type.png"><img class="alignnone size-full wp-image-1481" title="project_type" src="http://www.tech-juice.org/wp-content/uploads/2012/05/project_type.png" alt="" height="203" width="533" /></a></p>
<p><strong>4) </strong>Configure the Java Project and click Finish.</p>
<p><a href="http://www.tech-juice.org/wp-content/uploads/2012/05/project_setup.png"><img class="alignnone size-full wp-image-1480" title="project_setup" src="http://www.tech-juice.org/wp-content/uploads/2012/05/project_setup.png" alt="" height="649" width="533" /></a></p>
<p>Now finish setting up the project by adding any included the <code>.jar</code> files to your build path, and you should be good to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/05/01/how-to-add-a-new-eclipse-project-to-an-svn-repository/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Parsing Proteins in the GenBank/GenPept Flat File Format with BioJava 1.8.1</title>
		<link>http://www.tech-juice.org/2012/04/24/parsing-entries-from-a-genbankgenpept-flat-file-with-biojava-1-8-1/</link>
		<comments>http://www.tech-juice.org/2012/04/24/parsing-entries-from-a-genbankgenpept-flat-file-with-biojava-1-8-1/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 11:38:21 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[biojava]]></category>
		<category><![CDATA[biojava 1.8.1]]></category>
		<category><![CDATA[dna]]></category>
		<category><![CDATA[gbff]]></category>
		<category><![CDATA[genbank]]></category>
		<category><![CDATA[genbankformat]]></category>
		<category><![CDATA[genpept]]></category>
		<category><![CDATA[gpff]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[protein]]></category>
		<category><![CDATA[refseq]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[rna]]></category>
		<category><![CDATA[sequence]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=1415</guid>
		<description><![CDATA[<p>This post describes parsing annotated protein sequences from the <a href="http://www.ncbi.nlm.nih.gov/RefSeq/">RefSeq</a> database. I was unable to find any complete examples for parsing RefSeq protein sequences in .gpff.gz files with Java, so here is a quick and dirty one.</p>
<p>The Reference Sequence (RefSeq) collection aims to provide a comprehensive, integrated, non-redundant, well-annotated set of sequences, including genomic DNA, transcripts, and proteins. After downloading the <a href="ftp://ftp.ncbi.nlm.nih.gov/refseq/release/complete/">latest release</a> release from the FTP server, you end up with a lot of <code>.gz</code> files. An example of the filenames:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">complete.1.1.genomic.fna.gz
complete.1.bna.gz
complete.1.genomic.gbff.gz
complete.10.bna.gz
complete.10.genomic.gbff.gz
complete.100.protein.gpff.gz</pre></div></div>

<p>The <a href="ftp://ftp.ncbi.nlm.nih.gov/refseq/release/release-catalog/README">README</a> tells us that the filenames describe the type of information (genomic, protein, dna, rna). This information is split up in many (numbered) files. We are interested in protein information in the <a href="http://www.ncbi.nlm.nih.gov/Sitemap/samplerecord">GenPept/GenBank Flat File format</a>. Every file with protein information in this format has a name of the form <code>complete.&#60;number&#62;.protein.gpff.gz</code>. </p>
<p>Oh, and the regular expression for these filenames is:</p>

<div class="wp_syntax"><div class="code"><pre class="reg" style="font-family:monospace;">^complete.<span style="color: #000000;">&#91;</span>0-9<span style="color: #000000;">&#93;</span>+.protein.gpff.gz$</pre></div></div>

<p>Writing a parser&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>This post describes parsing annotated protein sequences from the <a href="http://www.ncbi.nlm.nih.gov/RefSeq/">RefSeq</a> database. I was unable to find any complete examples for parsing RefSeq protein sequences in .gpff.gz files with Java, so here is a quick and dirty one.</p>
<p>The Reference Sequence (RefSeq) collection aims to provide a comprehensive, integrated, non-redundant, well-annotated set of sequences, including genomic DNA, transcripts, and proteins. After downloading the <a href="ftp://ftp.ncbi.nlm.nih.gov/refseq/release/complete/">latest release</a> release from the FTP server, you end up with a lot of <code>.gz</code> files. An example of the filenames:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">complete.1.1.genomic.fna.gz
complete.1.bna.gz
complete.1.genomic.gbff.gz
complete.10.bna.gz
complete.10.genomic.gbff.gz
complete.100.protein.gpff.gz</pre></div></div>

<p>The <a href="ftp://ftp.ncbi.nlm.nih.gov/refseq/release/release-catalog/README">README</a> tells us that the filenames describe the type of information (genomic, protein, dna, rna). This information is split up in many (numbered) files. We are interested in protein information in the <a href="http://www.ncbi.nlm.nih.gov/Sitemap/samplerecord">GenPept/GenBank Flat File format</a>. Every file with protein information in this format has a name of the form <code>complete.&lt;number&gt;.protein.gpff.gz</code>. </p>
<p>Oh, and the regular expression for these filenames is:</p>

<div class="wp_syntax"><div class="code"><pre class="reg" style="font-family:monospace;">^complete.<span style="color: #000000;">&#91;</span>0-9<span style="color: #000000;">&#93;</span>+.protein.gpff.gz$</pre></div></div>

<p>Writing a parser for these files is a lot of work, but luckily we can use the BioJava framework to do the heavy lifting. We use version 1.8.1, which can be downloaded <a href="http://biojava.org/wiki/BioJava:Download_1.8">here</a>. This example requires three .jar files are included in your Java project: <code>core-1.8.1.jar</code>, <code>bytecode-1.8.1.jar</code> and <code>sequencing-1.8.1.jar</code>.</p>
<p>The code below opens a given .gz file, e.g. <code>complete.1.protein.gpff.gz</code>,  and parses all protein entries:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileNotFoundException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStreamReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.zip.GZIPInputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojava.bio.BioException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojava.bio.seq.ProteinTools</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojava.bio.seq.io.ParseException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojava.bio.seq.io.SymbolTokenization</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojava.bio.symbol.IllegalSymbolException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojavax.Namespace</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojavax.RichObjectFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojavax.SimpleNamespace</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojavax.bio.seq.RichSequence</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojavax.bio.seq.io.GenbankFormat</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojavax.bio.seq.io.SimpleRichSequenceBuilder</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> processFile<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> fileLocation<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Open the .gz file in a BufferedReader</span>
	<span style="color: #003399;">GZIPInputStream</span> gzipInputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">BufferedReader</span> fileIn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">Reader</span> decoder <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">try</span>
	<span style="color: #009900;">&#123;</span>
		gzipInputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GZIPInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>fileLocation<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		decoder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>gzipInputStream, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		fileIn <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span>decoder<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error opening file with GZIPInputStream: &quot;</span> <span style="color: #339933;">+</span> fileLocation, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Initialize the GenBank/GenPept flat file format parser</span>
	GenbankFormat gf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GenbankFormat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	SimpleRichSequenceBuilder listener <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleRichSequenceBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Namespace gbSpace <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Namespace<span style="color: #009900;">&#41;</span> RichObjectFactory.<span style="color: #006633;">getObject</span><span style="color: #009900;">&#40;</span>SimpleNamespace.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span>
			<span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;refseqNS&quot;</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	SymbolTokenization tokenization <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">try</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Tokenizer for the protein sequence. When parsing DNA you can use DNATools.getDNA().getTokenization(&quot;token&quot;)</span>
		tokenization<span style="color: #339933;">=</span> ProteinTools.<span style="color: #006633;">getAlphabet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTokenization</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;token&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>BioException e<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error creating tokenization from ProteinTools().&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Parse all protein entries in the file.</span>
	<span style="color: #000066; font-weight: bold;">boolean</span> hasNext <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>hasNext<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			hasNext <span style="color: #339933;">=</span> gf.<span style="color: #006633;">readRichSequence</span><span style="color: #009900;">&#40;</span>fileIn, tokenization, listener, gbSpace<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			RichSequence entry <span style="color: #339933;">=</span> listener.<span style="color: #006633;">makeRichSequence</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// The protein sequence is now parsed and loaded into 'entry'.</span>
			<span style="color: #666666; font-style: italic;">// Extract all the information you need here...</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span><span style="color: #339933;">|</span> BioException e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error reading RichSequence from RefSeq file.&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>For more documentation on using the BioJava parsers check out <a href="http://biojava.org/wiki/BioJava:BioJavaXDocs#Tools_for_reading.2Fwriting_files">this page</a> on the BioJava wiki.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/04/24/parsing-entries-from-a-genbankgenpept-flat-file-with-biojava-1-8-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing SAXParser Error &#8220;The system cannot find the file specified&#8221; for DTD files</title>
		<link>http://www.tech-juice.org/2012/04/18/fixing-saxparser-error-the-system-cannot-find-the-file-specified-for-dtd-files/</link>
		<comments>http://www.tech-juice.org/2012/04/18/fixing-saxparser-error-the-system-cannot-find-the-file-specified-for-dtd-files/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 14:28:09 +0000</pubDate>
		<dc:creator>tboerman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[dtd]]></category>
		<category><![CDATA[entityresolver]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[resolveentity]]></category>
		<category><![CDATA[saxparser]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.tech-juice.org/?p=1409</guid>
		<description><![CDATA[<p>When parsing an XML file with the SAXParser class, you may run into an error related to a <code>.dtd</code> file that cannot be found.</p>
<p>Example: We are parsing the file <code>D:\homologene\build65\homologene.xml</code>.</p>
<p>The first lines of the XML are:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&#34;1.0&#34;</span><span style="color: #000000; font-weight: bold;">?&#62;</span></span>
<span style="color: #00bbdd;">&#60;!DOCTYPE HG-EntrySet PUBLIC &#34;-//NCBI//HomoloGene/EN&#34; &#34;HomoloGene.dtd&#34;&#62;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;HG-EntrySet<span style="color: #000000; font-weight: bold;">&#62;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;HG-EntrySet_entries<span style="color: #000000; font-weight: bold;">&#62;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;HG-Entry<span style="color: #000000; font-weight: bold;">&#62;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;HG-Entry_hg-id<span style="color: #000000; font-weight: bold;">&#62;</span></span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&#60;/HG-Entry_hg-id<span style="color: #000000; font-weight: bold;">&#62;</span></span></span></pre></div></div>

<p>We see a <em>DOCTYPE</em> declaration that points to a DTD file. DTD stands for Document Type Definition, and it is used to define the format of the XML file. The SAXParser will automatically look for this file in the same directory as the XML file.</p>
<p>When parsing we get the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.io.FileNotFoundException: D:\homologene\build65\HomoloGene.dtd (The system cannot find the file specified)
  at java.io.FileInputStream.open(Native Method)
  at java.io.FileInputStream.&#60;init&#62;(Unknown Source)
  at java.io.FileInputStream.&#60;init&#62;(Unknown Source)
  at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
  at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown</pre></div>&#8230;</div>]]></description>
			<content:encoded><![CDATA[<p>When parsing an XML file with the SAXParser class, you may run into an error related to a <code>.dtd</code> file that cannot be found.</p>
<p>Example: We are parsing the file <code>D:\homologene\build65\homologene.xml</code>.</p>
<p>The first lines of the XML are:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE HG-EntrySet PUBLIC &quot;-//NCBI//HomoloGene/EN&quot; &quot;HomoloGene.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;HG-EntrySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;HG-EntrySet_entries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;HG-Entry<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;HG-Entry_hg-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/HG-Entry_hg-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We see a <em>DOCTYPE</em> declaration that points to a DTD file. DTD stands for Document Type Definition, and it is used to define the format of the XML file. The SAXParser will automatically look for this file in the same directory as the XML file.</p>
<p>When parsing we get the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.io.FileNotFoundException: D:\homologene\build65\HomoloGene.dtd (The system cannot find the file specified)
  at java.io.FileInputStream.open(Native Method)
  at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
  at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
  at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
  at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
  at loader.homologene.HomologeneParser.parseFile(HomologeneParser.java:95)
  at loader.homologene.HomologeneParser.parse(HomologeneParser.java:70)
  at loader.homologene.HomologeneMain.parse(HomologeneMain.java:33)</pre></div></div>

<p>The reason for the error is that <strong>the DTD file does not exist on the filesystem</strong>.</p>
<p>To suppress this error and parse the XML without using the DTD, we override the default resolveEntity() function in the EntityResolver interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ParseTest <span style="color: #000000; font-weight: bold;">implements</span> DefaultHandler
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> parse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    SAXParserFactory saxParserFactory <span style="color: #339933;">=</span> SAXParserFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    SAXParser saxParser <span style="color: #339933;">=</span> saxParserFactory.<span style="color: #006633;">newSAXParser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    saxParser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D:<span style="color: #000099; font-weight: bold;">\\</span>homologene<span style="color: #000099; font-weight: bold;">\\</span>build65<span style="color: #000099; font-weight: bold;">\\</span>homologene.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> InputSource resolveEntity<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> publicId, <span style="color: #003399;">String</span> systemId<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> InputSource<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;xml version='1.0' encoding='UTF-8'&gt;&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// ...other handler functions...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now the file will be parsed without problems. Of course it is better to find and use the DTD file, so use this workaround at your own risk.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tech-juice.org/2012/04/18/fixing-saxparser-error-the-system-cannot-find-the-file-specified-for-dtd-files/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
