Oracle ADF Architecture TV - Development - Error Handling
-
Upload
chris-muir -
Category
Technology
-
view
455 -
download
6
description
Transcript of Oracle ADF Architecture TV - Development - Error Handling
1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
2 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Real World ADF Design & Architecture Principles Error Handling
ORACLE PRODUCT
LOGO
15th Feb 2013 v1.0
3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Learning Objectives
• At the end of this module you should be able to:
– Understand the ADF error handling architecture – Know the scope of an error handler – Distinguish between error handling and incident reporting
Image: imagerymajestic/ FreeDigitalPhotos.net
4 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
5 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Fiction …
Wouldn't it be good if there was a single location to handle all application errors and exceptions?
6 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
… and Fact.
Not perfect, but a solution.
7 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
8 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ADF Error Handling Hooks - Overview
ADF Faces (ADFv)
EJB/JPA ADF Business Components Java
ADF Model (ADFm)
JAX-WS
Unhandled exceptions
Servlet Container U
nhan
dled
exc
eptio
ns
Render Response
Propagates exceptions
ADF Controller (ADFc)
Business Service Layer
ADF Binding Layer Data Control
9 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ADF Exception Handlers
• ADFm Exception Handler – Error Handler (DCErrorHandler) in BindingContext
• ADFc Exception Handler – Exception Handler activity in Task-Flow – Application wide Exception Handler
• ADFv Exception Handler – oracle.adf.view.rich.context.ExceptionHandler
• Container, error-page in web.xml
10 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ADF Error Handling Overview ADF Interaction in JSF Request Lifecycle
EJB/JPA ADF Business Components Java
ADF Binding
JAX-WS
Restore View
Apply Request
Process Validation Update Model
Invoke Action
Render Response 1
2
3
4
5
6
JSF
11 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
12 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
oracle.jbo.JboException
• JboException extends java.lang.RuntimeException – Thus Business Component method can throw exceptions without
a throws clause in the method signature • Translation of error messages occur by the client calling getLocalizedMessage() and not when an error is thrown
One Exception Super Class for All Exceptions
13 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Bundled Exception Mode
• Enabled by default • Presents maximal set of failed validation exceptions • Java supports single exception to be thrown at a time • ADF BC moves all exceptions under a parent exception
– Multiple entity attribute validation errors (ValidationException) are wrapped in RowValException parent exception
– Multiple rows that fail validation performed during commit will have the RowValException wrapped in TxnValException object
– You can recursively introspect exception by calling getDetails() on each exception
All Exceptions at Once
14 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 14 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Exceptions that are not handled in the business service propagate to the ADF model layer.
Image: Ambro/ FreeDigitalPhotos.net
15 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
16 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ADF Model Error Handler
• The ADF model error handler deals with – All ADF binding errors – Business service errors that occur when accessing a service
trough the ADF binding layer • Formats and displays error message • Default error handler skips the top-level JboException because it
only is a bundled exception wrapper for other business exceptions
17 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Creating A Custom Error Handler
• Extend oracle.adf.model.binding.DCErrorHandlerImpl and configure your custom error error handler in DataBindings.cpx
• Override the following methods as needed – reportException
• Override this method to analyze exceptions for errors you want to handle – getDisplayMessage
• Override to change or suppress (null value) the displayed message – getDetailedDisplayMessage
• Override this method to provide HTML markup formatted detail information – skipException(Exception ex)
• Called by the framework to check if an exception should be ignored
18 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Oracle JDeveloper 11g R2
19 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Custom Error Handler Example Skip Exception for SQL Exceptions
public class CustomErrorHandler extends DCErrorHandlerImpl { ... protected boolean skipException(Exception ex) { if (ex instanceof DMLConstraintException) { return false; //don't show details of SQL exception } else if (ex instanceof SQLException) { return true; } else { return super.skipException(ex); } } )
20 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Skip Exception Example SQLIntegrityConstraintViolationException
skipException == false skipException == true
21 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 21 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
The code is from a managed bean in our application. What happens in case of an
exception?
Image: imagerymajestic/ FreeDigitalPhotos.net
OperationBinding operationBinding = null; operationBinding = getOperationBinding("UpdateOrder"); operationBinding.execute(); …
22 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
On a Related Note
• It happens quite often that developers execute a client method directly on the Application Module – BindingContext > findDataControl("...") > getDataProvider() – Cast data provider to Application Module interface – Execute client methods on it
• The risk is that – Exceptions thrown by the client interface logic are not handled by the
ADFm error handler if invoked directly – Inconsistent error handling may occur
23 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
24 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 24 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
The ADF controller handles exceptions that occur during task flow execution
Image: Ambro/ FreeDigitalPhotos.net
25 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ADF Controller Exception Handling
• Exceptions that may occur during task flow execution include – Method call activity that throws an exception – Task flow initialize or finalize methods throwing an exception – Authorization failures when accessing an activity
• Controller exceptions are handled by a task flow activity that is designated as an exception handler
• When a task flow throws an exception, control flow is passed to the designated exception handling activity
Introduction
26 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Exception Handler Activity How-to Define an Activity as the Exception Handler
27 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Example: Method Error Handler
public void showErrorDialog() { ControllerContext context = ControllerContext.getInstance(); ViewPortContext currentViewPort = context.getCurrentViewPort(); if (currentViewPort.isExceptionPresent()) { FacesMessage fm = new FacesMessage( FacesMessage.SEVERITY_WARN, "Application Error", currentViewPort.getExceptionData().getMessage()); FacesContext fctx = FacesContext.getCurrentInstance(); fctx.addMessage("ApplicationInfo", fm); fctx.renderResponse(); } }
28 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ADF Controller
• Exceptions that occur in context of task flow are forwarded to exception handler activity
• Best Practices - Define exception handling activity in each bounded and unbounded task flows
• Define exception handler in task flow template for consistency
Exception Handler Activity
29 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ADF Controller
• AdfcExceptionHandler is the ADF controller internal framework class that passes control to the task flow's designated exception handler activity
• If no exception handler activity exists, the control is passed the browser
• Default exception handler does not handle exceptions thrown during RENDER_RESPONSE phase
Default Exception Handling
30 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Customizing the Framework Error Handling
• Configuration – Create oracle.adf.view.rich.context.ExceptionHandler
text file and store it in .adf\META-INF\services – you need to create the “services” folder – Add the absolute name of your custom exception handler
• Java class that extends ExceptionHandler
• Configured for whole ADF application – Deployed in EAR file
Add Your Own Exception Handler
31 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
import oracle.adf.view.rich.context.ExceptionHandler; public class CustomExceptionHandler extends ExceptionHandler { public CustomExceptionHandler() { super(); } public void handleException(FacesContext facesContext, Throwable throwable, PhaseId phaseId) throws Throwable{ if ( … Exception or Error to check …){ //handle issue } else { //pass error to default exception handler throw throwable; } } }
ADF Controller Overriding Default Exception Handler Class
32 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 32 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Be thoughtful. Don't override the framework exception handler to eagerly or for no reason. Keep in mind that the error handler is not deployed with bounded task flows but deployed with the application.
Image: Ambro/ FreeDigitalPhotos.net
33 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Log Exceptions, Report Exceptions ..
• Exception handler activities handle exceptions but don't report them – No programming error fixes itself – Log and report programming or system errors
• Implement a exception handling, logging and reporting strategy that can be reused and that is declaratively applied
• Report serious issues – Considers weekends and vacations
• Evaluate BPM (SOA Suite) Human Workflow integration – In case of emergency, send mail or SMS
34 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
35 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
What Could Happen Task Flow Exception Propagation
Memory Stack
Current Task Flow
Task Flow 4
Task Flow 3
Task Flow 2
Task Flow 1
Task Flow 2 Call
Task Flow 3 Call
Task Flow 4 Call
!
!
View1
View2
View3
View4
Task Flow 1
Task Flow 2
Task Flow 3
Task Flow 4
EH
EH
Method Call
Current Task Flow
BANG!
36 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
What Could Happen Task Flow Exception Propagation
Memory Stack
Current Task Flow
Task Flow 2
Task Flow 1
Task Flow 2 Call
Task Flow 3 Call
Task Flow 4 Call
Propagate Exception
Propagate Exception
!
!
View1
View2
View3
View4
Task Flow 1
Task Flow 2
Task Flow 3
Task Flow 4
EH
EH
Current Task Flow
Method Call
BANG!
37 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
What Could Happen Task Flow Exception Propagation
!
!
View1
View2
Task Flow 1
Task Flow 2
EH
EH
Memory Stack
Current Task Flow
Task Flow 2
Task Flow 1
Task Flow 2 Call
38 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Visual Example Exception Handler in Caller Task Flow
BANG!
!
39 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Visual Example Exception Handler in Top Level Task Flow
BANG!
!
40 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 40 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Every bounded task flow should have an error handler activity defined. Task flow templates help to configure error handlers consistent throughout an application.
Image: Ambro/ FreeDigitalPhotos.net
41 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
42 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Unbounded Task Flow
• There always is a single unbounded task flow instance available for an application – Independent of the number of adfc-config.xml files available in the
application class path • Unbounded task flows cannot be based on task flow templates
Exception Handling Strategy
43 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Unbounded Task Flow
• To build "error handling templates" for unbounded task flows you use an unbounded task flow definitions in an ADF Library – Keep adfc-config.xml as the unbounded task flow definition file – Deploy error handling logic as managed bean within ADF library – Load order of unbounded configuration files determines which
exception handler become active in cases of multiple defined handlers
– ADFc configuration files on the class path precede configurations in the application
Exception Handling Strategy
44 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
45 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Exception Handling • Typically, view layer exceptions occur because of
– Failed execution of client logic in managed beans – Failed access to the ADF binding layer
• EL references in page source • Java references in managed beans
– Failed EL expressions • Typos in the object name • Missing objects (e.g. entity object references) • Failed type casting (e.g. oracle.jbo.domain.Number to Long)
– Expired session
46 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Exception Raised In Managed Bean Example: Simulating A Problem
47 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Exception Raised In Managed Bean Solution: Routing Exception To Binding Layer
48 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
49 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Servlet Container
• Handles all exceptions not handled by an application – Available to all Java EE applications
• Looks in web.xml file for directive on how to handle a specific error code or the exception type
• If nothing is set-up in web.xml, the exception terminates the user application session – In Java, a stack trace is displayed in the browser, which exactly
what you don't want to happen
50 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Java EE Exception Handling Last Resort
<error-page> <error-code> … http error code here (e.g 404)… </error-code> <location>/unhandled_error_page.html</location> </error-page> <error-page> <exception-type> … exception class here … (e.g. java.lang.Throwable) </exception-type> <location>/unhandled_exception_page.html</location> </error-page>
51 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda • Error Handling Introduction • Handling ADF Lifecycle Errors • ADF Business Components • ADF Binding Layer • ADF Controller
– Bounded Task Flow – Unbounded Task Flow
• ADF View • Servlet Container • Summary
52 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
ADF Error Handling Summary Error Handling by Architecture Layers
• try-catch block
• ADFm Error Handler
• Exception Handler Activity • Custom ADFc Exception Handler
• try-catch block
• web.xml
ADF Faces
EJB/JPA ADF Business Components Java
ADF Model
JAX-WS
Unhandled exceptions
Servlet Container
Unh
andl
ed e
xcep
tions
Render Response
Propagates exceptions
ADF Controller
53 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Conclusion
• Anticipate problems and work on a mitigation strategy – Only leave unexpected errors and errors you
want for pass through unhandled for the handlers – Like for application security, perform a risk
analysis and identify threats (database down, WS not responsive, NPE …)
• Define exception handling strategies and code them into custom standard exception handler classes
• Log errors by severity
54 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Conclusion
• Access business service methods through the binding layer
• Use try/catch blocks in any managed bean method – Route managed bean exceptions to the binding layer
reportError() method • Use custom task flow exception handler thoughful
– .adf\META-INF\services – Avoid new exceptions in your exception handler – Keep in mind that this exception handler is on an
application level and not task flow / ADF library level
55 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Further Reading
• Oracle Magazine Article "Catch Me If You Can" – http://www.oracle.com/technetwork/issue-archive/2013/13-mar/
o23adf-1897193.html
• Oracle JDeveloper and ADF Documentation Library – Fusion Developer Guide
• "Handling and Displaying Exceptions in an ADF Application" • "Customizing Business Components Error Messages"
• How to log all errors to the database – http://one-size-doesnt-fit-all.blogspot.de/2008/06/jdev-adf-how-to-log-all-
errors-to.html
56 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.