6 Replies Latest reply on Mar 31, 2011 3:18 PM by markwigmans

    Access Component Name

    wrzep

      Hi there,


      I'd like to access component name programmatically, in a Java method. Is there a way to do this?


      There is a org.jboss.seam.Component class, but I don't know how to get its instance for the current component.


      It could be useful for logging :)


      Cheers,
      -Pawel

        • 1. Re: Access Component Name
          pmuir

          Put a method like


          public Component getComponent() {
             return null;
          }



          and it will magically return the Component for your Seam component.

          • 2. Re: Access Component Name
            wrzep

            Tricky! :)


            It works fine, but there is one major drawback. A call to the getComponent() mehtod must be intercepted (by ClientSideInterceptor or JavaBeanInterceptor as it seems from the code).


            So, for example, the following code:


            log.info("My name is '" + getComponent().getName() + "'");



            would fail with NPE, because getComponent() call to myself is not intercepted.


            Thoughts that came to my mind:


            1. Thing like EJB3 sessionContext.getBusinessObject() for Seam components would solve the problem with interceptors :)


            2. How about API to get current component? Like Component.current()?


            -Pawel

            • 3. Re: Access Component Name
              pmuir

              Ah, yes.


              I guess an API like (2) would be ok - file a JIRA FR.

              • 4. Re: Access Component Name
                coralfe

                Try


                Classname whatever = (Classname) Component.getInstance("name");
                


                • 5. Re: Access Component Name
                  durdina

                  In seam 2.2.0, there are some undocumented builtin components on method scope (undocumented as well).
                  org.jboss.seam.this
                  org.jboss.seam.method
                  org.jboss.seam.parameters
                  org.jboss.seam.component


                  So this prints out component name of current instance:


                  log.info("My name is #{org.jboss.seam.component.name}");
                  


                  • 6. Re: Access Component Name
                    markwigmans


                    Component.getComponentName(getClass())



                    works for me