View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
7    *******************************************************************************/
8   package it.imolinfo.jbi4ejb.exception;
9   
10  import it.imolinfo.jbi4ejb.jbi.Messages;
11  import java.util.MissingResourceException;
12  
13  /**
14   * The default exception used within the component.
15   *
16   * @author <a href="mailto:rspazzoli@imolinfo.it">Raffaele Spazzoli</a>
17   * @author <a href="mailto:acannone@imolinfo.it">Amedeo Cannone</a>
18   */
19  public class Jbi4EjbException extends Exception {
20  
21      /**
22       * Serial Version UID.
23       */
24      private static final long serialVersionUID = 3762815969835563319L;
25  
26      /**
27       * The localized description of this <code>Throwable</code>.
28       */
29      private String localizedMessage;
30  
31      /**
32       * A constructor.
33       *
34       * @param    message        The message of the exception.
35       */
36      public Jbi4EjbException(final String message) {
37          this(message, null, null);
38      }
39  
40      /**
41       * A constructor.
42       *
43       * @param    message        The message of the exception.
44       * @param    cause        The cause of the exception.
45       */
46      public Jbi4EjbException(final String message, final Throwable cause) {
47          this(message, null, cause);
48      }
49  
50      /**
51       * A constructor.
52       *
53       * @param    cause    The cause of the exception.
54       */
55      public Jbi4EjbException(final Throwable cause) {
56          this(cause.toString(), null, cause);
57  
58          // 'message' is computed from 'cause', so there is no need to I18N
59          localizedMessage = getMessage();
60      }
61  
62      /**
63       * A constructor with i18n support.
64       *
65       * @param   message  The message of the exception.
66       * @param   args     The <code>MessageFormat</code> arguments.
67       */
68      public Jbi4EjbException(final String message, final Object[] args) {
69          this(message, args, null);
70      }
71  
72      /**
73       * A constructor with i18n support.
74       *
75       * @param   message  The message of the exception.
76       * @param   args     The <code>MessageFormat</code> arguments.
77       * @param   cause    The cause of the exception.
78       */
79      public Jbi4EjbException(final String message, final Object[] args,
80                                final Throwable cause) {
81          super(message, cause);
82          setupLocalizedMessage(args);
83      }
84  
85      /**
86       * Calculates {@link #localizedMessage} value.
87       *
88       * @param  args  the optional arguments to define the complete message. It
89       *               may be <code>null</code>.
90       */
91      private void setupLocalizedMessage(final Object[] args) {
92          StackTraceElement[] stackTrace = getStackTrace();
93  
94          if (stackTrace.length == 0) {
95              localizedMessage = getMessage();
96          } else {
97              try {
98                  Class clazz = Class.forName(stackTrace[0].getClassName());
99                  Messages messages = Messages.getMessages(clazz);
100 
101                 localizedMessage = messages.getString(getMessage(), args);
102             } catch (ClassNotFoundException e) {
103                 localizedMessage = getMessage();
104             } catch (MissingResourceException e) {
105                 localizedMessage = getMessage();
106             }
107         }
108     }
109 
110     
111     /** {@inheritDoc} */
112     public String getLocalizedMessage() {                       // Overridden
113         return localizedMessage;
114     }
115     
116     
117 }