Getting started with Apache TomEE 
March 18, 2013 JonathanGallimore 
Originally appearing in December’s JAX Magazine, Jonathan Gallimore tells us how to get started in Apache TomEE in part 1 of this tutorial 
Originally appearing in December’s JAX Magazine, Jonathan Gallimore tells us how to get started in Apache TomEE in the first part of this tutorial. 
Introduction 
Apache TomEE (pronounced “Tommy”) is a new Java EE server, developed by the Apache Software Foundation, and as you can probably guess from its name, is result of taking Tomcat, and adding Java EE features: TomEE = Tomcat + Java EE. So, how it is different from other application servers? 
A number of application servers use Tomcat to provide servlet functionality, and that’s no bad thing – Tomcat is great servlet container. TomEE’s approach is different – instead of embedding Tomcat into an application server, TomEE embeds EJB, CDI and the other Java EE features into Tomcat, giving you a fully compliant Web Profile server but keeping Tomcat as the top dog. The TomEE bundle is created by unzipping Tomcat, adding our jars, and adding a single listener to conf/server.xml and zipping the result back up. 
There are three guiding principles behind the development of TomEE. They are: 
 Be small 
 Be certified 
 Be Tomcat 
Each of these is really important to us, but the “be Tomcat” part particularly so – we are adding features to Tomcat, and it is important that TomEE does not lose the Tomcat identity. The deployment mechanism is the same as regular Tomcat (just drop your .war file in the webapps/ folder). Resources work in the same way – you can use TomEE’s configuration file, but resources defined in server.xml or within your application’s context.xml also still work. Things that were previously impossible or required large amounts of work now work out of the box,
such as making all Java EE security concepts like WebService or EJB security work seamlessly on Tomcat Realms. 
This philosophy doesn’t just make things better in the server itself, but provides other advantages too. Since TomEE is simply an extended version of Tomcat, any tools that work with Tomcat, such as IDE tools like Eclipse WTP, all work with TomEE. 
TomEE really is a genuine Tomcat server with all the Java EE Web Profile features added and nothing taken away. 
Flavours 
There are 3 different flavours of Apache TomEE available: Webprofile, JAX-RS, and Plus. Webprofile provides the smallest distribution (just 27MB) and is fully Java EE Web Profile compliant. JAX-RS builds on Web Profile, adding JAX-RS support with a trimmed down version of Apache CXF, and is also Web Profile certified. The Plus package provides all the features for TomEE that are available, including JMS, JAX-WS and JCA, but is not Java EE certified at this time. 
The table below shows the different features available for the 3 different flavours. 
Feature 
WebProfile 
JAX-RS 
Plus Servlet 3.0 Yes Yes Yes 
CDI 
Yes 
Yes 
Yes EJB Yes Yes Yes 
JPA 
Yes 
Yes 
Yes JSF Yes Yes Yes 
JSP 
Yes 
Yes 
Yes JSTL Yes Yes Yes 
JTA 
Yes 
Yes 
Yes JavaMail Yes Yes Yes 
Bean Validation 
Yes 
Yes 
Yes JAX-RS No Yes Yes
JAX-WS 
No 
No 
Yes JMS No No Yes 
Connector 
No 
No 
Yes 
Getting started 
To get started, head over to Apache TomEE download page and grab one of the distributions. At the time of writing, version 1.5.0 is the latest. Once downloaded, extract the zip file somewhere on your machine. If you take a look at the folder structure, you’ll probably notice straightaway how similar TomEE is to a regular copy of Tomcat. The usual catalina shell/batch script is in bin/, TomEE config files are in conf/, and applications are deployed by copying .war files to webapps/. Startup TomEE by changing to the bin/ folder and running ./catalina.sh run (or catalina.bat run if you are using Windows). 
TomEE should start in a couple of seconds on a reasonable machine. Once it has started, open a browser and navigate to http://localhost:8080. If everything has started up correctly, the usual default Tomcat application will show. There is an additional button in this application that links to the TomEE console. The TomEE console provides some pages where you can check that your setup is working correctly and a JNDI browser. 
Setting up a development environment using Eclipse 
Let’s fire up an IDE, and take a look at a simple program. I’m using Eclipse here, but other IDEs such as Netbeans and Intellij IDEA can be configured in the same way. I have the Java EE Eclipse package, which includes WTP, which can start up a number of different servers, deploy your code to them and automatically redeploy any changes you make. Setting up TomEE in Eclipse is the same process you would use to setup Tomcat. 
Firstly, select the ‘New server’ link on the servers view.
Next, select Apache Tomcat 7, and change the name of the server if you wish: 
On the next page of the wizard, select the installation directory where you unzipped the TomEE package to:
Optional tweaks 
If you have configured Eclipse to use workspace metadata as opposed to taking over the TomEE installation, its a good idea to copy the files from the conf/ directory of the TomEE package to the workspace. When running in this mode, the configuration files are read from the conf/ directory in the workspace instead of from TomEE’s conf directory. The WTP plugin helpfully copies a number of Tomcat configuration files to the workspace automatically, but does not pick up tomee.xml or system.properties.
To copy the necessary files to the workspace, right click on the localhost-config folder under the Servers project, and import the files from the conf/ directory. 
By default, TomEE does not have the development parameter set for JSP compilation in web.xml, which means that JSP won’t update on the fly as you save changes to them. To change this, open the web.xml file under the Servers->Tomcat v7.0 Server at localhost-config folder in the project explorer, and change the jsp servlet, so development mode is set to true as shown in Listing 1. 
Listing 1: web.xml – configuring development mode for JSPs 
<servlet> 
<servlet-name>jsp</servlet-name> 
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> 
<init-param> 
<param-name>fork</param-name> 
<param-value>false</param-value> 
</init-param>
<init-param> 
<param-name>xpoweredBy</param-name> 
<param-value>false</param-value> 
</init-param> 
<init-param> 
<param-name>development</param-name> 
<param-value>true</param-value> 
</init-param> 
<load-on-startup>3</load-on-startup> 
</servlet> 
Sample application 
So now we have a development environment setup, lets take a look at a simple example application. Apache TomEE comes with a number of example applications that demonstrate a number of different features available in Java EE. Currently there are over 100 examples, and these can serve as great starting point for learning new Java EE features. These are all available in the TomEE Subversion repository. I’m going to use the moviefun example in this article. Full source is available from http://tomee.apache.org/examples-trunk/moviefun/README.html. This sample is a simple web application using some of the features available in the Web Profile. 
To start off with, the application has simple POJO which represents a Movie, and this is used by JPA to store and retrieve a Movie in the database. Listing 2 shows a movie POJO which has title, director, genre, year and rating attributes. 
Listing 2: Movie Entity 
@Entity 
public class Movie implements Serializable { 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
private long id; 
private String director;
private String title; 
private int year; 
private String genre; 
private int rating; 
public Movie() { 
} 
// SNIP getter and setters below... 
} 
This provides a simple object that can be persisted to the database, using JPA. To do this, a persistence context can be injected into a simple EJB or CDI bean, and that can be used to retrieve and persist Movie objects to the database. 
Apache TomEE provides support for EJB from version 1. right up to version 3.1. EJB 3.0 made EJB creation much simpler than previous versions, and 3.1 builds on this further still. One of the new features is the “no-interface” view, which means that an EJB session bean no longer needs to provide an interface. 
Listing 3 shows a simple stateless session EJB which can manage storing and retrieving data using JPA 2. This EJB is simple POJO that has been annotated with @Stateless (which is all that is needed to make it an EJB) and has an entity manager injected in by TomEE using the @PersistenceContext annotation. 
Listing 3: Stateless session bean using JPA2 
@Stateless 
public class MoviesBean { 
@PersistenceContext(unitName = "movie-unit") 
private EntityManager entityManager; 
public Movie find(Long id) { 
return entityManager.find(Movie.class, id); 
} 
public void addMovie(Movie movie) { 
entityManager.persist(movie);
} 
public void deleteMovie(Movie movie) { 
entityManager.remove(movie); 
} 
public void deleteMovieId(long id) { 
Movie movie = entityManager.find(Movie.class, id); 
deleteMovie(movie); 
} 
public List<Movie> getMovies() { 
CriteriaQuery<Movie> cq = entityManager.getCriteriaBuilder().createQuery(Movie.class); 
cq.select(cq.from(Movie.class)); 
return entityManager.createQuery(cq).getResultList(); 
} 
} 
This class provides a few simple methods, firstly there are search, add and delete methods which delegate to the entity manager. The getMovies() method retrieves the complete set of movies from the database by constructing a simple JPA2 query. Notice that there are no boilerplate transaction begin or commit statements here. EJBs methods are transactional by default, so this is not necessary. TomEE is already saving you time! 
This simple bean provides a simple API to interact with the database. Now we need something to use it! So let’s look at moviefun’s user interface. There’s a number of ways a web frontend can interact with this EJB – for example it can be referenced by a JSF ManagedBean or you could look up the EJB from another MVC framework. To keep it simple, this example uses a servlet as shown in listing 4, which interacts with the EJB and then forwards the result on to a JSP. 
As you’d expect, as TomEE has Tomcat 7 at its core, it supports the Servlet 3.0 spec. This allows us to create a class that extends javax.servlet.http.HttpServlet and is annotated with @WebServlet. Servlets and managed beans can have EJBs injected into their fields using the @EJB annotation on the field, and CDI beans can be injected in using the @Inject annotation. TomEE also supports @Inject constructor injection for Servlets, a feature which is being added for JavaEE 7.
Listing 4: Servlet with session bean injected 
@WebServlet("/moviefun/*") 
public class ActionServlet extends HttpServlet { 
private static final long serialVersionUID = -5832176047021911038L; 
@EJB 
private MoviesBean moviesBean; 
@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { 
List<Movie> range = moviesBean.getMovies(); 
request.setAttribute("movies", movies); 
} 
request.getRequestDispatcher("WEB-INF/moviefun.jsp").forward(request, response); 
} 
} 
We can then add a simple JSP to display the data and form to add a new entry, like the snippet in Listing 5. 
Listing 5: JSP to render movies 
<div> 
<table> 
<thead> 
<tr> 
<th>Title</th> 
<th>Director</th> 
<th>Genre</th> 
<th>Rating</th> 
<th>Year</th> 
</tr> 
</thead> 
<tbody>
<c:forEach items="${movies}" var="movie"> 
<tr> 
<td><c:out value="${movie.title}" /></td> 
<td><c:out value="${movie.director}" /></td> 
<td><c:out value="${movie.genre}" /></td> 
<td><c:out value="${movie.rating}" /></td> 
<td><c:out value="${movie.year}" /></td> 
</tr> 
</c:forEach> 
</tbody> 
</table> 
</div> 
So, with just three classes, and a JSP we are just about ready to go. We need one extra file, META-INF/persistence.xml, which will provide some basic information about the persistence unit that is injected into the EJB. 
Listing 6: Persistence.xml 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
<persistence-unit name="movie-unit"> 
<jta-data-source>movieDatabase</jta-data-source> 
<non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source> 
<class>org.superbiz.moviefun.Movie</class> 
<properties> 
<property name="openjpa.jdbc.SynchronizeMappings"value="buildSchema(ForeignKeys=true)"/> 
</properties> 
</persistence-unit> 
</persistence>
The persistence.xml example shown in Listing 6 indicates the @Entity classes being used (org.superbiz.moviefun.Movie in this case), and specifies that OpenJPA should build the schema for us in the database automatically if it does not already exist. 
Deploying the app 
We’re now ready to deploy the application. You can add this module to the server, and start the server up. 
You should see the application start up without any errors. The URL for the application will follow the .war filename (or project in Eclipse), so for example, example.war will be deployed to http://localhost:8080/example/. 
Look Mom, no EARs! 
You will notice that everything is in one module which will just be packaged into one single WAR file. There is no need to construct an EAR file with the EJB separated out into its own JAR, as has been the case with previous versions of Java EE. This is a feature that actually originated from TomEE, which has had it for a number of years, and was one of its first contributions to JavaEE. 
All your memory are belong to you!
It’s worth pointing out at this point that TomEE is running with the default memory parameters – on my Mac running a 64bit server JDK 1.6, that’s 128MB max by default, and TomEE running the moviefun example, uses around just 30MB of that. 
That leaves plenty of memory available for your application. TomEE is actually certified using Amazon EC2 micro instances, each of which have just 613MB of RAM – not very much by today’s standards. But using TomEE as your application server, plenty of that 613MB is available for your application to use. We have even been able to run TomEE and the moviefun example application on a Raspberry Pi with 256MB of RAM, which costs just $35. Who says Java EE is heavy or expensive? 
Database configuration 
You may also have noticed that we have developed and deployed a database application, without actually configuring anything in TomEE whatsoever. If you restart the TomEE server within Eclipse, you’ll notice that all the data in the application is persisted. So where exactly is that data being stored, and how is the data source configured? 
TomEE is configured using a single XML configuration file, tomee.xml, which lives in the conf/ directory. If you take a look at this file, you’ll see a few different things configured with the default settings – EJB pools, datasources and resource adapters. The syntax is maybe slightly different to other XML configuration files you may have seen for other application servers. It is based around the configuration style for the Apache httpd server, and aims to be as easily human
readable as possible. Times, such as the timeout time for a bean pool, rather than being specified in milliseconds, can be be expressed as a combination of any time units you like, for example “1 hour and 10 minutes and 30 seconds”. 
Defining a datasource can be done by adding a section to tomee.xml like Listing 7. 
Listing 7: Configuring a datasource in tomee.xml 
<Resource id="movieDatabase" type="DataSource"> 
JdbcDriver com.mysql.jdbc.Driver 
JdbcUrl jdbc:mysql://localhost:3306/moviefun 
UserName username 
Password password 
JtaManaged true 
</Resource> 
Set the JdbcDriver setting to match the class provided by the JDBC driver you wish to use, and set the JdbcUrl, UserName and Password settings to match your database. You will also need to deploy the JDBC driver you wish to use, which you can do simply by dropping the jar for it in the TomEE/lib directory. 
The Resource ID should match the <jta-data-source> or <non-jta-data-source> defined in the persistence.xml file. If no matching resources are found in the tomee.xml file, the default database will be used, which is what has been happening. The default database is a file based instance of HSQLDB that uses the data/ directory. This file can also be used to configure JMS queues and topics, and any other resources you may wish to use in your application. 
Configuration settings can also be specified via system properties. The datasource example can be specified as system properties as JAVA_OPTS like this: 
-DmovieDatabase.JdbcDriver=com.mysql.jdbc.Driver 
-DmovieDatabase.JdbcUrl=jdbc:mysql://localhost:3306/moviefun 
-DmovieDatabase.UserName=username -DmovieDatabase.Password=password
Or it can be added to conf/system.properties. More information and list of built in system properties is available on the TomEE website. 
Security 
TomEE tightly integrates Java EE security in a way that is seamless and consistent across the web tier, EJBs and webservices. It is really simple to add to an application and any custom Tomcat Realm implementations such as the JDBC, LDAP or MongoDB backed Realms will work in TomEE with no changes. Alternatively, custom security modules are also supported via a JAAS login.config file in the conf/ directory. Security using basic HTTP authentication can be added to the moviefun example by following these steps: 
Add a user and role to the tomcat-users.xml file: 
<user username=”movies” password=”movies” roles=”moviefun”/> 
Add some security configuration to web.xml, as shown in Listing 8. 
Listing 8: web.xml security configuration 
<login-config> 
<auth-method>BASIC</auth-method> 
<realm-name>UserDatabase</realm-name> 
</login-config> 
<security-role> 
<role-name>moviefun</role-name> 
</security-role> 
<security-constraint> 
<web-resource-collection> 
<web-resource-name>Everything</web-resource-name> 
<url-pattern>/*</url-pattern> 
</web-resource-collection> 
<auth-constraint> 
<role-name>moviefun</role-name>
</auth-constraint> 
<user-data-constraint> 
<transport-guarantee>NONE</transport-guarantee> 
</user-data-constraint> 
</security-constraint> 
And finally add @RolesDeclared to the MoviesBean class, and @RolesAllowed either to the MoviesBean class, or to the individual methods you wish to grant access to, as shown in Listing 9. 
Listing 9: adding roles to session bean 
@Stateless 
@DeclareRoles(value = { "moviefun" }) 
public class MoviesBean { 
@RolesAllowed(value = { "moviefun" }) 
public List<Movie> findAll(int firstResult, int maxResults) { 
... 
} 
} 
This configuration will require authentication to access any of the page within the application using a basic authentication dialog in the web browser. The username and password will be checked against the UserDatabase realm in Tomcat, which uses the tomcat-users.xml file. Finally TomEE will pass the logged in principal and associated roles to the EJB. Only users with the allowed roles will be able to invoke the methods on the EJB. Invoking the methods on the EJB from a remote client will also require the username and password to be sent, and will be authenticated against the UserDatabase realm. 
Editor’s Note: Keep your eyes peeled for the second part of the tutorial tomorrow focusing on TomEE testing! 
Author Bio: Jon is an Open Source and Java EE enthusiast working for Weatherbys Ltd in the UK. As an Open Source consumer-turned-contributor, Jon has been a committer for the Apache TomEE and OpenEJB projects for the past four years, and has worked on a number of different features including the early Tomcat 7 integration in TomEE, parts of the EJB 3.1 compliance, TomEE Arquillian adapters and the OpenEJB Eclipse Plugin. When not grinding away at work
by day, and burning the midnight oil on Apache TomEE at night, Jon enjoys cooking, watching Formula One, and playing the occasional game of golf. 
This article appeared in JAX Magazine:TomEE – download that and other issueshere.

More Related Content

PDF
Lecture11 b
PPT
Baocao Web Tech Java Mail
PPT
香港六合彩 &raquo; SlideShare
PPT
Auxiliary : Tomcat
DOC
Java Servlets & JSP
PDF
Java servlet technology
PDF
Jsp servlets
PPTX
Introduction of Tomcat
Lecture11 b
Baocao Web Tech Java Mail
香港六合彩 &raquo; SlideShare
Auxiliary : Tomcat
Java Servlets & JSP
Java servlet technology
Jsp servlets
Introduction of Tomcat

What's hot (20)

DOC
PPT
Web Tech Java Servlet Update1
PPT
Java Servlets
PPT
Servlet/JSP course chapter 1: Introduction to servlets
DOCX
Html servlet example
PDF
Jmeter memory profiling, server-side monitoring, memory and cpu monitoring
PDF
Tomcat + other things
PDF
Architecting Large Enterprise Java Projects
PDF
ZK_Arch_notes_20081121
PPT
1 java servlets and jsp
PPTX
Servlets
PPSX
Java server pages
PPT
Tomcat Clustering
PDF
Rest dengan restlet
PPT
An Introduction To Java Web Technology
PDF
Flask Introduction - Python Meetup
PPT
Knowledge Sharing : Java Servlet
PPT
JAVA Servlets
PPTX
Jsp and jstl
PDF
java servlet and servlet programming
Web Tech Java Servlet Update1
Java Servlets
Servlet/JSP course chapter 1: Introduction to servlets
Html servlet example
Jmeter memory profiling, server-side monitoring, memory and cpu monitoring
Tomcat + other things
Architecting Large Enterprise Java Projects
ZK_Arch_notes_20081121
1 java servlets and jsp
Servlets
Java server pages
Tomcat Clustering
Rest dengan restlet
An Introduction To Java Web Technology
Flask Introduction - Python Meetup
Knowledge Sharing : Java Servlet
JAVA Servlets
Jsp and jstl
java servlet and servlet programming
Ad

Viewers also liked (17)

PDF
qims_amegroups_com_about_editorialteam
PDF
cellandbiosci_Reviewers2016_10_1186_s13
DOCX
Estatudos de empresa
PDF
White Tigers
PDF
Модель мира основатели 20.04
PDF
A Guide To Slaying A Dragon - Happy St George's Day
PDF
O donnell: "estado democratización-y-ciudadanía"
PPT
ccna presentation
PDF
Google Scholar CSD-cited 17700_PM4840_ v3
PDF
1º reunión coordinadores 08 09 2016 2017
PDF
2ª reunión coordinación eo es 14 enero 2016
PDF
4ª reunión coordinación eoe 28 abril 2016
PDF
3ª reunión coordinación eo es 4 febrero 2016
PDF
qims_Li-webpage_111416
PDF
De la torre a la nube
PDF
No te enredes con el acoso
PPTX
Armed forces special powers act
qims_amegroups_com_about_editorialteam
cellandbiosci_Reviewers2016_10_1186_s13
Estatudos de empresa
White Tigers
Модель мира основатели 20.04
A Guide To Slaying A Dragon - Happy St George's Day
O donnell: "estado democratización-y-ciudadanía"
ccna presentation
Google Scholar CSD-cited 17700_PM4840_ v3
1º reunión coordinadores 08 09 2016 2017
2ª reunión coordinación eo es 14 enero 2016
4ª reunión coordinación eoe 28 abril 2016
3ª reunión coordinación eo es 4 febrero 2016
qims_Li-webpage_111416
De la torre a la nube
No te enredes con el acoso
Armed forces special powers act
Ad

Similar to Pp w tomee (20)

PPT
Apache TomEE - Tomcat with a kick
PDF
web component_development
PDF
Apache Tomcat + Java EE = Apache TomEE
PDF
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
PDF
Apache TomEE, Java EE 6 Web Profile on Tomcat - David Blevins
PDF
JavaEE6 my way
PDF
Pavimentando el camino con Jakarta EE 9 y Apache TomEE
PPT
Java J2EE
PDF
From Tomcat to Java EE, making the transition with TomEE
PDF
Pavimentando el Camino con Jakarta EE 9 y Apache TomEE 9.0.0
PDF
Paving the way with Jakarta EE and apache TomEE at cloudconferenceday
DOC
Java Interview Questions
PDF
Download full ebook of Tomcat 6 Developers Guide Damodar Chetty instant downl...
PPTX
Developing Web Application Using J2EE - Nex
PDF
JavaOne 2013 - Apache TomEE, Java EE Web Profile {and more} on Tomcat
PDF
Apache TomEE, Java EE 6 Web Profile {and more} on Tomcat
PDF
2011 JavaOne Apache TomEE Java EE 6 Web Profile
PPTX
J2ee seminar
PDF
Paving the way with Jakarta EE and Apache TomEE - JCConf
PDF
Paving the way with Jakarta EE and Apache TomEE - itkonekt 2020
Apache TomEE - Tomcat with a kick
web component_development
Apache Tomcat + Java EE = Apache TomEE
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Apache TomEE, Java EE 6 Web Profile on Tomcat - David Blevins
JavaEE6 my way
Pavimentando el camino con Jakarta EE 9 y Apache TomEE
Java J2EE
From Tomcat to Java EE, making the transition with TomEE
Pavimentando el Camino con Jakarta EE 9 y Apache TomEE 9.0.0
Paving the way with Jakarta EE and apache TomEE at cloudconferenceday
Java Interview Questions
Download full ebook of Tomcat 6 Developers Guide Damodar Chetty instant downl...
Developing Web Application Using J2EE - Nex
JavaOne 2013 - Apache TomEE, Java EE Web Profile {and more} on Tomcat
Apache TomEE, Java EE 6 Web Profile {and more} on Tomcat
2011 JavaOne Apache TomEE Java EE 6 Web Profile
J2ee seminar
Paving the way with Jakarta EE and Apache TomEE - JCConf
Paving the way with Jakarta EE and Apache TomEE - itkonekt 2020

Pp w tomee

  • 1. Getting started with Apache TomEE March 18, 2013 JonathanGallimore Originally appearing in December’s JAX Magazine, Jonathan Gallimore tells us how to get started in Apache TomEE in part 1 of this tutorial Originally appearing in December’s JAX Magazine, Jonathan Gallimore tells us how to get started in Apache TomEE in the first part of this tutorial. Introduction Apache TomEE (pronounced “Tommy”) is a new Java EE server, developed by the Apache Software Foundation, and as you can probably guess from its name, is result of taking Tomcat, and adding Java EE features: TomEE = Tomcat + Java EE. So, how it is different from other application servers? A number of application servers use Tomcat to provide servlet functionality, and that’s no bad thing – Tomcat is great servlet container. TomEE’s approach is different – instead of embedding Tomcat into an application server, TomEE embeds EJB, CDI and the other Java EE features into Tomcat, giving you a fully compliant Web Profile server but keeping Tomcat as the top dog. The TomEE bundle is created by unzipping Tomcat, adding our jars, and adding a single listener to conf/server.xml and zipping the result back up. There are three guiding principles behind the development of TomEE. They are:  Be small  Be certified  Be Tomcat Each of these is really important to us, but the “be Tomcat” part particularly so – we are adding features to Tomcat, and it is important that TomEE does not lose the Tomcat identity. The deployment mechanism is the same as regular Tomcat (just drop your .war file in the webapps/ folder). Resources work in the same way – you can use TomEE’s configuration file, but resources defined in server.xml or within your application’s context.xml also still work. Things that were previously impossible or required large amounts of work now work out of the box,
  • 2. such as making all Java EE security concepts like WebService or EJB security work seamlessly on Tomcat Realms. This philosophy doesn’t just make things better in the server itself, but provides other advantages too. Since TomEE is simply an extended version of Tomcat, any tools that work with Tomcat, such as IDE tools like Eclipse WTP, all work with TomEE. TomEE really is a genuine Tomcat server with all the Java EE Web Profile features added and nothing taken away. Flavours There are 3 different flavours of Apache TomEE available: Webprofile, JAX-RS, and Plus. Webprofile provides the smallest distribution (just 27MB) and is fully Java EE Web Profile compliant. JAX-RS builds on Web Profile, adding JAX-RS support with a trimmed down version of Apache CXF, and is also Web Profile certified. The Plus package provides all the features for TomEE that are available, including JMS, JAX-WS and JCA, but is not Java EE certified at this time. The table below shows the different features available for the 3 different flavours. Feature WebProfile JAX-RS Plus Servlet 3.0 Yes Yes Yes CDI Yes Yes Yes EJB Yes Yes Yes JPA Yes Yes Yes JSF Yes Yes Yes JSP Yes Yes Yes JSTL Yes Yes Yes JTA Yes Yes Yes JavaMail Yes Yes Yes Bean Validation Yes Yes Yes JAX-RS No Yes Yes
  • 3. JAX-WS No No Yes JMS No No Yes Connector No No Yes Getting started To get started, head over to Apache TomEE download page and grab one of the distributions. At the time of writing, version 1.5.0 is the latest. Once downloaded, extract the zip file somewhere on your machine. If you take a look at the folder structure, you’ll probably notice straightaway how similar TomEE is to a regular copy of Tomcat. The usual catalina shell/batch script is in bin/, TomEE config files are in conf/, and applications are deployed by copying .war files to webapps/. Startup TomEE by changing to the bin/ folder and running ./catalina.sh run (or catalina.bat run if you are using Windows). TomEE should start in a couple of seconds on a reasonable machine. Once it has started, open a browser and navigate to http://localhost:8080. If everything has started up correctly, the usual default Tomcat application will show. There is an additional button in this application that links to the TomEE console. The TomEE console provides some pages where you can check that your setup is working correctly and a JNDI browser. Setting up a development environment using Eclipse Let’s fire up an IDE, and take a look at a simple program. I’m using Eclipse here, but other IDEs such as Netbeans and Intellij IDEA can be configured in the same way. I have the Java EE Eclipse package, which includes WTP, which can start up a number of different servers, deploy your code to them and automatically redeploy any changes you make. Setting up TomEE in Eclipse is the same process you would use to setup Tomcat. Firstly, select the ‘New server’ link on the servers view.
  • 4. Next, select Apache Tomcat 7, and change the name of the server if you wish: On the next page of the wizard, select the installation directory where you unzipped the TomEE package to:
  • 5. Optional tweaks If you have configured Eclipse to use workspace metadata as opposed to taking over the TomEE installation, its a good idea to copy the files from the conf/ directory of the TomEE package to the workspace. When running in this mode, the configuration files are read from the conf/ directory in the workspace instead of from TomEE’s conf directory. The WTP plugin helpfully copies a number of Tomcat configuration files to the workspace automatically, but does not pick up tomee.xml or system.properties.
  • 6. To copy the necessary files to the workspace, right click on the localhost-config folder under the Servers project, and import the files from the conf/ directory. By default, TomEE does not have the development parameter set for JSP compilation in web.xml, which means that JSP won’t update on the fly as you save changes to them. To change this, open the web.xml file under the Servers->Tomcat v7.0 Server at localhost-config folder in the project explorer, and change the jsp servlet, so development mode is set to true as shown in Listing 1. Listing 1: web.xml – configuring development mode for JSPs <servlet> <servlet-name>jsp</servlet-name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <init-param> <param-name>fork</param-name> <param-value>false</param-value> </init-param>
  • 7. <init-param> <param-name>xpoweredBy</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>development</param-name> <param-value>true</param-value> </init-param> <load-on-startup>3</load-on-startup> </servlet> Sample application So now we have a development environment setup, lets take a look at a simple example application. Apache TomEE comes with a number of example applications that demonstrate a number of different features available in Java EE. Currently there are over 100 examples, and these can serve as great starting point for learning new Java EE features. These are all available in the TomEE Subversion repository. I’m going to use the moviefun example in this article. Full source is available from http://tomee.apache.org/examples-trunk/moviefun/README.html. This sample is a simple web application using some of the features available in the Web Profile. To start off with, the application has simple POJO which represents a Movie, and this is used by JPA to store and retrieve a Movie in the database. Listing 2 shows a movie POJO which has title, director, genre, year and rating attributes. Listing 2: Movie Entity @Entity public class Movie implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String director;
  • 8. private String title; private int year; private String genre; private int rating; public Movie() { } // SNIP getter and setters below... } This provides a simple object that can be persisted to the database, using JPA. To do this, a persistence context can be injected into a simple EJB or CDI bean, and that can be used to retrieve and persist Movie objects to the database. Apache TomEE provides support for EJB from version 1. right up to version 3.1. EJB 3.0 made EJB creation much simpler than previous versions, and 3.1 builds on this further still. One of the new features is the “no-interface” view, which means that an EJB session bean no longer needs to provide an interface. Listing 3 shows a simple stateless session EJB which can manage storing and retrieving data using JPA 2. This EJB is simple POJO that has been annotated with @Stateless (which is all that is needed to make it an EJB) and has an entity manager injected in by TomEE using the @PersistenceContext annotation. Listing 3: Stateless session bean using JPA2 @Stateless public class MoviesBean { @PersistenceContext(unitName = "movie-unit") private EntityManager entityManager; public Movie find(Long id) { return entityManager.find(Movie.class, id); } public void addMovie(Movie movie) { entityManager.persist(movie);
  • 9. } public void deleteMovie(Movie movie) { entityManager.remove(movie); } public void deleteMovieId(long id) { Movie movie = entityManager.find(Movie.class, id); deleteMovie(movie); } public List<Movie> getMovies() { CriteriaQuery<Movie> cq = entityManager.getCriteriaBuilder().createQuery(Movie.class); cq.select(cq.from(Movie.class)); return entityManager.createQuery(cq).getResultList(); } } This class provides a few simple methods, firstly there are search, add and delete methods which delegate to the entity manager. The getMovies() method retrieves the complete set of movies from the database by constructing a simple JPA2 query. Notice that there are no boilerplate transaction begin or commit statements here. EJBs methods are transactional by default, so this is not necessary. TomEE is already saving you time! This simple bean provides a simple API to interact with the database. Now we need something to use it! So let’s look at moviefun’s user interface. There’s a number of ways a web frontend can interact with this EJB – for example it can be referenced by a JSF ManagedBean or you could look up the EJB from another MVC framework. To keep it simple, this example uses a servlet as shown in listing 4, which interacts with the EJB and then forwards the result on to a JSP. As you’d expect, as TomEE has Tomcat 7 at its core, it supports the Servlet 3.0 spec. This allows us to create a class that extends javax.servlet.http.HttpServlet and is annotated with @WebServlet. Servlets and managed beans can have EJBs injected into their fields using the @EJB annotation on the field, and CDI beans can be injected in using the @Inject annotation. TomEE also supports @Inject constructor injection for Servlets, a feature which is being added for JavaEE 7.
  • 10. Listing 4: Servlet with session bean injected @WebServlet("/moviefun/*") public class ActionServlet extends HttpServlet { private static final long serialVersionUID = -5832176047021911038L; @EJB private MoviesBean moviesBean; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { List<Movie> range = moviesBean.getMovies(); request.setAttribute("movies", movies); } request.getRequestDispatcher("WEB-INF/moviefun.jsp").forward(request, response); } } We can then add a simple JSP to display the data and form to add a new entry, like the snippet in Listing 5. Listing 5: JSP to render movies <div> <table> <thead> <tr> <th>Title</th> <th>Director</th> <th>Genre</th> <th>Rating</th> <th>Year</th> </tr> </thead> <tbody>
  • 11. <c:forEach items="${movies}" var="movie"> <tr> <td><c:out value="${movie.title}" /></td> <td><c:out value="${movie.director}" /></td> <td><c:out value="${movie.genre}" /></td> <td><c:out value="${movie.rating}" /></td> <td><c:out value="${movie.year}" /></td> </tr> </c:forEach> </tbody> </table> </div> So, with just three classes, and a JSP we are just about ready to go. We need one extra file, META-INF/persistence.xml, which will provide some basic information about the persistence unit that is injected into the EJB. Listing 6: Persistence.xml <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="movie-unit"> <jta-data-source>movieDatabase</jta-data-source> <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source> <class>org.superbiz.moviefun.Movie</class> <properties> <property name="openjpa.jdbc.SynchronizeMappings"value="buildSchema(ForeignKeys=true)"/> </properties> </persistence-unit> </persistence>
  • 12. The persistence.xml example shown in Listing 6 indicates the @Entity classes being used (org.superbiz.moviefun.Movie in this case), and specifies that OpenJPA should build the schema for us in the database automatically if it does not already exist. Deploying the app We’re now ready to deploy the application. You can add this module to the server, and start the server up. You should see the application start up without any errors. The URL for the application will follow the .war filename (or project in Eclipse), so for example, example.war will be deployed to http://localhost:8080/example/. Look Mom, no EARs! You will notice that everything is in one module which will just be packaged into one single WAR file. There is no need to construct an EAR file with the EJB separated out into its own JAR, as has been the case with previous versions of Java EE. This is a feature that actually originated from TomEE, which has had it for a number of years, and was one of its first contributions to JavaEE. All your memory are belong to you!
  • 13. It’s worth pointing out at this point that TomEE is running with the default memory parameters – on my Mac running a 64bit server JDK 1.6, that’s 128MB max by default, and TomEE running the moviefun example, uses around just 30MB of that. That leaves plenty of memory available for your application. TomEE is actually certified using Amazon EC2 micro instances, each of which have just 613MB of RAM – not very much by today’s standards. But using TomEE as your application server, plenty of that 613MB is available for your application to use. We have even been able to run TomEE and the moviefun example application on a Raspberry Pi with 256MB of RAM, which costs just $35. Who says Java EE is heavy or expensive? Database configuration You may also have noticed that we have developed and deployed a database application, without actually configuring anything in TomEE whatsoever. If you restart the TomEE server within Eclipse, you’ll notice that all the data in the application is persisted. So where exactly is that data being stored, and how is the data source configured? TomEE is configured using a single XML configuration file, tomee.xml, which lives in the conf/ directory. If you take a look at this file, you’ll see a few different things configured with the default settings – EJB pools, datasources and resource adapters. The syntax is maybe slightly different to other XML configuration files you may have seen for other application servers. It is based around the configuration style for the Apache httpd server, and aims to be as easily human
  • 14. readable as possible. Times, such as the timeout time for a bean pool, rather than being specified in milliseconds, can be be expressed as a combination of any time units you like, for example “1 hour and 10 minutes and 30 seconds”. Defining a datasource can be done by adding a section to tomee.xml like Listing 7. Listing 7: Configuring a datasource in tomee.xml <Resource id="movieDatabase" type="DataSource"> JdbcDriver com.mysql.jdbc.Driver JdbcUrl jdbc:mysql://localhost:3306/moviefun UserName username Password password JtaManaged true </Resource> Set the JdbcDriver setting to match the class provided by the JDBC driver you wish to use, and set the JdbcUrl, UserName and Password settings to match your database. You will also need to deploy the JDBC driver you wish to use, which you can do simply by dropping the jar for it in the TomEE/lib directory. The Resource ID should match the <jta-data-source> or <non-jta-data-source> defined in the persistence.xml file. If no matching resources are found in the tomee.xml file, the default database will be used, which is what has been happening. The default database is a file based instance of HSQLDB that uses the data/ directory. This file can also be used to configure JMS queues and topics, and any other resources you may wish to use in your application. Configuration settings can also be specified via system properties. The datasource example can be specified as system properties as JAVA_OPTS like this: -DmovieDatabase.JdbcDriver=com.mysql.jdbc.Driver -DmovieDatabase.JdbcUrl=jdbc:mysql://localhost:3306/moviefun -DmovieDatabase.UserName=username -DmovieDatabase.Password=password
  • 15. Or it can be added to conf/system.properties. More information and list of built in system properties is available on the TomEE website. Security TomEE tightly integrates Java EE security in a way that is seamless and consistent across the web tier, EJBs and webservices. It is really simple to add to an application and any custom Tomcat Realm implementations such as the JDBC, LDAP or MongoDB backed Realms will work in TomEE with no changes. Alternatively, custom security modules are also supported via a JAAS login.config file in the conf/ directory. Security using basic HTTP authentication can be added to the moviefun example by following these steps: Add a user and role to the tomcat-users.xml file: <user username=”movies” password=”movies” roles=”moviefun”/> Add some security configuration to web.xml, as shown in Listing 8. Listing 8: web.xml security configuration <login-config> <auth-method>BASIC</auth-method> <realm-name>UserDatabase</realm-name> </login-config> <security-role> <role-name>moviefun</role-name> </security-role> <security-constraint> <web-resource-collection> <web-resource-name>Everything</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>moviefun</role-name>
  • 16. </auth-constraint> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint> And finally add @RolesDeclared to the MoviesBean class, and @RolesAllowed either to the MoviesBean class, or to the individual methods you wish to grant access to, as shown in Listing 9. Listing 9: adding roles to session bean @Stateless @DeclareRoles(value = { "moviefun" }) public class MoviesBean { @RolesAllowed(value = { "moviefun" }) public List<Movie> findAll(int firstResult, int maxResults) { ... } } This configuration will require authentication to access any of the page within the application using a basic authentication dialog in the web browser. The username and password will be checked against the UserDatabase realm in Tomcat, which uses the tomcat-users.xml file. Finally TomEE will pass the logged in principal and associated roles to the EJB. Only users with the allowed roles will be able to invoke the methods on the EJB. Invoking the methods on the EJB from a remote client will also require the username and password to be sent, and will be authenticated against the UserDatabase realm. Editor’s Note: Keep your eyes peeled for the second part of the tutorial tomorrow focusing on TomEE testing! Author Bio: Jon is an Open Source and Java EE enthusiast working for Weatherbys Ltd in the UK. As an Open Source consumer-turned-contributor, Jon has been a committer for the Apache TomEE and OpenEJB projects for the past four years, and has worked on a number of different features including the early Tomcat 7 integration in TomEE, parts of the EJB 3.1 compliance, TomEE Arquillian adapters and the OpenEJB Eclipse Plugin. When not grinding away at work
  • 17. by day, and burning the midnight oil on Apache TomEE at night, Jon enjoys cooking, watching Formula One, and playing the occasional game of golf. This article appeared in JAX Magazine:TomEE – download that and other issueshere.