You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2014/11/24 08:05:08 UTC

svn commit: r1641318 - in /openwebbeans/trunk: ./ webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java

Author: struberg
Date: Mon Nov 24 07:05:08 2014
New Revision: 1641318

URL: http://svn.apache.org/r1641318
Log:
OWB-1029 fallback from Unsafe to newInstance if not allowed (mainly for GoogleAppEngine)

ported over from owb-1.2.x branch

Modified:
    openwebbeans/trunk/   (props changed)
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java

Propchange: openwebbeans/trunk/
------------------------------------------------------------------------------
    svn:mergeinfo = /openwebbeans/branches/owb_1.2.x:1640945

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java?rev=1641318&r1=1641317&r2=1641318&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java Mon Nov 24 07:05:08 2014
@@ -24,9 +24,11 @@ import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.logging.Logger;
 
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.exception.WebBeansException;
+import org.apache.webbeans.logger.WebBeansLoggerFacade;
 import org.apache.xbean.asm5.ClassWriter;
 import org.apache.xbean.asm5.MethodVisitor;
 import org.apache.xbean.asm5.Opcodes;
@@ -47,15 +49,18 @@ public abstract class AbstractProxyFacto
     public static final int MODIFIER_VARARGS = 0x00000080;;
 
 
+    private static final Logger logger = WebBeansLoggerFacade.getLogger(AbstractProxyFactory.class);
+
     protected WebBeansContext webBeansContext;
 
+
     /**
      * contains the instance of sun.misc.Unsafe.
      * We use it for creating the proxy instance without fully
      * initializing the class.
      */
     private Object unsafe = null;
-    private Method unsafeAllocateInstance;
+    private Method unsafeAllocateInstance = null;
 
 
     /**
@@ -542,7 +547,21 @@ public abstract class AbstractProxyFacto
     {
         try
         {
-            return (T) unsafeAllocateInstance.invoke(unsafe, clazz);
+            if (unsafeAllocateInstance != null)
+            {
+                return (T) unsafeAllocateInstance.invoke(unsafe, clazz);
+            }
+            else
+            {
+                try
+                {
+                    return (T) clazz.newInstance();
+                }
+                catch (InstantiationException e)
+                {
+                    throw new IllegalStateException("Failed to allocateInstance of Proxy class " + clazz.getName(), e);
+                }
+            }
         }
         catch (IllegalAccessException e)
         {
@@ -603,30 +622,34 @@ public abstract class AbstractProxyFacto
                 }
                 catch (Exception e)
                 {
-                    throw new IllegalStateException("Cannot get sun.misc.Unsafe", e);
+                    logger.warning("ATTENTION: Cannot get sun.misc.Unsafe - will use newInstance() instead! Intended for GAE only!");
+                    return null;
                 }
             }
         });
 
         this.unsafe = unsafe;
 
-        unsafeAllocateInstance = AccessController.doPrivileged(new PrivilegedAction<Method>()
+        if (unsafe != null)
         {
-            @Override
-            public Method run()
+            unsafeAllocateInstance = AccessController.doPrivileged(new PrivilegedAction<Method>()
             {
-                try
-                {
-                    Method mtd = unsafeClass.getDeclaredMethod("allocateInstance", Class.class);
-                    mtd.setAccessible(true);
-                    return mtd;
-                }
-                catch (Exception e)
+                @Override
+                public Method run()
                 {
-                    throw new IllegalStateException("Cannot get sun.misc.Unsafe.allocateInstance", e);
+                    try
+                    {
+                        Method mtd = unsafeClass.getDeclaredMethod("allocateInstance", Class.class);
+                        mtd.setAccessible(true);
+                        return mtd;
+                    }
+                    catch (Exception e)
+                    {
+                        throw new IllegalStateException("Cannot get sun.misc.Unsafe.allocateInstance", e);
+                    }
                 }
-            }
-        });
+            });
+        }
     }
 
     /**