Presentation-tier request handling mechanism receives different type of requests. They require various type of processing such as authentication checking, validation checking, modifying, auditing. Therefore, preprocessing and post processing of a client request is required. In the normal scenario, we can do this by conditional checks and with a invaild check, abort the request. But this is not a good solution because, we have to change the code according to the type of request. Therefore, the best solution is to add and remove components, these specific components complete specific filtering actions.
Therefore, when a client send a request, FilterManager manages the needed filters according to the order. It is the FilterChain with appropriate filters. It is an ordered collection of independent filters. The Filter chain coordinates their processing that are mapped to a target. The target is the resource that client requests.
Thursday, December 30, 2010
Sunday, December 26, 2010
Apache Maven-What is a POM?
The fundamental unit of work in Maven is POM, Project Object Model. It is a XML file containing the project and configuration details used by Maven to build the project. When executing a task, Maven looks for the POM in the current directory to get needed configuration information such as project dependencies, the plugins, goals and the build profiles. POM contains other information such as project version, description, developers and mailing lists.
The Super POM is Maven's default POM. All POMs we create extend the Super POM and Super POM is inherited by the POMs. Minimum requirements for a POM are the project root, modelVersion, groupId, artifactId, version. Here is a example of a POM file with minimum requirements.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
If the configuration details are not specified, Maven will use their defaults. Here, with the minimum requirements, repositories are not defined. Therefore, when it is built, it will inherit the repositories configuration from Super POM.
If we want to add an another artifact to our project, my-app, it will be something like this.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
If we want to make the my-app project the parent of newly created artifact, we have to edit the newly created artifact module's pom file as below to include the parent's section.
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
The Super POM is Maven's default POM. All POMs we create extend the Super POM and Super POM is inherited by the POMs. Minimum requirements for a POM are the project root, modelVersion, groupId, artifactId, version. Here is a example of a POM file with minimum requirements.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
If the configuration details are not specified, Maven will use their defaults. Here, with the minimum requirements, repositories are not defined. Therefore, when it is built, it will inherit the repositories configuration from Super POM.
If we want to add an another artifact to our project, my-app, it will be something like this.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
If we want to make the my-app project the parent of newly created artifact, we have to edit the newly created artifact module's pom file as below to include the parent's section.
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
Wednesday, December 22, 2010
EntityManager
EntityManager manages entities. The javax.persistence.EntityManager instances represent the entity manager. Each EntityManager instance is associated with a persistent context. A persistence context defines the scope under which particular entity instances are created, persisted, and removed and it is a set of managed entity instances that exist in a particular data source. The EntityManager interface defines the methods that are used to interact with the persistence context like creating and removing persistent entity instances, finding entities by the entity’s primary key, and allowing queries to be run on entities.
Container-Managed Entity Managers
EntityManager instance’s persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction. The Java EE container manages the life cycle of container-managed entity managers.
To obtain an EntityManager instance, inject the entity manager into the application component:
@PersistenceContext
EntityManager em;
Application-Managed Entity Managers
The persistence context is not propagated to application components, and the life cycle of EntityManager instances is managed by the application. Applications create EntityManager instances using the createEntityManager method of javax.persistence.EntityManagerFactory.
To obtain an EntityManager instance, you first must obtain an EntityManagerFactory instance by injecting it into the application component by means of the javax.persistence.PersistenceUnit annotation:
@PersistenceUnit
EntityManagerFactory emf;
EntityManager em = emf.createEntityManager();
The EntityManager.find method is used to look up entities in the data store by the entity’s primary key.
@PersistenceContext
EntityManager em;
public void enterOrder(int custID, Order newOrder) {
Customer cust = em.find(Customer.class, custID);
cust.getOrders().add(newOrder);
newOrder.setCustomer(cust);
}
Managing an Entity Instance’s Life Cycle
The four states of an entity instance are new, managed, detached and removed. New entity instances have no persistent identity and are not yet associated with a persistence context. Managed entity instances have a persistent identity and are associated with a persistence context. Detached entity instances have a persistent identify and are not currently associated with a persistence context. Removed entity instances have a persistent identity, are associated with a persistent context, and are scheduled for removal from the data store.
By invoking the persist method or by a cascading persist operation invoked from related entities that have the cascade=PERSIST or cascade=ALL elements set in the relationship annotation, new entity instances become managed and persistent. The entity’s data is stored to the database when the transaction associated with the persist operation is completed. Managed entity instances are removed by invoking the remove method, or by a cascading remove operation invoked from related entities that have the cascade=REMOVE or cascade=ALL elements set in the relationship annotation.
Creating Queries
The EntityManager.createQuery and EntityManager.createNamedQuery methods are used to query the data store using Java Persistence query language queries. The createQuery method is used to create dynamic queries.
public List findWithName(String name) {
return em.createQuery(
"SELECT c FROM Customer c WHERE c.name LIKE :custName")
.setParameter("custName", name)
.setMaxResults(10)
.getResultList();
}
The createNamedQuery method is used to create static queries.
@NamedQuery(
name="findAllCustomersWithName",
query="SELECT c FROM Customer c WHERE c.name LIKE :custName"
)
@PersistenceContext
public EntityManager em;
...
customers = em.createNamedQuery("findAllCustomersWithName")
.setParameter("custName", "Smith")
.getResultList();
Container-Managed Entity Managers
EntityManager instance’s persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction. The Java EE container manages the life cycle of container-managed entity managers.
To obtain an EntityManager instance, inject the entity manager into the application component:
@PersistenceContext
EntityManager em;
Application-Managed Entity Managers
The persistence context is not propagated to application components, and the life cycle of EntityManager instances is managed by the application. Applications create EntityManager instances using the createEntityManager method of javax.persistence.EntityManagerFactory.
To obtain an EntityManager instance, you first must obtain an EntityManagerFactory instance by injecting it into the application component by means of the javax.persistence.PersistenceUnit annotation:
@PersistenceUnit
EntityManagerFactory emf;
EntityManager em = emf.createEntityManager();
The EntityManager.find method is used to look up entities in the data store by the entity’s primary key.
@PersistenceContext
EntityManager em;
public void enterOrder(int custID, Order newOrder) {
Customer cust = em.find(Customer.class, custID);
cust.getOrders().add(newOrder);
newOrder.setCustomer(cust);
}
Managing an Entity Instance’s Life Cycle
The four states of an entity instance are new, managed, detached and removed. New entity instances have no persistent identity and are not yet associated with a persistence context. Managed entity instances have a persistent identity and are associated with a persistence context. Detached entity instances have a persistent identify and are not currently associated with a persistence context. Removed entity instances have a persistent identity, are associated with a persistent context, and are scheduled for removal from the data store.
By invoking the persist method or by a cascading persist operation invoked from related entities that have the cascade=PERSIST or cascade=ALL elements set in the relationship annotation, new entity instances become managed and persistent. The entity’s data is stored to the database when the transaction associated with the persist operation is completed. Managed entity instances are removed by invoking the remove method, or by a cascading remove operation invoked from related entities that have the cascade=REMOVE or cascade=ALL elements set in the relationship annotation.
Creating Queries
The EntityManager.createQuery and EntityManager.createNamedQuery methods are used to query the data store using Java Persistence query language queries. The createQuery method is used to create dynamic queries.
public List findWithName(String name) {
return em.createQuery(
"SELECT c FROM Customer c WHERE c.name LIKE :custName")
.setParameter("custName", name)
.setMaxResults(10)
.getResultList();
}
The createNamedQuery method is used to create static queries.
@NamedQuery(
name="findAllCustomersWithName",
query="SELECT c FROM Customer c WHERE c.name LIKE :custName"
)
@PersistenceContext
public EntityManager em;
...
customers = em.createNamedQuery("findAllCustomersWithName")
.setParameter("custName", "Smith")
.getResultList();
Subscribe to:
Posts (Atom)