duke@0: /* malenkov@1077: * Copyright 1996-2009 Sun Microsystems, Inc. All Rights Reserved. duke@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@0: * duke@0: * This code is free software; you can redistribute it and/or modify it duke@0: * under the terms of the GNU General Public License version 2 only, as duke@0: * published by the Free Software Foundation. Sun designates this duke@0: * particular file as subject to the "Classpath" exception as provided duke@0: * by Sun in the LICENSE file that accompanied this code. duke@0: * duke@0: * This code is distributed in the hope that it will be useful, but WITHOUT duke@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@0: * version 2 for more details (a copy is included in the LICENSE file that duke@0: * accompanied this code). duke@0: * duke@0: * You should have received a copy of the GNU General Public License version duke@0: * 2 along with this work; if not, write to the Free Software Foundation, duke@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@0: * duke@0: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@0: * CA 95054 USA or visit www.sun.com if you need additional information or duke@0: * have any questions. duke@0: */ duke@0: jtulach@1238: package sun.applet; duke@0: malenkov@1077: import java.applet.Applet; malenkov@1077: import java.applet.AppletContext; malenkov@1077: import java.applet.AppletStub; malenkov@1077: import java.applet.AudioClip; malenkov@1077: import java.awt.Image; jtulach@1238: import java.beans.AppletInitializer; duke@0: import java.beans.beancontext.BeanContext; malenkov@1077: import java.io.IOException; malenkov@1077: import java.io.InputStream; jtulach@1238: import java.net.MalformedURLException; duke@0: import java.net.URL; malenkov@1077: import java.security.AccessController; malenkov@1077: import java.security.PrivilegedAction; malenkov@1077: import java.util.Enumeration; malenkov@1077: import java.util.Hashtable; malenkov@1077: import java.util.Iterator; malenkov@1077: import java.util.Vector; jtulach@1238: import sun.beans.AppletProxy; duke@0: duke@0: /** jtulach@1238: * Implementaiton of the bridge between jtulach@1238: * java.beans module and java.applet module. jtulach@1238: * jtulach@1238: * @author Jaroslav Tulach duke@0: */ duke@0: jtulach@1238: public class BeansBridge extends AppletProxy { jtulach@1238: public boolean initialize( jtulach@1238: Object result, Object init, boolean serialized, jtulach@1238: String beanName, BeanContext beanContext, ClassLoader cls jtulach@1238: ) throws MalformedURLException { jtulach@1238: if (!(result instanceof Applet)) { jtulach@1238: return false; jtulach@1238: } jtulach@1238: AppletInitializer initializer = (AppletInitializer)init; duke@0: jtulach@1238: AppletStub stub = null; jtulach@1238: Applet applet = (Applet) result; jtulach@1238: boolean needDummies = initializer == null; duke@0: jtulach@1238: if (needDummies) { duke@0: jtulach@1238: // Figure our the codebase and docbase URLs. We do this jtulach@1238: // by locating the URL for a known resource, and then jtulach@1238: // massaging the URL. duke@0: jtulach@1238: // First find the "resource name" corresponding to the bean jtulach@1238: // itself. So a serialzied bean "a.b.c" would imply a jtulach@1238: // resource name of "a/b/c.ser" and a classname of "x.y" jtulach@1238: // would imply a resource name of "x/y.class". duke@0: jtulach@1238: final String resourceName; duke@0: jtulach@1238: if (serialized) { jtulach@1238: // Serialized bean jtulach@1238: resourceName = beanName.replace('.', '/').concat(".ser"); jtulach@1238: } else { jtulach@1238: // Regular class jtulach@1238: resourceName = beanName.replace('.', '/').concat(".class"); jtulach@1238: } duke@0: jtulach@1238: URL objectUrl = null; jtulach@1238: URL codeBase = null; jtulach@1238: URL docBase = null; duke@0: jtulach@1238: // Now get the URL correponding to the resource name. jtulach@1238: jtulach@1238: final ClassLoader cloader = cls; jtulach@1238: objectUrl = (URL) AccessController.doPrivileged(new PrivilegedAction() { jtulach@1238: jtulach@1238: public Object run() { jtulach@1238: if (cloader == null) { jtulach@1238: return ClassLoader.getSystemResource(resourceName); jtulach@1238: } else { jtulach@1238: return cloader.getResource(resourceName); jtulach@1238: } jtulach@1238: } jtulach@1238: }); jtulach@1238: jtulach@1238: // If we found a URL, we try to locate the docbase by taking jtulach@1238: // of the final path name component, and the code base by taking jtulach@1238: // of the complete resourceName. jtulach@1238: // So if we had a resourceName of "a/b/c.class" and we got an jtulach@1238: // objectURL of "file://bert/classes/a/b/c.class" then we would jtulach@1238: // want to set the codebase to "file://bert/classes/" and the jtulach@1238: // docbase to "file://bert/classes/a/b/" jtulach@1238: jtulach@1238: if (objectUrl != null) { jtulach@1238: String s = objectUrl.toExternalForm(); jtulach@1238: jtulach@1238: if (s.endsWith(resourceName)) { jtulach@1238: int ix = s.length() - resourceName.length(); jtulach@1238: codeBase = new URL(s.substring(0, ix)); jtulach@1238: docBase = codeBase; jtulach@1238: jtulach@1238: ix = s.lastIndexOf('/'); jtulach@1238: jtulach@1238: if (ix >= 0) { jtulach@1238: docBase = new URL(s.substring(0, ix + 1)); jtulach@1238: } jtulach@1238: } duke@0: } jtulach@1238: jtulach@1238: // Setup a default context and stub. jtulach@1238: BeansAppletContext context = new BeansAppletContext(applet); jtulach@1238: jtulach@1238: stub = (AppletStub) new BeansAppletStub(applet, context, codeBase, docBase); jtulach@1238: applet.setStub(stub); jtulach@1238: } else { jtulach@1238: initializer.initialize(applet, beanContext); duke@0: } duke@0: jtulach@1238: // now, if there is a BeanContext, add the bean, if applicable. jtulach@1238: jtulach@1238: if (beanContext != null) { jtulach@1238: beanContext.add(result); duke@0: } duke@0: jtulach@1238: // If it was deserialized then it was already init-ed. jtulach@1238: // Otherwise we need to initialize it. duke@0: jtulach@1238: if (!serialized) { jtulach@1238: // We need to set a reasonable initial size, as many jtulach@1238: // applets are unhappy if they are started without jtulach@1238: // having been explicitly sized. jtulach@1238: applet.setSize(100, 100); jtulach@1238: applet.init(); duke@0: } duke@0: jtulach@1238: if (needDummies) { jtulach@1238: ((BeansAppletStub) stub).active = true; jtulach@1238: } else { jtulach@1238: initializer.activate(applet); duke@0: } duke@0: jtulach@1238: return true; duke@0: } duke@0: } duke@0: duke@0: duke@0: /** duke@0: * Package private support class. This provides a default AppletContext duke@0: * for beans which are applets. duke@0: */ duke@0: duke@0: class BeansAppletContext implements AppletContext { duke@0: Applet target; malenkov@1077: Hashtable imageCache = new Hashtable(); duke@0: duke@0: BeansAppletContext(Applet target) { duke@0: this.target = target; duke@0: } duke@0: duke@0: public AudioClip getAudioClip(URL url) { duke@0: // We don't currently support audio clips in the Beans.instantiate duke@0: // applet context, unless by some luck there exists a URL content duke@0: // class that can generate an AudioClip from the audio URL. duke@0: try { duke@0: return (AudioClip) url.getContent(); duke@0: } catch (Exception ex) { duke@0: return null; duke@0: } duke@0: } duke@0: duke@0: public synchronized Image getImage(URL url) { duke@0: Object o = imageCache.get(url); duke@0: if (o != null) { duke@0: return (Image)o; duke@0: } duke@0: try { duke@0: o = url.getContent(); duke@0: if (o == null) { duke@0: return null; duke@0: } duke@0: if (o instanceof Image) { duke@0: imageCache.put(url, o); duke@0: return (Image) o; duke@0: } duke@0: // Otherwise it must be an ImageProducer. duke@0: Image img = target.createImage((java.awt.image.ImageProducer)o); duke@0: imageCache.put(url, img); duke@0: return img; duke@0: duke@0: } catch (Exception ex) { duke@0: return null; duke@0: } duke@0: } duke@0: duke@0: public Applet getApplet(String name) { duke@0: return null; duke@0: } duke@0: malenkov@1077: public Enumeration getApplets() { malenkov@1077: Vector applets = new Vector(); duke@0: applets.addElement(target); duke@0: return applets.elements(); duke@0: } duke@0: duke@0: public void showDocument(URL url) { duke@0: // We do nothing. duke@0: } duke@0: duke@0: public void showDocument(URL url, String target) { duke@0: // We do nothing. duke@0: } duke@0: duke@0: public void showStatus(String status) { duke@0: // We do nothing. duke@0: } duke@0: duke@0: public void setStream(String key, InputStream stream)throws IOException{ duke@0: // We do nothing. duke@0: } duke@0: duke@0: public InputStream getStream(String key){ duke@0: // We do nothing. duke@0: return null; duke@0: } duke@0: malenkov@1077: public Iterator getStreamKeys(){ duke@0: // We do nothing. duke@0: return null; duke@0: } duke@0: } duke@0: duke@0: /** duke@0: * Package private support class. This provides an AppletStub duke@0: * for beans which are applets. duke@0: */ duke@0: class BeansAppletStub implements AppletStub { duke@0: transient boolean active; duke@0: transient Applet target; duke@0: transient AppletContext context; duke@0: transient URL codeBase; duke@0: transient URL docBase; duke@0: duke@0: BeansAppletStub(Applet target, duke@0: AppletContext context, URL codeBase, duke@0: URL docBase) { duke@0: this.target = target; duke@0: this.context = context; duke@0: this.codeBase = codeBase; duke@0: this.docBase = docBase; duke@0: } duke@0: duke@0: public boolean isActive() { duke@0: return active; duke@0: } duke@0: duke@0: public URL getDocumentBase() { duke@0: // use the root directory of the applet's class-loader duke@0: return docBase; duke@0: } duke@0: duke@0: public URL getCodeBase() { duke@0: // use the directory where we found the class or serialized object. duke@0: return codeBase; duke@0: } duke@0: duke@0: public String getParameter(String name) { duke@0: return null; duke@0: } duke@0: duke@0: public AppletContext getAppletContext() { duke@0: return context; duke@0: } duke@0: duke@0: public void appletResize(int width, int height) { duke@0: // we do nothing. duke@0: } duke@0: }