Recent Posts

Programming
30 May 2012 0 Comments

Using @DataSourceDefinition in Java EE 6 With Postgresql

Introduction

Java EE applications use DataSource objects when they access relational databases through the JDBC API. Typically the Datasource that should be used is referenced by its Java Naming and Directory Interface (JNDI) name in the persistence.xml file:

>java:app/env/myDatasource>

The Datasource 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. glassfish-web.xml or XML configuration files in the application server directory.

In Java EE 6 the @DataSourceDefinition annotation was introduced. With this annotation you can declare ‘application server agnostic’ 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’t have to change the configuration of the application server. You can just deploy the WAR in any application server and it will run.

In this example we run through the steps for…

Tags: datasourcedefinition, entitymanager,
Programming
28 May 2012 0 Comments

Adding JPA Support to a Maven/Eclipse/JSF2 Project

Introduction

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 existing Maven/Eclipse/JSF2 project with Java EE 6.

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):

Name.java

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Column;
 
@Entity
@Table(name="CUSTOMER_INFORMATION")
public class Customer {
    private String name;
 
    @Id
    @Column(name="FULL_NAME")
    public int getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name= name;
    }
}

And a client class that fetches all customer names with and prints them:
Client.java

import javax.persistence.Persistence;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
 
public class Client

Programming
20 May 2012 32 Comments

How to play UStream channels in VLC

Introduction

Ustream.tv doesn’t support opening streams in VLC by default, but it is possible with some effort. Most video streaming sites use the Real Time Messaging Protocol (RTMP) for transmitting video data to your flash player. Using rtmpdump we can connect to RTMP streams, and record them or play them in VLC.

1: Install rtmpdump

On Ubuntu:

$ sudo apt-get install rtmpdump

Binaries for all platforms: here.

2: Find the RTMPDump Parameters

It’s not trivial to find out the correct parameters for rtmpdump, so use this Python script:

#!/usr/bin/env python
# This script finds the rtmpdump command syntax for opening a UStream stream.
 
import sys
import urllib2
import re
 
 
def getVideoData(url):
    # Get the HTML contents
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    html = response.read()
 
    # Extract the channel ID from the HTML
    channelId = None
    m = re.search("Channel\sID\:\s+(\d+)", html)
    if (m):
        channelId = m.group(1)
 
    # Extract the channel title from the HTML
    channelTitle = None
    m = re.search("property\=\"og\:url\"

Programming
13 May 2012 12 Comments

Hello World with JSF 2.0, Glassfish 3, Maven, SVN and Eclipse

Introduction

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.

In this post we’ll take a look at setting up a Web application project from start to finish. The software we will use is:

What Are We Building?

Different applications come in different forms.

  • A Windows application written in C++ may be compiled to a .exe file.
  • Runnable Java GUI applications often come in a JAR (Java ARchive).
  • Java Web applications are packaged in WAR and EAR files.

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, web.xml that describes how the application server should run the WAR.

The structure of a WAR file looks like this (source):

Project Setup with Maven Archetypes

Now that we know what we are building, it is time to start setting up the project. The directory/file structure…

Programming
12 May 2012 4 Comments

How Maven Builds a WAR File

Introduction

When dealing with a Java Web applications, the completed application is commonly delivered as a WAR file. The Maven 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.

Used software: Apache Maven 3.0.4.

The structure of a WAR file looks like this (source):

Example Project

Let’s look at a very simple Mavenized Web application project in a directory named myprojectname. The contents of this folder are:

The pom.xml file looks like this:

 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  >4.0.0>
  >mygroup.com>
  >myprojectname>
  >war>
  >1.0-SNAPSHOT>
  >myprojectname Maven Webapp>
  >http://maven.apache.org

General
10 May 2012 2 Comments

An Introduction To Java Web Applications

Introduction

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.

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.

Building Blocks

For the sake of simplicity we distinguish three types of Web application building blocks: servlets, JSPs and frameworks

Servlets

In Java, Web applications consist of servlets. 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.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public

Tags: ear, enterprise edition, framework, , jar, , , , jsp, servlet, standard edition, tomcat,
Programming
7 May 2012 2 Comments

Setting Up Git/Github on Ubuntu In Five Minutes

1: Install Git

sudo apt-get install git-core git-doc

2: Configure Git

Bare minimum:

git config --global user.name "Firstname Lastname"
git config --global user.email ""

Enable colors when outputting to terminal:

git config --global color.ui true

These settings are saved in ~/.gitconfig.

3: Generate Cryptographic Keys

Create an RSA public/private key pair in the ~/.ssh/ directory. Be sure to use a passphrase.

ssh-keygen -t rsa -C "" -f ~/.ssh/git_rsa

This generates a private key in ~/.ssh/git_rsa and the corresponding public key in ~/.ssh/git_rsa.pub

4: Add Public Key To Github

Copy all contents of ~/.ssh/git_rsa.pub and add it to your Github profile.

$ cat ~/.ssh/git_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJCqSW4MyOESvnNL6Xjc9jQ3Vvj+yZSqD+HB5Dcbewc3vKpUISaC4mnUetN2B0Xn5QUOVTwdfDti+N+uMHod5VTeDPN7jNdbA/b/Vjo+C+hdJ8tissJDaCSQ29Wluhlogoe/4H3uSWmwKvMkgnK6DK3rXBKdy/T6Xeb8iFlxK5LLUt1B5nv9wxOGlhLd6ul5VeVGZKYxdwRLRkWE1w+ffEnfhJualOOZrW71up2EYl/FnmOMCMA7oKRyje+uQ2XSai220MlMwFH/VSOFskEM9dmilRxtFv27cq1vasiAIloKwFP8uYh42P4m3FPbWMulNXZvA3F4YsDfTBfDzQ3SUt

Programming
1 May 2012 2 Comments

How to Add a New Eclipse Project to an SVN Repository

Introduction

Any serious development effort requires some type of version control software such as Subversion (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.

If you are to learn one thing from this guide it is that you should think carefully about commiting IDE-specific files.

Installing Subclipse

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.

1) Open the Eclipse Marketplace through Help -Eclipse Marketplace.

2) Search for Subclipse, and click Install.

3) Select all available Subclipse modules for installation, and accept the license agreement. Choose to restart Eclipse when prompted.

Connecting to Your Repository

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.

1) Go to Windows -> Views -> Other to open a list of all available views. …

Programming
24 April 2012 0 Comments

Parsing Proteins in the GenBank/GenPept Flat File Format with BioJava 1.8.1

This post describes parsing annotated protein sequences from the RefSeq 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.

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 latest release release from the FTP server, you end up with a lot of .gz files. An example of the filenames:

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

The README 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 GenPept/GenBank Flat File format. Every file with protein information in this format has a name of the form complete..protein.gpff.gz.

Oh, and the regular expression for these filenames is:

^complete.[0-9]+.protein.gpff.gz$

Writing a parser…

Tags: biojava, biojava 1.8.1, dna, gbff, genbank, genbankformat, genpept, gpff, gzip, , parsing, protein, refseq, release, rna, sequence
Programming
18 April 2012 0 Comments

Fixing SAXParser Error “The system cannot find the file specified” for DTD files

When parsing an XML file with the SAXParser class, you may run into an error related to a .dtd file that cannot be found.

Example: We are parsing the file D:\homologene\build65\homologene.xml.

The first lines of the XML are:

 version="1.0"?>

>
  >
    >
      >3>

We see a DOCTYPE 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.

When parsing we get the following error:

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.(Unknown Source)
  at java.io.FileInputStream.(Unknown Source)
  at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
  at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown