Interceptors
❖ Defining an Interceptor
● An interceptor method may be defined on an enterprise bean class or on an
interceptor class associated with the bean.
● An interceptor class is a class which is distinct from the bean class itself and
whose methods are invoked in response to business method invocations and /or
lifecycle events on the bean.
● Any number of interceptor classes may be defined for a bean class.
● It is possible to carry state across multiple interceptor method invocations for a
single business method invocation or lifecycle callback event for a bean in the
context data of the InvocationContext object.
● An interceptor class must have a public no-arg constructor.
● Interceptors can be defined in Java as a method interceptor or a class
interceptor.
● The preferred way to define in Java code is by using meta data annotations.
● They can be defined in the application descriptor as well but in that case they
are not portable across Java EE servers.Some of the meta data annotations
found in the javax.interceptor package are @AroundInvoke,@AroundTimeout
,@PostConstruct and @PreDestroy.
● An AroundInvoke method can be defined on
Enterprise bean class
Interceptor class
AroundInvoke method
An AroundInvoke method can be defined by either of the following
Annotating the method with an @AroundInvoke annotation
Using an element in bean’s deployment descriptor
An AroundInvoke method must satisfy the following requirements
-One AroundInvoke method is allowed for each class.
-It must have a no argument public constructor.
-It must accept javax.interceptor.InvocationContext object as an argument and return a
java.lang.Object object.
-It can through any application exception tht is specified in the business interface
It can be declared private,protected or public.
❖ Life cycle of Interceptor
● The lifecycle of an interceptor instance is the same as that of the bean instance with which
it is associated.
● When the bean instance is created, interceptor,instances are created for each interceptor
class defined for the bean,
● These interceptor instances are destroyed when the bean instance is removed.
● In the case of interceptors associated with stateful session beans, the interceptor instances
are passivated upon bean instance passivation and activated when the bean instance is
activated.
● Both the Interceptor instance and the bean instance are created or activated before any of
the respective PostConstruct or PostActivate callbacks are invoked.
● Any PreDestroy and PrePassivate callbacks are invoked before the respective destruction
or passivation of either the bean instance or interceptor instance.
● An interceptor instance may hold state. An interceptor instance may be the target of
dependency injection. Dependency injection is performed when the interceptor instance is
created.
● The PostConstruct interceptor callback method is invoked after this dependency Injection
has taken place on both the interceptor instances and the bean instance.
● Interceptors can invoke JNDI,JDBC,JMS, other enterprise beans and EntityManager.
JNDI
● The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that
allows Java software clients to discover and look up data and objects via a name.
JNDI Naming
● In a distributed application, components need to access other components and resources
such as databases.
● For example, a servlet might invoke remote methods on an enterprise bean that retrieves
information from a database.
● The Java Naming and Directory Interface (JNDI) naming service enables components to
locate other components and resources.
● To locate a JDBC resource, for example, an enterprise bean invokes the JNDI lookup
method.
● The JNDI naming service maintains a set of bindings that relate names to objects.
● The lookup method passes a JNDI name parameter and returns the related object.
Naming Service
● A naming service is an entity that
i. associates names with objects. We call this binding names to objects. This is similar to
a telephone company ’s associating a person ’s name with a specific residence ’s telephone
number.
ii. provides a facility to find an object based on a name. We call this looking up or
searching for an object. This is similar to a telephone operator finding a person ’s
telephone number based on that person ’s name and connecting the two people.
Directory Service
● A directory object differs from a generic object because we can store attributes with
directory objects.
● For example,we can use a directory object to represent a user in your company.we can
store information about that user,like the user ’s password,as attributes in the directory
object.
● A directory service is a naming service that has been extended and enhanced to provide
directory object operations for manipulating attributes.
● A directory is a system of directory objects that are all connected. Some examples of
directory products are Netscape Directory Server and Microsoft ’s Active Directory.
● Directories are similar to DataBases, except that they typically are organized in a
hierarchical tree-like structure.
● Examples of Directory Service
1. Netscape Directory Server
2. Microsoft ’s Active Directory
3. Lotus Notes (IBM)
4. NIS (Network Information System) by Sun
5. NDS (Network Directory Service) by Novell
6. LDAP (Lightweight Directory Access Protocol) most commonly used
LDAP
● LDAP stands for Lightweight Directory Access Protocol.
● It is a lightweight client-server protocol for accessing directory services, specifically
X.500-based (electronic)directory services.
● LDAP runs over TCP/IP or other connection oriented transfer services.
● A directory is similar to a database, but tends to contain more descriptive, attribute-based
information.
● give quick-response to high-volume lookup or search operations
● They may have the ability to replicate information widely in order to increase availability
and reliability, while reducing response time.
JNDI Architecture
JNDI Packages
javax.naming -Accesses simple naming services.
javax.naming.directory -Accesses directory services.
javax.naming.event -Handles event notification when dealing with naming and directory
services.
javax.naming.ldap - Deals with LDAP v3 controls and extended operations.
javax.naming.spi -Consists of the Service Provider Interface (SPI) classes and interfaces
used by LDAP service implementers to provide access to a specific type of naming or
directory service.
● The Java Naming and Directory Interface(JNDI) is an application programming
interface(API) that provides naming and directory functionality to applications written
using the Java programming language.
● JNDI is an API specified in Java technology that provides naming and directory
functionality to applications written in the Java programming language.
● The API provides:
- A mechanism to bind an object to a name.
- A directory lookup interface that allows general queries.
- An event interface that allows clients to determine when directory entries have been
modified.
- LDAP extensions to support the additional capabilities of an LDAP service.
Basic Lookup:
● JNDI organizes its names into a hierarchy.
● A name can be any string such as “com.mypack.ejb.TBean”.
● A name can also be an object that implements the Name interface,however a string is
the most common way to name an object.
● A name is bound to an object in the directory by storing either the object or a
reference to the object in the directory service identified by the name.
● The Context class is the core of the JNDI API.We use it to perform any lookup and to
add any new name-value associations.When you use JNDI ,you typically create an
InitialContext object first.
Context ctx=new InitialContext();
The InitialContext constructor looks for a system property called
java.naming.factory .
● The initial that contains the name of the class that creates the InitialContext.
● To look up an object from the naming service use Context.lookup() and pass it the
name of the object that you want to retrieve .
Object obj=ctx.lookup(“cn=tilak,ou=People”);
JNDI Namespace in Java EE7
Three JNDI namespaces are used for JNDI lookups:java:global,java:module and java:app.
1.The java:module JNDI namespace
The java:module namespace is used to lookup local enterprise beans within the same
module.JNDI addresses using the java:modulenamespace are of the following form:
java:module/enterprise bean name/interface name
2.The java:app JNDI Namespace
The java:app namespace is used to lookup local enterprise beans packaged within the
same application.
java:app[/module name]/enterprise bean name/interface name
3.The java:global JNDI Namespace
The java:global JNDI namespace is the portable way of finding remote enterprise beans
using JNDI lookups.
java:global/application name/module name/enterprise bean name/interface name
Resources and JNDI
In a distributed application, components need to access other components and resources
such as databases.
For example, a servlet might invoke remote methods on an enterprise bean that retrieves
information from a database.
A resource is program object that provides connections to systems such as database servers
and messaging systems. Each resource object is identified by a unique, people-friendly
name called the JNDI name.
A resource object and its JNDI name are bound together by the naming and directory
service. To create a new resource a new name/object binding is entered into the JNDI
namespace. You inject resources by using @Resource annotation in an application.
Resource Injection
The javax.annotation.Resource annotation is used to declare a refrence to a
resource,@Resource can decorate a class ,a field or a method.The container will inject the
resource referred to by @Resource into the component either at runtime or when the
component is initialized,depending on whether field /method injection or class injection is
used.
The @Resource annotation has following elements:
-name:The JNDI name of the resource.
-type: The Java language type of the resource
authentication type: The authentication type to use for the resource
sharable: Indicates whether resource can be shared
mappedName:A non-portable, implementation specific name to which the resource should
be mapped.
Description: The description of the resource
Datasource Resource Defination in Java EE
Datasource resources are used to define a set of properties required to identify and access
a database through the JDBC API.These properties include information such as the URL of
the datatbase server, the name of the database and network protocol to use to communicate
with the server.
java:comp-Names in this namespace have per component visibility.
java:module-Names in this namespace are shared by all components in a module for
example the EJB components defined in an ejb-jar.xml file.
java:app-Names in this namespace are shared by all components and modules in an
application for example,the application-client
java:global-Names in this namespace are shared by all applications in the server.