< prev index next >

jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/util/exception/JAXWSExceptionBase.java

Print this page


   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


  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()


 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 }
   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


  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()


 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 }
< prev index next >