3 Replies Latest reply on Mar 12, 2010 12:54 PM by cash1981

    Adding Observers dynamically in seam Event Mechanisum

    devika.nrgh

      hi,
      i am working on Seam component events i am getting the output with the help of annotations.


      Now i want to add the component Programatically and want to add as obsesrver for existing events.

      i.e registering component as observer dynamaically. without using annotations.


      Any ideas?????????


      Thanks in advance.


      Regards
      Devika.N

        • 1. Re: Adding Observers dynamically in seam Event Mechanisum
          cash1981

          I am not sure if this will work. But you can try this:




          Init.instance().addObserverMethod(eventType, method, component, create)



          • 2. Re: Adding Observers dynamically in seam Event Mechanisum
            devika.nrgh

            Thanks for the reply.


            pls can u send me some sample code.


            Regards
            Devika.N

            • 3. Re: Adding Observers dynamically in seam Event Mechanisum
              cash1981

              I just did.


              You just need to fill out whatever you want.


              This is taken from Component.java (source of seam)
              There you see how observers are registered using reflection



              if ( method.isAnnotationPresent(Observer.class) )
                    {
                       //can't use Init.instance() here because of unit tests
                       Init init = (Init) applicationContext.get(Seam.getComponentName(Init.class));
                        
                       Observer observer = method.getAnnotation(Observer.class);
                       for ( String eventType : observer.value() )
                       {
                          if ( eventType.length()==0 ) eventType = method.getName(); //TODO: new defaulting rule to map @Observer onFooEvent() -> event type "fooEvent"
                          init.addObserverMethod( eventType, method, this, observer.create() );
                       }
                    }