/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package java.beans;

import java.applet.Applet;
import java.beans.beancontext.BeanContext;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import org.apache.harmony.beans.internal.nls.Messages;

public class Beans {

    private static boolean designTime = false;

    private static boolean guiAvailable = true;

    public Beans() {
    }

    public static Object instantiate(ClassLoader cls, String beanName,
            BeanContext beanContext, AppletInitializer initializer)
            throws IOException, ClassNotFoundException {
        Object result = instantiate(cls, beanName, beanContext);

        if (result instanceof Applet) {
            initializer.initialize((Applet) result, beanContext);
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    public static Object instantiate(ClassLoader cls, String beanName,
            BeanContext beanContext) throws IOException, ClassNotFoundException {
        Object result = instantiate(cls, beanName);

        if (beanContext != null) {
            beanContext.add(result);
        }

        return result;
    }

    public static Object instantiate(ClassLoader cls, String beanName)
            throws IOException, ClassNotFoundException {
        Object result = null;

        String beanResourceName = getBeanResourceName(beanName);

        InputStream is = (cls == null) ? ClassLoader
                .getSystemResourceAsStream(beanResourceName) : cls
                .getResourceAsStream(beanResourceName);

        if (is != null) {
            try {
                ObjectInputStream ois = (cls == null) ? new ObjectInputStream(
                        is) : new CustomizedObjectInputStream(is, cls);

                try {
                    result = ois.readObject();
                } catch (ClassNotFoundException cnfe) {
                    // skip exception
                }
            } catch (IOException ioe) {
                // skip exception
            }
        }

        if (result == null) {
            try {
                Class<?> c = Class.forName(beanName, true,
                        cls == null ? ClassLoader.getSystemClassLoader() : cls);

                try {
                    result = c.newInstance();

                    if (result instanceof Applet) {
                        Applet applet = (Applet) result;

                        applet.init();
                    }
                } catch (IllegalAccessException iae) {
                    throw new ClassNotFoundException(iae.getClass() + ": " //$NON-NLS-1$
                            + iae.getMessage());
                }
            } catch (InstantiationException ie) {
                throw new ClassNotFoundException(ie.getClass() + ": " //$NON-NLS-1$
                        + ie.getMessage());
            }
        }

        return result;
    }

    public static Object getInstanceOf(Object bean, Class<?> targetType) {
        return bean;
    }

    public static boolean isInstanceOf(Object bean, Class<?> targetType) {
        if (bean == null) {
            throw new NullPointerException(Messages.getString("beans.1D")); //$NON-NLS-1$
        }

        return targetType == null ? false : targetType.isInstance(bean);
    }

    public static void setGuiAvailable(boolean isGuiAvailable)
            throws SecurityException {
        checkPropertiesAccess();
        guiAvailable = isGuiAvailable;
    }

    public static void setDesignTime(boolean isDesignTime)
            throws SecurityException {
        checkPropertiesAccess();
        designTime = isDesignTime;
    }

    public static boolean isGuiAvailable() {
        return guiAvailable;
    }

    public static boolean isDesignTime() {
        return designTime;
    }

    private static void checkPropertiesAccess() throws SecurityException {
        SecurityManager sm = System.getSecurityManager();

        if (sm != null) {
            sm.checkPropertiesAccess();
        }
    }

    private static String getBeanResourceName(String beanName) {
        return beanName.replace('.', '/') + ".ser"; //$NON-NLS-1$
    }

}

@@ -4102,32 +4102,45 @@
   public static 
+synchronized 
 void setGuiAvail
@@ -4168,16 +4168,16 @@
 ilable)%0A
-
         
@@ -4300,24 +4300,37 @@
 blic static 
+synchronized 
 void setDesi
@@ -4482,32 +4482,45 @@
   public static 
+synchronized 
 boolean isGuiAva
@@ -4558,32 +4558,32 @@
 ailable;%0A    %7D%0A%0A
-
     public stati
@@ -4576,32 +4576,45 @@
   public static 
+synchronized 
 boolean isDesign

Add synchronized keywords to guarantee atomic behavior of designTime and guiAvailable accessors and mutators.