src/share/classes/java/beans/Beans.java
changeset 1238 57914fd9382f
parent 1077 27dabbdfdcac
child 1239 0b5692417910
     1.1 --- a/src/share/classes/java/beans/Beans.java	Thu Feb 05 17:00:57 2009 +0300
     1.2 +++ b/src/share/classes/java/beans/Beans.java	Tue Jun 16 17:53:32 2009 +0200
     1.3 @@ -27,13 +27,7 @@
     1.4  
     1.5  import com.sun.beans.finder.ClassFinder;
     1.6  
     1.7 -import java.applet.Applet;
     1.8 -import java.applet.AppletContext;
     1.9 -import java.applet.AppletStub;
    1.10 -import java.applet.AudioClip;
    1.11 -
    1.12  import java.awt.GraphicsEnvironment;
    1.13 -import java.awt.Image;
    1.14  
    1.15  import java.beans.beancontext.BeanContext;
    1.16  
    1.17 @@ -43,17 +37,15 @@
    1.18  import java.io.ObjectStreamClass;
    1.19  import java.io.StreamCorruptedException;
    1.20  
    1.21 -import java.net.URL;
    1.22  
    1.23  import java.security.AccessController;
    1.24  import java.security.PrivilegedAction;
    1.25  
    1.26 -import java.util.Enumeration;
    1.27 -import java.util.Hashtable;
    1.28  import java.util.Iterator;
    1.29 -import java.util.Vector;
    1.30 +import java.util.ServiceLoader;
    1.31  
    1.32  import sun.awt.AppContext;
    1.33 +import sun.beans.AppletProxy;
    1.34  
    1.35  /**
    1.36   * This class provides some general purpose beans control methods.
    1.37 @@ -155,8 +147,14 @@
    1.38       * @exception IOException if an I/O error occurs.
    1.39       */
    1.40  
    1.41 -    public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
    1.42 -                        throws IOException, ClassNotFoundException {
    1.43 +    public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext,
    1.44 +            /** TBD: Oops , this is bad. The AppletInitializer is used from
    1.45 +             * public API. I have not noticed that sooner. Opps.
    1.46 +            AppletInitializer initializer
    1.47 +             * meanwhile turning into object, but this cannot be final solution:
    1.48 +             */
    1.49 +            Object initializer
    1.50 +    ) throws IOException, ClassNotFoundException {
    1.51  
    1.52          InputStream ins;
    1.53          ObjectInputStream oins = null;
    1.54 @@ -231,7 +229,7 @@
    1.55               * Try to instantiate the class.
    1.56               */
    1.57  
    1.58 -            try {
    1.59 +                try {
    1.60                  result = cl.newInstance();
    1.61              } catch (Exception ex) {
    1.62                  // We have to remap the exception to one in our signature.
    1.63 @@ -243,108 +241,13 @@
    1.64          if (result != null) {
    1.65  
    1.66              // Ok, if the result is an applet initialize it.
    1.67 -
    1.68 -            AppletStub stub = null;
    1.69 -
    1.70 -            if (result instanceof Applet) {
    1.71 -                Applet  applet      = (Applet) result;
    1.72 -                boolean needDummies = initializer == null;
    1.73 -
    1.74 -                if (needDummies) {
    1.75 -
    1.76 -                    // Figure our the codebase and docbase URLs.  We do this
    1.77 -                    // by locating the URL for a known resource, and then
    1.78 -                    // massaging the URL.
    1.79 -
    1.80 -                    // First find the "resource name" corresponding to the bean
    1.81 -                    // itself.  So a serialzied bean "a.b.c" would imply a
    1.82 -                    // resource name of "a/b/c.ser" and a classname of "x.y"
    1.83 -                    // would imply a resource name of "x/y.class".
    1.84 -
    1.85 -                    final String resourceName;
    1.86 -
    1.87 -                    if (serialized) {
    1.88 -                        // Serialized bean
    1.89 -                        resourceName = beanName.replace('.','/').concat(".ser");
    1.90 -                    } else {
    1.91 -                        // Regular class
    1.92 -                        resourceName = beanName.replace('.','/').concat(".class");
    1.93 -                    }
    1.94 -
    1.95 -                    URL objectUrl = null;
    1.96 -                    URL codeBase  = null;
    1.97 -                    URL docBase   = null;
    1.98 -
    1.99 -                    // Now get the URL correponding to the resource name.
   1.100 -
   1.101 -                    final ClassLoader cloader = cls;
   1.102 -                    objectUrl = (URL)
   1.103 -                        AccessController.doPrivileged
   1.104 -                        (new PrivilegedAction() {
   1.105 -                            public Object run() {
   1.106 -                                if (cloader == null)
   1.107 -                                    return ClassLoader.getSystemResource
   1.108 -                                                                (resourceName);
   1.109 -                                else
   1.110 -                                    return cloader.getResource(resourceName);
   1.111 -                            }
   1.112 -                    });
   1.113 -
   1.114 -                    // If we found a URL, we try to locate the docbase by taking
   1.115 -                    // of the final path name component, and the code base by taking
   1.116 -                    // of the complete resourceName.
   1.117 -                    // So if we had a resourceName of "a/b/c.class" and we got an
   1.118 -                    // objectURL of "file://bert/classes/a/b/c.class" then we would
   1.119 -                    // want to set the codebase to "file://bert/classes/" and the
   1.120 -                    // docbase to "file://bert/classes/a/b/"
   1.121 -
   1.122 -                    if (objectUrl != null) {
   1.123 -                        String s = objectUrl.toExternalForm();
   1.124 -
   1.125 -                        if (s.endsWith(resourceName)) {
   1.126 -                            int ix   = s.length() - resourceName.length();
   1.127 -                            codeBase = new URL(s.substring(0,ix));
   1.128 -                            docBase  = codeBase;
   1.129 -
   1.130 -                            ix = s.lastIndexOf('/');
   1.131 -
   1.132 -                            if (ix >= 0) {
   1.133 -                                docBase = new URL(s.substring(0,ix+1));
   1.134 -                            }
   1.135 -                        }
   1.136 -                    }
   1.137 -
   1.138 -                    // Setup a default context and stub.
   1.139 -                    BeansAppletContext context = new BeansAppletContext(applet);
   1.140 -
   1.141 -                    stub = (AppletStub)new BeansAppletStub(applet, context, codeBase, docBase);
   1.142 -                    applet.setStub(stub);
   1.143 -                } else {
   1.144 -                    initializer.initialize(applet, beanContext);
   1.145 -                }
   1.146 -
   1.147 -                // now, if there is a BeanContext, add the bean, if applicable.
   1.148 -
   1.149 -                if (beanContext != null) {
   1.150 -                    beanContext.add(result);
   1.151 -                }
   1.152 -
   1.153 -                // If it was deserialized then it was already init-ed.
   1.154 -                // Otherwise we need to initialize it.
   1.155 -
   1.156 -                if (!serialized) {
   1.157 -                    // We need to set a reasonable initial size, as many
   1.158 -                    // applets are unhappy if they are started without
   1.159 -                    // having been explicitly sized.
   1.160 -                    applet.setSize(100,100);
   1.161 -                    applet.init();
   1.162 -                }
   1.163 -
   1.164 -                if (needDummies) {
   1.165 -                  ((BeansAppletStub)stub).active = true;
   1.166 -                } else initializer.activate(applet);
   1.167 -
   1.168 -            } else if (beanContext != null) beanContext.add(result);
   1.169 +            Iterator<AppletProxy> it = ServiceLoader.load(AppletProxy.class).iterator();
   1.170 +            AppletProxy ap = it.hasNext() ? it.next() : null;
   1.171 +            if (ap != null || !ap.initialize(
   1.172 +                result, initializer, serialized, beanName, beanContext, cls
   1.173 +            )) {
   1.174 +                if (beanContext != null) beanContext.add(result);
   1.175 +            }
   1.176          }
   1.177  
   1.178          return result;
   1.179 @@ -504,134 +407,3 @@
   1.180      }
   1.181  }
   1.182  
   1.183 -/**
   1.184 - * Package private support class.  This provides a default AppletContext
   1.185 - * for beans which are applets.
   1.186 - */
   1.187 -
   1.188 -class BeansAppletContext implements AppletContext {
   1.189 -    Applet target;
   1.190 -    Hashtable imageCache = new Hashtable();
   1.191 -
   1.192 -    BeansAppletContext(Applet target) {
   1.193 -        this.target = target;
   1.194 -    }
   1.195 -
   1.196 -    public AudioClip getAudioClip(URL url) {
   1.197 -        // We don't currently support audio clips in the Beans.instantiate
   1.198 -        // applet context, unless by some luck there exists a URL content
   1.199 -        // class that can generate an AudioClip from the audio URL.
   1.200 -        try {
   1.201 -            return (AudioClip) url.getContent();
   1.202 -        } catch (Exception ex) {
   1.203 -            return null;
   1.204 -        }
   1.205 -    }
   1.206 -
   1.207 -    public synchronized Image getImage(URL url) {
   1.208 -        Object o = imageCache.get(url);
   1.209 -        if (o != null) {
   1.210 -            return (Image)o;
   1.211 -        }
   1.212 -        try {
   1.213 -            o = url.getContent();
   1.214 -            if (o == null) {
   1.215 -                return null;
   1.216 -            }
   1.217 -            if (o instanceof Image) {
   1.218 -                imageCache.put(url, o);
   1.219 -                return (Image) o;
   1.220 -            }
   1.221 -            // Otherwise it must be an ImageProducer.
   1.222 -            Image img = target.createImage((java.awt.image.ImageProducer)o);
   1.223 -            imageCache.put(url, img);
   1.224 -            return img;
   1.225 -
   1.226 -        } catch (Exception ex) {
   1.227 -            return null;
   1.228 -        }
   1.229 -    }
   1.230 -
   1.231 -    public Applet getApplet(String name) {
   1.232 -        return null;
   1.233 -    }
   1.234 -
   1.235 -    public Enumeration getApplets() {
   1.236 -        Vector applets = new Vector();
   1.237 -        applets.addElement(target);
   1.238 -        return applets.elements();
   1.239 -    }
   1.240 -
   1.241 -    public void showDocument(URL url) {
   1.242 -        // We do nothing.
   1.243 -    }
   1.244 -
   1.245 -    public void showDocument(URL url, String target) {
   1.246 -        // We do nothing.
   1.247 -    }
   1.248 -
   1.249 -    public void showStatus(String status) {
   1.250 -        // We do nothing.
   1.251 -    }
   1.252 -
   1.253 -    public void setStream(String key, InputStream stream)throws IOException{
   1.254 -        // We do nothing.
   1.255 -    }
   1.256 -
   1.257 -    public InputStream getStream(String key){
   1.258 -        // We do nothing.
   1.259 -        return null;
   1.260 -    }
   1.261 -
   1.262 -    public Iterator getStreamKeys(){
   1.263 -        // We do nothing.
   1.264 -        return null;
   1.265 -    }
   1.266 -}
   1.267 -
   1.268 -/**
   1.269 - * Package private support class.  This provides an AppletStub
   1.270 - * for beans which are applets.
   1.271 - */
   1.272 -class BeansAppletStub implements AppletStub {
   1.273 -    transient boolean active;
   1.274 -    transient Applet target;
   1.275 -    transient AppletContext context;
   1.276 -    transient URL codeBase;
   1.277 -    transient URL docBase;
   1.278 -
   1.279 -    BeansAppletStub(Applet target,
   1.280 -                AppletContext context, URL codeBase,
   1.281 -                                URL docBase) {
   1.282 -        this.target = target;
   1.283 -        this.context = context;
   1.284 -        this.codeBase = codeBase;
   1.285 -        this.docBase = docBase;
   1.286 -    }
   1.287 -
   1.288 -    public boolean isActive() {
   1.289 -        return active;
   1.290 -    }
   1.291 -
   1.292 -    public URL getDocumentBase() {
   1.293 -        // use the root directory of the applet's class-loader
   1.294 -        return docBase;
   1.295 -    }
   1.296 -
   1.297 -    public URL getCodeBase() {
   1.298 -        // use the directory where we found the class or serialized object.
   1.299 -        return codeBase;
   1.300 -    }
   1.301 -
   1.302 -    public String getParameter(String name) {
   1.303 -        return null;
   1.304 -    }
   1.305 -
   1.306 -    public AppletContext getAppletContext() {
   1.307 -        return context;
   1.308 -    }
   1.309 -
   1.310 -    public void appletResize(int width, int height) {
   1.311 -        // we do nothing.
   1.312 -    }
   1.313 -}