The Source for Java Technology Collaboration
User: Password:



Ludovic Champenois's Blog

Ludovic Champenois (ludo) is a senior staff engineer at Sun Microsystems since 1996, working in between the Application Server (J2EE) organization and the Java tools organization. He is currently the chief architect for the J2EE developer support effort in the NetBeans open source project (http://j2ee.netbeans.org). This project is used as the foundation for other tools like Sun Java Creator and Sun Java Studio.



Worst ever Java EE 6 Blog

Posted by ludo on May 25, 2009 at 09:09 PM | Permalink | Comments (7)

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





GlassFish Tooling

Posted by ludo on May 22, 2009 at 06:19 AM | Permalink | Comments (0)

Bonjour Comment Java?

Finally the GlassFish Tools bundle for Eclipse has been released: version 1.0 is available at http://download.java.net/glassfish/eclipse

The Tools bundle contains:
  • Eclipse 3.4.2 IDE with WTP Java EE support
  • GlassFish v2.1 pre-registered and configured
  • GlassFish v3 Prelude pre-registered and configured
  • JavaDB sample database pre-registered and configured
  • GlassFish Plugins
  • GlassFish documentation
  • And optionally, a JDK 1.6.
splash1.0.bmp

Installers are available for Windows and MacOSX, and tar.gz for Linux systems.
If you have used a preview release (pre version 1.0), make sure to read the release notes  http://download.java.net/glassfish/eclipse/releasenotes.html for information about a known issue regarding reusing workspaces created with preview releases. Commercial support for this bundle is also available at http://developers.sun.com/services/buying_guide.jsp.

I like the fact that the GlassFish servers are preconfigured, ready to use, even with a sample Java DB database pre registered (to use the JPA entities tooling). By default the "Deploy On Save" feature is enabled for GlassFish v3 Prelude: it means that the moment you save a Servlet, a utility class, or even an EJB, the application is automatically updated and redeployed to the running server in an optimal way. If you want to play with leading edge bits for GlassFish v3 Java EE 6, you can even register and download from the bundle a promoted build (happens every week) of the latest GlassFish v3 server. We will demo more about that at the JavaOne conference.

We are now working on a version 1.1 (and nightly builds will be available next week. Version 1.1 will add the OpenSolaris 2009.05 target, an optimized installer with Pack200 technology to reduce the download size by almost 2!, the JAX-WS and JAX-RS Jersey RestFul GlassFish Plugins, Maven 2, a MySQL JDBC driver pre registered for better out of the box experience with the MySQL database, the entire Java EE 6 Javadoc available in the Eclipse Editor code completion, and of course the latest bug fixes.

I hope to see you at JavaOne 2009.
Ludo




EclipseCon GlassFish Slides

Posted by ludo on March 25, 2009 at 12:18 PM | Permalink | Comments (0)

Bonjour, Comment Java?
EclipseCon 2009 GlassFish Java EE 5, Eclipse Slides are now online:


and:


Ludo

GlassFish and Eclipse

Posted by ludo on March 24, 2009 at 12:26 AM | Permalink | Comments (0)

Bonjour Comment Java?

In a few hours, I'll be attending the EclipseCon Conference as a speaker. I've 2 talks, one around the GlassFish v2.1, v3 Prelude and GlassFish v3 Eclipse Bundle tooling (both for Java EE 5 and the coming Java EE 6) support, and the other one more targeted to GlassFish v3, its OSGi kernel (either Felix or Equinox) and some cool demos using Java EE features like EJBs inside a Web Application, or the new @Singleton or @Schedule timer annotations.

Also, this year, Sun Microsystems is a Gold Sponsor of the EclipseCon conference, so if you are around make sure you stop at our booths (GlassFish, OpenSolaris and JavaFX)
and see what has been done to better support Eclipse users.


q    e



Talk to you soon.





May 2009
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            


Search this blog:
  

Categories
Business
Community: Glassfish
Community: Java Enterprise
Community: Java Patterns
Community: Java Specification Requests
Community: Java Tools
Community: Java User Groups
Community: Java Web Services and XML
Community: JavaDesktop
Community: JDK
Community: linux.java.net
Community: Mac Java Community
Community: NetBeans
Community: Portlet
Databases
Deployment
Distributed
Eclipse
Extreme Programming
J2EE
J2ME
J2SE
JavaOne
JSR
Linux
Open Source
OpenSolaris
Patterns
Programming
Swing
Tools
Web Applications
Web Services and XML
Archives

May 2009
March 2009
November 2008
October 2008
September 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
November 2007
September 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
June 2006
May 2006
January 2006
November 2005
October 2005
September 2005
June 2005
May 2005
April 2005

Recent Entries

Worst ever Java EE 6 Blog

GlassFish Tooling

EclipseCon GlassFish Slides



Powered by
Movable Type 3.01D


 Feed java.net RSS Feeds