3 Replies Latest reply on Mar 2, 2008 7:25 PM by Alex Kachanov

    a JSF Validator cannot be cast to javax.faces.validator.Vali

    Alex Kachanov Newbie

      JBoss portal is using idenity portlet to register new users and edit profiles for existing users. I added a my own JSF validator to the identty portlet which when opened now throws the following exception:

      java.lang.ClassCastException: com.kachanov.test004.validators.MyValidator cannot be cast to javax.faces.validator.Validator
      at org.apache.myfaces.application.ApplicationImpl.createValidator(ApplicationImpl.java:613)

      The code of my validator is:

      package com.kachanov.test004.validators;

      import javax.faces.application.FacesMessage;
      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.validator.Validator;
      import javax.faces.validator.ValidatorException;

      public class MyValidator implements Validator {

      public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException {

      System.out.println("testing this validator");


      So please thell me how can it bee that a class that implements Validator cannot be cast to javax.faces.validator.Validator??????

      Is in't it against basic design of Java language?

      I reported this to JIRA and got this answer:
      Not a bug. Please see classloading rules.
      It looks like your application embeds JSF libs, while they are already available in JBoss AS.
      But I have no idea where to look for solution

        • 1. Re: a JSF Validator cannot be cast to javax.faces.validator.
          Chris Laprun Master

          The JIRA answer pretty much answers it. A class loaded twice in different class loaders will appear different to the VM. In your case what most likely happens is that your class loads Validator from your application library while the identity portlet loads it from MyFaces, thus causing the ClassCastException. Try to remove the JSF libs from your application.

          • 2. Re: a JSF Validator cannot be cast to javax.faces.validator.
            Alex Kachanov Newbie

            JBoss Portal identity application is using MyFaces
            MyFaces jar libraries are in
            portal-identity.sar\portal-identity.war\WEB-INF\lib2 (sic!) folder
            this is where portal-core-identity-ui-lib.jar files is which contains build in CaptachValidator class.

            I placed my jar file into jboss-portal.sar\portal-identity.sar\lib\ which makes it available to the whole identity application.
            So does the anwser mean I need to put my jar file (which contains just validator class) into portal-identity.sar\portal-identity.war\WEB-INF\lib2?

            • 3. Re: a JSF Validator cannot be cast to javax.faces.validator.
              Alex Kachanov Newbie

              to everybody: the problem is solved

              I put the jar file containing MyValidator.class into lib2 folder, and it worked. Apparently, myfaces uses some tricks to load jar files from "lib2" (not "lib") folder. You were right if I put my class in lib2 folder the same Class Loader that loads myfaces libs loads my validator too and makes it castable to Validator interface.