0N/A/*
2362N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.datatransfer;
0N/A
0N/Aimport java.awt.datatransfer.DataFlavor;
0N/Aimport java.awt.datatransfer.Transferable;
0N/Aimport java.awt.datatransfer.UnsupportedFlavorException;
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectStreamClass;
0N/Aimport java.io.OutputStream;
0N/Aimport java.lang.reflect.Modifier;
0N/Aimport java.lang.reflect.Proxy;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.Map;
0N/Aimport java.util.Set;
0N/A
0N/A
0N/A/**
0N/A * Proxies for another Transferable so that Serializable objects are never
0N/A * returned directly by DnD or the Clipboard. Instead, a new instance of the
0N/A * object is returned.
0N/A *
0N/A * @author Lawrence P.G. Cable
0N/A * @author David Mendenhall
0N/A *
0N/A * @since 1.4
0N/A */
0N/Apublic class TransferableProxy implements Transferable {
0N/A public TransferableProxy(Transferable t, boolean local) {
0N/A transferable = t;
0N/A isLocal = local;
0N/A }
0N/A public DataFlavor[] getTransferDataFlavors() {
0N/A return transferable.getTransferDataFlavors();
0N/A }
0N/A public boolean isDataFlavorSupported(DataFlavor flavor) {
0N/A return transferable.isDataFlavorSupported(flavor);
0N/A }
0N/A public Object getTransferData(DataFlavor df)
0N/A throws UnsupportedFlavorException, IOException
0N/A {
0N/A Object data = transferable.getTransferData(df);
0N/A
0N/A // If the data is a Serializable object, then create a new instance
0N/A // before returning it. This insulates applications sharing DnD and
0N/A // Clipboard data from each other.
0N/A if (data != null && isLocal && df.isFlavorSerializedObjectType()) {
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A
0N/A ClassLoaderObjectOutputStream oos =
0N/A new ClassLoaderObjectOutputStream(baos);
0N/A oos.writeObject(data);
0N/A
0N/A ByteArrayInputStream bais =
0N/A new ByteArrayInputStream(baos.toByteArray());
0N/A
0N/A try {
0N/A ClassLoaderObjectInputStream ois =
0N/A new ClassLoaderObjectInputStream(bais,
0N/A oos.getClassLoaderMap());
0N/A data = ois.readObject();
0N/A } catch (ClassNotFoundException cnfe) {
0N/A throw (IOException)new IOException().initCause(cnfe);
0N/A }
0N/A }
0N/A
0N/A return data;
0N/A }
0N/A
0N/A protected final Transferable transferable;
0N/A protected final boolean isLocal;
0N/A}
0N/A
6018N/Afinal class ClassLoaderObjectOutputStream extends ObjectOutputStream {
0N/A private final Map<Set<String>, ClassLoader> map =
0N/A new HashMap<Set<String>, ClassLoader>();
0N/A
6018N/A ClassLoaderObjectOutputStream(OutputStream os) throws IOException {
0N/A super(os);
0N/A }
0N/A
0N/A protected void annotateClass(final Class<?> cl) throws IOException {
0N/A ClassLoader classLoader =
0N/A (ClassLoader)AccessController.doPrivileged(new PrivilegedAction() {
0N/A public Object run() {
0N/A return cl.getClassLoader();
0N/A }
0N/A });
0N/A
0N/A Set<String> s = new HashSet<String>(1);
0N/A s.add(cl.getName());
0N/A
0N/A map.put(s, classLoader);
0N/A }
0N/A protected void annotateProxyClass(final Class<?> cl) throws IOException {
0N/A ClassLoader classLoader =
0N/A (ClassLoader)AccessController.doPrivileged(new PrivilegedAction() {
0N/A public Object run() {
0N/A return cl.getClassLoader();
0N/A }
0N/A });
0N/A
0N/A Class[] interfaces = cl.getInterfaces();
0N/A Set<String> s = new HashSet<String>(interfaces.length);
0N/A for (int i = 0; i < interfaces.length; i++) {
0N/A s.add(interfaces[i].getName());
0N/A }
0N/A
0N/A map.put(s, classLoader);
0N/A }
0N/A
6018N/A Map<Set<String>, ClassLoader> getClassLoaderMap() {
0N/A return new HashMap(map);
0N/A }
0N/A}
0N/A
6018N/Afinal class ClassLoaderObjectInputStream extends ObjectInputStream {
0N/A private final Map<Set<String>, ClassLoader> map;
0N/A
6018N/A ClassLoaderObjectInputStream(InputStream is,
0N/A Map<Set<String>, ClassLoader> map)
0N/A throws IOException {
0N/A super(is);
0N/A if (map == null) {
0N/A throw new NullPointerException("Null map");
0N/A }
0N/A this.map = map;
0N/A }
0N/A
0N/A protected Class<?> resolveClass(ObjectStreamClass classDesc)
0N/A throws IOException, ClassNotFoundException {
0N/A String className = classDesc.getName();
0N/A
0N/A Set<String> s = new HashSet<String>(1);
0N/A s.add(className);
0N/A
0N/A ClassLoader classLoader = map.get(s);
6018N/A if (classLoader != null) {
6018N/A return Class.forName(className, false, classLoader);
6018N/A } else {
6018N/A return super.resolveClass(classDesc);
6018N/A }
0N/A }
0N/A
0N/A protected Class<?> resolveProxyClass(String[] interfaces)
0N/A throws IOException, ClassNotFoundException {
0N/A
0N/A Set<String> s = new HashSet<String>(interfaces.length);
0N/A for (int i = 0; i < interfaces.length; i++) {
0N/A s.add(interfaces[i]);
0N/A }
0N/A
0N/A ClassLoader classLoader = map.get(s);
6018N/A if (classLoader == null) {
6018N/A return super.resolveProxyClass(interfaces);
6018N/A }
0N/A
0N/A // The code below is mostly copied from the superclass.
0N/A ClassLoader nonPublicLoader = null;
0N/A boolean hasNonPublicInterface = false;
0N/A
0N/A // define proxy in class loader of non-public interface(s), if any
0N/A Class[] classObjs = new Class[interfaces.length];
0N/A for (int i = 0; i < interfaces.length; i++) {
0N/A Class cl = Class.forName(interfaces[i], false, classLoader);
0N/A if ((cl.getModifiers() & Modifier.PUBLIC) == 0) {
0N/A if (hasNonPublicInterface) {
0N/A if (nonPublicLoader != cl.getClassLoader()) {
0N/A throw new IllegalAccessError(
0N/A "conflicting non-public interface class loaders");
0N/A }
0N/A } else {
0N/A nonPublicLoader = cl.getClassLoader();
0N/A hasNonPublicInterface = true;
0N/A }
0N/A }
0N/A classObjs[i] = cl;
0N/A }
0N/A try {
0N/A return Proxy.getProxyClass(hasNonPublicInterface ?
0N/A nonPublicLoader : classLoader,
0N/A classObjs);
0N/A } catch (IllegalArgumentException e) {
0N/A throw new ClassNotFoundException(null, e);
0N/A }
0N/A }
0N/A}