Oracle ADF Architecture TV - Development - Error Handling

56
1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

description

Slides from Oracle's ADF Architecture TV series covering the Development phase of ADF projects, discussing error handling in your ADF applications. Like to know more? Check out: - Subscribe to the YouTube channel - http://bit.ly/adftvsub - Development Playlist - http://www.youtube.com/playlist?list=PLJz3HAsCPVaQfFop-QTJUE6LtjkyP_SOp - Read the episode index on the ADF Architecture Square - http://bit.ly/adfarchsquare

Transcript of Oracle ADF Architecture TV - Development - Error Handling

Page 1: Oracle ADF Architecture TV - Development - Error Handling

1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Page 2: Oracle ADF Architecture TV - Development - Error Handling

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

Page 3: Oracle ADF Architecture TV - Development - Error Handling

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

Page 4: Oracle ADF Architecture TV - Development - Error Handling

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

Page 5: Oracle ADF Architecture TV - Development - Error Handling

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?

Page 6: Oracle ADF Architecture TV - Development - Error Handling

6 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

… and Fact.

Not perfect, but a solution.

Page 7: Oracle ADF Architecture TV - Development - Error Handling

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

Page 8: Oracle ADF Architecture TV - Development - Error Handling

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

Page 9: Oracle ADF Architecture TV - Development - Error Handling

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

Page 10: Oracle ADF Architecture TV - Development - Error Handling

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

Page 11: Oracle ADF Architecture TV - Development - Error Handling

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

Page 12: Oracle ADF Architecture TV - Development - Error Handling

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

Page 13: Oracle ADF Architecture TV - Development - Error Handling

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

Page 14: Oracle ADF Architecture TV - Development - Error Handling

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

Page 15: Oracle ADF Architecture TV - Development - Error Handling

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

Page 16: Oracle ADF Architecture TV - Development - Error Handling

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

Page 17: Oracle ADF Architecture TV - Development - Error Handling

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

Page 18: Oracle ADF Architecture TV - Development - Error Handling

18 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Oracle JDeveloper 11g R2

Page 19: Oracle ADF Architecture TV - Development - Error Handling

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); } } )

Page 20: Oracle ADF Architecture TV - Development - Error Handling

20 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Skip Exception Example SQLIntegrityConstraintViolationException

skipException == false skipException == true

Page 21: Oracle ADF Architecture TV - Development - Error Handling

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(); …

Page 22: Oracle ADF Architecture TV - Development - Error Handling

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

Page 23: Oracle ADF Architecture TV - Development - Error Handling

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

Page 24: Oracle ADF Architecture TV - Development - Error Handling

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

Page 25: Oracle ADF Architecture TV - Development - Error Handling

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

Page 26: Oracle ADF Architecture TV - Development - Error Handling

26 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Exception Handler Activity How-to Define an Activity as the Exception Handler

Page 27: Oracle ADF Architecture TV - Development - Error Handling

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(); } }

Page 28: Oracle ADF Architecture TV - Development - Error Handling

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

Page 29: Oracle ADF Architecture TV - Development - Error Handling

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

Page 30: Oracle ADF Architecture TV - Development - Error 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

Page 31: Oracle ADF Architecture TV - Development - Error Handling

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

Page 32: Oracle ADF Architecture TV - Development - Error Handling

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

Page 33: Oracle ADF Architecture TV - Development - Error Handling

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

Page 34: Oracle ADF Architecture TV - Development - Error Handling

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

Page 35: Oracle ADF Architecture TV - Development - Error Handling

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!

Page 36: Oracle ADF Architecture TV - Development - Error Handling

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!

Page 37: Oracle ADF Architecture TV - Development - Error Handling

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

Page 38: Oracle ADF Architecture TV - Development - Error Handling

38 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Visual Example Exception Handler in Caller Task Flow

BANG!

!

Page 39: Oracle ADF Architecture TV - Development - Error Handling

39 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Visual Example Exception Handler in Top Level Task Flow

BANG!

!

Page 40: Oracle ADF Architecture TV - Development - Error Handling

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

Page 41: Oracle ADF Architecture TV - Development - Error Handling

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

Page 42: Oracle ADF Architecture TV - Development - Error Handling

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

Page 43: Oracle ADF Architecture TV - Development - Error Handling

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

Page 44: Oracle ADF Architecture TV - Development - Error Handling

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

Page 45: Oracle ADF Architecture TV - Development - Error Handling

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

Page 46: Oracle ADF Architecture TV - Development - Error Handling

46 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Exception Raised In Managed Bean Example: Simulating A Problem

Page 47: Oracle ADF Architecture TV - Development - Error Handling

47 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Exception Raised In Managed Bean Solution: Routing Exception To Binding Layer

Page 48: Oracle ADF Architecture TV - Development - Error Handling

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

Page 49: Oracle ADF Architecture TV - Development - Error Handling

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

Page 50: Oracle ADF Architecture TV - Development - Error Handling

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>

Page 51: Oracle ADF Architecture TV - Development - Error Handling

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

Page 52: Oracle ADF Architecture TV - Development - Error Handling

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

Page 53: Oracle ADF Architecture TV - Development - Error Handling

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

Page 54: Oracle ADF Architecture TV - Development - Error Handling

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

Page 55: Oracle ADF Architecture TV - Development - Error Handling

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

Page 56: Oracle ADF Architecture TV - Development - Error Handling

56 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.