1/*
2 * Copyright (c) 2009, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6329581
27 * @summary Tests encoding of a class with custom ClassLoader
28 * @author Sergey Malenkov
29 */
30
31import java.beans.ExceptionListener;
32import java.beans.XMLDecoder;
33import java.beans.XMLEncoder;
34import java.io.ByteArrayInputStream;
35import java.io.ByteArrayOutputStream;
36import java.net.URL;
37import java.net.URLClassLoader;
38
39public class Test6329581 extends URLClassLoader implements ExceptionListener {
40    public static final class Bean {
41    }
42
43    public static void main(String[] args) throws Exception {
44        new Test6329581().decode(new Test6329581().encode(Bean.class.getName()));
45    }
46
47    private Test6329581() {
48        super(new URL[] {
49                Test6329581.class.getProtectionDomain().getCodeSource().getLocation()
50        });
51    }
52
53    @Override
54    protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
55        Class c = findLoadedClass(name);
56        if (c == null) {
57            if (Bean.class.getName().equals(name)) {
58                c = findClass(name);
59            }
60            else try {
61                c = getParent().loadClass(name);
62            }
63            catch (ClassNotFoundException exception) {
64                c = findClass(name);
65            }
66        }
67        if (resolve) {
68            resolveClass(c);
69        }
70        return c;
71    }
72
73    public void exceptionThrown(Exception exception) {
74        throw new Error("unexpected exception", exception);
75    }
76
77    private void validate(Object object) {
78        if (!object.getClass().getClassLoader().equals(this)) {
79            throw new Error("Bean is loaded with unexpected class loader");
80        }
81    }
82
83    private byte[] encode(String name) throws Exception {
84        Object object = loadClass(name).newInstance();
85        validate(object);
86        ByteArrayOutputStream out = new ByteArrayOutputStream();
87        XMLEncoder encoder = new XMLEncoder(out);
88        encoder.setExceptionListener(this);
89        encoder.writeObject(object);
90        encoder.close();
91        return out.toByteArray();
92    }
93
94    private Object decode(byte[] array) {
95        ByteArrayInputStream in = new ByteArrayInputStream(array);
96        XMLDecoder decoder = new XMLDecoder(in, null, this, this);
97        Object object = decoder.readObject();
98        validate(object);
99        decoder.close();
100        return object;
101    }
102}
103