1 /*
   2  * Copyright (c) 1997, 2017, 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, LocalizableMessageFactory.ResourceBundleSupplier {
  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      * @param key
  57      * @param args
  58      * @deprecated Should use the localizable constructor instead.
  59      */
  60     @Deprecated
  61     protected JAXWSExceptionBase(String key, Object... args) {
  62         super(findNestedException(args));
  63         this.msg = new LocalizableMessage(getDefaultResourceBundleName(), this, key, args);
  64     }
  65 
  66 
  67     protected JAXWSExceptionBase(String message) {
  68         this(new NullLocalizable(message));
  69     }
  70 
  71     /**
  72      * Creates a new exception that wraps the specified exception.
  73      * @param throwable
  74      */
  75     protected JAXWSExceptionBase(Throwable throwable) {
  76         this(new NullLocalizable(throwable.toString()),throwable);
  77     }
  78 
  79     protected JAXWSExceptionBase(Localizable msg) {
  80         this.msg = msg;
  81     }
  82 
  83     protected JAXWSExceptionBase(Localizable msg, Throwable cause) {
  84         super(cause);
  85         this.msg = msg;
  86     }
  87 
  88     /**
  89      * @serialData Default fields,  followed by information in Localizable which comprises of.
  90      *  ResourceBundle name, then key and followed by arguments array.
  91      *  If there is no arguments array, then -1 is written.  If there is a argument array (possible of zero
  92      * length) then the array length is written as an integer, followed by each argument (Object).
  93      * If the Object is serializable, the argument is written. Otherwise the output of Object.toString()
  94      * is written.
  95      */
  96     private void writeObject(ObjectOutputStream out) throws IOException {
  97         // We have to call defaultWriteObject first.
  98         out.defaultWriteObject();
  99 
 100         out.writeObject(msg.getResourceBundleName());
 101         out.writeObject(msg.getKey());
 102         Object[] args = msg.getArguments();
 103         if (args == null) {
 104             out.writeInt(-1);
 105             return;
 106         }
 107         out.writeInt(args.length);
 108         // Write Object values for the parameters, if it is serializable otherwise write String form of it..
 109         for (int i = 0; i < args.length; i++) {
 110             if (args[i] == null || args[i] instanceof Serializable) {
 111                 out.writeObject(args[i]);
 112             } else {
 113                 out.writeObject(args[i].toString());
 114             }
 115         }
 116     }
 117 
 118     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
 119         // We have to call defaultReadObject first.
 120         in.defaultReadObject();
 121         Object[] args;
 122         String resourceBundleName = (String) in.readObject();
 123         String key = (String) in.readObject();
 124         int len = in.readInt();
 125         if (len == -1) {
 126             args = null;
 127         } else {
 128             args = new Object[len];
 129             for (int i = 0; i < args.length; i++) {
 130                 args[i] = in.readObject();
 131             }
 132         }
 133         msg = new LocalizableMessageFactory(resourceBundleName).getMessage(key,args);

 134     }
 135 
 136     private static Throwable findNestedException(Object[] args) {
 137         if (args == null)
 138             return null;
 139 
 140         for( Object o : args )
 141             if(o instanceof Throwable)
 142                 return (Throwable)o;
 143         return null;
 144     }
 145 
 146     @Override
 147     public String getMessage() {
 148         Localizer localizer = new Localizer();
 149         return localizer.localize(this);
 150     }
 151 
 152     /**
 153      * Gets the default resource bundle name for this kind of exception.
 154      * Used for {@link #JAXWSExceptionBase(String, Object[])}.
 155      * @return
 156      */
 157     protected abstract String getDefaultResourceBundleName();
 158 










 159 //
 160 // Localizable delegation
 161 //
 162     @Override
 163     public final String getKey() {
 164         return msg.getKey();
 165     }
 166 
 167     @Override
 168     public final Object[] getArguments() {
 169         return msg.getArguments();
 170     }
 171 
 172     @Override
 173     public final String getResourceBundleName() {
 174         return msg.getResourceBundleName();
 175     }
 176 
 177     @Override
 178     public ResourceBundle getResourceBundle(Locale locale) {
 179         return ResourceBundle.getBundle(getDefaultResourceBundleName(), locale);
 180     }
 181 
 182 }
--- EOF ---