<?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>Programmer's testimony &#187; JPA</title>
	<atom:link href="http://en.jakubiak.info/tag/jpa/feed" rel="self" type="application/rss+xml" />
	<link>http://en.jakubiak.info</link>
	<description>My experience with: Java, JEE, Adobe Flex, PHP, Video</description>
	<lastBuildDate>Wed, 25 Nov 2009 19:02:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Red5 and JPA with Maven2</title>
		<link>http://en.jakubiak.info/2009/05/27/red5-and-jpa-with-maven2.html</link>
		<comments>http://en.jakubiak.info/2009/05/27/red5-and-jpa-with-maven2.html#comments</comments>
		<pubDate>Wed, 27 May 2009 14:16:38 +0000</pubDate>
		<dc:creator>Antoni Jakubiak</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Red5]]></category>

		<guid isPermaLink="false">http://en.jakubiak.info/2009/05/27/red5-and-jpa-with-maven2.html</guid>
		<description><![CDATA[This article tells you how to build an application for Red5 server with Maven2. We are going to use Spring, JPA, Hibernate and PostgreSQL database. We aren&#8217;t going to use any IDE, instead we are going to use only Bash commands. I will run it on the FreeBSD server. The task of the application is [...]]]></description>
			<content:encoded><![CDATA[This article tells you how to build an application for <a href="http://osflash.org/red5" onclick="pageTracker._trackPageview('/outgoing/osflash.org/red5?referer=');">Red5 server</a> with <a href="http://maven.apache.org/" onclick="pageTracker._trackPageview('/outgoing/maven.apache.org/?referer=');">Maven2</a>. We are going to use <a href="http://www.springsource.org/" onclick="pageTracker._trackPageview('/outgoing/www.springsource.org/?referer=');">Spring</a>, <a href="http://pl.wikipedia.org/wiki/Java_Persistence_API" onclick="pageTracker._trackPageview('/outgoing/pl.wikipedia.org/wiki/Java_Persistence_API?referer=');">JPA</a>, <a href="https://www.hibernate.org/" onclick="pageTracker._trackPageview('/outgoing/www.hibernate.org/?referer=');">Hibernate</a> and <a href="http://www.postgresql.org/" onclick="pageTracker._trackPageview('/outgoing/www.postgresql.org/?referer=');">PostgreSQL</a> database. We aren&#8217;t going to use any <a href="http://en.wikipedia.org/wiki/Integrated_development_environment" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Integrated_development_environment?referer=');">IDE</a>, instead we are going to use only <a href="http://pl.wikipedia.org/wiki/Bash" onclick="pageTracker._trackPageview('/outgoing/pl.wikipedia.org/wiki/Bash?referer=');">Bash</a> commands. I will run it on the <a href="http://www.freebsd.org/" onclick="pageTracker._trackPageview('/outgoing/www.freebsd.org/?referer=');">FreeBSD</a> server.

The task of the application is not important. It&#8217;s very simple. It&#8217;s going to be a <a href="http://en.wikipedia.org/wiki/Hello_world" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Hello_world?referer=');">Hello World</a> program. The application is going to check if a user has a permission to publish a video on Red5 server. The permission is granted based on sender&#8217;s IP address. Accepted IPs are stored in a PostgreSQL database. And, the best thing is that I have already written and <a href="http://jakubiak-red5.googlecode.com/svn/trunk/jakubiak-red5-demo-jpa/" onclick="pageTracker._trackPageview('/outgoing/jakubiak-red5.googlecode.com/svn/trunk/jakubiak-red5-demo-jpa/?referer=');">published</a> the app, so I&#8217;m going to write about a ready product.

Let&#8217;s get started. My server has already installed:
<ul>
	<li>Java 1.6 (Diablo JDK)</li>
	<li>Maven 2.0.9</li>
	<li>PostgreSQL 8.3</li>
</ul>
You have to get the application sources from SVN:

 
<pre><code>svn checkout http://jakubiak-red5.googlecode.com/svn/trunk/ jakubiak-red5-read-only
</code></pre>
 

 

This way, you get sources of <a href="http://code.google.com/p/jakubiak-red5/source/browse/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/jakubiak-red5/source/browse/?referer=');">jakubiak-red5</a> project, which include our example application. jakubiak-red5 project is linked to Red5 sources using <a href="http://www.jakubiak.eu/2008/12/svnexternals.html" onclick="pageTracker._trackPageview('/outgoing/www.jakubiak.eu/2008/12/svnexternals.html?referer=');">svn:externals</a>. This mean, Red5 sources are downloaded together with my project.

Now it&#8217;s time for the first compilation. Thanks to a <a href="http://code.google.com/u/marcello.teodori/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/u/marcello.teodori/?referer=');">Marcello Teodorii&#8217;s</a> contribution the application is built without any problems.

 
<pre><code>cd jakubiak-red5-read-only/jakubiak-red5-pom/
mvn clean install
</code></pre>
 

 

Now, maven starts getting dependencies for the application from the Internet. It can take some time. After downloading, we have to wait for compilation of about 700 Java classes. At the end, we will see a success message. (As you can see, I have been waiting 16 minutes, however it&#8217;s built much faster for second time.)

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw1.png" alt="" />

Now, you have to create PostgreSQL a database and a user. As a password type <em>demojpa</em>.

 
<pre><code>createuser -h localhost -U pgsql -S -D -R -P demojpa
createdb -h localhost -U pgsql -O demojpa demojpa
</code></pre>
 

 

You have to create the database now, because it&#8217;s necessary for compiling and testing the application.

 
<pre><code>cd ../jakubiak-red5-demo-jpa/
mvn clean install
</code></pre>
 

 

You have to wait few minutes.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw2.png" alt="" />

You have just built the application. The result of maven tasks is a <a href="http://en.wikipedia.org/wiki/WAR_(file_format)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/WAR_file_format?referer=');">war</a> file. You have to deploy the war file on <a href="http://tomcat.apache.org/index.html" onclick="pageTracker._trackPageview('/outgoing/tomcat.apache.org/index.html?referer=');">Apache Tomcat</a>. I prefer 6.0.18 version.

 
<pre><code>cd ../..
wget "http://ftp.tpnet.pl/vol/d1/apache/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz"
tar xvfz apache-tomcat-6.0.18.tar.gz
cp jakubiak-red5-read-only/jakubiak-red5-demo-jpa/target/tomcat/webapps/jakubiak-red5-demo-jpa-war.war apache-tomcat-6.0.18/webapps/
</code></pre>
 

 

You make Tom cat do it&#8217;s job. Mrrrau <img src='http://en.jakubiak.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 

 
<pre><code>cd apache-tomcat-6.0.18/
./bin/startup.sh
tail -f logs/catalina.out
</code></pre>
 

 

In Tomcat log we are looking for a success.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw3.png" alt="" />

It&#8217;s time for testing. You have to start a web browser:

 
<pre><code>http://ADRES-IP-SERWERA:8080/jakubiak-red5-demo-jpa-war/publisher.swf
</code></pre>
 

 

It loads a standard application distributed with Red5 – <em>publisher</em>. In &#8220;Location&#8221; field type:

 
<pre><code>rtmp://ADRES-IP-SERWERA/demojpa
</code></pre>
 

 

<a href="http://en.wikipedia.org/wiki/Real_Time_Messaging_Protocol" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Real_Time_Messaging_Protocol?referer=');">RTMP</a> is a protocol used by Flash applications. &#8220;<em>demojpa&#8221;</em> is an accessible name of our application. Press &#8220;Connect&#8221; button.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw4.png" alt="" />

On the &#8220;Settings&#8221; tab you have to select a web camera and a microphone. You have to test publishing. It shouldn&#8217;t work, because your IP address doesn&#8217;t have permission for the application.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw5.png" alt="" />

We can find a warning message in Apache Tomcat logs.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw6.png" alt="" />

You have to put your IP address in the database.

 
<pre><code>psql -h localhost -U demojpa
INSERT INTO hostallow VALUES ( '192.168.0.114' );
</code></pre>
 

 

Let&#8217;s go back to the web browser. Now, you can publish the video and admire the results.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw7.png" alt="" />

Now you have a working application. It&#8217;s time for you to learn.

 
<pre><code>find . -type f | egrep -v .svn
./src/test/java/eu/jakubiak/red5/demo/jpa/<strong>StreamPublishSecurityTest</strong>.java
./src/main/java/eu/jakubiak/red5/demo/jpa/entities/<strong>HostAllow</strong>.java
./src/main/java/eu/jakubiak/red5/demo/jpa/<strong>DemojpaApplicationAdapter</strong>.java
./src/main/java/eu/jakubiak/red5/demo/jpa/<strong>StreamPublishSecurity</strong>.java
./src/main/resources/META-INF/<strong>persistence.xml</strong>
./src/main/resources/<strong>red5-services.xml</strong>
./src/main/resources/<strong>logback.xml</strong>
./src/main/resources/<strong>red5-core.xml</strong>
./src/main/resources/<strong>red5-common.xml</strong>
./src/main/resources/<strong>beanRefContext.xml</strong>
./src/main/resources/<strong>defaultContext.xml</strong>
./src/main/resources/<strong>red5-web.xml</strong>
./src/main/resources/<strong>jdbc.properties</strong>
./src/main/webapp/META-INF/MANIFEST.MF
./src/main/webapp/WEB-INF/<strong>demojpa-servlet.xml</strong>
./src/main/webapp/WEB-INF/<strong>web.xml</strong>
./src/main/webapp/<strong>publisher.swf</strong>
./<strong>pom.xml</strong>
</code></pre>
 

 

From the beginning, <strong>StreamPublishSecurityTest</strong> – is a Java class for testing sensitive code in your application. <strong>HostAllow</strong> is a JPA entity class defining a table in the database. <strong>DemojpaApplicationAdapter</strong> defines a starting point for Red5 application, where I have placed a code enabling the security rules. <strong>StreamPublishSecurity</strong> is a real worker class – it checks the security rules against the database.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw8.png" alt="" />

Next: <strong>persistence.xml</strong> is a file for JPA, you put entity classes there. <strong>red5-services.xml</strong> is a very important file, where we you configure Spring beans used by your application. There are also definitions of the database connection. Also, you can put Red5 remote services there.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw9.png" alt="" />

<strong>logback.xml</strong> is a log system configuration. <strong>red5-core.xml</strong>, <strong>red5-common.xml</strong>, <strong>beanRefContext.xml</strong> and <strong>defaultContext.xml</strong> are configuration files for Spring beans used by Red5. If you want to improve Red5 this is the place to hack. <strong>red5-web.xml</strong> is a file where you can change RTMP name of your application &#8211; &#8220;demojpa&#8221;.

<img src="http://en.jakubiak.info/wp-content/uploads/2009/05/052709-1416-red5andjpaw10.png" alt="" />

<strong>jdbc.properties</strong> is a file with database properties. <strong>demojpa-servlet.xml</strong> is a file for defining MVC controllers of your web application. <strong>web.xml</strong> is the most important file for war archive, it defines web application components. We have dealt with <strong>publisher.swf </strong>earlier. <strong>pom.xml</strong> is a magic Maven file, which describes how maven has to build the application.

Cool, isn&#8217;t it?]]></content:encoded>
			<wfw:commentRss>http://en.jakubiak.info/2009/05/27/red5-and-jpa-with-maven2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
