You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by le...@apache.org on 2007/07/17 07:48:33 UTC

svn commit: r556820 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/Beans.java test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java

Author: leoli
Date: Mon Jul 16 22:48:30 2007
New Revision: 556820

URL: http://svn.apache.org/viewvc?view=rev&rev=556820
Log:
Apply patch for HARMONY-3777( [classlib][beans]java.beans.Beans.instantiate should supply the applet with a default AppletStub and AppletContext when the target is an applet).

Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java
    harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java?view=diff&rev=556820&r1=556819&r2=556820
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java Mon Jul 16 22:48:30 2007
@@ -18,10 +18,22 @@
 package java.beans;
 
 import java.applet.Applet;
+import java.applet.AppletContext;
+import java.applet.AppletStub;
+import java.applet.AudioClip;
+import java.awt.Image;
 import java.beans.beancontext.BeanContext;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Vector;
+
 import org.apache.harmony.beans.internal.nls.Messages;
 
 public class Beans {
@@ -33,70 +45,70 @@
     public Beans() {
     }
 
-    public static Object instantiate(ClassLoader cls, String beanName,
-            BeanContext beanContext, AppletInitializer initializer)
+    public static Object instantiate(ClassLoader loader, String name)
             throws IOException, ClassNotFoundException {
-        Object result = instantiate(cls, beanName, beanContext);
-
-        if (result instanceof Applet) {
-            initializer.initialize((Applet) result, beanContext);
-        }
-
-        return result;
+        return instantiate(loader, name, null, null);
     }
 
-    @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) {            
-            ObjectInputStream ois = (cls == null) ? new ObjectInputStream(is) : new CustomizedObjectInputStream(is, cls);
-            result = ois.readObject();
-        }
-
-        if (result == null) {
-            try {
-                Class<?> c = Class.forName(beanName, true,
-                        cls == null ? ClassLoader.getSystemClassLoader() : cls);
-
-                try {
-                    result = c.newInstance();
+			BeanContext beanContext) throws IOException, ClassNotFoundException {
+		return instantiate(cls, beanName, beanContext, null);
 
-                    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;
-    }
+    @SuppressWarnings("unchecked")
+	public static Object instantiate(ClassLoader cls, String beanName,
+			BeanContext context, AppletInitializer initializer)
+			throws IOException, ClassNotFoundException {
+		Object result = null;
+
+		boolean deserialized = true;
+
+		ClassLoader loader = null;
+
+		String beanResourceName = getBeanResourceName(beanName);
+
+		InputStream is = (cls == null) ? ClassLoader
+				.getSystemResourceAsStream(beanResourceName) : cls
+				.getResourceAsStream(beanResourceName);
+
+		if (is != null) {
+			ObjectInputStream ois = (cls == null) ? new ObjectInputStream(is)
+					: new CustomizedObjectInputStream(is, cls);
+			result = ois.readObject();
+		}
+
+		if (result == null) {
+			deserialized = false;
+			try {
+				loader = cls == null ? ClassLoader.getSystemClassLoader() : cls;
+				Class<?> c = Class.forName(beanName, true, loader);
+
+				try {
+					result = c.newInstance();
+
+				} 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());
+			}
+		}
+
+		if (result != null) {
+			// Applet specific initialization
+			if (result instanceof Applet) {
+				appletLoaded((Applet) result, loader, beanName, context,
+						initializer, deserialized);
+			}
+			if (null != context) {
+				context.add(result);
+			}
+		}
+		return result;
+	}
 
     public static Object getInstanceOf(Object bean, Class<?> targetType) {
         return bean;
@@ -143,5 +155,152 @@
     private static String getBeanResourceName(String beanName) {
         return beanName.replace('.', '/') + ".ser"; //$NON-NLS-1$
     }
+    
+    
+    private static void appletLoaded(Applet applet, ClassLoader loader,
+            String name, BeanContext context, AppletInitializer initializer,
+            boolean deserialized) throws ClassNotFoundException {
+
+        // If there is an initializer
+        if (initializer != null) {
+            initializer.initialize(applet, context);
+        } else {
+            setStub(applet, loader, deserialized, name);
+        }
+
+        if (!deserialized) {
+            applet.init();
+        }
+
+        if (initializer != null) {
+            initializer.activate(applet);
+        }
+    }
+
+    private static void setStub(Applet applet, final ClassLoader loader,
+            boolean serialized, String beanName) throws ClassNotFoundException {
+        // Get path to the resource representing the applet.
+        String pathName = beanName.replace('.', '/');
+        final String resourceName = serialized ? pathName.concat(".ser") : pathName.concat(".class"); //$NON-NLS-1$ //$NON-NLS-2$
+        URL objectUrl = AccessController
+                .doPrivileged(new PrivilegedAction<URL>() {
+                    public URL run() {
+                        if (loader == null)
+                            return ClassLoader.getSystemResource(resourceName);
+                        return loader.getResource(resourceName);
+                    }
+                });
+
+        // If we can't get to the applet itself, the codebase and doc base are
+        // left as null.
+        if (objectUrl == null) {
+            applet.setStub(getAppletStub(applet, getStubAppletContext(applet),
+                    null, null));
+            return;
+        }
+
+        // Try to decompose the resource URL to get to the doc/code URL
+        String urlString = objectUrl.toExternalForm();
+
+        // This is the URL of the directory that contains the applet.
+        int codeURLlength = urlString.length() - resourceName.length();
+        URL codeBase = safeURL(urlString.substring(0, codeURLlength));
+
+        // URL of the document containing the applet.
+        int docURLlength = urlString.lastIndexOf('/');
+        URL docBase = safeURL(urlString.substring(0, docURLlength + 1));
+
+        applet.setStub(getAppletStub(applet, getStubAppletContext(applet),
+                codeBase, docBase));
+    }
+    
+    
+    private static AppletStub getAppletStub(final Applet target,
+            final AppletContext context, final URL codeBase, final URL docBase) {
+
+        return new AppletStub() {
+            public boolean isActive() {
+                return true;
+            }
+
+            public URL getDocumentBase() {
+                return docBase;
+            }
+
+            public URL getCodeBase() {
+                return codeBase;
+            }
+
+            public String getParameter(String name) {
+                // Applet beans have no params.
+                return null;
+            }
+
+            public AppletContext getAppletContext() {
+                return context;
+            }
+
+            public void appletResize(int width, int height) {
+                // Do nothing.
+            }
+        };
+    }
+
+    private static AppletContext getStubAppletContext(final Applet target) {
+        return new AppletContext() {
+            public AudioClip getAudioClip(URL url) {
+                return null;
+            }
+
+            public synchronized Image getImage(URL url) {
+                return null;
+            }
+
+            public Applet getApplet(String name) {
+                return null;
+            }
+
+            public Enumeration<Applet> getApplets() {
+                Vector<Applet> applets = new Vector<Applet>();
+                applets.addElement(target);
+                return applets.elements();
+            }
+
+            public void showDocument(URL url) {
+                // Do nothing.
+            }
+
+            public void showDocument(URL url, String aTarget) {
+                // Do nothing.
+            }
+
+            public void showStatus(String status) {
+                // Do nothing.
+            }
+
+            public void setStream(String key, InputStream stream)
+                    throws IOException {
+                // Do nothing.
+            }
+
+            public InputStream getStream(String key) {
+                return null;
+            }
+
+            public Iterator<String> getStreamKeys() {
+                return null;
+            }
+        };
+    }
+    
+
+	//  Maps malformed URL exception to ClassNotFoundException
+	private static URL safeURL(String urlString) throws ClassNotFoundException {
+		try {
+			return new URL(urlString);
+		} catch (MalformedURLException exception) {
+			throw new ClassNotFoundException(exception.getMessage());
+		}
+	}
 
 }

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java?view=diff&rev=556820&r1=556819&r2=556820
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java Mon Jul 16 22:48:30 2007
@@ -17,6 +17,7 @@
 
 package org.apache.harmony.beans.tests.java.beans;
 
+import java.applet.Applet;
 import java.awt.Component;
 import java.beans.AppletInitializer;
 import java.beans.Beans;
@@ -444,6 +445,13 @@
             // expected
         }
     }
+    
+    //Regression for HARMONY-3777
+    public void test_instantiate_with_applet() throws Exception{
+		Applet applet = (Applet) Beans.instantiate(null, "java.applet.Applet");
+		assertNotNull(applet.getAppletContext());
+		assertTrue(applet.isActive());
+	}
 
     /**
      *