1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.rugged.errors;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21
22 import org.apache.commons.math3.exception.util.ExceptionContext;
23 import org.apache.commons.math3.exception.util.ExceptionContextProvider;
24 import org.apache.commons.math3.exception.util.Localizable;
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class RuggedException extends Exception {
39
40
41 private static final long serialVersionUID = 20140309L;
42
43
44 private final ExceptionContext context;
45
46
47 private final Localizable specifier;
48
49
50 private final Object[] parts;
51
52
53
54
55
56
57 public RuggedException(final Localizable specifier, final Object ... parts) {
58 this.context = null;
59 this.specifier = specifier;
60 this.parts = (parts == null) ? new Object[0] : parts.clone();
61 }
62
63
64
65
66 public RuggedException(final RuggedException exception) {
67 super(exception);
68 this.context = exception.context;
69 this.specifier = exception.specifier;
70 this.parts = exception.parts.clone();
71 }
72
73
74
75
76
77
78 public RuggedException(final Localizable message, final Throwable cause) {
79 super(cause);
80 this.context = null;
81 this.specifier = message;
82 this.parts = new Object[0];
83 }
84
85
86
87
88
89
90
91 public RuggedException(final Throwable cause, final Localizable specifier,
92 final Object ... parts) {
93 super(cause);
94 this.context = null;
95 this.specifier = specifier;
96 this.parts = (parts == null) ? new Object[0] : parts.clone();
97 }
98
99
100
101
102
103 public RuggedException(final ExceptionContextProvider provider) {
104 super(provider.getContext().getThrowable());
105 this.context = provider.getContext();
106 this.specifier = null;
107 this.parts = new Object[0];
108 }
109
110
111
112
113
114 public String getMessage(final Locale locale) {
115 return (context != null) ?
116 context.getMessage(locale) :
117 buildMessage(locale, specifier, parts);
118 }
119
120
121 @Override
122 public String getMessage() {
123 return getMessage(Locale.US);
124 }
125
126
127 @Override
128 public String getLocalizedMessage() {
129 return getMessage(Locale.getDefault());
130 }
131
132
133
134
135 public Localizable getSpecifier() {
136 return specifier;
137 }
138
139
140
141
142 public Object[] getParts() {
143 return parts.clone();
144 }
145
146
147
148
149
150
151
152
153 private static String buildMessage(final Locale locale, final Localizable specifier,
154 final Object ... parts) {
155 return (specifier == null) ? "" : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
156 }
157
158
159
160
161
162 public static RuntimeException createInternalError(final Throwable cause) {
163
164
165 final Localizable specifier = RuggedMessages.INTERNAL_ERROR;
166
167
168 final String parts = "rugged-developers@orekit.org";
169
170 return new RuntimeException() {
171
172
173 private static final long serialVersionUID = 20140309L;
174
175
176 @Override
177 public String getMessage() {
178 return buildMessage(Locale.US, specifier, parts);
179 }
180
181
182 @Override
183 public String getLocalizedMessage() {
184 return buildMessage(Locale.getDefault(), specifier, parts);
185 }
186
187 };
188
189 }
190
191 }