12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 8 ■ CALLBACKS AND INTERCEPTORSTable 8-2. Definition of the InvocationContext InterfaceMethodgetContextDatagetMethodgetParametersgetTargetgetTimerProceedsetParametersDescriptionAllows values to be passed between interceptor methods in the sameInvocationContext instance using a Map.Returns the method of the bean class for which the interceptor was invoked.Returns the parameters that will be used to invoke the business method.Returns the bean instance that the intercepted method belongs to.Returns the timer associated <strong>with</strong> a @Timeout method.Causes the invocation of the next interceptor method in the chain. It returns theresult of the next method invoked. If a method is of type void, proceed returns null.Modifies the value of the parameters used for the target class method invocation.The types and the number of parameters must match the bean’s method signature,or IllegalArgumentException is thrown.To explain how the code works in Listing 8-3, let’s take a look at the sequence diagram shown inFigure 8-4 to see what happens when a client invokes the createCustomer() method. First of all, thecontainer intercepts the call and, instead of directly processing createCustomer(), first invokes thelogMethod() method. logMethod() uses the InvocationContext interface to get the name of the invokedbean (ic.getTarget()) and invoked method (ic.getMethod()) to log an entry message(logger.entering()). Then, the proceed() method is called. Calling InvocationContext.proceed() isextremely important as it tells the container that it should proceed to the next interceptor or call thebean’s business method. Not calling proceed() would stop the interceptors chain and would avoidcalling the business method. The createCustomer() is finally invoked, and, once it returns, theinterceptor finishes its execution by logging an exit message (logger.exiting()). The same sequencewould happen if a client invokes the findCustomerById() method.246

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!