jdk/src/share/classes/sun/awt/datatransfer/TransferableProxy.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.datatransfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.datatransfer.DataFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.datatransfer.UnsupportedFlavorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ObjectStreamClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Proxies for another Transferable so that Serializable objects are never
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * returned directly by DnD or the Clipboard. Instead, a new instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * object is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Lawrence P.G. Cable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class TransferableProxy implements Transferable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public TransferableProxy(Transferable t, boolean local) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        transferable = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        isLocal = local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public DataFlavor[] getTransferDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return transferable.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public boolean isDataFlavorSupported(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return transferable.isDataFlavorSupported(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public Object getTransferData(DataFlavor df)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throws UnsupportedFlavorException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Object data = transferable.getTransferData(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // If the data is a Serializable object, then create a new instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // before returning it. This insulates applications sharing DnD and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // Clipboard data from each other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (data != null && isLocal && df.isFlavorSerializedObjectType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            ClassLoaderObjectOutputStream oos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                new ClassLoaderObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            oos.writeObject(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            ByteArrayInputStream bais =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                new ByteArrayInputStream(baos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                ClassLoaderObjectInputStream ois =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    new ClassLoaderObjectInputStream(bais,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                                     oos.getClassLoaderMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                data = ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw (IOException)new IOException().initCause(cnfe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected final Transferable transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected final boolean isLocal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
class ClassLoaderObjectOutputStream extends ObjectOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private final Map<Set<String>, ClassLoader> map =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        new HashMap<Set<String>, ClassLoader>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public ClassLoaderObjectOutputStream(OutputStream os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        super(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    protected void annotateClass(final Class<?> cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ClassLoader classLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            (ClassLoader)AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    return cl.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Set<String> s = new HashSet<String>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        s.add(cl.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        map.put(s, classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected void annotateProxyClass(final Class<?> cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ClassLoader classLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            (ClassLoader)AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    return cl.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Class[] interfaces = cl.getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Set<String> s = new HashSet<String>(interfaces.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            s.add(interfaces[i].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        map.put(s, classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public Map<Set<String>, ClassLoader> getClassLoaderMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return new HashMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
class ClassLoaderObjectInputStream extends ObjectInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private final Map<Set<String>, ClassLoader> map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public ClassLoaderObjectInputStream(InputStream is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                        Map<Set<String>, ClassLoader> map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        super(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new NullPointerException("Null map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.map = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected Class<?> resolveClass(ObjectStreamClass classDesc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String className = classDesc.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        Set<String> s = new HashSet<String>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        s.add(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        ClassLoader classLoader = map.get(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return Class.forName(className, false, classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected Class<?> resolveProxyClass(String[] interfaces)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
      throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Set<String> s = new HashSet<String>(interfaces.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            s.add(interfaces[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        ClassLoader classLoader = map.get(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // The code below is mostly copied from the superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        ClassLoader nonPublicLoader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        boolean hasNonPublicInterface = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // define proxy in class loader of non-public interface(s), if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Class[] classObjs = new Class[interfaces.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Class cl = Class.forName(interfaces[i], false, classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if ((cl.getModifiers() & Modifier.PUBLIC) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                if (hasNonPublicInterface) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    if (nonPublicLoader != cl.getClassLoader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        throw new IllegalAccessError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                            "conflicting non-public interface class loaders");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    nonPublicLoader = cl.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    hasNonPublicInterface = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            classObjs[i] = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return Proxy.getProxyClass(hasNonPublicInterface ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                       nonPublicLoader : classLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                       classObjs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new ClassNotFoundException(null, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}