View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, 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 javax.xml.ws.WebServiceException;
38  
39  /**
40   * Represents a {@link WebServiceException} with
41   * localizable message.
42   *
43   * @author WS Development Team
44   */
45  public abstract class JAXWSExceptionBase
46      extends WebServiceException implements Localizable {
47  
48      //Don't worry about previous  serialVersionUID = 4818235090198755494L;, this class was not serializable before.
49      private static final long serialVersionUID = 1L;
50  
51      private transient Localizable msg;
52  
53      /**
54       * @deprecated
55       *      Should use the localizable constructor instead.
56       */
57      protected JAXWSExceptionBase(String key, Object... args) {
58          super(findNestedException(args));
59          this.msg = new LocalizableMessage(getDefaultResourceBundleName(), key, args);
60      }
61  
62  
63      protected JAXWSExceptionBase(String message) {
64          this(new NullLocalizable(message));
65      }
66  
67      /**
68       * Creates a new exception that wraps the specified exception.
69       */
70      protected JAXWSExceptionBase(Throwable throwable) {
71          this(new NullLocalizable(throwable.toString()),throwable);
72      }
73  
74      protected JAXWSExceptionBase(Localizable msg) {
75          this.msg = msg;
76      }
77  
78      protected JAXWSExceptionBase(Localizable msg, Throwable cause) {
79          super(cause);
80          this.msg = msg;
81      }
82  
83      /**
84       * @serialData Default fields,  followed by information in Localizable which comprises of.
85       *  ResourceBundle name, then key and followed by arguments array.
86       *  If there is no arguments array, then -1 is written.  If there is a argument array (possible of zero
87       * length) then the array length is written as an integer, followed by each argument (Object).
88       * If the Object is serializable, the argument is written. Otherwise the output of Object.toString()
89       * is written.
90       */
91      private void writeObject(ObjectOutputStream out) throws IOException {
92          // We have to call defaultWriteObject first.
93          out.defaultWriteObject();
94  
95          out.writeObject(msg.getResourceBundleName());
96          out.writeObject(msg.getKey());
97          Object[] args = msg.getArguments();
98          if (args == null) {
99              out.writeInt(-1);
100             return;
101         }
102         out.writeInt(args.length);
103         // Write Object values for the parameters, if it is serializable otherwise write String form of it..
104         for (int i = 0; i < args.length; i++) {
105             if (args[i] == null || args[i] instanceof Serializable) {
106                 out.writeObject(args[i]);
107             } else {
108                 out.writeObject(args[i].toString());
109             }
110         }
111     }
112 
113     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
114         // We have to call defaultReadObject first.
115         in.defaultReadObject();
116         Object[] args;
117         String resourceBundleName = (String) in.readObject();
118         String key = (String) in.readObject();
119         int len = in.readInt();
120         if (len == -1) {
121             args = null;
122         } else {
123             args = new Object[len];
124             for (int i = 0; i < args.length; i++) {
125                 args[i] = in.readObject();
126             }
127         }
128         msg = new LocalizableMessageFactory(resourceBundleName).getMessage(key,args);
129     }
130 
131     private static Throwable findNestedException(Object[] args) {
132         if (args == null)
133             return null;
134 
135         for( Object o : args )
136             if(o instanceof Throwable)
137                 return (Throwable)o;
138         return null;
139     }
140 
141     public String getMessage() {
142         Localizer localizer = new Localizer();
143         return localizer.localize(this);
144     }
145 
146     /**
147      * Gets the default resource bundle name for this kind of exception.
148      * Used for {@link #JAXWSExceptionBase(String, Object[])}.
149      */
150     protected abstract String getDefaultResourceBundleName();
151 
152 //
153 // Localizable delegation
154 //
155     public final String getKey() {
156         return msg.getKey();
157     }
158 
159     public final Object[] getArguments() {
160         return msg.getArguments();
161     }
162 
163     public final String getResourceBundleName() {
164         return msg.getResourceBundleName();
165     }
166 }