1   /* Copyright 2002-2013 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 org.apache.commons.math3.exception.util.ExceptionContextProvider;
20  import org.apache.commons.math3.exception.util.Localizable;
21  
22  /** This class is the base class for all specific exceptions thrown by
23   * during the propagation computation.
24   *
25   * @author Luc Maisonobe
26   */
27  public class PropagationException extends OrekitException {
28  
29      /** Serializable UID. */
30      private static final long serialVersionUID = 1684307015196169376L;
31  
32      /** Simple constructor.
33       * Build an exception with a translated and formatted message
34       * @param specifier format specifier (to be translated)
35       * @param parts parts to insert in the format (no translation)
36       */
37      public PropagationException(final Localizable specifier, final Object ... parts) {
38          super(specifier, parts);
39      }
40  
41      /** Simple constructor.
42       * Build an exception from a cause and with a specified message
43       * @param cause underlying cause
44       * @param specifier format specifier (to be translated)
45       * @param parts parts to insert in the format (no translation)
46       */
47      public PropagationException(final Throwable cause, final Localizable specifier,
48                                  final Object ... parts) {
49          super(cause, specifier, parts);
50      }
51  
52      /** Simple constructor.
53       * Build an exception wrapping an {@link OrekitException} instance
54       * @param exception underlying cause
55       */
56      public PropagationException(final OrekitException exception) {
57          super(exception);
58      }
59  
60      /** Simple constructor.
61       * Build an exception wrapping an Apache Commons Math exception context exception
62       * @param provider underlying cause
63       */
64      public PropagationException(final ExceptionContextProvider provider) {
65          super(provider);
66      }
67  
68      /** Recover a PropagationException, possibly embedded in a {@link OrekitException}.
69       * <p>
70       * If the {@code OrekitException} does not embed a PropagationException, a
71       * new one will be created.
72       * </p>
73       * @param oe OrekitException to analyze
74       * @return a (possibly embedded) PropagationException
75       */
76      public static PropagationException unwrap(final OrekitException oe) {
77  
78          for (Throwable t = oe; t != null; t = t.getCause()) {
79              if (t instanceof PropagationException) {
80                  return (PropagationException) t;
81              }
82          }
83  
84          return new PropagationException(oe);
85  
86      }
87  
88      /** Recover a PropagationException, possibly embedded in an {@link ExceptionContextProvider}.
89       * <p>
90       * If the {@code ExceptionContextProvider} does not embed a PropagationException, a
91       * new one will be created.
92       * </p>
93       * @param provider ExceptionContextProvider to analyze
94       * @return a (possibly embedded) PropagationException
95       */
96      public static PropagationException unwrap(final ExceptionContextProvider provider) {
97  
98          for (Throwable t = provider.getContext().getThrowable(); t != null; t = t.getCause()) {
99              if (t instanceof OrekitException) {
100                 if (t instanceof PropagationException) {
101                     return (PropagationException) t;
102                 } else {
103                     return new PropagationException((OrekitException) t);
104                 }
105             }
106         }
107 
108         return new PropagationException(provider);
109 
110     }
111 
112 }