001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau;
014
015import java.text.*;
016
017/**
018 * General bean runtime operation exception.
019 */
020public final class BeanRuntimeException extends BasicRuntimeException {
021
022   private static final long serialVersionUID = 1L;
023
024   /**
025    * Constructor.
026    *
027    * @param cause The cause of this exception.
028    * @param c The class name of the bean that caused the exception.
029    * @param message The {@link MessageFormat}-style message.
030    * @param args Optional {@link MessageFormat}-style arguments.
031    */
032   public BeanRuntimeException(Throwable cause, Class<?> c, String message, Object... args) {
033      super(cause, getMessage(cause, c, message), args);
034   }
035
036   /**
037    * Constructor.
038    *
039    * @param message The error message.
040    */
041   public BeanRuntimeException(String message) {
042      this((Throwable)null, null, message);
043   }
044
045   /**
046    * Constructor.
047    *
048    * @param message The error message.
049    * @param args Arguments passed in to the {@code String.format()} method.
050    */
051   public BeanRuntimeException(String message, Object...args) {
052      this(null, null, message, args);
053   }
054
055   /**
056    * Constructor.
057    *
058    * @param c The class name of the bean that caused the exception.
059    * @param message The error message.
060    * @param args Arguments passed in to the {@code String.format()} method.
061    */
062   public BeanRuntimeException(Class<?> c, String message, Object... args) {
063      this(null, c,  message, args);
064   }
065
066   /**
067    * Constructor.
068    *
069    * @param cause The initial cause of the exception.
070    */
071   public BeanRuntimeException(Throwable cause) {
072      this(cause, null, null);
073   }
074
075   private static String getMessage(Throwable cause, Class<?> c, String msg) {
076      if (msg != null)
077         return (c == null ? "" : c.getName() + ": ") + msg;
078      if (cause != null)
079         return (c == null ? "" : c.getName() + ": ") + cause.getMessage();
080      return null;
081   }
082}