Thanks for the reply Howard. What I'm trying to achieve here is to
create a declarative transaction. So before the service method is
invoked, I want to begin a transaction, and commit when the service
method is finished.

I'm going to try out the explanation you have given me. But I think
I'm still far off from creating an interceptor with javassist.

Another question: Is the SampleInterceptor.foo invoked from the
decorator method?

PS: I think you need to add a reference regarding on creating custom
decorator here just to prevent others asking the same question like I
did. :-D

Regards,
joshua

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).

So, the interceptor has the same methods as the service, and a
do-nothing interceptor would just re-invoke each method on the
service.

Example:

public interface Sample() {
 String foo(String bar);
}

A hand-assembled interceptor would look like:

public class SampleInterceptor {
 private final Sample _delegate;

 public SampleInterceptor(Sample delegate) { _delegate = delegate; }

 public String foo(String bar) {
   // Logic before delegate invocation here.
   String result =  _delegate.foo(bar);
  // Logic after delegate invocation here.

 return result;
 }
}

Of course, we don't use hand-written interceptors often, we instead
brew up the interceptors on the fly inside decorator methods, using
JDK dynamic proxies, or Javassist (ClassFactory and friends).

Notice the two comments; you see that you can add logic before
delegating, even to the point of changing method parameters before
re-invoking.  In theory you could even invoke a different method on
delegate, or not invoke the method, or invoke a method on some other
object.

Likewise, you can have logic after invoking the method.  You could
even but a try ... finally arround the call to _delegate and trap
exceptions.

On 7/8/07, Joshua Jackson <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> Sorry for the long subject :-D.
>
> Ok straight to the point. I'm currently building a decorator for a
> Service. But the problem I'm facing is, the method inside the
> decorator is only invoked before the service method is invoked. What I
> want is the decorator method to be invoked before and after the
> service method is invoked. How do I get this? I saw the logging
> decorator method is invoked before and after, but I just still can not
> get the idea. Perhaps there should be a reference on building custom
> decorator? :-D
>
> Thanks in advance
>
> --
> Let's create a highly maintainable and efficient code
>
> YM!: thejavafreak
> Blog: http://www.nagasakti.or.id/roller/joshua/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


--
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

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




--
Let's create a highly maintainable and efficient code

YM!: thejavafreak
Blog: http://www.nagasakti.or.id/roller/joshua/

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

Reply via email to