openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1640945 - /openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
Date Fri, 21 Nov 2014 15:52:14 GMT
Author: struberg
Date: Fri Nov 21 15:52:14 2014
New Revision: 1640945

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

Modified:
    openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java

Modified: openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java?rev=1640945&r1=1640944&r2=1640945&view=diff
==============================================================================
--- openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
(original)
+++ openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
Fri Nov 21 15:52:14 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);
+                    }
                 }
-            }
-        });
+            });
+        }
     }
 
     /**



Mime
View raw message