View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 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   
9   package it.imolinfo.jbi4corba.exception;
10  
11  import java.util.MissingResourceException;
12  import it.imolinfo.jbi4corba.jbi.Messages;
13  
14  /**
15   * The default 'run time' exception used within the component.
16   *
17   * @author <a href="mailto:rspazzoli@imolinfo.it">Raffaele Spazzoli</a>
18   * @author <a href="mailto:acannone@imolinfo.it">Amedeo Cannone</a>
19   */
20  public class Jbi4CorbaRuntimeException extends RuntimeException {
21  
22      /**
23       * Serial Version UID.
24       */
25      private static final long serialVersionUID = 8309654013669032549L;
26  
27      /**
28       * The localized description of this <code>Throwable</code>.
29       */
30      private String localizedMessage;
31  
32      /**
33       * A constructor.
34       *
35       * @param    message        The message of the exception.
36       */
37      public Jbi4CorbaRuntimeException(final String message) {
38          this(message, null, null);
39      }
40  
41      /**
42       * A constructor.
43       *
44       * @param    message        The message of the exception.
45       * @param    cause        The cause of the exception.
46       */
47      public Jbi4CorbaRuntimeException(final String message,
48                                       final Throwable cause) {
49          this(message, null, cause);
50      }
51  
52      /**
53       * A constructor.
54       *
55       * @param    cause    The cause of the exception.
56       */
57      public Jbi4CorbaRuntimeException(final Throwable cause) {
58          this(cause.toString(), null, cause);
59  
60          // 'message' is computed from 'cause', so there is no need to I18N
61          localizedMessage = getMessage();
62      }
63  
64      /**
65       * A constructor with i18n support.
66       *
67       * @param   message  The message of the exception.
68       * @param   args     The <code>MessageFormat</code> arguments.
69       */
70      public Jbi4CorbaRuntimeException(final String message,
71                                       final Object[] args) {
72          this(message, args, null);
73      }
74  
75      /**
76       * A constructor with i18n support.
77       *
78       * @param   message  The message of the exception.
79       * @param   args     The <code>MessageFormat</code> arguments.
80       * @param   cause    The cause of the exception.
81       */
82      public Jbi4CorbaRuntimeException(
83              final String message, final Object[] args, final Throwable cause) {
84          super(message, cause);
85          setupLocalizedMessage(args);
86      }
87  
88      /**
89       * Calculates {@link #localizedMessage} value.
90       *
91       * @param  args  the optional arguments to define the complete message. It
92       *               may be <code>null</code>.
93       */
94      @SuppressWarnings("unchecked")
95  	private void setupLocalizedMessage(final Object[] args) {
96          StackTraceElement[] stackTrace = getStackTrace();
97  
98          if (stackTrace.length == 0) {
99              localizedMessage = getMessage();
100         } else {
101             try {
102                 Class clazz = Class.forName(stackTrace[0].getClassName());
103                 Messages messages = Messages.getMessages(clazz);
104 
105                 localizedMessage = messages.getString(getMessage(), args);
106             } catch (ClassNotFoundException e) {
107                 localizedMessage = getMessage();
108             } catch (MissingResourceException e) {
109                 localizedMessage = getMessage();
110             }
111         }
112     }
113 
114     /**
115      * Override.
116      * 
117      * @return  The return
118      */
119     public String getLocalizedMessage() {                       // Overridden
120         return localizedMessage;
121     }
122 }