RE: Redirecting to a page from a MethodAdvice implementation

2014-06-19 Thread Davide Vecchi
I'm not sure if the following is what the hints above meant, however it works, 
in case it can be useful to others. Thanks for the help.

-
package myproject.tapestry.services;

import org.apache.tapestry5.plastic.MethodAdvice;

public interface ExceptionDisplayMethodAdvice extends MethodAdvice
{
   // No code in this subclass.
}

-
package myproject.tapestry.services;

public class ExceptionDisplayMethodAdviceImpl implements 
ExceptionDisplayMethodAdvice
{
   @Inject
   private PageRenderLinkSource pageRenderLinkSource;

   @Inject
   private Response response;

   /**
   * @see MethodAdvice#advise(MethodInvocation)
   */
   @Override
   public void advise(MethodInvocation invocation)
   {
  System.err.println(this.pageRenderLinkSource + ; + 
this.response);

MethodInvocation result = invocation.proceed();

  if (result.didThrowCheckedException())
  {
 Exception exception = 
result.getCheckedException(Exception.class);

 Exception.printStackTrace();

 // Redirect to the exception page :

 result.setCheckedException(null);

 Link exceptionDisplayPage = 
this.pageRenderLinkSource.createPageRenderLink(ExceptionDisplayPage.class);

 try
 {
   this.response.sendRedirect(exceptionDisplayPage);
 }
 catch (IOException e)
 {
   e.printStackTrace();
 }
  }
   }
}

-
public class ExceptionDisplayWorker implements ComponentClassTransformWorker2
{
   @Inject
   private ExceptionDisplayMethodAdvice advice;

   @Override
   public void transform(PlasticClass plasticClass, TransformationSupport 
support, MutableComponentModel model)
   {
  for (PlasticMethod method : 
plasticClass.getMethodsWithAnnotation(ExceptionDisplay.class))
  {
 method.addAdvice(this.advice);
  }
   }
}

-
package myproject.tapestry.services;

public class MyProjectModule
{
public static void bind(ServiceBinder binder)
{
   binder.bind(ExceptionDisplayMethodAdvice.class, 
ExceptionDisplayMethodAdviceImpl.class);
}
}
-



RE: Redirecting to a page from a MethodAdvice implementation

2014-06-19 Thread Lance Java
You could get rid of the unnecessary interface using @InjectService or a
marker annotation

eg:

binder.bind(MethodAdvice.class,
ExceptionDisplayMethodAdviceImpl.class).withId(foo);
...
@InjectService(foo)
private MethodAdvice advice;

More info here:
http://tapestry.apache.org/defining-tapestry-ioc-services.html


Re: Redirecting to a page from a MethodAdvice implementation

2014-06-18 Thread Thiago H de Paula Figueiredo

On Wed, 18 Jun 2014 11:47:16 -0300, Davide Vecchi d...@amc.dk wrote:


Hello,


Hi!

I have the following class to handle the execution of methods that are  
annotated with a custom @ExceptionDisplay annotation. Everything works  
as expected, but I would like to redirect to a page. I'm using  
PageRenderLinkSource and Response to do that. I inject them in the same  
way I usually inject them into pages, but in this case it does not work,  
because the PageRenderLinkSource and Response instances are both null. 
I   imagine that's because they should be only injected into pages or  
components,


You're right, but you forgot include Tapestry-IoC services and objects  
instantiated using ObjectLocator.autobuild().


--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

-
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org



RE: Redirecting to a page from a MethodAdvice implementation

2014-06-18 Thread Davide Vecchi
Hi,

Sorry, I don't understand. Do you mean that I'm right that PageRenderLinkSource 
and Response should be only injected into pages or  components ?

Could you elaborate a little bit about including Tapestry-IoC services and 
objects  instantiated using ObjectLocator.autobuild() ?


Re: Redirecting to a page from a MethodAdvice implementation

2014-06-18 Thread Thiago H de Paula Figueiredo

On Wed, 18 Jun 2014 12:01:16 -0300, Davide Vecchi d...@amc.dk wrote:


Hi,


Hi!

Sorry, I don't understand. Do you mean that I'm right that  
PageRenderLinkSource and Response should be only injected into pages or   
components ?


Nope. I said it can be injected into pages, components, mixins,  
Tapestry-IoC services and objects returned by ObjectLocator.autobuild().


Could you elaborate a little bit about including Tapestry-IoC services  
and objects  instantiated using ObjectLocator.autobuild() ?


I meant including as being in the list of stuff that gets injections  
done.


--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

-
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org



Re: Redirecting to a page from a MethodAdvice implementation

2014-06-18 Thread Lance Java
As a simple rule, if you instantiated the object yourself, @Inject won't
work.

See here:
private final MethodAdvice advice = new MethodAdvice() ...

You've used new so it's not under tapestry's control.
 Hello,
I have the following class to handle the execution of methods that are
annotated with a custom @ExceptionDisplay annotation. Everything works as
expected, but I would like to redirect to a page. I'm using
PageRenderLinkSource and Response to do that. I inject them in the same way
I usually inject them into pages, but in this case it does not work,
because the PageRenderLinkSource and Response instances are both null. I
imagine that's because they should be only injected into pages or
components, so I'm wondering if / how is it possible to redirect to a page
from a MethodAdvice. Thanks in advance for any possible suggestion.


ExceptionDisplayWorker.java


package tapestry.util.annotations;

import java.io.IOException;

import org.apache.tapestry5.Link;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.plastic.MethodAdvice;
import org.apache.tapestry5.plastic.MethodInvocation;
import org.apache.tapestry5.plastic.PlasticClass;
import org.apache.tapestry5.plastic.PlasticMethod;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.apache.tapestry5.services.Response;
import
org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
import org.apache.tapestry5.services.transform.TransformationSupport;

import tapestry.pages.ExceptionDisplayPage;
import tapestry.annotations.ExceptionDisplay;

public class ExceptionDisplayWorker implements
ComponentClassTransformWorker2
{
   private final MethodAdvice advice = new MethodAdvice()
{
  @Inject
  private PageRenderLinkSource pageRenderLinkSource;

  @Inject
  private Response response;

@Override
public void advise(MethodInvocation invocation)
{
System.out.println(this.pageRenderLinkSource + ; +
this.response); // Both null.

MethodInvocation result = invocation.proceed();

if (result.didThrowCheckedException())
{
 Exception exception =
result.getCheckedException(Exception.class);

 exception.printStackTrace();

 // Redirect to the exception page :

 result.setCheckedException(null);

 Link exceptionDisplayPage =
this.pageRenderLinkSource.createPageRenderLink(ExceptionDisplayPage.class);
// Problem here, pageRenderLinkSource is null.

 try
{

 this.response.sendRedirect(exceptionDisplayPage); // Problem here too,
response is null too.

}
catch (IOException e)
{
   e.printStackTrace();
}
}
}
};

   @Override
   public void transform(PlasticClass plasticClass,
TransformationSupport support, MutableComponentModel model)
   {
  for (PlasticMethod method :
plasticClass.getMethodsWithAnnotation(ExceptionDisplay.class))
  {
 method.addAdvice(this.advice);
  }
   }
}


-