Created
February 2, 2012 17:07
-
-
Save carlosspohr/1724602 to your computer and use it in GitHub Desktop.
Inject like modo jegue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package testes.rmi.vraptor; | |
| import org.apache.log4j.Logger; | |
| import persistency.annotation.RMI; | |
| import persistency.manager.ObjectManager; | |
| import br.com.caelum.vraptor.InterceptionException; | |
| import br.com.caelum.vraptor.Intercepts; | |
| import br.com.caelum.vraptor.core.InterceptorStack; | |
| import br.com.caelum.vraptor.http.MutableRequest; | |
| import br.com.caelum.vraptor.http.ParameterNameProvider; | |
| import br.com.caelum.vraptor.interceptor.Interceptor; | |
| import br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor; | |
| import br.com.caelum.vraptor.resource.ResourceMethod; | |
| import br.com.caelum.vraptor.view.FlashScope; | |
| import testes.components.IFacadeProvider; | |
| @Intercepts(before=ParametersInstantiatorInterceptor.class) | |
| public class InstanciaManagerInterceptor implements Interceptor{ | |
| private static final Logger logger = Logger.getLogger(InstanciaManagerInterceptor.class); | |
| private final MutableRequest request; | |
| private final FlashScope flash; | |
| private final ParameterNameProvider provider; | |
| private final IFacadeProvider facadeProvider; | |
| public InstanciaManagerInterceptor(MutableRequest request, | |
| FlashScope flash, ParameterNameProvider providerName, | |
| IFacadeProvider fac) { | |
| super(); | |
| this.request = request; | |
| this.flash = flash; | |
| this.provider = providerName; | |
| this.facadeProvider = fac; | |
| } | |
| @Override | |
| public boolean accepts(ResourceMethod method) { | |
| logger.info("Passando pelo interceptador de instância......................................."); | |
| return true; | |
| } | |
| @SuppressWarnings("unchecked") | |
| public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance) throws InterceptionException | |
| { | |
| String[] names = provider.parameterNamesFor(method.getMethod()); | |
| Class<?>[] types = method.getMethod().getParameterTypes(); | |
| Object[] args = flash.consumeParameters(method); | |
| for (int i = 0; i < names.length; i++) | |
| { | |
| logger.info("Nome do parâmetro: " + names[i]); | |
| // Montar a instância... | |
| if(types[i].isAnnotationPresent(RMI.class)){ | |
| logger.info("Montando a instância....................."); | |
| Class<? extends ObjectManager> clazz = (Class<? extends ObjectManager>) types[i]; | |
| ObjectManager manager = facadeProvider.getInstance(clazz); | |
| if (args != null) | |
| args[i] = manager; | |
| else | |
| request.setAttribute(names[i], manager); | |
| } | |
| } | |
| flash.includeParameters(method, args); | |
| stack.next(method, resourceInstance); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment