Tuesday, January 16, 2018

Introduction to Java Platform, Enterprise Edition

Companies need distributed, transactional and portable applications: enterprise java applications fulfill the business logic for the enterprise.

Java platform provides APIs to developers and shortens developement time

The Java Community Process (JCP) writes Java Specification Requests (JSRs) to define the Java EE technologies

The Java EE 7 platform simplifies the programming model:

  • Developers can use annotations in Java source files, instead of optional XML deployment descriptors. The Java EE server reads annotations and configures components at deployment and runtime
  • With dependency Injection, required resources can be injected in component, instead of using JNDI lookup code. Dependency injection can be used in any container type: EJB container, web container and application client container. The container is aware of annotations and inject references to other component/resource

1 Java EE Application Model

Java application model begins with the Java programming language and the Java virtual machine

Java EE applications implement enterprise services for customers, access data from different sources and distribute applications to a variety of clients

The business functions of Java enterprise applications are in the middle tier: the middle tier is usually on a dedicated server hardware.

The Java application model architecture implements the customer's services as multitier applications

The Java model partitions the work in two parts:

  • business logic and presentation implemented by developers
  • standard services provided by Java EE platform

2 Distributed Multitiered Applications

The Java platform uses distributed mutitier application model

  • applications divide logic into components
  • application components are installed on various machines depending on the tier they belong

The figure shows two multitiered Java EE applications divided in tiers:

  • the client tier components on the client machine
  • the web-tier components on the Java server
  • the business tier components on the Java server
  • the Enterprise information tier software on the EIS server

Java EE applications are three-tiered applications because they are distributed over three locations: client machine, the Java server and the database or legacy-machine

  • the three tiered applications extends the two tiered client-server model placing an application server between the client application and back-end storage.
2.1 Security

The Java EE security environment enables security constraints to be defined at deployment time

The Java EE platform provides:

  • declarative access control rules
  • standard login mechanisms
2.2 Java EE Components

A Java EE component is a functional software unit:

  • components are assembled into a Java EE application, with its related classes and files

The Java EE specification defines the following Java EE components:

  • components that run on the client: application clients and applets
  • web components running on the server: Servlets, JavaServer Faces and JavaServer Pages
  • business components running on the server: EJB components or enterprise beans

What is the difference between Java EE components and standard Java classes?

  • Java EE components are assembled into a Java EE application
  • Java EE components must be in compliance with the Java EE specifications
  • Java EE components are managed by a Java EE server
2.3 Java EE Clients

Java client components are either a web client or an application client

Web Clients
  • a web client component consists of two parts: dynamic web pages and a web browser
  • a web client is also called a thin client, because they delegate heavyweight operations to enterprise beans
Application Clients

An application client runs on a client machine and provides user with a richer user interface (GUI)

  • application clients access enterprise beans running on the business tier
The JavaBeans Component Architecture

Server and client tiers may include components based on the JavaBeans architecture

  • JavaBeans components are not considered Java EE components in the Java specification
  • JavaBeans manage the data flow between the following:
    • application clients and components running on the Java EE server
    • server components and a database

Clients Server Communications

The clients communicates with the business tier in the Java EE server in two ways: either directly or going through servlets in the web tier

2.4 Web Components

Java EE web components are either servlets or web pages created with JSF or JSP technologies

  • servlets are Java classes that dinamically process requests and responses
  • JSP pages are text-based documents which allow a natural approch to creating content
  • JSF builds on servlets and JSP and provide a UI component framework for web apps

static HTML pages and utility classes are included with web components but not considered web components

JavaBeans are included in the web tier to manage user input and send data to enterprise beans

2.5 Business Components

The business code is handled by enterprise beans

  • the business code is logic that solves the needs of a particular business domain
  • enterprise beans run either on the business tier or on the web tier
2.6 Enterprise Information System Tier

The enterprise information system tier includes enterprise infrastructure system such as:

  • enterprise resource planning (ERP)
  • mainframe transaction processing
  • database system
  • legacy information system

3 Java EE Containers

Multitiered application are hard to write because they involve a lot of code to handle transactions, security, multithreading et cetera.

Java EE applications are easy to write for two reasons: application logic is organized into reusable components, besides the Java EE server provides services in the form of container for every component type so that developers only have to solve business problem.

3.1 Container Services

Containers are the interface between a component and the functionalities that support the component

To execute a component, it must be assembled into a Java EE module and deployed into its container

The assembly process involves specifying the configuration settings for each component and for the Java application itself

  • the container settings customize the support provided by the Java EE server
  • the Java EE server support includes such services as security, transaction management, Java Naming and Directory Interface (JNDI) API and remote connectivity (RMI) API
3.2 Container Types

The server and containers are:

  • Java EE server: provides EJB and web container
  • EJB container: manages the execution of enterprise beans
  • Web container: manages the execution of web pages and some EJB components
  • Application client container: manages the execution of application client components

4 Web Services Support

Web services are web-based application that use standard based transport protocols to exchange data with clients

  • the Java platform provides APIs and tools to develop web services and clients that interoperate with other web services developed in Java or other languages

5 Java EE Application Assembly and Deployment

A Java EE application is packaged into one or more standard units

  • application units can be deployed to any Java EE compliant system
  • application units contain:
    • application components, such as web pages and enterprise beans
    • an optional deployment descriptor that describes the content

6 Java EE 7 APIs

6.1 Enterprise JavaBeans Technology

An EJB component is a class that implement a unit of business logic

Enterprise beans are either session beans or message-driven beans

New Java EE 7 platform EJB features include EJBLite

6.2 Java Servlet Technology

Java Servlet tecnology allow to define HTTP-specific servlet classes

  • a java servlet class extends the capabilities of web servers

Java EE 7 platform new Java Servlet technology features includes: Nonblocking I/O and HTTP protocol upgrade

6.3 JavaServer Faces Technology (JSF)

JavaServer Faces technology is a user interface framework for building web applications

6.4 JavaServer Pages Technology

JavaServer Pages technology allow to write snippets of servlet code directly into a HTML documents. The Java EE 7 platform recommends the use of Facelets as view technology.

6.5 JavaServer Pages Standard Tag Library

JavaServer Pages Standard Tag Library (JSTL) contains core functionality that are common to JSP applications.

6.6 Java Persistence API (JPA since Java EE 5)

The Java Persistence API (JPA) is the standard API for persisting the state of objects to a relational database, the object-relational mapping (ORM) API. The JPA API can be used in Java SE applications or in the Java EE environament

6.7 Java Transaction API (JTA since j2ee 1.2)

The Java Transaction API (JTA) provides a standard interface for demarcating transactions. The JTA API is useful to demarcate transactions that spans over two separate database access operations.

6.8 Java API for RESTful Web Services (since Java EE 6)

The Java API for RESTful Web Services (JAX-RS) defines APIs for developing web services built following the REST architectural style.

6.9 Managed Beans (since Java EE 6)

Managed Beans are POJO objects treated as managed component by the Java EE container

    Manage Beans:
  • are a generalization of JSF managed bean: JSF managed beans are used only in web modules, Managed Beans can be used anywhere in a Java EE application
  • supports the basic services: resource injection, lifecycle callbacks and interceptors
  • provides a common foundation for the different kinds of component: CDI Beans and EJBs
6.10 Contexts and Dependency Injection for Java EE (CDI since Java EE 6)

Contexts and Dependency Injection for Java EE defines a set of containers' services for using CDI managed beans (for example Managed Beans or EJBs) in web applications.

  • CDI managed beans, compared to Managed Beans, add support for lifecycle scopes and events.
6.11 Dependency Injection for Java (since Java EE 6)

Dependency Injection for Java defines a standard set of annotations for use on injectable classes.

  • The Java CDI API supports Dependency Injection: you can inject beans using @Inject annotation only in a CDI-enabled application.
6.12 Bean Validation (since Java EE 6)

The Bean Validation specification allows the developer to define the validation of data of JavaBeans in a single layer of the application.

6.13 Java Message Service API (JMS since j2ee 1.3)

The Java Message Service API is a standard for messaging that allows Java EE application components to send and receive asynchronous messages.

6.14 Java EE Connector Architecture (JCA since j2ee 1.3)

Java EE Connector Architecture (JCA) is a technology for connecting application servers and enterprise information systems (EIS). The Java EE Connector Architecture API enables tools vendors and system integrators to create resource adapters.

6.15 JavaMail API (since j2ee 1.2)

Java EE applications use the JavaMail API to send email messages.

6.16 Java Authorization Contract for Containers (JACC since j2ee 1.4)

The Java Authorization Contract for Containers specification defines a contract between a Java application server and a provider that creates an authorization policy.

6.17 Java Authentication Service Provider Interface for Containers (JASPIC since Java EE 6)

The Java Authentication Service Provider Interface for Containers (JASPIC) specification defines a service provider interface (SPI) by which authentication providers may be integrated in containers that do message-processing.

6.18 Java API for WebSocket (new in Java EE 7)

WebSocket is a protocol that provides full-duplex communication channels over a single TCP connection: this API enables the creation of WebSocket endpoints using annotations.

6.19 Java API for JSON Processing (JSON-P new in Java EE 7)

JSON is a text-based data format used to exchange data over HTTP. The Java API for JSON Processing enables Java EE applications to handle JSON data using streams.

6.20 Concurrency Utilities for Java EE (new in Java EE 7)

Concurrency Utilities for Java EE is an API that provides asynchronous capabilities to Java EE application components.

6.21 Batch Applications for the Java Platform (new in Java EE 7)

Batch jobs are tasks that can be executed in background, without human supervision.

The Batch Applications for the Java Platform specification is a framework that provides support for handling batch jobs in Java applications.

7 Java Standard Edition 7 APIs for the Java EE platform

Java EE 7 applications require Java Standard Edition (Java SE 7) APIs

  • Java EE includes Java SE and make the Java SE available to Java EE applications
7.1 Java Database Connectivity API

The Database Connectivity (JDBC) API allows the invocation of SQL commands in Java code

7.2 Java Naming and Directory Interface API

The Java Naming and Directory Interface (JNDI) API enables applications to access multiple naming and directory services, such as LDAP, DNS and NIS.

  • With JNDI, Java applications can store and retrieve any type of Java object

JNDI naming environment

  • JNDI provide components (applications clients, enterprise beans and web components) with access to a JNDI naming environment
  • the naming environment allows a component to be customized without the need to change the component's source code
  • the container implements the component's JNDI environment and the component has access to its JNDI environment as a naming context

Naming Environment Namespaces

  • The naming environment has four logical namespaces:
    • java:comp for objects available to a component
    • java:module for objects available to a module
    • java:app for objects available to a single application
    • java:global for objects shared by all deployed applications

a component can access user-defined or system-provided JNDI named objects

  • system-provided objects names such as default JDBC DataSource object, default JMS connection factory and JTA UserTransaction object are stored in the java:comp namespace
  • user-defined objects are: enterprise beans, JDBC DataSource objects and JMS destination.

A Java EE component locates its environment naming context using the JNDI interfaces: a component creates an InitialContext object and look up the JNDI context under the java:comp/env name.

7.3 JavaBeans Activation Framework

The JavaBeans Activation Framework (JAF) is used by the JavaMail API

7.4 Java API for XML Processing

The Java API for XML Processing (JAXP) supports the SAX and DOM standards for parsing XML documents and can convert XML data using the XSLT standard

7.5 Java Architecture for XML Binding (JAXB)

The Java Architecture for XML Binding binds an XML schema to a representation in Java language

7.6 Java API for XML Web Services (JAX-WS)

The Java API for XML Web Services provides support for web services. The JAX-WS specification supports the implementation of web service endpoints and web service clients.

The JAX-WS specification supports the use of message handlers for processing message requests and responses

7.7 SOAP with Attachments API for Java (SAAJ)

The SOAP with Attachments API for Java is API that enables JAX-WS services to produce and consume SOAP messages.

7.8 Java Authentication and Authorization Service (JAAS)

Authentication is the process of identifying a user or an application and validating their credentials. This means verifying that the user is who it claims to be. For example, a user to be authenticate provides its credentials in the form of a username and a password.

Once the user has been authenticated it must be authorized. Authorization is the process of determining which resources an authenticated user is permitted to access. The simplest way to implement authorization is using an access control list. The access control list assigns users to permission to access a resource.

The Java Authentication and Authorization Service enables Java EE applications to authenticate users and groups to make sure they have the permissions required to execute the Java code

7.9 Common Annotations for the Java Platform

Annotations enable a declarative programming style in Java

Friday, October 20, 2017

Java EE Component Model

Component-Based Development

The Java EE platform supports component-based development
  • component-based development is based on the principle of loose coupling
  • loosely coupled systems are easy to test and maintain

Java EE Components

A Java EE component is a group of classes and interfaces that implements a single functionality

  • a component is assembled into a Java EE application, with its related classes and files
The java EE specifications define different types of components:
  • Application clients and applets are components that run on the client.
  • Java Servlet, JavaServer Faces, and JavaServer Pages (JSP) technology components are web components that run on the server.
  • EJBs components (enterprise beans) and Entity classes are business components that run on the server.

Friday, September 15, 2017

Requirements of Java EE 7 Enterprise Applications

The Java EE technology elements

Java EE specifications incorporates a group of other technologies and specifications to provide server-side functionalities for enterprise application developers

  • application components: these components allow creation of application business logic and control elements
  • integration: integration elements allow to interact with the functionalities from other applications and systems
  • container management: these elements provide runtime support for Java EE application components

Monday, August 28, 2017

Creating a Simple Maven Project

Generating a simple project with Maven and Eclipse

To generate a project with maven from command line:

 mvn -B archetype:generate \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DgroupId=com.mycompany.application \
-DartifactId=maven-project \
-Dpackage=com.mycompany.application \
  • the maven command mvn archetype:generate executes the generate goal of the archetype plugin
  • an archetype is defined as a "model from which similar thing are patterned; a prototype"
  • when you run the archetype:generate goal you pass the goal parameter: archetypeArtifactId=maven-archetype-quickstart
  • you can select the maven archetypes that fits your purpose
  • the maven-archetype-quickstart is the most basic archetype to create a java project

Friday, July 21, 2017

Serializing Java Objects with JAXB

Java Model

The model class Team

package j2s.team;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

 * As the class Team is annotated with @XmlRootElement, JAXB transforms a Team 
 * instance into an XML document whose root element is <team> 
public class Team {

 // fields
 private String name;
 private String description;

 @XmlElement(name = "player")
 @XmlElementWrapper(name = "players")
 private List<Player> playerList;

 public Team(String name, String description) {
  this.name = name;
  this.description = description;
  this.playerList = new ArrayList<Player>();

 // default constructor required by JAXB
 public Team() {
  this("team name", "team descr");

 public String getName() {
  return name;

 public void setName(String name) {
  this.name = name;

 public String getDescription() {
  return description;

 public void setDescription(String description) {
  this.description = description;

 public List<Player> getPlayers() {
  return playerList;

 public void setPlayers(List<Player> playerList) {
  this.playerList = playerList;

 public void addPlayer(Player dev) {

 public String toString() {
  return "Team [name=" + name + ", description=" + description + ", players=" + playerList + "]";


Tuesday, June 6, 2017

Web Services Introduction

Web Service definition

  • A web service is a distributed software whose components can be deployed and executed on distinct devices.
  • A web service consists of a service (a.k.a. producer) and a client (a.k.a. consumer or requester).

Web service terminology

  • message: the basic unit of communication between a web service and a requester; data to be communicated to or from a web service as a single logical transmission.
  • operation: a description of an action supported by the service; a set of messages related to a single web service action.
  • synchronous: an interaction is said to be synchronous when the participating agents must be available to receive and process the associated messages from the time the interaction is initiated until all messages are actually received or some failure condition is determined.
  • asynchronous: an interaction is said to be asynchronous when the associated messages are chronologically and procedurally decoupled.

Web Service over HTTP

  • A web service is an application typically running over HTTP.
  • A web service over HTTP is a web service that uses the HTTP as transport, HTTP messages are infrastructure.

Web Service architecture

  • The architecture of a simple web service is a client and a server.
  • The architecture of a complicated web service have many clients and a service composed of other services.
    • for example, an e-commerce service can be composed of multiple software components (each hosted on a separate web server) and any combination of PCs, tablets, mobile phones and other networked devices may host programs that make requests to the service.

Web services come in two flavors: SAOP-based and REST-style.

  • SOAP is an XML dialect that specifies the structure of XML documents that count as SOAP envelopes
  • A REST-style service is one that treats HTTP not only as a transport infrastructure but also as a set of guidelines for making service requests and responses.

Web service features

  • Web service major attractiveness is interoperability: clients and services can interact despite differences in programming languages, operating systems and hardware platforms.
  • XML and JSON are web service data interchange formats that provide an intermediary level and handle the differences in data types between different programming languages.

Tuesday, May 9, 2017

How to validate an xml document against a schema

STEP 1: Create the XML

To create a new XML file, start eclipse IDE and choose File->New->Other->XML file->Create XML file from XML template.
Model the data of interest in the XML file. In my case, I have created the following XML.

The teams.xml file

<?xml version="1.0" encoding="UTF-8"?>
 <name>the great team</name>
 <description>these team was recruited by google to develop a secret product</description>
  <developer role="frontend">
   <name>John Carrot</name>
  <developer role="backend">
   <name>Joshua Allock</name>
  <developer role="frontend">
   <name>Brendan Tich</name>