Thursday 9 October 2014

Apache MyFaces Orchestra

Introduction

Orchestra eases the development of applications that perform a lot of persistence (ie are strongly coupled with a database). It provides the following features:
  • A conversation (aka dialog) scope for beans
  • Conversation-scope persistence contexts. This fixes the dreaded LazyInitializationException or NonUniqueObjectException problems when working with persistent objects.
  • Declarative transaction annotations
  • A "dynaForm" JSF component that helps create forms for editing persistent data.

Supported Web Presentation Frameworks

Orchestra currently supports JSF1.1, JSF1.2 and JSF2.0.

Supported JDKs

Orchestra is JDK 1.5 compatible, because JDK 1.4 has reached its End of Life. For use in JDK 1.4 please use 1.3.1 artifacts.

Dependencies

Orchestra requires Spring 2.x to declare managed beans that will be stored in conversation context.

Highlights

  • Orchestra uses Spring to configure an Orchestra "conversation" scope. You can declare a JSF Managed bean in Spring using this scope, then when that bean is referenced from a JSF EL expression it is automatically created within that conversation scope.

A small JSF example

Here's a quick demonstration of Orchestra's main features:

  • First, we use the Spring Framework to configure a conversation-scoped bean.

<bean name="ballotTopic"
   class="org.apache.myfaces.examples.ballot.backings.BallotTopic"
   scope="conversation.access"/>


We wired the conversational scope to Orchestra, so that the bean named "ballotTopic" will support JSF features - it defines properties, methods and event-listeners as usual managed-beans do.


  • We define a JSF action-method as requiring a transaction with the @Transactional annotation. with this, a commit will be executed at the end of the method. When the conversation needs to be closed again, close it by calling the invalidate method on the current conversational instance.
@Transactional
public String saveAction() {
  topic.setOwner(getVoterDao().getByKey(getBallotState().getVoterId()));
  topicDao.save(topic);
  Conversation.getCurrentInstance().invalidate();
  return "success";
}

The developer keeps on defining DAO's as she is used to do - here is a simple example using the JPA-syntax with an injected EntityManager.
public class VoterDao {
  @PersistenceContext
  private EntityManager entityManager;
  public Voter getByKey(Long id) {
    return entityManager.find(Voter.class, id);
  }
}