org.openmdx.kernel.exception
Interface BasicException.Mapper

Enclosing class:
BasicException

public static interface BasicException.Mapper

An Mapper implementation maps foreign exceptions to BasicExceptions.

The default mapper operates on any Throwable

The BasicException searches first for an Mapper that matches the exception class itself, if none is registered it searches recursively for a mapper that maps its superclasses.


Method Summary
 BasicException map(Throwable throwable)
          Maps a Throwable to a BasicException.
 

Method Detail

map

BasicException map(Throwable throwable)
Maps a Throwable to a BasicException.

Sample: Lets assume you've defined a PluginException as

   public class PluginException extends Exception
   {
     public PluginException(
                String pluginId, String message, Throwable cause)
     {
       super(message);
       this.pluginId = pluginId;
       this.cause    = cause;
     }

     public String toString()
     {
        return "PluginException: " + this.pluginID + " " + this.message;
     }

     public String    getPluginId() { return this.pluginId; }
     public Throwable getCause()    { return this.cause; }

     private String    pluginId;
     private String    message;
     private Throwable cause;
   }
 

The associated mapper could look like:

   public class PluginExceptionMapper implements Mapper
   {
     public BasicException map(Throwable throwable)
     {
       PluginException   pex = (PluginException)throwable;

       String[] backTrace = BasicException.breakupStackTrace(
                                                           throwable);

       HashMap map = BasicException.parseStackTraceEntry(
                                                        backTrace, 0);

       // Note: This constructor maps nested cause Throwables for free
       BasicException se = new BasicException(
                                   pex.getCause(),
                                   (String)map.get("class"),
                                   (String)map.get("method"),
                                   ((Integer)map.get("line")).intValue(),
                                   BasicException.Code.DEFAULT_DOMAIN,
                                   BasicException.Code.GENERIC,
                                   new BasicException.Parameter[] {
                                     new BasicException.Parameter(
                                       "plugin-id",pex.getPluginId())
                                   }
                                   pex.getMessage(),
                                   backTrace,
                                   new Date());
     }
   }
 

The following sample shows a 3-level BasicException produced by the mapper

      PluginException pe = new PluginException(
                                  CorbaExportObjectPlugin.PLUGIN_ID,
                                  "A plugin exception message",
                                  new IllegalArgumentException(
                                              "An exception message"));

      BasicException se = new BasicException(
                                  pe,
                                  BasicException.Code.DEFAULT_DOMAIN,
                                  BasicException.Code.SYSTEM_EXCEPTION,
                                  new BasicException.Parameter[] { new BasicException.Parameter("0", 0) },
                                  "A stacked exception message");


      BasicException.Entry[0]
        Timestamp=2003-04-18 18:55:59.556
        Class=org.openmdx.sample.Test
        Method=init
        Line=128
        ExceptionDomain=DefaultDomain
        ExceptionCode=SYSTEM_EXCEPTION
        Param[0]: 0=0
        Description=A stacked exception message
        Backtrace:
          org.openmdx.sample.Test.init(Test.java:128)
          ...
          org.openmdx.sample.Test.main(Test.java:81)

      BasicException.Entry[1]
        Timestamp=2003-04-18 18:55:59.556
        Class=org.openmdx.sample.Test
        Method=init
        Line=122
        ExceptionDomain=DefaultDomain
        ExceptionCode=GENERIC
        Param[0]: plugin-id=CorbaExportObject
        Description=org.openmdx.PluginException: A plugin exception message
        Backtrace:
          org.openmdx.sample.Test.init(Test.java:122)
          ...
          org.openmdx.sample.Test.main(Test.java:81)

      BasicException.Entry[2]
        Timestamp=2003-04-18 18:55:59.556
        Class=org.openmdx.sample.Test
        Method=init
        Line=126
        ExceptionDomain=DefaultDomain
        ExceptionCode=GENERIC
        Param[0]: class=java.lang.IllegalArgumentException
        Description=An exception message
        Backtrace:
          org.openmdx.sample.Test.init(Test.java:126)
          ...
          org.openmdx.sample.Test.main(Test.java:81)
 

Parameters:
throwable - A throwable
Returns:
A mapped BasicException or null if the mapper cannot map the Throwable


This software is published under the BSD license. Copyright © 2003-2007, OMEX AG, Switzerland, All rights reserved. Use is subject to license terms.