OrekitException.java

  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. import java.text.MessageFormat;
  19. import java.text.ParseException;
  20. import java.util.Locale;

  21. import org.apache.commons.math3.exception.util.ExceptionContext;
  22. import org.apache.commons.math3.exception.util.ExceptionContextProvider;
  23. import org.apache.commons.math3.exception.util.Localizable;

  24. /** This class is the base class for all specific exceptions thrown by
  25.  * the orekit classes.

  26.  * <p>When the orekit classes throw exceptions that are specific to
  27.  * the package, these exceptions are always subclasses of
  28.  * OrekitException. When exceptions that are already covered by the
  29.  * standard java API should be thrown, like
  30.  * ArrayIndexOutOfBoundsException or InvalidParameterException, these
  31.  * standard exceptions are thrown rather than the commons-math specific
  32.  * ones.</p>
  33.  * <p>This class also provides utility methods to throw some standard
  34.  * java exceptions with localized messages.</p>
  35.  *
  36.  * @author Luc Maisonobe

  37.  */

  38. public class OrekitException extends Exception {

  39.     /** Serializable UID. */
  40.     private static final long serialVersionUID = 3366757982695469677L;

  41.     /** Exception context (may be null). */
  42.     private final ExceptionContext context;

  43.     /** Format specifier (to be translated). */
  44.     private final Localizable specifier;

  45.     /** Parts to insert in the format (no translation). */
  46.     private final Object[] parts;

  47.     /** Simple constructor.
  48.      * Build an exception with a translated and formatted message
  49.      * @param specifier format specifier (to be translated)
  50.      * @param parts parts to insert in the format (no translation)
  51.      */
  52.     public OrekitException(final Localizable specifier, final Object ... parts) {
  53.         this.context   = null;
  54.         this.specifier = specifier;
  55.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  56.     }

  57.     /** Copy constructor.
  58.      * @param exception exception to copy from
  59.      * @since 5.1
  60.      */
  61.     public OrekitException(final OrekitException exception) {
  62.         super(exception);
  63.         this.context   = exception.context;
  64.         this.specifier = exception.specifier;
  65.         this.parts     = exception.parts.clone();
  66.     }

  67.     /** Simple constructor.
  68.      * Build an exception from a cause and with a specified message
  69.      * @param message descriptive message
  70.      * @param cause underlying cause
  71.      */
  72.     public OrekitException(final Localizable message, final Throwable cause) {
  73.         super(cause);
  74.         this.context   = null;
  75.         this.specifier = message;
  76.         this.parts     = new Object[0];
  77.     }

  78.     /** Simple constructor.
  79.      * Build an exception from a cause and with a translated and formatted message
  80.      * @param cause underlying cause
  81.      * @param specifier format specifier (to be translated)
  82.      * @param parts parts to insert in the format (no translation)
  83.      */
  84.     public OrekitException(final Throwable cause, final Localizable specifier,
  85.                            final Object ... parts) {
  86.         super(cause);
  87.         this.context   = null;
  88.         this.specifier = specifier;
  89.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  90.     }

  91.     /** Simple constructor.
  92.      * Build an exception from an Apache Commons Math exception context context
  93.      * @param provider underlying exception context provider
  94.      * @since 6.0
  95.      */
  96.     public OrekitException(final ExceptionContextProvider provider) {
  97.         super(provider.getContext().getThrowable());
  98.         this.context   = provider.getContext();
  99.         this.specifier = null;
  100.         this.parts     = new Object[0];
  101.     }

  102.     /** Gets the message in a specified locale.
  103.      * @param locale Locale in which the message should be translated
  104.      * @return localized message
  105.      * @since 5.0
  106.      */
  107.     public String getMessage(final Locale locale) {
  108.         return (context != null) ?
  109.                 context.getMessage(locale) :
  110.                 buildMessage(locale, specifier, parts);
  111.     }

  112.     /** {@inheritDoc} */
  113.     @Override
  114.     public String getMessage() {
  115.         return getMessage(Locale.US);
  116.     }

  117.     /** {@inheritDoc} */
  118.     @Override
  119.     public String getLocalizedMessage() {
  120.         return getMessage(Locale.getDefault());
  121.     }

  122.     /** Get the localizable specifier of the error message.
  123.      * @return localizable specifier of the error message
  124.      * @since 5.1
  125.      */
  126.     public Localizable getSpecifier() {
  127.         return specifier;
  128.     }

  129.     /** Get the variable parts of the error message.
  130.      * @return a copy of the variable parts of the error message
  131.      * @since 5.1
  132.      */
  133.     public Object[] getParts() {
  134.         return parts.clone();
  135.     }

  136.     /**
  137.      * Builds a message string by from a pattern and its arguments.
  138.      * @param locale Locale in which the message should be translated
  139.      * @param specifier format specifier (to be translated)
  140.      * @param parts parts to insert in the format (no translation)
  141.      * @return a message string
  142.      */
  143.     private static String buildMessage(final Locale locale, final Localizable specifier,
  144.                                        final Object ... parts) {
  145.         return (specifier == null) ? "" : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
  146.     }

  147.     /** Create an {@link java.lang.IllegalArgumentException} with localized message.
  148.      * @param specifier format specifier (to be translated)
  149.      * @param parts parts to insert in the format (no translation)
  150.      * @return an {@link java.lang.IllegalArgumentException} with localized message
  151.      */
  152.     public static IllegalArgumentException createIllegalArgumentException(final Localizable specifier,
  153.                                                                           final Object ... parts) {
  154.         return new IllegalArgumentException() {

  155.             /** Serializable UID. */
  156.             private static final long serialVersionUID = 2601215225271704045L;

  157.             /** {@inheritDoc} */
  158.             @Override
  159.             public String getMessage() {
  160.                 return buildMessage(Locale.US, specifier, parts);
  161.             }

  162.             /** {@inheritDoc} */
  163.             @Override
  164.             public String getLocalizedMessage() {
  165.                 return buildMessage(Locale.getDefault(), specifier, parts);
  166.             }

  167.         };

  168.     }

  169.     /** Create an {@link java.lang.IllegalStateException} with localized message.
  170.      * @param specifier format specifier (to be translated)
  171.      * @param parts parts to insert in the format (no translation)
  172.      * @return an {@link java.lang.IllegalStateException} with localized message
  173.      */
  174.     public static IllegalStateException createIllegalStateException(final Localizable specifier,
  175.                                                                     final Object ... parts) {

  176.         return new IllegalStateException() {

  177.             /** Serializable UID. */
  178.             private static final long serialVersionUID = -5527779242879685212L;

  179.             /** {@inheritDoc} */
  180.             @Override
  181.             public String getMessage() {
  182.                 return buildMessage(Locale.US, specifier, parts);
  183.             }

  184.             /** {@inheritDoc} */
  185.             @Override
  186.             public String getLocalizedMessage() {
  187.                 return buildMessage(Locale.getDefault(), specifier, parts);
  188.             }

  189.         };

  190.     }

  191.     /** Create an {@link java.text.ParseException} with localized message.
  192.      * @param specifier format specifier (to be translated)
  193.      * @param parts parts to insert in the format (no translation)
  194.      * @return an {@link java.text.ParseException} with localized message
  195.      */
  196.     public static ParseException createParseException(final Localizable specifier,
  197.                                                       final Object ... parts) {

  198.         return new ParseException("", 0) {

  199.             /** Serializable UID. */
  200.             private static final long serialVersionUID = 4771367217940584391L;

  201.             /** {@inheritDoc} */
  202.             @Override
  203.             public String getMessage() {
  204.                 return buildMessage(Locale.US, specifier, parts);
  205.             }

  206.             /** {@inheritDoc} */
  207.             @Override
  208.             public String getLocalizedMessage() {
  209.                 return buildMessage(Locale.getDefault(), specifier, parts);
  210.             }

  211.         };

  212.     }

  213.     /** Create an {@link java.lang.RuntimeException} for an internal error.
  214.      * @param cause underlying cause
  215.      * @return an {@link java.lang.RuntimeException} for an internal error
  216.      */
  217.     public static RuntimeException createInternalError(final Throwable cause) {

  218.         /** Format specifier (to be translated). */
  219.         final Localizable specifier = OrekitMessages.INTERNAL_ERROR;

  220.         /** Parts to insert in the format (no translation). */
  221.         final String parts     = "orekit@c-s.fr";

  222.         return new RuntimeException() {

  223.             /** Serializable UID. */
  224.             private static final long serialVersionUID = -6493358459835909138L;

  225.             /** {@inheritDoc} */
  226.             @Override
  227.             public String getMessage() {
  228.                 return buildMessage(Locale.US, specifier, parts);
  229.             }

  230.             /** {@inheritDoc} */
  231.             @Override
  232.             public String getLocalizedMessage() {
  233.                 return buildMessage(Locale.getDefault(), specifier, parts);
  234.             }

  235.         };

  236.     }

  237. }