Sunday, 28 June 2015

JCP

The JCP is an open organization, created in 1998 by Sun Microsystems, that is involved in the definition of future versions and features of the Java platform. When the need for standardizing an existing component or API is identified, the initiator (a.k.a. specification lead) creates a JSR and forms a group of experts. This group, made of companies’ representatives, organizations, universities, or private individuals, is responsible for the development of the JSR and has to deliver:
  • One or more specifications that explain the details and define the fundamentals of the JSR (Java Specification Requests (JSRs) are the actual descriptions of proposed and final specifications for the Java platform. At any one time there are numerous JSRs moving through the review and approval process. A simple way to stay up to date and track the JSRs in each stage of review is to join the Mailing List.),
  • A Reference Implementation (RI), which is an actual implementation of the specification,
  • Compatibility Test Kit (a.k.a. Technology Compatibility Kit, or TCK), which is a set of tests every implementation needs to pass before claiming to conform to the specification.
Once approved by the executive committee (EC), the specification is released to the community for implementation.

Reference:

Beginning Java EE 7

Annotations and Deployment Descriptors

Annotations and Deployment Descriptors

In programming paradigm, there are two approaches: imperative programming and declarative programming. Imperative programming specifies the algorithm to achieve a goal (what has to be done), whereas declarative programming specifies how to achieve this goal (how it has to be done). In Java EE, declarative programming is done by using metadata, that is, annotations or/and deployment descriptors.

As you’ve seen earlier, components run in a container and this container gives the component a set of services. Metadata are used to declare and customize these services and associates additional information along with Java classes, interfaces, constructors, methods, fields or parameters.

Since Java EE 5, annotations have been proliferating in the enterprise platform. They decorate your code (Java classes, interfaces, fields, methods . . .) with metadata information. Listing 1-1 shows a POJO (Plain Old Java Object) that declares certain behavior using annotations on the class and on an attribute (more on EJBs, persistence context and annotations in the coming posts).

Listing 1-1.  An EJB with Annotations

@Stateless
@Remote(ItemRemote.class)
@Local(ItemLocal.class)
@LocalBean
public class ItemEJB implements ItemLocal, ItemRemote {

  @PersistenceContext (unitName = "chapter01PU")
  private EntityManager em;

  public Book findBookById(Long id) {
    return em.find(Book.class, id);
  }
}

The other manner of declaring metadata is by using deployment descriptors. A deployment descriptor (DD) refers to an XML configuration file that is deployed with the component in the container. Listing 1-2 shows an EJB deployment descriptor. Like most of the Java EE 7 deployment descriptors, it defines the http://xmlns.jcp.org/xml/ns/javaee namespace and contains a version attribute with the version of the specification.

Listing 1-2.  An EJB Deployment Descriptor

<ejb-jar xmlns=" http://xmlns.jcp.org/xml/ns/javaee " →

         xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " →

         xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee →

                             http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd " →

         version="3.2" >

 

  <enterprise-beans>

    <session>

      <ejb-name>ItemEJB</ejb-name>

      <remote>org.agoncal.book.javaee7.ItemRemote</remote>

      <local>org.agoncal.book.javaee7.ItemLocal</local>

      <local-bean/>

      <ejb-class>org.agoncal.book.javaee7.ItemEJB</ejb-class>

      <session-type>Stateless</session-type>

      <transaction-type>Container</transaction-type>

    </session>

  </enterprise-beans>

</ejb-jar>

Deployment descriptors need to be packaged with the components in the special META-INF or WEB-INF directory to be taken in account. Table 1-1 shows the list of the Java EE deployment descriptors and the related specification (more on that in the coming chapters).

Table 1-1. Deployment Descriptors in Java EE
File
Specification
Paths
application.xml
Java EE
META-INF
application-client.xml
Java EE
META-INF
beans.xml
CDI
META-INF or WEB-INF
ra.xml
JCA
META-INF
ejb-jar.xml
EJB
META-INF or WEB-INF
faces-config.xml
JSF
WEB -INF
persistence.xml
JPA
META-INF
validation.xml
Bean Validation
META-INF or WEB-INF
web.xml
Servlet
WEB-INF
web-fragment.xml
Servlet
WEB-INF
webservices.xml
SOAP Web Services
META-INF or WEB-INF

Since Java EE 5 most deployment descriptors are optional and you can use annotations instead. But you can also use the best of both for your application. The biggest advantage of annotations is that they significantly reduce the amount of code a developer needs to write, and by using annotations you can avoid the need for deployment descriptors. On the other hand, deployment descriptors are external XML files that can be changed without requiring modifications to source code and recompilation. If you use both, then the metadata are overridden by the deployment descriptor (i.e., XML takes precedence over annotations) when the application or component is deployed.

 Note  In today’s development annotations are preferred over deployment descriptors in Java EE. That is because there is a trend to replace a dual language programming (Java + XML) with only one (Java). This is also true because it's easy to analyze and prototype an application, when everything (data, methods, and metadata with annotations) is in one place.

Java EE uses the notion of Programming by Exception (a.k.a. Convention over Configuration) so that most of the common behavior doesn’t need to be declared with metadata (“programming metadata is the exception, the container takes care of the defaults”). Which means that with only a small amount of annotations or XML the container can give you a default set of services with default behavior.

Reference:

Beginning Java EE 7



Saturday, 27 June 2015

Packaging of components

Packaging

To be deployed in a container, components have first to be packaged in a standard formatted archive(jar, war or ear). Java SE defines Java Archive (jar) files, which are used to aggregate many files (Java classes, deployment descriptors, resources, or external libraries) into one compressed file (based on the ZIP format). As seen in Figure, Java EE defines different types of modules that have their own packaging format based on this common jar format.

Figure: Archives(war and jar) in containers

Note:

A JAR (Java ARchive) file is a file that contains the class, image, and sound files for a Java application or applet gathered into a single file and possibly compressed.
WAR file (or Web application ARchive) is a JAR file used to distribute a collection of JavaServer Pages, JavaServlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web application.
An EAR (Enterprise ARchive) is a file format used by Java EE for packaging one or more modules into a single archive so that the deployment of the various modules onto an application server happens simultaneously and coherently.
  • An application client module contains Java classes and other resource files packaged in a jar file. This jar file can be executed in a Java SE environment or in an application client container. Like any other archive format, the jar file contains an optional META-INF directory for meta information describing the archive. The META-INF/MANIFEST.MF file is used to define extension- and package-related data. If deployed in an ACC, the deployment descriptor can optionally be located at META-INF/application-client.xml.
  • An EJB module contains one or more session and/or message-driven beans (MDBs) packaged in a jar file (often called an EJB jar file). It contains an optional META-INF/ejb-jar.xml deployment descriptor and can be deployed only in an EJB container.
  • A web application module contains servlets, JSPs, JSF pages, and web services, as well as any other web-related files (HTML and XHTML pages, Cascading Style Sheets (CSS), Java-Scripts, images, videos, and so on). Since Java EE 6, a web application module can also contain EJB Lite beans (a subset of the EJB API which will be described later). All these artifacts are packaged in a jar file with a .war extension (commonly referred to as a war file, or a Web Archive). The optional web deployment descriptor is defined in the WEB-INF/web.xml file(For a Java servlet to be accessible from a browser, you must tell the servlet container what servlets to deploy, and what URL's to map the servlets to. This is done in the web.xml file of your Java web application). If the war contains EJB Lite beans, an optional deployment descriptor can be set at WEB-INF/ejb-jar.xml. Java.class files are placed under the WEB-INF/classes directory and dependent jar files in the WEB-INF/lib directory.
  • An enterprise module can contain zero or more web application modules, zero or more EJB modules, and other common or external libraries. All this is packaged into an enterprise archive (a jar file with an .ear extension) so that the deployment of these various modules happens simultaneously and coherently. The optional enterprise module deployment descriptor is defined in the META-INF/application.xml file. The special lib directory is used to share common libraries between the modules.
Reference:

Beginning Java EE 7



Network Protocols

Network Protocols


Figure: Services provided by containers


As shown in Figure, components deployed in containers can be invoked through different protocols. For example, a servlet deployed in a web container can be called with HTTP as well as a web service with an EJB endpoint deployed in an EJB container. Here is the list of protocols supported by Java EE:

  • HTTP: HTTP is the Web protocol and is ubiquitous in modern applications. The client-side API is defined by the java.net package in Java SE. The HTTP server-side API is defined by servlets, JSPs, and JSF interfaces, as well as SOAP and RESTful web services.
  • HTTPS is a combination of HTTP and the Secure Sockets Layer (SSL) protocol.
  • RMI-IIOP : Remote Method Invocation (RMI) allows you to invoke remote objects independently of the underlying protocol. The Java SE native RMI protocol is Java Remote Method Protocol (JRMP). RMI-IIOP is an extension of RMI used to integrate with CORBA. Java interface description language (IDL) allows Java EE application components to invoke external CORBA objects using the IIOP protocol. CORBA objects can be written in many languages (Ada, C, C++, Cobol, etc.) as well as Java.
Reference:

Beginning Java EE 7



Services provided by Containers

Services

Figure: Services provided by containers


Containers provide underlying services to their deployed components. As a developer, containers allow you to concentrate on implementing business logic rather than solving technical problems faced in enterprise applications. Figure shows you the services provided by each container. For example, web and EJB containers provide connectors to access EIS, but not the applet container or the ACCs. Java EE offers the following services:

  • Java Transaction API: This service offers a transaction demarcation API used by the container and the application. It also provides an interface between the transaction manager and a resource manager at the Service Provider Interface (SPI) level.
  • Java Persistence API : Standard API for object-relational mapping (ORM). With its Java Persistence Query Language (JPQL), you can query objects stored in the underlying database.
  • Validation: Bean Validation provides class and method level constraint declaration and validation facilities.
  • Java Message Service : This allows components to communicate asynchronously through messages. It supports reliable point-to-point (P2P) messaging as well as the publish-subscribe (pub-sub) model.
  • Java Naming and Directory Interface: This API, included in Java SE, is used to access naming and directory systems. Your application uses it to associate (bind) names to objects and then to find these objects (lookup) in a directory. You can look up data sources, JMS factories, EJBs, and other resources. Omnipresent in your code until J2EE 1.4, JNDI is used in a more transparent way through injection.
  • JavaMail : Many applications require the ability to send e-mails, which can be implemented through use of the JavaMail API.
  • JavaBeans Activation Framework: The JAF API, included in Java SE, provides a framework for handling data in different MIME types. It is used by JavaMail.
  • XML processing: Most Java EE components can be deployed with optional XML deployment descriptors, and applications often have to manipulate XML documents. The Java API for XML Processing (JAXP) provides support for parsing documents with SAX and DOM APIs, as well as for XSLT. The Streaming API for XML (StAX) provides a pull-parsing API for XML.
  • JSON processing: New in Java EE 7 the Java API for JSON Processing (JSON-P) allows applications to parse, generate, transform, and query JSON.
  • Java EE Connector Architecture: Connectors allow you to access EIS from a Java EE component. These could be databases, mainframes, or enterprise resource planning (ERP) programs.
  • Security services: Java Authentication and Authorization Service (JAAS) enables services to authenticate and enforce access controls upon users. The Java Authorization Service Provider Contract for Containers (JACC) defines a contract between a Java EE application server and an authorization service provider, allowing custom authorization service providers to be plugged into any Java EE product. Java Authentication Service Provider Interface for Containers (JASPIC) defines a standard interface by which authentication modules may be integrated with containers so that these modules may establish the authentication identities used by containers.
  • Web services: Java EE provides support for SOAP and RESTful web services. The Java API for XML Web Services (JAX-WS), replacing the Java API for XML-based RPC (JAX-RPC), provides support for web services using the SOAP/HTTP protocol. The Java API for RESTful Web Services (JAX-RS) provides support for web services using the REST style.
  • Dependency Injection: Since Java EE 5, some resources (data sources, JMS factories, persistence units, EJBs . . .) can be injected in managed components. Java EE 7 goes further by using CDI as well as the DI (Dependency Injection for Java) specifications.
  • Management: Java EE defines APIs for managing containers and servers using a special management enterprise bean. The Java Management Extensions (JMX) API is also used to provide some management support.
  • Deployment: The Java EE Deployment Specification defines a contract between deployment tools and Java EE products to standardize application deployment.
Reference:

Beginning Java EE 7



Containers

Containers





The Java EE infrastructure is partitioned into logical domains called containers (see Figure). Each container has a specific role, supports a set of APIs, and offers services to components (security, database access, transaction handling, naming directory, resource injection). Containers hide technical complexity and enhance portability. Depending on the kind of application you want to build, you will have to understand the capabilities and constraints of each container in order to use one or more. For example, if you need to develop a web application, you will develop a JSF tier with an EJB Lite tier and deploy them into a web container. But if you want a web application to invoke a business tier remotely and use messaging and asynchronous calls, you will need both the web and EJB containers. Java EE has four different containers:

  • Applet containers are provided by most web browsers to execute applet components. When you develop applets, you can concentrate on the visual aspect of the application while the container gives you a secure environment. The applet container uses a sandbox security model where code executed in the “sandbox” is not allowed to “play outside the sandbox.” This means that the container prevents any code downloaded to your local computer from accessing local system resources, such as processes or files.
  • The application client container (ACC) includes a set of Java classes, libraries, and other files required to bring injection, security management, and naming service to Java SE applications (swing, batch processing, or just a class with a main() method). The ACC communicates with the EJB container using RMI-IIOP and the web container with HTTP (e.g., for web services).
  • The web container provides the underlying services for managing and executing web components (servlets, EJBs Lite, JSPs, filters, listeners, JSF pages, and web services). It is responsible for instantiating, initializing, and invoking servlets and supporting the HTTP and HTTPS protocols. It is the container used to feed web pages to client browsers.
  • The EJB container is responsible for managing the execution of the enterprise beans (session beans and message-driven beans) containing the business logic tier of your Java EE application. It creates new instances of EJBs, manages their life cycle, and provides services such as transaction, security, concurrency, distribution, naming service, or the possibility to be invoked asynchronously.
Reference:

Beginning Java EE 7



Architecture of Java EE

Java EE is nothing more than a set of specifications implemented by several containers. Containers are Java EE runtime environments which supply certain services to the components they host like concurrency, dependency injection, life-cycle management etc. These components communicate with other components and the Java EE infrastructure using well-defined contracts. Packaging is done using a standard way (using directory structure that is compressed into archive files) before deploying. Java SE APIs can be used by any Java EE componentsas Java EE is a superset of the Java SE platform.

Figure: Standard Java EE containers


Figure shows the logical relationships between various containers. Protocols like RMI-IIOP, HTTP, SSL etc are used by one container to access another.