1 /* Copyright 2013-2016 CS Systèmes d'Information
2 * Licensed to CS Systèmes d'Information (CS) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * CS licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 /** This class is the base class for all specific exceptions thrown by
27 * the rugged library classes.
28
29 * <p>
30 * This class is heavily based on Orekit {@link org.orekit.errors.OrekitException},
31 * which is distributed under the terms of the Apache License V2.
32 * </p>
33 *
34 * @author Luc Maisonobe
35
36 */
37
38 public class RuggedException extends Exception {
39
40 /** Serializable UID. */
41 private static final long serialVersionUID = 20140309L;
42
43 /** Exception context (may be null). */
44 private final ExceptionContext context;
45
46 /** Format specifier (to be translated). */
47 private final Localizable specifier;
48
49 /** Parts to insert in the format (no translation). */
50 private final Object[] parts;
51
52 /** Simple constructor.
53 * Build an exception with a translated and formatted message
54 * @param specifier format specifier (to be translated)
55 * @param parts parts to insert in the format (no translation)
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 /** Copy constructor.
64 * @param exception exception to copy from
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 /** Simple constructor.
74 * Build an exception from a cause and with a specified message
75 * @param message descriptive message
76 * @param cause underlying cause
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 /** Simple constructor.
86 * Build an exception from a cause and with a translated and formatted message
87 * @param cause underlying cause
88 * @param specifier format specifier (to be translated)
89 * @param parts parts to insert in the format (no translation)
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 /** Simple constructor.
100 * Build an exception from an Apache Commons Math exception context context
101 * @param provider underlying exception context provider
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 /** Gets the message in a specified locale.
111 * @param locale Locale in which the message should be translated
112 * @return localized message
113 */
114 public String getMessage(final Locale locale) {
115 return (context != null) ?
116 context.getMessage(locale) :
117 buildMessage(locale, specifier, parts);
118 }
119
120 /** {@inheritDoc} */
121 @Override
122 public String getMessage() {
123 return getMessage(Locale.US);
124 }
125
126 /** {@inheritDoc} */
127 @Override
128 public String getLocalizedMessage() {
129 return getMessage(Locale.getDefault());
130 }
131
132 /** Get the localizable specifier of the error message.
133 * @return localizable specifier of the error message
134 */
135 public Localizable getSpecifier() {
136 return specifier;
137 }
138
139 /** Get the variable parts of the error message.
140 * @return a copy of the variable parts of the error message
141 */
142 public Object[] getParts() {
143 return parts.clone();
144 }
145
146 /**
147 * Builds a message string by from a pattern and its arguments.
148 * @param locale Locale in which the message should be translated
149 * @param specifier format specifier (to be translated)
150 * @param parts parts to insert in the format (no translation)
151 * @return a message string
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 /** Create an {@link java.lang.RuntimeException} for an internal error.
159 * @param cause underlying cause
160 * @return an {@link java.lang.RuntimeException} for an internal error
161 */
162 public static RuntimeException createInternalError(final Throwable cause) {
163
164 /** Format specifier (to be translated). */
165 final Localizable specifier = RuggedMessages.INTERNAL_ERROR;
166
167 /** Parts to insert in the format (no translation). */
168 final String parts = "rugged-developers@orekit.org";
169
170 return new RuntimeException() {
171
172 /** Serializable UID. */
173 private static final long serialVersionUID = 20140309L;
174
175 /** {@inheritDoc} */
176 @Override
177 public String getMessage() {
178 return buildMessage(Locale.US, specifier, parts);
179 }
180
181 /** {@inheritDoc} */
182 @Override
183 public String getLocalizedMessage() {
184 return buildMessage(Locale.getDefault(), specifier, parts);
185 }
186
187 };
188
189 }
190
191 }