The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


Worst ever Java EE 6 Blog

Posted by ludo on May 25, 2009 at 9:09 PM PDT
Bonjour, comment Java?

I'm preparing some Java EE 6 JavaOne demos. While doing that, I was thinking: how can I compress most of the Java EE 6 technology inside *one* single Java Class?
If you are my manager, stop reading now...
If you believe you are an architect, stop reading now...
If you are a member of the Java Blueprints team, stop reading now...
If you are a regular java blog reader, stop reading now...
The following code is PG 40. Talk to you parents if you are less than 40.

Still reading? Not sure why, but here we go.

The following Java EE 6 compliant Application is a Web Application with just 2 source files: MonsterServlet.java and persistence.xml.
It demonstrates the following Java EE 6 features:
  • Annotated Servlets 3.0, avoiding the need for web.xml!!!
  • EJB without local Interface: 100% POJO, and transactional (managed by the container) and securable
  • EJB inside a Web Application
  • JPA (still needs a persistence.xml ,  here it's defined as creating the tables at deploy time, assuming the database is already running)
  • EclipseLink Implementation
  • Default GlassFish v3 JavaDB database registered as "jdbc/__default" datasource.
  • Beans Validation framework (JSR 303...Merci Emmanuel)
  • Injection of EJBs inside a Servlet
Are you still reading? Remember the 3 tier architecture, with Data Layer, Business Layer, then Presentation Layer? Well forget it for this monster application which merges all the layers into one single Java Class, which is a Servlet, an EJB and a Entity JPA bean validated with some Bean Validation annotations...



MonsterServlet.java
package monster;

import java.io.*;
import java.util.*;
import javax.ejb.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.persistence.*;
import javax.servlet.*;
import javax.validation.*;
import javax.validation.constraints.*;
/**
* @author: Not Me!!!
**/
@Stateless @Entity @WebServlet(urlPatterns = "/monster")
@Table(name = "MONSTERTABLE")
@NamedQueries({@NamedQuery(name = "MonsterEJB.findAll", query = "SELECT c FROM MonsterServlet c")})
@PersistenceContext(name = "monsterContext", unitName = "MonsterWebAppPU")
public class MonsterServlet extends HttpServlet {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private int monsterId;
@Max(2) @NotNull private String name;
@Transient @EJB MonsterServlet monsterEJB;
/*@Transient @PersistenceUnit(unitName = "MonsterWebAppPU") //does not work see EclipeLink bug
private EntityManagerFactory emf;*/ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=277550

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.getWriter().println("In Servlet calling the EJB side " + monsterEJB.EJBBusinessMethod("" + this));
}

public String EJBBusinessMethod(String name) {
try {
InitialContext ic = new InitialContext();
EntityManager em = (EntityManager) ic.lookup("java:comp/env/monsterContext");
this.name = name;

em.persist(this);

Query allMonsterQuery = em.createNamedQuery("MonsterEJB.findAll");
List allMonsters = allMonsterQuery.getResultList();
String error = validateTheMonsterJPA((MonsterServlet) allMonsters.get(0));
return "BusinessMethod from EJB" + allMonsters.toString() + error;
} catch (NamingException ex) {
return "Error in EJBBusinessMethod "+ex.getMessage();
}
}

private String validateTheMonsterJPA(MonsterServlet m) {
String error=" ";
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<MonsterServlet>> s = validator.validate(m);
for (ConstraintViolation<MonsterServlet> c : s) {
error = error+ " Contraint Violation: " + c.getMessage());
}
return error;
}
}


And the persistence.xml to put in the WEB-INF/classes/META-INF directory of this Web Application
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="MonsterWebAppPU" transaction-type="JTA">
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>

This Web Application can be deployed as it to the latest builds of GlassFish v3. The page output is something like:

In Servlet calling the EJB side BusinessMethod from EJB[monster.MonsterServlet@5718c3a7]  Contraint Violation: must be less than or equal to 2

Now the quiz:
  1. Explain the following statement: @Stateless @Entity @WebServlet
  2. what do you need to change in the code to remove the Contraint Violation statement in the output?
  3. Why do we need to inject the EJB aspect of this class into the servlet and cannot  just use "this" ?
  4. Do you think this bug will be fixed?  https://bugs.eclipse.org/bugs/show_bug.cgi?id=277550 

Still reading? Now I have just corrupted your brain: while not recommended, it is possible to write a 3-tier Java EE 6 Application with Servlets, EJBs, JPAs, Beans Validation in one single Java class and one single xml file...
Can't wait for Java EE 7 when persistence.xml will become optional:-)

See you at JavaOne. You can now forget this blog. Someone had to write it. It's over now:-)
Ludo



Related Topics >> Java Enterprise      
Comments
Comments are listed in date ascending order (oldest first)

les Francais sont drole ;)

well, you can be releasing

well, you can be releasing Java ME Platform SDK 10.0 for all i thesis | online thesis | how to write thesis | thesis writing care but where is the mac and linux support that people have been wanting for years? thesis help | custom thesis | buy thesis | theses

PG40? Oh well... I still like it. Asides not being recommended, I think this is pretty good an example and very well worth being spread around, as it surely addresses one of the usual "Java EE myths" ("there is loads and loads of source and XML files to be written even to achieve simple functionality"). Good shot. ;> K.

I love it !

since you are injecting the EJB, any method using the "monsterEJB" reference will not be thread safe... for me, this is the only pending oddity of Java EE.. and if you include the @Singleton annotation, goodbye scalability :)

Excellent ! LoL

The servlet code is a disaster - unfit for human viewing even by the bravest of souls.

Crappy code formatting doesn't improve readability either.

good

thanks providing such information Research Paper

Re;

Re;

java is my favorite

java is my favorite programming language since it is platform independent.this web is very good for a beginner. frm caser phentermine 37.5