1   /* Copyright 2013-2017 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.hipparchus.exception.Localizable;
23  import org.hipparchus.exception.LocalizedException;
24  
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 implements LocalizedException {
39  
40      /** Serializable UID. */
41      private static final long serialVersionUID = 20140309L;
42  
43      /** Format specifier (to be translated). */
44      private final Localizable specifier;
45  
46      /** Parts to insert in the format (no translation). */
47      private final Object[] parts;
48  
49      /** Simple constructor.
50       * Build an exception with a translated and formatted message
51       * @param specifier format specifier (to be translated)
52       * @param parts parts to insert in the format (no translation)
53       */
54      public RuggedException(final Localizable specifier, final Object... parts) {
55          this.specifier = specifier;
56          this.parts     = (parts == null) ? new Object[0] : parts.clone();
57      }
58  
59      /** Simple constructor.
60       * Build an exception from a cause and with a translated and formatted message
61       * @param cause underlying cause
62       * @param specifier format specifier (to be translated)
63       * @param parts parts to insert in the format (no translation)
64       */
65      public RuggedException(final Throwable cause, final Localizable specifier,
66                             final Object... parts) {
67          super(cause);
68          this.specifier = specifier;
69          this.parts     = (parts == null) ? new Object[0] : parts.clone();
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public String getMessage(final Locale locale) {
75          return buildMessage(locale, specifier, parts);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public String getMessage() {
81          return getMessage(Locale.US);
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public String getLocalizedMessage() {
87          return getMessage(Locale.getDefault());
88      }
89  
90      /** Get the localizable specifier of the error message.
91       * @return localizable specifier of the error message
92       */
93      public Localizable getSpecifier() {
94          return specifier;
95      }
96  
97      /** Get the variable parts of the error message.
98       * @return a copy of the variable parts of the error message
99       */
100     public Object[] getParts() {
101         return parts.clone();
102     }
103 
104     /**
105      * Builds a message string by from a pattern and its arguments.
106      * @param locale Locale in which the message should be translated
107      * @param specifier format specifier (to be translated)
108      * @param parts parts to insert in the format (no translation)
109      * @return a message string
110      */
111     private static String buildMessage(final Locale locale, final Localizable specifier,
112                                        final Object... parts) {
113         return (specifier == null) ? "" : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
114     }
115 
116     /** Create an {@link java.lang.RuntimeException} for an internal error.
117      * @param cause underlying cause
118      * @return an {@link java.lang.RuntimeException} for an internal error
119      */
120     public static RuntimeException createInternalError(final Throwable cause) {
121 
122         /** Format specifier (to be translated). */
123         final Localizable specifier = RuggedMessages.INTERNAL_ERROR;
124 
125         /** Parts to insert in the format (no translation). */
126         final String parts     = "rugged-developers@orekit.org";
127 
128         return new RuntimeException() {
129 
130             /** Serializable UID. */
131             private static final long serialVersionUID = 20140309L;
132 
133             /** {@inheritDoc} */
134             @Override
135             public String getMessage() {
136                 return buildMessage(Locale.US, specifier, parts);
137             }
138 
139             /** {@inheritDoc} */
140             @Override
141             public String getLocalizedMessage() {
142                 return buildMessage(Locale.getDefault(), specifier, parts);
143             }
144 
145         };
146 
147     }
148 
149 }