View Javadoc

1   /**
2    * - Digitalis Internal Framework v2.0 - (C) 2007, Digitalis Informatica. Distribuicao e Gestao de Informatica, Lda.
3    * Estrada de Paco de Arcos num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999
4    * http://www.digitalis.pt
5    */
6   package pt.digitalis.dif.exception;
7   
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import pt.digitalis.dif.utils.ObjectFormatter;
12  
13  /**
14   * Base class for the framework Exceptions.
15   * 
16   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
17   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
18   * @created 2007/03/16
19   */
20  abstract public class DIFException extends Exception implements IContextException {
21  
22      /**  */
23      private static final long serialVersionUID = -462214831205636575L;
24  
25      /** Exception context. */
26      private Map<String, Object> exceptionContext;
27  
28      /**
29       * Constructs a new DIFException that wraps a Java Exception.
30       * 
31       * @param exception
32       *            the exception to encapsulate.
33       */
34      public DIFException(Exception exception)
35      {
36          super(exception);
37      }
38  
39      /**
40       * Constructs a new DIFException from a reason.
41       * 
42       * @param reason
43       *            the exception reason.
44       */
45      public DIFException(String reason)
46      {
47          super(reason);
48      }
49  
50      /**
51       * Constructs a new DIFException from a reason and a Java Exception.
52       * 
53       * @param reason
54       *            the exception reason.
55       * @param exception
56       *            the exception to encapsulate.
57       */
58      public DIFException(String reason, Exception exception)
59      {
60          super(reason, exception);
61      }
62  
63      /**
64       * @see pt.digitalis.dif.exception.IContextException#addToExceptionContext(String, Object)
65       */
66      public void addToExceptionContext(String key, Object value)
67      {
68          if (this.exceptionContext == null)
69              this.exceptionContext = new HashMap<String, Object>();
70  
71          this.exceptionContext.put(key, value);
72      }
73  
74      /**
75       * @see pt.digitalis.dif.exception.IContextException#getExceptionContext()
76       */
77      public Map<String, Object> getExceptionContext()
78      {
79          return exceptionContext;
80      }
81  
82      /**
83       * @see pt.digitalis.dif.exception.IContextException#getRenderedExceptionContext()
84       */
85      public String getRenderedExceptionContext()
86      {
87  
88          ObjectFormatter formatter = new ObjectFormatter();
89          formatter.addItemIfNotNull("Cause", getCause());
90          formatter.addItemIfNotNull("Message", getMessage());
91          formatter.addItemIfNotNull("Exception Context", exceptionContext);
92  
93          return "DIF Exception:\n" + formatter.getFormatedObject() + "\n";
94      }
95  
96      /**
97       * @see pt.digitalis.dif.exception.IContextException#setExceptionContext(Map)
98       */
99      public void setExceptionContext(Map<String, Object> exceptionContext)
100     {
101         if (this.exceptionContext == null)
102             this.exceptionContext = new HashMap<String, Object>();
103 
104         this.exceptionContext = exceptionContext;
105     }
106 }