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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/util/exception/JAXWSExceptionBase.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,168 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.util.exception;
    1.30 +
    1.31 +import com.sun.xml.internal.ws.util.localization.*;
    1.32 +
    1.33 +import javax.xml.ws.WebServiceException;
    1.34 +import java.io.IOException;
    1.35 +import java.io.ObjectInputStream;
    1.36 +import java.io.ObjectOutputStream;
    1.37 +import java.io.Serializable;
    1.38 +
    1.39 +/**
    1.40 + * Represents a {@link WebServiceException} with
    1.41 + * localizable message.
    1.42 + *
    1.43 + * @author WS Development Team
    1.44 + */
    1.45 +public abstract class JAXWSExceptionBase
    1.46 +    extends WebServiceException implements Localizable {
    1.47 +
    1.48 +    //Don't worry about previous  serialVersionUID = 4818235090198755494L;, this class was not serializable before.
    1.49 +    private static final long serialVersionUID = 1L;
    1.50 +
    1.51 +    private transient Localizable msg;
    1.52 +
    1.53 +    /**
    1.54 +     * @deprecated
    1.55 +     *      Should use the localizable constructor instead.
    1.56 +     */
    1.57 +    protected JAXWSExceptionBase(String key, Object... args) {
    1.58 +        super(findNestedException(args));
    1.59 +        this.msg = new LocalizableImpl(key,fixNull(args),getDefaultResourceBundleName());
    1.60 +    }
    1.61 +
    1.62 +
    1.63 +    protected JAXWSExceptionBase(String message) {
    1.64 +        this(new NullLocalizable(message));
    1.65 +    }
    1.66 +
    1.67 +    private static Object[] fixNull(Object[] x) {
    1.68 +        if(x==null)     return new Object[0];
    1.69 +        else            return x;
    1.70 +    }
    1.71 +
    1.72 +    /**
    1.73 +     * Creates a new exception that wraps the specified exception.
    1.74 +     */
    1.75 +    protected JAXWSExceptionBase(Throwable throwable) {
    1.76 +        this(new NullLocalizable(throwable.toString()),throwable);
    1.77 +    }
    1.78 +
    1.79 +    protected JAXWSExceptionBase(Localizable msg) {
    1.80 +        this.msg = msg;
    1.81 +    }
    1.82 +
    1.83 +    protected JAXWSExceptionBase(Localizable msg, Throwable cause) {
    1.84 +        super(cause);
    1.85 +        this.msg = msg;
    1.86 +    }
    1.87 +
    1.88 +    /**
    1.89 +     * @serialData Default fields,  followed by information in Localizable which comprises of.
    1.90 +     *  ResourceBundle name, then key and followed by arguments array.
    1.91 +     *  If there is no arguments array, then -1 is written.  If there is a argument array (possible of zero
    1.92 +     * length) then the array length is written as an integer, followed by each argument (Object).
    1.93 +     * If the Object is serializable, the argument is written. Otherwise the output of Object.toString()
    1.94 +     * is written.
    1.95 +     */
    1.96 +    private void writeObject(ObjectOutputStream out) throws IOException {
    1.97 +        // We have to call defaultWriteObject first.
    1.98 +        out.defaultWriteObject();
    1.99 +
   1.100 +        out.writeObject(msg.getResourceBundleName());
   1.101 +        out.writeObject(msg.getKey());
   1.102 +        Object[] args = msg.getArguments();
   1.103 +        if (args == null) {
   1.104 +            out.writeInt(-1);
   1.105 +            return;
   1.106 +        }
   1.107 +        out.writeInt(args.length);
   1.108 +        // Write Object values for the parameters, if it is serializable otherwise write String form of it..
   1.109 +        for (int i = 0; i < args.length; i++) {
   1.110 +            if (args[i] == null || args[i] instanceof Serializable) {
   1.111 +                out.writeObject(args[i]);
   1.112 +            } else {
   1.113 +                out.writeObject(args[i].toString());
   1.114 +            }
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
   1.119 +        // We have to call defaultReadObject first.
   1.120 +        in.defaultReadObject();
   1.121 +        Object[] args;
   1.122 +        String resourceBundleName = (String) in.readObject();
   1.123 +        String key = (String) in.readObject();
   1.124 +        int len = in.readInt();
   1.125 +        if (len == -1) {
   1.126 +            args = null;
   1.127 +        } else {
   1.128 +            args = new Object[len];
   1.129 +            for (int i = 0; i < args.length; i++) {
   1.130 +                args[i] = in.readObject();
   1.131 +            }
   1.132 +        }
   1.133 +        msg = new LocalizableMessageFactory(resourceBundleName).getMessage(key,args);
   1.134 +    }
   1.135 +
   1.136 +    private static Throwable findNestedException(Object[] args) {
   1.137 +        if (args == null)
   1.138 +            return null;
   1.139 +
   1.140 +        for( Object o : args )
   1.141 +            if(o instanceof Throwable)
   1.142 +                return (Throwable)o;
   1.143 +        return null;
   1.144 +    }
   1.145 +
   1.146 +    public String getMessage() {
   1.147 +        Localizer localizer = new Localizer();
   1.148 +        return localizer.localize(this);
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * Gets the default resource bundle name for this kind of exception.
   1.153 +     * Used for {@link #JAXWSExceptionBase(String, Object[])}.
   1.154 +     */
   1.155 +    protected abstract String getDefaultResourceBundleName();
   1.156 +
   1.157 +//
   1.158 +// Localizable delegation
   1.159 +//
   1.160 +    public final String getKey() {
   1.161 +        return msg.getKey();
   1.162 +    }
   1.163 +
   1.164 +    public final Object[] getArguments() {
   1.165 +        return msg.getArguments();
   1.166 +    }
   1.167 +
   1.168 +    public final String getResourceBundleName() {
   1.169 +        return msg.getResourceBundleName();
   1.170 +    }
   1.171 +}

mercurial