6669869: Beans.isDesignTime() and other queries should be per-AppContext
authormalenkov
Thu, 05 Feb 2009 17:00:57 +0300
changeset 107727dabbdfdcac
parent 1076 62a84e564a8c
child 1078 0960e96d0de8
6669869: Beans.isDesignTime() and other queries should be per-AppContext
Reviewed-by: peterz, rupashka
src/share/classes/java/beans/Beans.java
test/java/beans/Beans/6669869/TestDesignTime.java
test/java/beans/Beans/6669869/TestGuiAvailable.java
     1.1 --- a/src/share/classes/java/beans/Beans.java	Thu Feb 05 14:48:10 2009 +0300
     1.2 +++ b/src/share/classes/java/beans/Beans.java	Thu Feb 05 17:00:57 2009 +0300
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * Copyright 1996-2009 Sun Microsystems, Inc.  All Rights Reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -27,26 +27,41 @@
    1.11  
    1.12  import com.sun.beans.finder.ClassFinder;
    1.13  
    1.14 -import java.applet.*;
    1.15 +import java.applet.Applet;
    1.16 +import java.applet.AppletContext;
    1.17 +import java.applet.AppletStub;
    1.18 +import java.applet.AudioClip;
    1.19  
    1.20 -import java.awt.*;
    1.21 -
    1.22 -import java.beans.AppletInitializer;
    1.23 +import java.awt.GraphicsEnvironment;
    1.24 +import java.awt.Image;
    1.25  
    1.26  import java.beans.beancontext.BeanContext;
    1.27  
    1.28 -import java.io.*;
    1.29 -
    1.30 -import java.lang.reflect.Constructor;
    1.31 +import java.io.IOException;
    1.32 +import java.io.InputStream;
    1.33 +import java.io.ObjectInputStream;
    1.34 +import java.io.ObjectStreamClass;
    1.35 +import java.io.StreamCorruptedException;
    1.36  
    1.37  import java.net.URL;
    1.38 -import java.lang.reflect.Array;
    1.39 +
    1.40 +import java.security.AccessController;
    1.41 +import java.security.PrivilegedAction;
    1.42 +
    1.43 +import java.util.Enumeration;
    1.44 +import java.util.Hashtable;
    1.45 +import java.util.Iterator;
    1.46 +import java.util.Vector;
    1.47 +
    1.48 +import sun.awt.AppContext;
    1.49  
    1.50  /**
    1.51   * This class provides some general purpose beans control methods.
    1.52   */
    1.53  
    1.54  public class Beans {
    1.55 +    private static final Object DESIGN_TIME = new Object();
    1.56 +    private static final Object GUI_AVAILABLE = new Object();
    1.57  
    1.58      /**
    1.59       * <p>
    1.60 @@ -59,12 +74,12 @@
    1.61       * @param     beanName    the name of the bean within the class-loader.
    1.62       *                        For example "sun.beanbox.foobah"
    1.63       *
    1.64 -     * @exception java.lang.ClassNotFoundException if the class of a serialized
    1.65 +     * @exception ClassNotFoundException if the class of a serialized
    1.66       *              object could not be found.
    1.67 -     * @exception java.io.IOException if an I/O error occurs.
    1.68 +     * @exception IOException if an I/O error occurs.
    1.69       */
    1.70  
    1.71 -    public static Object instantiate(ClassLoader cls, String beanName) throws java.io.IOException, ClassNotFoundException {
    1.72 +    public static Object instantiate(ClassLoader cls, String beanName) throws IOException, ClassNotFoundException {
    1.73          return Beans.instantiate(cls, beanName, null, null);
    1.74      }
    1.75  
    1.76 @@ -80,12 +95,12 @@
    1.77       *                        For example "sun.beanbox.foobah"
    1.78       * @param     beanContext The BeanContext in which to nest the new bean
    1.79       *
    1.80 -     * @exception java.lang.ClassNotFoundException if the class of a serialized
    1.81 +     * @exception ClassNotFoundException if the class of a serialized
    1.82       *              object could not be found.
    1.83 -     * @exception java.io.IOException if an I/O error occurs.
    1.84 +     * @exception IOException if an I/O error occurs.
    1.85       */
    1.86  
    1.87 -    public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws java.io.IOException, ClassNotFoundException {
    1.88 +    public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws IOException, ClassNotFoundException {
    1.89          return Beans.instantiate(cls, beanName, beanContext, null);
    1.90      }
    1.91  
    1.92 @@ -135,19 +150,19 @@
    1.93       * @param     beanContext The BeanContext in which to nest the new bean
    1.94       * @param     initializer The AppletInitializer for the new bean
    1.95       *
    1.96 -     * @exception java.lang.ClassNotFoundException if the class of a serialized
    1.97 +     * @exception ClassNotFoundException if the class of a serialized
    1.98       *              object could not be found.
    1.99 -     * @exception java.io.IOException if an I/O error occurs.
   1.100 +     * @exception IOException if an I/O error occurs.
   1.101       */
   1.102  
   1.103      public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
   1.104 -                        throws java.io.IOException, ClassNotFoundException {
   1.105 +                        throws IOException, ClassNotFoundException {
   1.106  
   1.107 -        java.io.InputStream ins;
   1.108 -        java.io.ObjectInputStream oins = null;
   1.109 +        InputStream ins;
   1.110 +        ObjectInputStream oins = null;
   1.111          Object result = null;
   1.112          boolean serialized = false;
   1.113 -        java.io.IOException serex = null;
   1.114 +        IOException serex = null;
   1.115  
   1.116          // If the given classloader is null, we check if an
   1.117          // system classloader is available and (if so)
   1.118 @@ -166,8 +181,8 @@
   1.119          // Try to find a serialized object with this name
   1.120          final String serName = beanName.replace('.','/').concat(".ser");
   1.121          final ClassLoader loader = cls;
   1.122 -        ins = (InputStream)java.security.AccessController.doPrivileged
   1.123 -            (new java.security.PrivilegedAction() {
   1.124 +        ins = (InputStream)AccessController.doPrivileged
   1.125 +            (new PrivilegedAction() {
   1.126                  public Object run() {
   1.127                      if (loader == null)
   1.128                          return ClassLoader.getSystemResourceAsStream(serName);
   1.129 @@ -185,7 +200,7 @@
   1.130                  result = oins.readObject();
   1.131                  serialized = true;
   1.132                  oins.close();
   1.133 -            } catch (java.io.IOException ex) {
   1.134 +            } catch (IOException ex) {
   1.135                  ins.close();
   1.136                  // Drop through and try opening the class.  But remember
   1.137                  // the exception in case we can't find the class either.
   1.138 @@ -264,8 +279,8 @@
   1.139  
   1.140                      final ClassLoader cloader = cls;
   1.141                      objectUrl = (URL)
   1.142 -                        java.security.AccessController.doPrivileged
   1.143 -                        (new java.security.PrivilegedAction() {
   1.144 +                        AccessController.doPrivileged
   1.145 +                        (new PrivilegedAction() {
   1.146                              public Object run() {
   1.147                                  if (cloader == null)
   1.148                                      return ClassLoader.getSystemResource
   1.149 @@ -377,10 +392,11 @@
   1.150       * @return  True if we are running in an application construction
   1.151       *          environment.
   1.152       *
   1.153 -     * @see java.beans.DesignMode
   1.154 +     * @see DesignMode
   1.155       */
   1.156      public static boolean isDesignTime() {
   1.157 -        return designTime;
   1.158 +        Object value = AppContext.getAppContext().get(DESIGN_TIME);
   1.159 +        return (value instanceof Boolean) && (Boolean) value;
   1.160      }
   1.161  
   1.162      /**
   1.163 @@ -393,11 +409,12 @@
   1.164       *     false in a server environment or if an application is
   1.165       *     running as part of a batch job.
   1.166       *
   1.167 -     * @see java.beans.Visibility
   1.168 +     * @see Visibility
   1.169       *
   1.170       */
   1.171      public static boolean isGuiAvailable() {
   1.172 -        return guiAvailable;
   1.173 +        Object value = AppContext.getAppContext().get(GUI_AVAILABLE);
   1.174 +        return (value instanceof Boolean) ? (Boolean) value : !GraphicsEnvironment.isHeadless();
   1.175      }
   1.176  
   1.177      /**
   1.178 @@ -423,7 +440,7 @@
   1.179          if (sm != null) {
   1.180              sm.checkPropertiesAccess();
   1.181          }
   1.182 -        designTime = isDesignTime;
   1.183 +        AppContext.getAppContext().put(DESIGN_TIME, Boolean.valueOf(isDesignTime));
   1.184      }
   1.185  
   1.186      /**
   1.187 @@ -449,14 +466,7 @@
   1.188          if (sm != null) {
   1.189              sm.checkPropertiesAccess();
   1.190          }
   1.191 -        guiAvailable = isGuiAvailable;
   1.192 -    }
   1.193 -
   1.194 -
   1.195 -    private static boolean designTime;
   1.196 -    private static boolean guiAvailable;
   1.197 -    static {
   1.198 -        guiAvailable = !GraphicsEnvironment.isHeadless();
   1.199 +        AppContext.getAppContext().put(GUI_AVAILABLE, Boolean.valueOf(isGuiAvailable));
   1.200      }
   1.201  }
   1.202  
   1.203 @@ -501,7 +511,7 @@
   1.204  
   1.205  class BeansAppletContext implements AppletContext {
   1.206      Applet target;
   1.207 -    java.util.Hashtable imageCache = new java.util.Hashtable();
   1.208 +    Hashtable imageCache = new Hashtable();
   1.209  
   1.210      BeansAppletContext(Applet target) {
   1.211          this.target = target;
   1.212 @@ -546,8 +556,8 @@
   1.213          return null;
   1.214      }
   1.215  
   1.216 -    public java.util.Enumeration getApplets() {
   1.217 -        java.util.Vector applets = new java.util.Vector();
   1.218 +    public Enumeration getApplets() {
   1.219 +        Vector applets = new Vector();
   1.220          applets.addElement(target);
   1.221          return applets.elements();
   1.222      }
   1.223 @@ -573,7 +583,7 @@
   1.224          return null;
   1.225      }
   1.226  
   1.227 -    public java.util.Iterator getStreamKeys(){
   1.228 +    public Iterator getStreamKeys(){
   1.229          // We do nothing.
   1.230          return null;
   1.231      }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/java/beans/Beans/6669869/TestDesignTime.java	Thu Feb 05 17:00:57 2009 +0300
     2.3 @@ -0,0 +1,52 @@
     2.4 +/*
     2.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    2.24 + * have any questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 6669869
    2.30 + * @summary Tests DesignTime property in different application contexts
    2.31 + * @author Sergey Malenkov
    2.32 + */
    2.33 +
    2.34 +import java.beans.Beans;
    2.35 +import sun.awt.SunToolkit;
    2.36 +
    2.37 +public class TestDesignTime implements Runnable {
    2.38 +    public static void main(String[] args) throws InterruptedException {
    2.39 +        if (Beans.isDesignTime()) {
    2.40 +            throw new Error("unexpected DesignTime property");
    2.41 +        }
    2.42 +        Beans.setDesignTime(!Beans.isDesignTime());
    2.43 +        ThreadGroup group = new ThreadGroup("$$$");
    2.44 +        Thread thread = new Thread(group, new TestDesignTime());
    2.45 +        thread.start();
    2.46 +        thread.join();
    2.47 +    }
    2.48 +
    2.49 +    public void run() {
    2.50 +        SunToolkit.createNewAppContext();
    2.51 +        if (Beans.isDesignTime()) {
    2.52 +            throw new Error("shared DesignTime property");
    2.53 +        }
    2.54 +    }
    2.55 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/java/beans/Beans/6669869/TestGuiAvailable.java	Thu Feb 05 17:00:57 2009 +0300
     3.3 @@ -0,0 +1,53 @@
     3.4 +/*
     3.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    3.24 + * have any questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 6669869
    3.30 + * @summary Tests GuiAvailable property in different application contexts
    3.31 + * @author Sergey Malenkov
    3.32 + */
    3.33 +
    3.34 +import java.awt.GraphicsEnvironment;
    3.35 +import java.beans.Beans;
    3.36 +import sun.awt.SunToolkit;
    3.37 +
    3.38 +public class TestGuiAvailable implements Runnable {
    3.39 +    public static void main(String[] args) throws InterruptedException {
    3.40 +        if (Beans.isGuiAvailable() == GraphicsEnvironment.isHeadless()) {
    3.41 +            throw new Error("unexpected GuiAvailable property");
    3.42 +        }
    3.43 +        Beans.setGuiAvailable(!Beans.isGuiAvailable());
    3.44 +        ThreadGroup group = new ThreadGroup("$$$");
    3.45 +        Thread thread = new Thread(group, new TestGuiAvailable());
    3.46 +        thread.start();
    3.47 +        thread.join();
    3.48 +    }
    3.49 +
    3.50 +    public void run() {
    3.51 +        SunToolkit.createNewAppContext();
    3.52 +        if (Beans.isGuiAvailable() == GraphicsEnvironment.isHeadless()) {
    3.53 +            throw new Error("shared GuiAvailable property");
    3.54 +        }
    3.55 +    }
    3.56 +}