Eclipse with Ubuntu 9.10 (karmic)
I had some problems running eclipse 3.5 with Ubuntu 9.10 (Gnome 2.28.0 and GTK+ 2.18). When I clicked on an “open window” button nothing happens.
So after some googling I found that the problem is related with gtk. In version 2.18, GDK has been changed to use client-side windows.
I added the following start script for eclipse and the problem seems to be gone.
#!/bin/bash GDK_NATIVE_WINDOWS=1 /home/mr/bin/eclipse-3.5/eclipse
Source:
http://library.gnome.org/devel/gtk/2.18/gtk-migrating-ClientSideWindows.html
https://bugs.launchpad.net/ubuntu/+source/gtk+2.0/+bug/442078 9.10
Upcoming Reveiw Apache Maven 2 Effective Implementation
Recently I received a E-mail from Packt Publishing. They asked me to do a Review about the book Apache Maven 2 Effective Implementation. Packt Publishing was so kind to provided me with a complimentary copy which I will be reviewing very soon.

So I’m looking forward to read the book and learn some new stuff about maven, continuum and archiva.
More readable and typeable…
Hamcrest allows you to write readable constraints, which for example can be used with an assert statement. Hamcrest is included since JUnit 4.4 and it is the first time that third-party classes have been included in JUnit. But when you play around with Hamcrest and the assertThat statement you don’t want go back to old complex and unreadable assert statements.
JUnit and assertThat
With Hamcrest it’s very easy to write readable assert statements in a JUnit test case . In the following example we want to check if a person is older than 21 years.
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
...
Person person1 = new Person();
person1.setName("Bob");
person1.setAge(22);
assertThat(person1.getAge(), greaterThan(21));
As you can see the assert statement is pretty straightforward. The second parameter of an assertThat statement is a Matcher which evaluates if the first parameter is greater than the integer value from the Matcher. This syntax allows you to think in terms of subject, verb, object and not in assertEquals.
Hamcrest uses Matcher which are responsible to evaluate a given statement. A good overview about Hamcrest is the official tutorial.
JUnit currently ships with a few matchers, defined in org.hamcrest.CoreMatchers and org.junit.matchers.JUnitMatchers. To use the other matchers download the hamcrest-all.*.jar library.
hamcrest-collections
hamcrest-collections implements features such as select, reject, map, reduce and zip which can be used on collections. This is a useful library which uses Hamcrest to operate on collections.
The following example show how to select from a List of integers all numbers between 5 and 9.
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrestcollections.Selector.*;
...
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i <= 10; i++) {
numbers.add(i);
}
Iterable<Integer> items = select(numbers, allOf(greaterThan(5), lessThan(9)));
System.out.println(items);
Which genearte the following output:
[6, 7, 8]
Further reading...
A good starting point as already mention is the official Hamcrest Homepage.
JYaml – YAML for Java
“YAML Ain’t Markup Language” (abbreviated YAML) is a data serialization language designed to be human-friendly according to the official Yaml homepage. One implementation for the Java language is the JYaml framework.
There are a lot of other YAML frameworks for different languages. You find a list of some frameworks on the official Yaml homepage.
Short Introduction to YAML
In YAML there are different kinds of nodes:
- Scalar is a value that can be presented as a series of zero or more Unicode characters.
- Sequence is an ordered series of zero or more nodes.
- Mapping is an unordered set of key: value node pairs, with the restriction that each of the keys is unique.
YAML uses three dashes (“- – -”) to show that a document begins. A comment is denoted by a #. In the following sections we now have a look at the YAML constructs sequences, mappings, tags, anchors and aliases.
Sequence
A sequence is a series of nodes, each denoted by a leading “-” indicator.
- Memoirs Found in a Bathtub - Snow Crash - Ghost World
Mapping
A mapping represents a key: value pair.
Stanislaw Lem: Memoirs Found in a Bathtub Neal Stephenson: Snowcrash Daniel Clowes: Ghost World
Tag
YAML represents type information of native data structures with a simple identifier, called a tag (!). The tag serves to restrict the set of possible values the content can have. In this example from JYaml the tag specifies from which type of class the node is.
!my.app.yaml.model.Book
Anchors and Aliases
In the representation graph, a node may appear in more than one collection. An anchor is denoted by the “&” indicator and an alias node by the “*” indicator. The alias refers to the most recent preceding node having the same anchor.
- !my.app.yaml.model.Book
authors:
- &3 !my.app.yaml.model.Author
firstName: Gary
lastName: Michaels
- !my.app.yaml.model.Book
authors:
- !my.app.yaml.model.Author
firstName: Bob
lastName: Jackson
- *3
You find more detail about the YAML standard in the YAML specification.
Note: Some of the examples are from the homepage Yaml In Five Minutes.
JYaml
When you want to serialize and deserialize Java classes with JYaml then all these classes have to obey the following Java beans naming convention.
- The class must have a public default constructor.
- Set/get methods for properties
JYaml currently supports the serialization and deserialization of the following types of Java objects:
- Primitives and respective wrapper classes
- Collection (List and Set)
- Maps
- Arrays
- BigInteger and BigDecimal
- Date
- Custom Java Objects by implementing ObjectsWrappers yourself
JYaml is also in the maven repository. You have just to add the following lines in your pom.xml file.
<dependency> <groupId>org.jyaml</groupId> <artifactId>jyaml</artifactId> <version>1.3</version> </dependency>
Data structure
So we want to serialize and deserialize our data structure which are books and authors. The following line of codes shows you the simple data structure as Java code.
public class Book {
private String isbn;
private String title;
private int pages;
private List<Author> authors = new ArrayList<Author>();
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public List<Author> getAuthors() {
return authors;
}
public void setAuthors(List<Author> authors) {
this.authors = authors;
}
}
public class Author {
private String firstName;
private String lastName;
private Date dateOfBirth;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
Serialize
The serialize step is quite simple all work is done with the static methods from the org.ho.yaml.Yaml class.
List<Book> books = new ArrayList<Book>();
Book book1 = new Book();
book1.setIsbn("123-x");
book1.setTitle("YAML in Action");
book1.setPages(330);
Author author1 = new Author();
author1.setDateOfBirth(new Date());
author1.setFirstName("Gary");
author1.setLastName("Michaels");
book1.getAuthors().add(author1);
books.add(book1);
Book book2 = new Book();
book2.setIsbn("897-x");
book2.setTitle("JYAML for dummies");
book2.setPages(230);
Author author2 = new Author();
author2.setDateOfBirth(new Date());
author2.setFirstName("Bob");
author2.setLastName("Jackson");
book2.getAuthors().add(author2);
book2.getAuthors().add(author1);
books.add(book2);
// write to file
Yaml.dump(books, new File("books.yml"));
The output after the serialization process looks as follow:
---
- !my.app.yaml.model.Book
authors:
- &3 !my.app.yaml.model.Author
dateOfBirth: !java.util.Date "1234212351636"
firstName: Gary
lastName: Michaels
isbn: 123-x
pages: 330
title: YAML in Action
- !my.app.yaml.model.Book
authors:
- !my.app.yaml.model.Author
dateOfBirth: !java.util.Date "1234212351636"
firstName: Bob
lastName: Jackson
- *3
isbn: 897-x
pages: 230
title: JYAML for dummies
Deserialize
To obtain the Java objects from a YAML file you have just to add the following line of code:
List<Book> out = Yaml.loadType(new File("books.yml"), ArrayList.class);
.Net (C#) and SOAP with Attachments
In our project we offer a web service to our customers and to simplify the use of the web service we have implemented web service clients as examples in different frameworks. So we have the usual suspects for Java (JAX-WS, Apache CXF and Axis).
I was asked if I’m interested in program a web service client for the .Net framework. I had already some little experience with C# and ASP.Net and so I thought it would be nice to see what .Net has to offer.
So first the goal was to implement a web service client for a contract first document / literal / wrapped web service which is WS-I Attachments Profile conform.
The big problem is that .Net does not support SwA (SOAP Messages with Attachments). There are three approaches which could work for you when you have the same problem.
- Use the open source library PocketSOAP
- Use the commercial library from Alotsoft.com
- Create your own web service stub
In this post I will show you how you could send a SOAP request manually, receive the SOAP response and parse the MIME attachments with the open source library SharpMimeTools.
So keep in mind that I’m a Java developer and not a C# developer. I also wrote these pieces of code only to show that there is also a simple way to receive a SOAP response with MIME attachments when your were forced to write a web service stub manually in C#.
Data contract
So first we create our Data contract with the xsd.exe. After that we can serialize or deserialize XML with the System.Xml.XMLSerializer from the .Net framework. You will find a reference to a XSD schema in your WSDL file from your web service for which you want to create a web service client.
Xsd.exe data_contract.xsd /c /o:c:\out
Send SOAP request
With System.Net.HttpWebRequest you can send a SOAP request manually to your endpoint. We serialize our XML with the XMLSerializer and add the SOAP Envelope, SOAP Header and SOAP Body.
The followng example show how you could use the XMLSerializer and the HttpWebRequest to create a SOAP request and send it to a web service endpoint.
FileStream fs = new FileStream(“C:\data.xml”, FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(MyData));
MyData myData = (MyData) serializerReq.Deserialize(fs);
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter,
writerSettings))
{
serializerReq.Serialize(xmlWriter, myData);
}
string xmlText = stringWriter.ToString();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = @"text/xml;charset=""utf-8""";
req.Accept = "text.xml";
req.Method = "POST";
// create soap message (with SOAP Header, Body and Envelope)
String msg = START_MSG + xmlText + END_MSG;
byte[] reqBytes = System.Text.Encoding.UTF8.GetBytes(msg);
req.ContentLength = reqBytes.Length;
Stream reqStream = req.GetRequestStream();
// send soap request
reqStream.Write(reqBytes, 0, reqBytes.Length); reqStream.Close();
reqStream.Close();
Receive SOAP response
We will use the SharpMimeTools to parse MIME attachments. So for that we have to make our response stream MIME multipart conform.
The example show how you can create a stream which can be parsed from the SharpMimeTool library. You have to copy the HTTP headers Date, Content-Type, Content-Length and the content of the HttpWebReponse stream to a memory stream. The new memeory stream can then be parsed by the SharpMimeTool library.
HttpWebResponse res = …;
MemoryStream httpStream = …;
MemoryStream mimeStream = …;
Encoding utf8 = Encoding.UTF8;
TextReader readerReq = new StreamReader(httpStream, utf8);
TextWriter writer = new StreamWriter(mimeStream, utf8);
// create a correct mime stream form the HttpResponseStream
// add http headers which were required for a correct mime
// format
writer.WriteLine("Date: " + res.GetResponseHeader("Date"));
writer.WriteLine("Content-Type: " + res.ContentType);
writer.WriteLine("Content-Length: " + res.ContentLength);
writer.WriteLine(Environment.NewLine);
// copy rest of the stream
while (true)
{
string line = readerReq.ReadLine();
if (line == null)
{
break;
}
writer.WriteLine(line);
}
writer.Flush();
mimeStream.Position = 0;
// parse the stream for mime attachments
SharpMessage message = new SharpMessage(mimeStream,
SharpDecodeOptions.Default | SharpDecodeOptions.DecodeTnef |
SharpDecodeOptions.UuDecode);
Parse MIME attachments from the SOAP response
With the SharpMessage from the SharpMimeTools we can access the SOAP response and also the MIME attachments.
SharpMessage message = …;
if (message.Attachments != null)
{
foreach (attachment in message.Attachments)
{
Stream stream = attachment.Stream;
…
}
Finally…
To access the soap payload you could retrieve it with XPath and then deserialize it with the XMLSerializer.
… I hope this post and line of codes gave you a hint how you could parse a SOAP response with MIME attachments manually in C#.
I wanted to buy an apache wicket book but I got stuck on stripes
So today I wanted to buy an apache wicket book, because I try to create a simple example which shows how you could use apache activemq as embedded JMS broker in a web application with spring and hibernate to process data asynchronously.
I have already written the whole business layer (spring, hibernate and JMS) but for a simple demonstration I need a web ui framework.
I didn’t want to write the ui in JSF, so I thought this is a good way to learn Apache wicket. I went to my locale bookstore but they didn’t had the “wicket in action” book.
But an other book got my attention. Stripes …and Java Web Development is fun again.
Stripes is an open source web application framework based on the model-view-controller pattern. It aims to be a more lightweight framework than Struts by using Java technologies such as annotations and generics that were introduced in Java 1.5, to achieve “convention over configuration”. [Wikipedia]
As fare as I can tell Stripes looks quite interesting. I’m not yet finished with reading, but I must say after few chapters I already have a glue how to implement the ui layer. The book covers also a chapter how you could integrate hibernate and spring annotations.
So I’m looking forward to see how stripes fits in with spring and hibernate.
Thanks Head First Servlet and JSP
…. I am now a Sun Certified Web Component Developer (SCWCD). The Oreilly Book Head First Servlet and JSP gives you a quick overview about what you should know. One advantages of the Book is it’s very “brain friendly”. But I got some tough questions on the exam which were not covered in the Book.
When you want a Book which covers almost everything and when your are interested in a Book which you can keep after the exam as reference book the I recommend you the Book from David Bridgewater.
Also a good source to prepare yourself on a Sun certificate is the javaranch webiste.
I also got asked why should I take the exam. One statement I mostly heard was they only test you on “old technologies” like JSP and Servlets” and why should I take the exam when JSF is not part of the exam.
But most of the Java web frameworks use JSP and Servlets as the fundamentals. So with the SCWCD exam you will be forced to learn the fundamentals. You get a glue how JSF could be realized. Honestly there are a lot of things you will never use but I think when you prepare yourself seriously then it gives you a great benefit.
Steve which works in the same company as I took recently the Sun Certified Business Component Developer and he created some nice fact sheets which you can use to prepare yourself for the Sun Certified Business Component Developer exam.
EJB 3.1 and JSF 2.0 with GlassFish V3 Prelude
I got finally some time to write a simple CRUD web application with EJB 3.1 (JPA) and JSF 2.0 (facelets). Keep in mind that JSF 2.0 and EJB 3.1 are still in development and that some features are not yet implemented or can change. The goal was to make a simple GlassFish V3 Prelude project setup for a CRUD web application with EJB 3.1, JSF 2.0 (facelets) and maven.
First Install EJB 3.1 and JSF 2.0
I used for this this example the GalssFish V3 Prelude Platform-independent download file with Java 1.6 on a Ubuntu 8.04 Linux.
Unzip the GlassFish zip file and run the updatetool to install the EJB 3.1 and JSF 2.0 components.
$ ./updatetool
Remark: You can also install JSF 2.0 and EJB 3.1 over the GlassFish web administration console (http://localhost:4848). Be sure to restart GlassFish after you have updated JSF 2.0 and EJB 3.1.
After that you can run the pkg command to list the installed components.
$ ./pkg list
As you can see the EJB and JSF components were installed.
felix 1.2.2-0 installed ---- glassfish-amx 3.0-28.3 installed ---- glassfish-api 3.0-28.3 installed ---- glassfish-common 3.0-28.3 installed ---- glassfish-ejb 3.0-28.3 installed ---- glassfish-grizzly 1.8.6.2-0 installed ---- glassfish-gui 3.0-28.3 installed ---- glassfish-hk2 3.0-28.3 installed ---- glassfish-jca 3.0-28.3 installed ---- glassfish-jdbc 3.0-28.3 installed ---- glassfish-jdbc-gui 3.0-28.3 installed ---- glassfish-jdbc-management 3.0-28.3 installed ---- glassfish-jpa 3.0-28.3 installed ---- glassfish-jsf 2.0.0-5 installed ---- glassfish-jta 3.0-28.3 installed ---- glassfish-management 3.0-28.3 installed ---- glassfish-nucleus 3.0-28.3 installed ---- glassfish-registration 3.0-28.3 installed ---- glassfish-scripting 3.0-28.3 installed ---- glassfish-web 3.0-28.3 installed ---- glassfish-web-gui 3.0-28.3 installed ---- glassfish-web-management 3.0-28.3 installed ---- javadb 10.2.2.1-0 installed ---- pkg 1.0.7-15.1269 installed ---- pkg-java 1.0.7-15.1269 installed ---- python2.4-minimal 2.4.4.0-15.1269 installed ---- updatetool 2.0.0-15.1269 installed ---- wxpython2.8-minimal 2.8.8-15.1269 installed ----
Create a simple maven project setup
With the maven-archetype-webapp I created a simple webapp maven project.
mvn archetype:create -DgroupId=my.app.crud -DartifactId=CRUD-GVFv3 -DarchetypeArtifactId=maven-archetype-webapp
Now we have to customize our pom.xml file. We need some dependencies and the appropriate repositories. I used the following repositories:
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
<repository>
<id>glassfish-repository</id>
<name>Java.net Repository for Glassfish</name>
<url>http://download.java.net/maven/glassfish</url>
</repository>
</repositories>
To have the Java EE 6 JAR’s like EJB 3.1, JSF 2.0, etc in the build classpath I put the following dependencies in my pom.xml file.
<dependency> <groupId>org.glassfish</groupId> <artifactId>javax.javaee</artifactId> <version>3.0-Prelude-b28b</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.0.0-b05</version> <scope>provided</scope> </dependency>
I must say I’m not sure if these API’s are in sync with the current installed GlassFish. But an alternate way could be to use the maven system dependencies and point them to the the EJB, JSF, JPA, and common annotations JAR’s of the installed GlassFish.
I’m sure when the Java EE 6 API gets stable we will find it in the java.net maven repository, but for now we have to be patient. :-)
We will deploy our application as WAR and so we need to customize our maven project a little. I added src/main/resources as web resource to the maven war plugin, because we want to add some resources like META-INF/persistence.xml to the WAR file. The persistence.xml file is used to configure JPA and it must be placed inside of the META-INF directory of the WAR file.
pom.xml
<project 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">
<modelVersion>4.0.0</modelVersion>
<groupId>my.app.crud</groupId>
<artifactId>CRUD-GFv3</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>CRUD-GVFv3 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.javaee</artifactId>
<version>3.0-Prelude-b28b</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.0-b05</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<warName>${pom.artifactId}</warName>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/resources</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
<repository>
<id>glassfish-repository</id>
<name>Java.net Repository for Glassfish</name>
<url>http://download.java.net/maven/glassfish</url>
</repository>
</repositories>
</project>
Configure JPA
For this example I used the default datasource jdbc/__default in GlassFish to store the JPA entities. The jdbc/__default datasource uses the apache derby database that comes preconfigured with GlassFish.
As you can see the persistence.xml was adapted for EclipseLink, which is the new JPA implementation in GlassFIsh V3.
I used an example from the EclipseLink homepage and adapted it for the GlassFish container.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="defaultPersistenceUnit"
transaction-type="JTA">
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="eclipselink.jdbc.driver"
value="org.apache.derby.jdbc.ClientDriver" />
<property name="eclipselink.jdbc.url"
value="jdbc:derby://localhost:1527/CRUDGFv3;create=true" />
<property name="eclipselink.jdbc.user" value="app" />
<property name="eclipselink.jdbc.password" value="app" />
<property name="eclipselink.ddl-generation"
value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<!-- Logging -->
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.timestamp" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.thread" value="false" />
</properties>
</persistence-unit>
</persistence>
Configure JSF
Hint: Be sure to use at least the 2.5 web.xml deployment descriptor. When you use a previous version of the deployment descriptor dependency injection like @EJB will not work.
In the web.xml we declare the mapping for the JSF servlet.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Sample CRUD EJB 3.1 / JSF 2.0 App</display-name> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> </web-app>
The faces-config was customized for JSF 2.0 and so we can use faclets which was include in JSF 2.0. Be sure that you use a JSF 2.0 faces-config.xml file when you want to use the new JSF 2.0 features.
I just added some old fashion navigation rules and to declare the managed bean we will use the new @ManagedBean annotation.
faces-config.xml
<?xml version="1.0"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> <navigation-rule> <from-view-id>/listBooks.xhtml</from-view-id> <navigation-case> <from-outcome>editBook.xhtml</from-outcome> <to-view-id>/editBook.xhtml</to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id>/editBook.xhtml</from-view-id> <navigation-case> <from-outcome>listBooks.xhtml</from-outcome> <to-view-id>/listBooks.xhtml</to-view-id> </navigation-case> </navigation-rule> </faces-config>
JSF 2.0 has some cool new annotations to declare managed beans an their scope. The following example shows a managed bean declared with the @ManagedBean annotation. The session scope for this managed bean is specified with the @SessionScoped annotation.
import javax.faces.model.ManagedBean;
import javax.faces.model.SessionScoped;
@ManagedBean(name = "BookBean")
@SessionScoped
public class BookManagedBean {
...
}
Create a simple CRUD Application with EJB 3.1 and JSF 2.0
I created the following Java artifacts.
- BookServiceBean.java (Session Bean)
- Book.java (JPA Entity)
- BookManagedBean.java (Managed Bean)
And the following JSF pages.
Prepare deployment
So now it’s time to startup GlassFish and the include derby database
$ ./asadmin start-domain $ ./asadmin start-database
We create our WAR file with the following maven command.
$ mvn package
The last step is to deploy our application.
$ ./asadmin deploy /home/mr/project/CRUD-GFv3/target/CRUD-GFv3.war
Remark: It seemed that when I deploy the WAR over the web administration console I had to restart GlassFish. After the restart the CRUD example worked correctly.
Finally the hard work is over…. :-) The application should now be deployed and you can try it out under the following address.
http://localhost:8080/CRUD-GFv3
Source Code CRUD-GFv3
You can download the full source code as ZIP file.
Remark I also tested my example with Windows XP and the GlassFish V3 Prelude Platform-independent download file. So nobody can say I only preferred Linux…. :-) But bear in mind that the example could not work because of some changes in the Java EE 6 API that occurred after the writing of this post.
Is Linux the better choice for Java developers?
Last week I had a discussion with some colleges from work, if it would be better when we used Linux instead of Windows. I don’t want to start a flame war with my blog. I personally use Linux because when I was a student I couldn’t effort the licenses cost :-) and since then I pretty happy with my choice. But I must say that I don’t’ have a problem to work with Windows, but sometimes I think it would be easier for me to work with Linux. But thats my personal opinion. I really miss a package manager (apt-get, emerge, etc.) under Windows and the shell (bash, etc).
I started to think about this topic when they did a update of the antivirus software last week. Suddenly the build of our Java application took longer, because the antivirus software scanned during the build all jar’s, war’s, ear’s and class files. It took about a day and the problem was fixed from the IT support! Now the problem is solved but there are still some points which could be improved, like to exclude XML files from the scan.
A big company mostly have a Java development crew and they have to customize the workstations for them. For example when you are lucky you have local administration rights and you can install a JDK. So would it be better to give them Linux workstations (or a MacBook Air…;-) with a Windows Terminal Server client to access the MS applications?
A interesting post is from Cay Horstmann about why Java developers should switch to Linux.
http://weblogs.java.net/blog/cayhorstmann/archive/2006/06/why_java_develo.html
And naturally a blog from Eitan Suez about Mac and Java.
http://weblogs.java.net/blog/eitan/archive/2004/11/java_developmen_1.html
First look at GlassFish V3 Prelude
So finally I was able to install GlassFish V3 (glassfish-v3-prelude-b28 ) on my Ubuntu Linux. The previous version didn’t work because I was not able to run the update tool.
First download GlassFish V3 Prelude b28 and run the following commands.
$ chmod 755 glassfish-v3-prelude-b28b-unix.sh $ ./glassfish-v3-prelude-b28b-unix.sh
Hint : When you choose a password for your GlassFish make sure that it has at least eight characters. The Installer does not remark that. When you choose a password which has not eight characters the GlassFish Installer is not able to create a domain.
With the update tool we can now enable features like EJB 3.1 and JSF 2.0.
$ cd glassfish-v3-prelude-b28b/bin $ ./updatetool
To deploy some EJB Apps we have to install the EJB container. Obviously the EJB container is just 463.4 kB.
I think we can effort some 600 kB more and the we have JSF 2.0 enabled.
So it’s about time to start GlassFish and login for the first time.
$ ./asadmin start-domain
