1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/share/classes/sun/applet/BeansBridge.java Tue Jun 16 17:53:32 2009 +0200
1.3 @@ -0,0 +1,298 @@
1.4 +/*
1.5 + * Copyright 1996-2009 Sun Microsystems, Inc. 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. Sun designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
1.26 + * have any questions.
1.27 + */
1.28 +
1.29 +package sun.applet;
1.30 +
1.31 +import java.applet.Applet;
1.32 +import java.applet.AppletContext;
1.33 +import java.applet.AppletStub;
1.34 +import java.applet.AudioClip;
1.35 +import java.awt.Image;
1.36 +import java.beans.AppletInitializer;
1.37 +import java.beans.beancontext.BeanContext;
1.38 +import java.io.IOException;
1.39 +import java.io.InputStream;
1.40 +import java.net.MalformedURLException;
1.41 +import java.net.URL;
1.42 +import java.security.AccessController;
1.43 +import java.security.PrivilegedAction;
1.44 +import java.util.Enumeration;
1.45 +import java.util.Hashtable;
1.46 +import java.util.Iterator;
1.47 +import java.util.Vector;
1.48 +import sun.beans.AppletProxy;
1.49 +
1.50 +/**
1.51 + * Implementaiton of the bridge between
1.52 + * java.beans module and java.applet module.
1.53 + *
1.54 + * @author Jaroslav Tulach
1.55 + */
1.56 +
1.57 +public class BeansBridge extends AppletProxy {
1.58 + public boolean initialize(
1.59 + Object result, Object init, boolean serialized,
1.60 + String beanName, BeanContext beanContext, ClassLoader cls
1.61 + ) throws MalformedURLException {
1.62 + if (!(result instanceof Applet)) {
1.63 + return false;
1.64 + }
1.65 + AppletInitializer initializer = (AppletInitializer)init;
1.66 +
1.67 + AppletStub stub = null;
1.68 + Applet applet = (Applet) result;
1.69 + boolean needDummies = initializer == null;
1.70 +
1.71 + if (needDummies) {
1.72 +
1.73 + // Figure our the codebase and docbase URLs. We do this
1.74 + // by locating the URL for a known resource, and then
1.75 + // massaging the URL.
1.76 +
1.77 + // First find the "resource name" corresponding to the bean
1.78 + // itself. So a serialzied bean "a.b.c" would imply a
1.79 + // resource name of "a/b/c.ser" and a classname of "x.y"
1.80 + // would imply a resource name of "x/y.class".
1.81 +
1.82 + final String resourceName;
1.83 +
1.84 + if (serialized) {
1.85 + // Serialized bean
1.86 + resourceName = beanName.replace('.', '/').concat(".ser");
1.87 + } else {
1.88 + // Regular class
1.89 + resourceName = beanName.replace('.', '/').concat(".class");
1.90 + }
1.91 +
1.92 + URL objectUrl = null;
1.93 + URL codeBase = null;
1.94 + URL docBase = null;
1.95 +
1.96 + // Now get the URL correponding to the resource name.
1.97 +
1.98 + final ClassLoader cloader = cls;
1.99 + objectUrl = (URL) AccessController.doPrivileged(new PrivilegedAction() {
1.100 +
1.101 + public Object run() {
1.102 + if (cloader == null) {
1.103 + return ClassLoader.getSystemResource(resourceName);
1.104 + } else {
1.105 + return cloader.getResource(resourceName);
1.106 + }
1.107 + }
1.108 + });
1.109 +
1.110 + // If we found a URL, we try to locate the docbase by taking
1.111 + // of the final path name component, and the code base by taking
1.112 + // of the complete resourceName.
1.113 + // So if we had a resourceName of "a/b/c.class" and we got an
1.114 + // objectURL of "file://bert/classes/a/b/c.class" then we would
1.115 + // want to set the codebase to "file://bert/classes/" and the
1.116 + // docbase to "file://bert/classes/a/b/"
1.117 +
1.118 + if (objectUrl != null) {
1.119 + String s = objectUrl.toExternalForm();
1.120 +
1.121 + if (s.endsWith(resourceName)) {
1.122 + int ix = s.length() - resourceName.length();
1.123 + codeBase = new URL(s.substring(0, ix));
1.124 + docBase = codeBase;
1.125 +
1.126 + ix = s.lastIndexOf('/');
1.127 +
1.128 + if (ix >= 0) {
1.129 + docBase = new URL(s.substring(0, ix + 1));
1.130 + }
1.131 + }
1.132 + }
1.133 +
1.134 + // Setup a default context and stub.
1.135 + BeansAppletContext context = new BeansAppletContext(applet);
1.136 +
1.137 + stub = (AppletStub) new BeansAppletStub(applet, context, codeBase, docBase);
1.138 + applet.setStub(stub);
1.139 + } else {
1.140 + initializer.initialize(applet, beanContext);
1.141 + }
1.142 +
1.143 + // now, if there is a BeanContext, add the bean, if applicable.
1.144 +
1.145 + if (beanContext != null) {
1.146 + beanContext.add(result);
1.147 + }
1.148 +
1.149 + // If it was deserialized then it was already init-ed.
1.150 + // Otherwise we need to initialize it.
1.151 +
1.152 + if (!serialized) {
1.153 + // We need to set a reasonable initial size, as many
1.154 + // applets are unhappy if they are started without
1.155 + // having been explicitly sized.
1.156 + applet.setSize(100, 100);
1.157 + applet.init();
1.158 + }
1.159 +
1.160 + if (needDummies) {
1.161 + ((BeansAppletStub) stub).active = true;
1.162 + } else {
1.163 + initializer.activate(applet);
1.164 + }
1.165 +
1.166 + return true;
1.167 + }
1.168 +}
1.169 +
1.170 +
1.171 +/**
1.172 + * Package private support class. This provides a default AppletContext
1.173 + * for beans which are applets.
1.174 + */
1.175 +
1.176 +class BeansAppletContext implements AppletContext {
1.177 + Applet target;
1.178 + Hashtable imageCache = new Hashtable();
1.179 +
1.180 + BeansAppletContext(Applet target) {
1.181 + this.target = target;
1.182 + }
1.183 +
1.184 + public AudioClip getAudioClip(URL url) {
1.185 + // We don't currently support audio clips in the Beans.instantiate
1.186 + // applet context, unless by some luck there exists a URL content
1.187 + // class that can generate an AudioClip from the audio URL.
1.188 + try {
1.189 + return (AudioClip) url.getContent();
1.190 + } catch (Exception ex) {
1.191 + return null;
1.192 + }
1.193 + }
1.194 +
1.195 + public synchronized Image getImage(URL url) {
1.196 + Object o = imageCache.get(url);
1.197 + if (o != null) {
1.198 + return (Image)o;
1.199 + }
1.200 + try {
1.201 + o = url.getContent();
1.202 + if (o == null) {
1.203 + return null;
1.204 + }
1.205 + if (o instanceof Image) {
1.206 + imageCache.put(url, o);
1.207 + return (Image) o;
1.208 + }
1.209 + // Otherwise it must be an ImageProducer.
1.210 + Image img = target.createImage((java.awt.image.ImageProducer)o);
1.211 + imageCache.put(url, img);
1.212 + return img;
1.213 +
1.214 + } catch (Exception ex) {
1.215 + return null;
1.216 + }
1.217 + }
1.218 +
1.219 + public Applet getApplet(String name) {
1.220 + return null;
1.221 + }
1.222 +
1.223 + public Enumeration getApplets() {
1.224 + Vector applets = new Vector();
1.225 + applets.addElement(target);
1.226 + return applets.elements();
1.227 + }
1.228 +
1.229 + public void showDocument(URL url) {
1.230 + // We do nothing.
1.231 + }
1.232 +
1.233 + public void showDocument(URL url, String target) {
1.234 + // We do nothing.
1.235 + }
1.236 +
1.237 + public void showStatus(String status) {
1.238 + // We do nothing.
1.239 + }
1.240 +
1.241 + public void setStream(String key, InputStream stream)throws IOException{
1.242 + // We do nothing.
1.243 + }
1.244 +
1.245 + public InputStream getStream(String key){
1.246 + // We do nothing.
1.247 + return null;
1.248 + }
1.249 +
1.250 + public Iterator getStreamKeys(){
1.251 + // We do nothing.
1.252 + return null;
1.253 + }
1.254 +}
1.255 +
1.256 +/**
1.257 + * Package private support class. This provides an AppletStub
1.258 + * for beans which are applets.
1.259 + */
1.260 +class BeansAppletStub implements AppletStub {
1.261 + transient boolean active;
1.262 + transient Applet target;
1.263 + transient AppletContext context;
1.264 + transient URL codeBase;
1.265 + transient URL docBase;
1.266 +
1.267 + BeansAppletStub(Applet target,
1.268 + AppletContext context, URL codeBase,
1.269 + URL docBase) {
1.270 + this.target = target;
1.271 + this.context = context;
1.272 + this.codeBase = codeBase;
1.273 + this.docBase = docBase;
1.274 + }
1.275 +
1.276 + public boolean isActive() {
1.277 + return active;
1.278 + }
1.279 +
1.280 + public URL getDocumentBase() {
1.281 + // use the root directory of the applet's class-loader
1.282 + return docBase;
1.283 + }
1.284 +
1.285 + public URL getCodeBase() {
1.286 + // use the directory where we found the class or serialized object.
1.287 + return codeBase;
1.288 + }
1.289 +
1.290 + public String getParameter(String name) {
1.291 + return null;
1.292 + }
1.293 +
1.294 + public AppletContext getAppletContext() {
1.295 + return context;
1.296 + }
1.297 +
1.298 + public void appletResize(int width, int height) {
1.299 + // we do nothing.
1.300 + }
1.301 +}