1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.util.exception;
  27 
  28 import com.sun.istack.internal.localization.Localizable;
  29 import com.sun.istack.internal.localization.LocalizableMessage;
  30 import com.sun.istack.internal.localization.LocalizableMessageFactory;
  31 import com.sun.istack.internal.localization.Localizer;
  32 import com.sun.istack.internal.localization.NullLocalizable;
  33 import java.io.IOException;
  34 import java.io.ObjectInputStream;
  35 import java.io.ObjectOutputStream;
  36 import java.io.Serializable;
  37 import java.util.Locale;
  38 import java.util.ResourceBundle;
  39 import javax.xml.ws.WebServiceException;
  40 
  41 /**
  42  * Represents a {@link WebServiceException} with
  43  * localizable message.
  44  *
  45  * @author WS Development Team
  46  */
  47 public abstract class JAXWSExceptionBase
  48     extends WebServiceException implements Localizable {
  49 
  50     //Don't worry about previous  serialVersionUID = 4818235090198755494L;, this class was not serializable before.
  51     private static final long serialVersionUID = 1L;
  52 
  53     private transient Localizable msg;
  54 
  55     /**
  56      * @deprecated
  57      *      Should use the localizable constructor instead.

  58      */

  59     protected JAXWSExceptionBase(String key, Object... args) {
  60         super(findNestedException(args));
  61         this.msg = new LocalizableMessage(getDefaultResourceBundleName(), key, args);
  62     }
  63 
  64 
  65     protected JAXWSExceptionBase(String message) {
  66         this(new NullLocalizable(message));
  67     }
  68 
  69     /**
  70      * Creates a new exception that wraps the specified exception.

  71      */
  72     protected JAXWSExceptionBase(Throwable throwable) {
  73         this(new NullLocalizable(throwable.toString()),throwable);
  74     }
  75 
  76     protected JAXWSExceptionBase(Localizable msg) {
  77         this.msg = msg;
  78     }
  79 
  80     protected JAXWSExceptionBase(Localizable msg, Throwable cause) {
  81         super(cause);
  82         this.msg = msg;
  83     }
  84 
  85     /**
  86      * @serialData Default fields,  followed by information in Localizable which comprises of.
  87      *  ResourceBundle name, then key and followed by arguments array.
  88      *  If there is no arguments array, then -1 is written.  If there is a argument array (possible of zero
  89      * length) then the array length is written as an integer, followed by each argument (Object).
  90      * If the Object is serializable, the argument is written. Otherwise the output of Object.toString()
  91      * is written.
  92      */
  93     private void writeObject(ObjectOutputStream out) throws IOException {
  94         // We have to call defaultWriteObject first.
  95         out.defaultWriteObject();
  96 
  97         out.writeObject(msg.getResourceBundleName());
  98         out.writeObject(msg.getKey());
  99         Object[] args = msg.getArguments();
 100         if (args == null) {
 101             out.writeInt(-1);
 102             return;
 103         }
 104         out.writeInt(args.length);
 105         // Write Object values for the parameters, if it is serializable otherwise write String form of it..
 106         for (int i = 0; i < args.length; i++) {
 107             if (args[i] == null || args[i] instanceof Serializable) {
 108                 out.writeObject(args[i]);
 109             } else {
 110                 out.writeObject(args[i].toString());
 111             }
 112         }
 113     }
 114 
 115     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
 116         // We have to call defaultReadObject first.
 117         in.defaultReadObject();
 118         Object[] args;
 119         String resourceBundleName = (String) in.readObject();
 120         String key = (String) in.readObject();
 121         int len = in.readInt();
 122         if (len == -1) {
 123             args = null;
 124         } else {
 125             args = new Object[len];
 126             for (int i = 0; i < args.length; i++) {
 127                 args[i] = in.readObject();
 128             }
 129         }
 130         msg = new LocalizableMessageFactory(resourceBundleName, this::getResourceBundle)
 131                         .getMessage(key,args);
 132     }
 133 
 134     private static Throwable findNestedException(Object[] args) {
 135         if (args == null)
 136             return null;
 137 
 138         for( Object o : args )
 139             if(o instanceof Throwable)
 140                 return (Throwable)o;
 141         return null;
 142     }
 143 

 144     public String getMessage() {
 145         Localizer localizer = new Localizer();
 146         return localizer.localize(this);
 147     }
 148 
 149     /**
 150      * Gets the default resource bundle name for this kind of exception.
 151      * Used for {@link #JAXWSExceptionBase(String, Object[])}.

 152      */
 153     protected abstract String getDefaultResourceBundleName();
 154 
 155     /*
 156      * Returns the ResourceBundle in this module.
 157      *
 158      * Subclasses in a different module has to override this method.
 159      */
 160     @Override
 161     public ResourceBundle getResourceBundle(Locale locale) {
 162         return ResourceBundle.getBundle(getDefaultResourceBundleName(), locale);
 163     }
 164 
 165 //
 166 // Localizable delegation
 167 //

 168     public final String getKey() {
 169         return msg.getKey();
 170     }
 171 

 172     public final Object[] getArguments() {
 173         return msg.getArguments();
 174     }
 175 

 176     public final String getResourceBundleName() {
 177         return msg.getResourceBundleName();
 178     }






 179 }
--- EOF ---