ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.util.exception; ohair@286: alanb@368: import com.sun.istack.internal.localization.Localizable; alanb@368: import com.sun.istack.internal.localization.LocalizableMessage; alanb@368: import com.sun.istack.internal.localization.LocalizableMessageFactory; alanb@368: import com.sun.istack.internal.localization.Localizer; alanb@368: import com.sun.istack.internal.localization.NullLocalizable; ohair@286: import java.io.IOException; ohair@286: import java.io.ObjectInputStream; ohair@286: import java.io.ObjectOutputStream; ohair@286: import java.io.Serializable; alanb@368: import javax.xml.ws.WebServiceException; ohair@286: ohair@286: /** ohair@286: * Represents a {@link WebServiceException} with ohair@286: * localizable message. ohair@286: * ohair@286: * @author WS Development Team ohair@286: */ ohair@286: public abstract class JAXWSExceptionBase ohair@286: extends WebServiceException implements Localizable { ohair@286: ohair@286: //Don't worry about previous serialVersionUID = 4818235090198755494L;, this class was not serializable before. ohair@286: private static final long serialVersionUID = 1L; ohair@286: ohair@286: private transient Localizable msg; ohair@286: ohair@286: /** ohair@286: * @deprecated ohair@286: * Should use the localizable constructor instead. ohair@286: */ ohair@286: protected JAXWSExceptionBase(String key, Object... args) { ohair@286: super(findNestedException(args)); alanb@368: this.msg = new LocalizableMessage(getDefaultResourceBundleName(), key, args); ohair@286: } ohair@286: ohair@286: ohair@286: protected JAXWSExceptionBase(String message) { ohair@286: this(new NullLocalizable(message)); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a new exception that wraps the specified exception. ohair@286: */ ohair@286: protected JAXWSExceptionBase(Throwable throwable) { ohair@286: this(new NullLocalizable(throwable.toString()),throwable); ohair@286: } ohair@286: ohair@286: protected JAXWSExceptionBase(Localizable msg) { ohair@286: this.msg = msg; ohair@286: } ohair@286: ohair@286: protected JAXWSExceptionBase(Localizable msg, Throwable cause) { ohair@286: super(cause); ohair@286: this.msg = msg; ohair@286: } ohair@286: ohair@286: /** ohair@286: * @serialData Default fields, followed by information in Localizable which comprises of. ohair@286: * ResourceBundle name, then key and followed by arguments array. ohair@286: * If there is no arguments array, then -1 is written. If there is a argument array (possible of zero ohair@286: * length) then the array length is written as an integer, followed by each argument (Object). ohair@286: * If the Object is serializable, the argument is written. Otherwise the output of Object.toString() ohair@286: * is written. ohair@286: */ ohair@286: private void writeObject(ObjectOutputStream out) throws IOException { ohair@286: // We have to call defaultWriteObject first. ohair@286: out.defaultWriteObject(); ohair@286: ohair@286: out.writeObject(msg.getResourceBundleName()); ohair@286: out.writeObject(msg.getKey()); ohair@286: Object[] args = msg.getArguments(); ohair@286: if (args == null) { ohair@286: out.writeInt(-1); ohair@286: return; ohair@286: } ohair@286: out.writeInt(args.length); ohair@286: // Write Object values for the parameters, if it is serializable otherwise write String form of it.. ohair@286: for (int i = 0; i < args.length; i++) { ohair@286: if (args[i] == null || args[i] instanceof Serializable) { ohair@286: out.writeObject(args[i]); ohair@286: } else { ohair@286: out.writeObject(args[i].toString()); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { ohair@286: // We have to call defaultReadObject first. ohair@286: in.defaultReadObject(); ohair@286: Object[] args; ohair@286: String resourceBundleName = (String) in.readObject(); ohair@286: String key = (String) in.readObject(); ohair@286: int len = in.readInt(); ohair@286: if (len == -1) { ohair@286: args = null; ohair@286: } else { ohair@286: args = new Object[len]; ohair@286: for (int i = 0; i < args.length; i++) { ohair@286: args[i] = in.readObject(); ohair@286: } ohair@286: } ohair@286: msg = new LocalizableMessageFactory(resourceBundleName).getMessage(key,args); ohair@286: } ohair@286: ohair@286: private static Throwable findNestedException(Object[] args) { ohair@286: if (args == null) ohair@286: return null; ohair@286: ohair@286: for( Object o : args ) ohair@286: if(o instanceof Throwable) ohair@286: return (Throwable)o; ohair@286: return null; ohair@286: } ohair@286: ohair@286: public String getMessage() { ohair@286: Localizer localizer = new Localizer(); ohair@286: return localizer.localize(this); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gets the default resource bundle name for this kind of exception. ohair@286: * Used for {@link #JAXWSExceptionBase(String, Object[])}. ohair@286: */ ohair@286: protected abstract String getDefaultResourceBundleName(); ohair@286: ohair@286: // ohair@286: // Localizable delegation ohair@286: // ohair@286: public final String getKey() { ohair@286: return msg.getKey(); ohair@286: } ohair@286: ohair@286: public final Object[] getArguments() { ohair@286: return msg.getArguments(); ohair@286: } ohair@286: ohair@286: public final String getResourceBundleName() { ohair@286: return msg.getResourceBundleName(); ohair@286: } ohair@286: }