5 Replies Latest reply on Jan 4, 2009 3:54 PM by Pete Muir

    Login example customized

    fiorenzo pizza Newbie

      Hi,
      i'm starting with web beans.
      For test, i modified the Login example, adding a web beans with Application Scope, for user existence verification (a stupid case.).


      @Users:


      @Target( { TYPE, METHOD, PARAMETER, FIELD })
      @Retention(RUNTIME)
      @Documented
      @BindingType
      public @interface Users
      {}



      AppUsers:


      @ApplicationScoped
      public class AppUsers {
      
           private List<String> users;
      
           public AppUsers() {
      
           }
      
           List<String> getUsersList() {
                if (users == null) {
                     users = new ArrayList<String>();
                     users.add("fiorenzo");
                     users.add("anna");
                }
                return users;
      
           }
      
           @Produces
           @Users
           List<String> getUsers() {
                return getUsersList();
           }




      in the Login class



      @SessionScoped
      @Named
      public class Login {
      
           @Current
           Credentials credentials;
      
           @Dependent
           @Users
           private List<String> users;
           private User user;
      
           public Login() {
           }
      
           public void login() {
      
                if (users.contains(credentials.getUsername())) {
                     this.user = new User(credentials.getUsername(), credentials
                               .getUsername(), credentials.getPassword());
                     FacesContext.getCurrentInstance().addMessage(null,
                               new FacesMessage("Welcome, " + user.getName()));
                } else {
                     this.user = new User(credentials.getUsername(), credentials
                               .getUsername(), credentials.getPassword());
      
                     users.add(credentials.getUsername());
                     FacesContext.getCurrentInstance().addMessage(null,
                               new FacesMessage("Welcome New User,  " + user.getName()));
                }
      
           }
      
           public void logout() {
                FacesContext.getCurrentInstance().addMessage(null,
                          new FacesMessage("Goodbye, " + user.getName()));
                user = null;
           }
      
           public boolean isLoggedIn() {
                return user != null;
           }
      
           @Produces
           @LoggedIn
           User getCurrentUser() {
                return user;
           }



      And all work correctly.


      But if i use

      HashMap<String, User> users

      , in place to
      List<String> users

        in AppUsers, it generate this exception:


      javax.webbeans.UnsatisfiedDependencyException: Annotated class Class java.util.HashMap
      Constructor public HashMap(float);
        Constructor public HashMap();
        Constructor public HashMap();
        Constructor public HashMap();
        Method Object public put(java.lang.Object, java.lang.Object);
        Method Object public clone();
        Method void public clear();
        Method Set public entrySet();
        Method Object public get(java.lang.Object);
        Method void public putAll(java.util.Map);
        Method int public size();
        Method Collection public values();
        Method Object public remove(java.lang.Object);
        Method boolean public containsKey(java.lang.Object);
        Method boolean public containsValue(java.lang.Object);
        Method boolean public isEmpty();
        Method Set public keySet();
        Method int public hashCode();
        Method boolean public equals(java.lang.Object);
        Method String public toString();
        Method Class public final native getClass();
        Method void public final native wait(long);
        Method void public final wait(long, int);
        Method void public final wait();
        Method void public final native notify();
        Method void public final native notifyAll();
      Unable to resolve any Web Beans
           at org.jboss.webbeans.ManagerImpl.getInstanceByType(ManagerImpl.java:557)
           at org.jboss.webbeans.ManagerImpl.getInstanceByType(ManagerImpl.java:513)
           at org.jboss.webbeans.introspector.jlr.AbstractAnnotatedMember.getValue(AbstractAnnotatedMember.java:140)
           at org.jboss.webbeans.introspector.jlr.AnnotatedFieldImpl.inject(AnnotatedFieldImpl.java:127)
           at org.jboss.webbeans.bean.SimpleBean.injectBoundFields(SimpleBean.java:197)
           at org.jboss.webbeans.bean.SimpleBean.create(SimpleBean.java:88)
           at org.jboss.webbeans.contexts.AbstractContext.get(AbstractContext.java:87)
           at org.jboss.webbeans.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79)
           at org.jboss.webbeans.examples.login.Login_$$_javassist_2.isLoggedIn(Login_$$_javassist_2.java)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:585)
           at javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
           at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
           at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
           at org.apache.el.parser.AstValue.getValue(AstValue.java:118)
           at org.apache.el.parser.AstNot.getValue(AstNot.java:42)
           at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
           at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
           ... 31 more 
      



      Some suggestions for inject HashMap??


      Thanks


      Fiorenzo