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 it.imolinfo.jbi4corba.jbi.Messages;
12 import java.util.MissingResourceException;
13
14 /**
15 * The default 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 Jbi4CorbaException extends Exception {
21
22 /**
23 * Serial Version UID.
24 */
25 private static final long serialVersionUID = 3762815969835563319L;
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 Jbi4CorbaException(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 Jbi4CorbaException(final String message, final Throwable cause) {
48 this(message, null, cause);
49 }
50
51 /**
52 * A constructor.
53 *
54 * @param cause The cause of the exception.
55 */
56 public Jbi4CorbaException(final Throwable cause) {
57 this(cause.toString(), null, cause);
58
59 // 'message' is computed from 'cause', so there is no need to I18N
60 localizedMessage = getMessage();
61 }
62
63 /**
64 * A constructor with i18n support.
65 *
66 * @param message The message of the exception.
67 * @param args The <code>MessageFormat</code> arguments.
68 */
69 public Jbi4CorbaException(final String message, final Object[] args) {
70 this(message, args, null);
71 }
72
73 /**
74 * A constructor with i18n support.
75 *
76 * @param message The message of the exception.
77 * @param args The <code>MessageFormat</code> arguments.
78 * @param cause The cause of the exception.
79 */
80 public Jbi4CorbaException(final String message, final Object[] args,
81 final Throwable cause) {
82 super(message, cause);
83 setupLocalizedMessage(args);
84 }
85
86 /**
87 * Calculates {@link #localizedMessage} value.
88 *
89 * @param args the optional arguments to define the complete message. It
90 * may be <code>null</code>.
91 */
92 @SuppressWarnings("unchecked")
93 private void setupLocalizedMessage(final Object[] args) {
94 StackTraceElement[] stackTrace = getStackTrace();
95
96 if (stackTrace.length == 0) {
97 localizedMessage = getMessage();
98 } else {
99 try {
100 Class clazz = Class.forName(stackTrace[0].getClassName());
101 Messages messages = Messages.getMessages(clazz);
102
103 localizedMessage = messages.getString(getMessage(), args);
104 } catch (ClassNotFoundException e) {
105 localizedMessage = getMessage();
106 } catch (MissingResourceException e) {
107 localizedMessage = getMessage();
108 }
109 }
110 }
111
112 /**
113 * Override.
114 * @return The return
115 */
116 public String getLocalizedMessage() { // Overridden
117 return localizedMessage;
118 }
119 }