1   /* Copyright 2002-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.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 orekit classes.
28  
29   * <p>When the orekit classes throw exceptions that are specific to
30   * the package, these exceptions are always subclasses of
31   * OrekitException. When exceptions that are already covered by the
32   * standard java API should be thrown, like
33   * ArrayIndexOutOfBoundsException or InvalidParameterException, these
34   * standard exceptions are thrown rather than the commons-math specific
35   * ones.</p>
36   * <p>This class also provides utility methods to throw some standard
37   * java exceptions with localized messages.</p>
38   *
39   * @author Luc Maisonobe
40  
41   */
42  
43  public class OrekitException extends Exception implements LocalizedException {
44  
45      /** Serializable UID. */
46      private static final long serialVersionUID = 20150611L;
47  
48      /** Exception context (may be null). */
49      private final ExceptionContext context;
50  
51      /** Format specifier (to be translated). */
52      private final Localizable specifier;
53  
54      /** Parts to insert in the format (no translation). */
55      private final Object[] parts;
56  
57      /** Simple constructor.
58       * Build an exception with a translated and formatted message
59       * @param specifier format specifier (to be translated)
60       * @param parts parts to insert in the format (no translation)
61       */
62      public OrekitException(final Localizable specifier, final Object ... parts) {
63          this.context   = null;
64          this.specifier = specifier;
65          this.parts     = (parts == null) ? new Object[0] : parts.clone();
66      }
67  
68      /** Copy constructor.
69       * @param exception exception to copy from
70       * @since 5.1
71       */
72      public OrekitException(final OrekitException exception) {
73          super(exception);
74          this.context   = exception.context;
75          this.specifier = exception.specifier;
76          this.parts     = exception.parts.clone();
77      }
78  
79      /** Simple constructor.
80       * Build an exception from a cause and with a specified message
81       * @param message descriptive message
82       * @param cause underlying cause
83       */
84      public OrekitException(final Localizable message, final Throwable cause) {
85          super(cause);
86          this.context   = null;
87          this.specifier = message;
88          this.parts     = new Object[0];
89      }
90  
91      /** Simple constructor.
92       * Build an exception from a cause and with a translated and formatted message
93       * @param cause underlying cause
94       * @param specifier format specifier (to be translated)
95       * @param parts parts to insert in the format (no translation)
96       */
97      public OrekitException(final Throwable cause, final Localizable specifier,
98                             final Object ... parts) {
99          super(cause);
100         this.context   = null;
101         this.specifier = specifier;
102         this.parts     = (parts == null) ? new Object[0] : parts.clone();
103     }
104 
105     /** Simple constructor.
106      * Build an exception from an Apache Commons Math exception context context
107      * @param provider underlying exception context provider
108      * @since 6.0
109      */
110     public OrekitException(final ExceptionContextProvider provider) {
111         super(provider.getContext().getThrowable());
112         this.context   = provider.getContext();
113         this.specifier = null;
114         this.parts     = new Object[0];
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public String getMessage(final Locale locale) {
120         return (context != null) ?
121                 context.getMessage(locale) :
122                 buildMessage(locale, specifier, parts);
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public String getMessage() {
128         return getMessage(Locale.US);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public String getLocalizedMessage() {
134         return getMessage(Locale.getDefault());
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public Localizable getSpecifier() {
140         return specifier;
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public Object[] getParts() {
146         return parts.clone();
147     }
148 
149     /**
150      * Builds a message string by from a pattern and its arguments.
151      * @param locale Locale in which the message should be translated
152      * @param specifier format specifier (to be translated)
153      * @param parts parts to insert in the format (no translation)
154      * @return a message string
155      */
156     private static String buildMessage(final Locale locale, final Localizable specifier, final Object ... parts) {
157         return (specifier == null) ? "" : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
158     }
159 
160     /** Create an {@link java.lang.IllegalArgumentException} with localized message.
161      * @param specifier format specifier (to be translated)
162      * @param parts parts to insert in the format (no translation)
163      * @return an {@link java.lang.IllegalArgumentException} that also implements
164      * @deprecated as of 7.1, replaced with {@link
165      * OrekitIllegalArgumentException#OrekitIllegalArgumentException(Localizable, Object...)}
166      */
167     @Deprecated
168     public static OrekitIllegalArgumentException createIllegalArgumentException(final Localizable specifier,
169                                                                                 final Object ... parts) {
170         return new OrekitIllegalArgumentException(specifier, parts);
171     }
172 
173     /** Create an {@link java.lang.IllegalStateException} with localized message.
174      * @param specifier format specifier (to be translated)
175      * @param parts parts to insert in the format (no translation)
176      * @return an {@link java.lang.IllegalStateException} with localized message
177      * @deprecated as of 7.1, replaced with {@link
178      * OrekitIllegalStateException#OrekitIllegalStateException(Localizable, Object...)}
179      */
180     @Deprecated
181     public static OrekitIllegalStateException createIllegalStateException(final Localizable specifier,
182                                                                           final Object ... parts) {
183 
184         return new OrekitIllegalStateException(specifier, parts);
185     }
186 
187     /** Create an {@link java.text.ParseException} with localized message.
188      * @param specifier format specifier (to be translated)
189      * @param parts parts to insert in the format (no translation)
190      * @return an {@link java.text.ParseException} with localized message
191      * @deprecated as of 7.1, replaced with {@link
192      * OrekitParseException#OrekitParseException(Localizable, Object...)}
193      */
194     @Deprecated
195     public static OrekitParseException createParseException(final Localizable specifier,
196                                                             final Object ... parts) {
197         return new OrekitParseException(specifier, parts);
198     }
199 
200     /** Create an {@link java.lang.RuntimeException} for an internal error.
201      * @param cause underlying cause
202      * @return an {@link java.lang.RuntimeException} for an internal error
203      * @deprecated as of 7.1, replaced with {@link
204      * OrekitInternalError#OrekitInternalError(Throwable)}
205      */
206     @Deprecated
207     public static RuntimeException createInternalError(final Throwable cause) {
208         return new OrekitInternalError(cause);
209     }
210 
211 }