Archive

Posts Tagged ‘jboss-seam’

Formatting messages on JSF page in JBoss Seam

November 18th, 2009 Shrihari No comments

In many scenarios we may need to format messages on the JSF page using JBoss Seam without calling a backing bean method, One typical example could be displaying a welcome message when the user logs in to the application. This tip explores a component called Interpolator in Seam which helps us the solution.

Lets assume we have defined a message property as welcome.message as below

#different message properties
...
welcome.message= Welcome! Dear #0
...

In the homepage.xhtml (jsf page), we come populate the greeting message with, say the logged in username, using interpolator as below:

   ....
   <h:outputText value="#{interpolator.interpolate(messages['welcome.message'],identity.username)}"/>
....
Categories: jee-light Tags:

How to end JBoss Seam conversation in menu links?

November 17th, 2009 Shrihari No comments

Not all the times, we may be interested in using JBoss Seam’s link tag s:link to end conversation scope propagation. This holds good specifically for menu links and random hotkey navigations. One quick tip could be to specify the redirection url link with the parameter conversationPropagation=end.

Categories: jee-light Tags:

How to change authentication success/failure message in JBoss Seam?

November 16th, 2009 Shrihari No comments

If we rely on JBoss Seam’s Security Identity Management for user authentication on a web application, we certainly think of changing the default ‘Welcome <username>’ to something different and catchy. This particular tip explores such a possibility successfully in 2-3 steps:

1)  Assuming that you have defined the security identity and core resource loader components have been defined in WEB-INF/components.xml as below, we have to modify the properties myapplication_en.properties (_en is the default locale)

<security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
<core:resource-loader bundle-names="myapplication"/>

define your custom message like this, where #0 is a parameter placeholder.

welcome.message=Good to see you, #0
loginfailed.message=Unable to login mate, please check your credentials!

2) Modify your authenticator component to add custom message if login/authentication is successful.

public boolean authenticate()
{  User loggedInUser = authenticationservice.autheticate(credentials.getUsername(), credentials.getPassword());
   FacesMessages messages = FacesMessages.instance();
    if(loggedInUser==null)
    {    messages.addFromResourceBundle("loginfailed.message", null);
         return false;
    }
    messages.addFromResourceBundle("welcome.message", loggedInUser.getFullname());
    return true;
}

Are we done? wait… We need to reset the earlier messages (the default ones)

3) Add two more entries to myapplication_en.properties and set these properties as blank. This would reset the existing default ones.

org.jboss.seam.loginSuccessful=
org.jboss.seam.loginFailed=

Aint it easy and cool!

Categories: jee-light Tags:

Integrating Hibernate Search on PrimeFaces using Google Guice and JBoss Seam

November 14th, 2009 Shrihari No comments

Recently I have been working on an application involving PrimeFaces and required search capabilities directly on the persisted data. Though I can use Hibernate search for implementing search services, the Google Guice, the underlying IoC framework, which PrimeFaces provide does not provide me the out-of-the-box integration. I am extending from my previous blog post which talked about using Google Guice to inject JPA EntityManager. This post builds on the previous post and explores the option of search. Here are the following sequence of steps I followed to ensure PrimeFaces widgets are Hibernate Search compliant:

1)  Follow the steps mentioned to get an EntityManager instance fro m JBoss Seam’s component xml and register in the Google Guice injector using a Provider  implementation. Make a little change where the Provider’s getter method is made static.  Following is the EntityManagerProvider code snippet to do that:

class EntityManagerProvider implements Provider
{       static EntityManagerFactory entityManagerFactory = null;
        public static EntityManager get()
        {   return entityManagerFactory.createEntityManager();
        }
}

2) Implement Guice injection Provider for constructing a FullTextEntityManager from EntityManager

public class FullTextEntityManagerProvider implements Provider
{    static FullTextEntityManager fullTextManager = null;
     private static EntityManager entityManager;

     public static void setEntityManager(EntityManager em)
     {   entityManager = em;
     }

      public FullTextEntityManager get()
      {   if(fullTextManager==null)
             fullTextManager = Search.getFullTextEntityManager(entityManager);
           return fullTextManager;
      }
}

3) Modify the custom module (now refactored as MySearchEntityManagerModule) to construct the FullTextEntityManager using EntityManager instance as shown below.

public class MySearchEntityManagerModule extends AbstractModule
{      public void configure()
       {   Expressions expressions = Expressions.instance();
           ValueExpression emfVE = expressions.createValueExpression("#{my_persistence_unit}");
           EntityManagerFactory emf = (EntityManagerFactory)emfVE.getValue();
           EntityManagerProvider.setEntityManagerFactory(emf);
           FullTextEntityManagerProvider.setEntityManager(provider.get());

            //bind EntityManager and FullTextEntityManager for both pure database and search specific integration
            bind(EntityManager.class).toProvider(EntityManagerProvider.class).in(Scopes.SINGLETON);
            bind(FullTextEntityManager.class).toProvider(FullTextEntityManagerProvider.class).in(Scopes.SINGLETON);
     }
}

3) Inject the FullTextEntityManager in the dataservice (DAO) for searching for text tokens using Hibernate Search.

@Name("hybrid")
@Guice
public class HybridDAO
{  @Inject FullTextEntityManager searchManager;
    //other DAO methods to perform search using Hibernate Search and Lucene
}

This quick tip provides us a capability of enhancing Primefaces widgets to use the power of Hibernate Search for search and much more extensible features.

Context specific Outjection modes in JBoss Seam

November 13th, 2009 Shrihari No comments

There are 7 types of contexts available in JBoss Seam. In order to outject (make an instantiated/ populated object available to other components participating in the same context) for a particular scope, say CONVERSATION, there are at least 2 ways of outjecting an instance as follows:

1) Declaratively : This could be used when there are component boundaries clearly demarcated and the instance needs to available for a longer running conversation. An example snippet is given below:

@Name("producer")
public class Producer
{   @Out(scope=ScopeType.CONVERSATION)
      private QueueFeed feed;

      //methods to produce and populate the feed
      @Begin public void doStart(){}
      @End public void close(){}
}
@Name("consumer")
public class Consumer
{   @In  private QueueFeed feed;
       //methods to consume and cleanup the feed
}

2) Programmaticaly : This approach is used when one would want place the instance in the already established context, and other seam components just reference the instance and used as given below:

@Name("producer")
public class Producer
{   public void produceIntermediate()
    {   QueueFeed queueFeed;
         //populate queueFeed
        Contexts.getConversationContext().set("queueFeed", queueFeed);
     }
}
@Name("consumer")
public class Consumer
{  public void consumeIntermediate()
    {   QueueFeed queueFeed = (QueueFeed) ( Contexts.getConversationContext().get("queueFeed",));
     }
}

This way one can manage scopes effectively with regard to instance usage across participating components

Categories: jee-light Tags:

Forking navigations based on user roles in JBoss Seam using Seam-Security

October 14th, 2009 Shrihari 1 comment

This particular tip on loading different homepages for the role associated for a specific logged in user for a JSF web application written using JBoss Seam and identity management using Seam-Security. This tip assumes that a single user will be associated with a single role.

Assume we need to develop a course management JSF application which supports users of 2 different roles : teacher and student. This means if a user with teacher role logs in teacher specific homepage should come  and student logging in should display student homepage. The following are steps to achieve this requirement.

1) Change WEB-INF/components.xml to include the seam-security authentication patterns

  <components xmlns="http://jboss.com/products/seam/components"
          ....
          xmlns:security="http://jboss.com/products/seam/security"
          xsi:schemaLocation="http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd
                ....
          ">
 ...
 <security:identity authenticate-method="#{authenticator.authenticate}"/>
</components>

2) Define an Authenticator action class with JBoss Seam component name “authenticator” and define an authenticate method inside

@Name("authenticator")
public class AuthenticatorAction
{   @In
    private Identity identity;
    @In(create=true)
    private AuthService service;

    ...
    @SuppressWarnings("deprecated")
    public boolean authenticate()
    {   String username = identity.getUsername();
        String password = identity.getPassword();
        if(username==null || password==null)
           return false;
        //authenticate the user from database
        User user = service.authenticate(username,password);
        // check user validity.
        identity.addRole(user.getRole());
        return true;
    }
}

3) Define the login portal widget in the login-page (say login.xhtml)

<ui:composition xmlns="http://www.w3.org/1999/xhtml">
 .....
   <h:panelGrid columns="2" style="width:27px">
      <h:outputText value="Username: "/>
      <h:inputText id="username" value="#{identity.username}" rows="1" cols="12">
         <f:validateLength minimum="8"  />
      </h:inputText>
      <h:outputText value="Password:" rows="1" cols="12"/>
      <h:inputSecret id="password" value="#{identity.password}" />
   </h:panelGrid>
   <h:commandButton id="login" action="#{identity.login}" value="Login"/>
    ...
 </ui:composition>

4) Wire the action outcomes to set up the navigation paths in WEB-INF/pages.xml

<pages xmlns="http://jboss.com/products/seam/pages"
       ....
       no-conversation-view-id="/index.html">

     <page view-id="/login.xhtml">
        <navigation from-action="#{authenticator.checkLogin}">
           <rule if="#{identity.loggedIn and identity.role=='Teacher'}">
               <redirect view-id="/teacher/homepage.xhtml" />
          </rule>
       </navigation>
       <navigation from-action="#{identity.login}">
           <rule if="#{identity.loggedIn and identity.hasRole('Teacher')}">
              <redirect view-id="/teacher/homepage.xhtml" />
          </rule>
       </navigation>
       <navigation from-action="#{authenticator.checkLogin}">
          <rule if="#{identity.loggedIn and identity.role=='Student'}">
               <redirect view-id="/student/homepage.xhtml" />
          </rule>
       </navigation>
       <navigation from-action="#{identity.login}">
           <rule if="#{identity.loggedIn and identity.hasRole('Student')}">
              <redirect view-id="/student/homepage.xhtml" />
          </rule>
       </navigation>
    </page>
    ....
   <exception>
        <redirect view-id="/index.xhtml">
           <message>Please log in first</message>
        </redirect>
   </exception>
</pages>

You can extend this tip to come out of different other user-role combinations.

Categories: jee-light Tags: ,

Injecting JPA EntityManager in Google Guice through JBoss Seam

July 31st, 2009 Shrihari No comments

This cool tip describes a 3 step process to use JPA’s EntityManager initialized in JBoss Seam inside the Google Guice environment.

1). Define Seam’s components.xml with entity-manager-factory component as below

<components>
    <persistence:entity-manager-factory name="my_persistence_unit"/>
    <guice:init injector="#{myInjector}"/>
    <guice:injector name="myInjector">
        <guice:modules>
           <value>mypackage.MyEntityManagerModule</value>
        </guice:modules>
    </guice:injector>
</components>

where my_persistence_unit is the persistence unit name under META-INF/persistence.xml.

2). Define a Google Guice Module to wire-up Seam’s EnitiyManagerFactory by looking-up based on the expression-value and binding it to Guice context using a Provider

public class MyEntityManagerModule extends AbstractModule
{   public void configure()
    {   Expressions expressions = Expressions.instance();
        ValueExpression emfVE = expressions.createValueExpression("#{my_persistence_unit}");
        EntityManagerFactory emf = (EntityManagerFactory)emfVE.getValue();
        EntityManagerProvider.setEntityManagerFactory(emf);
        bind(EntityManager.class).toProvider(EntityManagerProvider.class).in(Scopes.SINGLETON);
     }
 }
 
class EntityManagerProvider implements Provider
    {   static EntityManagerFactory entityManagerFactory = null;
        public EntityManager get()
        {   return entityManagerFactory.createEntityManager();
        }
    }
 

3) One could use the EntityManager anywhere in the Seam-Guice hybrid component:

@Name("hybrid")
@Guice
public class HybridDAO
{  @Inject EntityManager entityManager;
   //other DAO methods.
}

This makes Guice components use EntityManager normally use Seam established EntityManagerFactory

Categories: jee-light Tags: , ,