try this:

delete clases Delegate and delegateImpl


then change the interceptor:
public class Interceptor implements AnotherService{

   private final Service service;
   private final Log log;
   private final AnotherService delegate;

   public Interceptor(AnotherService delegate,Service service, Log log) {
       this.delegate = delegate;
       this.service = service;
       this.log = log;
   }

   public void run() {
       // Logic before delegate invocation here.
       log.info("<<< Before delegate >>>");
       service.execute();

       delegate.run();

       service.execute();
       // Logic after delegate invocation here.
       log.info("<<< After delegate >>>");
   }
}

and change appmodule.decorateAnotherService

      public static AnotherService decorateAnotherService(
              Object delegate, Service service, Log log)
      {
              return new Interceptor((AnotherService)delegate, service, log
);
      }




interceptor is an implementation of the service interface,
you then code manualy method implementations,
choosing when and how to call the delegate service.


the code above outputed this to log:
11:28:28.390 INFO   [SocketListener0-1] services.Interceptor.run(
Interceptor.java:19) >52> <<< Before delegate >>>
11:28:28.390 INFO   [SocketListener0-1] services.ServiceImpl.execute(
ServiceImpl.java:14) >54> <-- Inside Service method -->
11:28:28.390 INFO   [SocketListener0-1] services.AnotherServiceImpl.run(
AnotherServiceImpl.java:13) >53> Inside another service
11:28:28.390 INFO   [SocketListener0-1] services.ServiceImpl.execute(
ServiceImpl.java:14) >54> <-- Inside Service method -->
11:28:28.390 INFO   [SocketListener0-1] services.Interceptor.run(
Interceptor.java:26) >52> <<< After delegate >>>

Davor Hrg

On 7/11/07, Joshua Jackson <[EMAIL PROTECTED]> wrote:

I tried the hint you gave me but it seems to be not working. Perhaps I
am missing something. Let me paste the code snippets so there won't be
any confusion.

This is the AppModule:
public class AppModule {
    public static void bind(ServiceBinder binder){
        binder.bind(Decorator.class, DecoratorImpl.class);
        binder.bind(Service.class, ServiceImpl.class);
        binder.bind(AnotherService.class, AnotherServiceImpl.class);
    }

    public static Object decorateAnotherService(
            Object delegate, Service service,

            @InjectService("Decorator")
            Decorator decorator)
    {
            return decorator.decorate( delegate, service );
    }
}

Basically I binded everything to their interface. What I want is to
decorate AnotherService with Service and I want the execute method in
Service to be invoked before and after any method in AnotherService is
invoked. Which means before and after the AnotherService's run method
is invoked, the Service's execute method is invoked.

This is the ServiceImpl:
public class ServiceImpl implements Service {
    public void execute() {
        log.debug("<-- Inside Service method -->");
    }
}

And this is the AnotherServiceImpl:
public class AnotherServiceImpl implements AnotherService{
    public void run(){
        log.debug("Inside another service");
    }
}

This is the DecoratorImpl:
public class DecoratorImpl implements Decorator {
    public Object decorate(Object delegate, Service service) {
        log.debug("<<< Begin decorator >>>");

        Interceptor interceptor= new Interceptor(service);
        interceptor.execute();

        log.debug("<<< End decorator >>>");

        return service;
    }
}

And this is the Interceptor:
public class Interceptor {
    private final Service service;

    public Interceptor(Service service) {
        this.service = service;
    }

    public void execute() {
        // Logic before delegate invocation here.
        log.debug("<<< Before delegate >>>");

        service.execute();

        // Logic after delegate invocation here.
        log.debug("<<< After delegate >>>");

    }
}

I already did what you said by creating the Interceptor inside the
Decorator and then execute the service method inside the Interceptor.
But that didn't work, because the Service.execute() is still executed
only before the AnotherService.run().

What have I gotten wrong here.

Thanks in advance

On 7/9/07, Howard Lewis Ship <[EMAIL PROTECTED]> wrote:
> The decorator creates the interceptor.
>
> The interceptor has the same interface as the service.
>
> The decorator is passed the service, which it can then provide to the
> interceptor.  It's called the "delegate" (because it may not be the
> service, it may be another interceptor, but that's actually not
> relevant).

--
It's not just about coding, it's a matter of fulfilling you core being

YM!: thejavafreak
Blog: http://joshuajava.wordpress.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to