Archive

Posts Tagged ‘JPA’

Red5 and JPA with Maven2

May 27th, 2009 7 comments
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’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 not important. It’s very simple. It’s going to be a Hello World 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’s IP address. Accepted IPs are stored in a PostgreSQL database. And, the best thing is that I have already written and published the app, so I’m going to write about a ready product. Let’s get started. My server has already installed:
  • Java 1.6 (Diablo JDK)
  • Maven 2.0.9
  • PostgreSQL 8.3
You have to get the application sources from SVN:  
svn checkout http://jakubiak-red5.googlecode.com/svn/trunk/ jakubiak-red5-read-only
    This way, you get sources of jakubiak-red5 project, which include our example application. jakubiak-red5 project is linked to Red5 sources using svn:externals. This mean, Red5 sources are downloaded together with my project. Now it’s time for the first compilation. Thanks to a Marcello Teodorii’s contribution the application is built without any problems.  
cd jakubiak-red5-read-only/jakubiak-red5-pom/
mvn clean install
    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’s built much faster for second time.) Now, you have to create PostgreSQL a database and a user. As a password type demojpa.  
createuser -h localhost -U pgsql -S -D -R -P demojpa
createdb -h localhost -U pgsql -O demojpa demojpa
    You have to create the database now, because it’s necessary for compiling and testing the application.  
cd ../jakubiak-red5-demo-jpa/
mvn clean install
    You have to wait few minutes. You have just built the application. The result of maven tasks is a war file. You have to deploy the war file on Apache Tomcat. I prefer 6.0.18 version.  
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/
    You make Tom cat do it’s job. Mrrrau :)  
cd apache-tomcat-6.0.18/
./bin/startup.sh
tail -f logs/catalina.out
    In Tomcat log we are looking for a success. It’s time for testing. You have to start a web browser:  
http://ADRES-IP-SERWERA:8080/jakubiak-red5-demo-jpa-war/publisher.swf
    It loads a standard application distributed with Red5 – publisher. In “Location” field type:  
rtmp://ADRES-IP-SERWERA/demojpa
    RTMP is a protocol used by Flash applications. “demojpa” is an accessible name of our application. Press “Connect” button. On the “Settings” tab you have to select a web camera and a microphone. You have to test publishing. It shouldn’t work, because your IP address doesn’t have permission for the application. We can find a warning message in Apache Tomcat logs. You have to put your IP address in the database.  
psql -h localhost -U demojpa
INSERT INTO hostallow VALUES ( '192.168.0.114' );
    Let’s go back to the web browser. Now, you can publish the video and admire the results. Now you have a working application. It’s time for you to learn.  
find . -type f | egrep -v .svn
./src/test/java/eu/jakubiak/red5/demo/jpa/StreamPublishSecurityTest.java
./src/main/java/eu/jakubiak/red5/demo/jpa/entities/HostAllow.java
./src/main/java/eu/jakubiak/red5/demo/jpa/DemojpaApplicationAdapter.java
./src/main/java/eu/jakubiak/red5/demo/jpa/StreamPublishSecurity.java
./src/main/resources/META-INF/persistence.xml
./src/main/resources/red5-services.xml
./src/main/resources/logback.xml
./src/main/resources/red5-core.xml
./src/main/resources/red5-common.xml
./src/main/resources/beanRefContext.xml
./src/main/resources/defaultContext.xml
./src/main/resources/red5-web.xml
./src/main/resources/jdbc.properties
./src/main/webapp/META-INF/MANIFEST.MF
./src/main/webapp/WEB-INF/demojpa-servlet.xml
./src/main/webapp/WEB-INF/web.xml
./src/main/webapp/publisher.swf
./pom.xml
    From the beginning, StreamPublishSecurityTest – is a Java class for testing sensitive code in your application. HostAllow is a JPA entity class defining a table in the database. DemojpaApplicationAdapter defines a starting point for Red5 application, where I have placed a code enabling the security rules. StreamPublishSecurity is a real worker class – it checks the security rules against the database. Next: persistence.xml is a file for JPA, you put entity classes there. red5-services.xml 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. logback.xml is a log system configuration. red5-core.xml, red5-common.xml, beanRefContext.xml and defaultContext.xml are configuration files for Spring beans used by Red5. If you want to improve Red5 this is the place to hack. red5-web.xml is a file where you can change RTMP name of your application – “demojpa”. jdbc.properties is a file with database properties. demojpa-servlet.xml is a file for defining MVC controllers of your web application. web.xml is the most important file for war archive, it defines web application components. We have dealt with publisher.swf earlier. pom.xml is a magic Maven file, which describes how maven has to build the application. Cool, isn’t it?
Categories: Java Tags: , , , ,