openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1835726 - /openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
Date Thu, 12 Jul 2018 12:40:40 GMT
Author: rmannibucau
Date: Thu Jul 12 12:40:40 2018
New Revision: 1835726

URL: http://svn.apache.org/viewvc?rev=1835726&view=rev
Log:
OWB-1248 for java 11 fallbacks on the 'not unsafe' mode

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

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=1835726&r1=1835725&r2=1835726&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
Thu Jul 12 12:40:40 2018
@@ -33,6 +33,7 @@ import java.security.PrivilegedAction;
 import java.security.ProtectionDomain;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.logging.Logger;
 
 import org.apache.webbeans.config.WebBeansContext;
@@ -63,7 +64,6 @@ public abstract class AbstractProxyFacto
 
     private static final Logger logger = WebBeansLoggerFacade.getLogger(AbstractProxyFactory.class);
 
-
     protected WebBeansContext webBeansContext;
 
     /**
@@ -73,7 +73,7 @@ public abstract class AbstractProxyFacto
      */
     private Object unsafe;
     private Method unsafeAllocateInstance;
-    private Method unsafeDefineClass;
+    private AtomicReference<Method> unsafeDefineClass;
 
     private final int javaVersion;
 
@@ -405,7 +405,8 @@ public abstract class AbstractProxyFacto
             }
             else
             {
-                definedClass = (Class<T>) unsafeDefineClass.invoke(unsafe, proxyName,
proxyBytes, 0, proxyBytes.length, classLoader, null);
+
+                definedClass = (Class<T>) unsafeDefineClass().invoke(unsafe, proxyName,
proxyBytes, 0, proxyBytes.length, classLoader, null);
             }
 
             return (Class<T>) Class.forName(definedClass.getName(), true, classLoader);
@@ -431,6 +432,36 @@ public abstract class AbstractProxyFacto
         }
     }
 
+    private Method unsafeDefineClass()
+    {
+        Method value = unsafeDefineClass.get();
+        if (value == null)
+        {
+            synchronized (this)
+            {
+                final Class<?> unsafeClass = getUnsafeClass();
+                value = AccessController.doPrivileged(new PrivilegedAction<Method>()
+                {
+                    @Override
+                    public Method run()
+                    {
+                        try
+                        {
+                            return unsafeClass.getDeclaredMethod("defineClass",
+                                    String.class, byte[].class, int.class, int.class, ClassLoader.class,
ProtectionDomain.class);
+                        }
+                        catch (final Exception e)
+                        {
+                            throw new IllegalStateException("Cannot get Unsafe.defineClass
or equivalent", e);
+                        }
+                    }
+                });
+                unsafeDefineClass.compareAndSet(null, value);
+            }
+        }
+        return value;
+    }
+
 
     protected boolean unproxyableMethod(Method delegatedMethod)
     {
@@ -705,9 +736,9 @@ public abstract class AbstractProxyFacto
             {
                 try
                 {
-                    return (T) clazz.newInstance();
+                    return clazz.getConstructor().newInstance();
                 }
-                catch (InstantiationException e)
+                catch (final Exception e)
                 {
                     throw new IllegalStateException("Failed to allocateInstance of Proxy
class " + clazz.getName(), e);
                 }
@@ -728,36 +759,7 @@ public abstract class AbstractProxyFacto
 
     private void initializeUnsafe()
     {
-        Class<?> unsafeClass;
-        try
-        {
-            unsafeClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>()
-            {
-                @Override
-                public Class<?> run()
-                {
-                    try
-                    {
-                        return Thread.currentThread().getContextClassLoader().loadClass("sun.misc.Unsafe");
-                    }
-                    catch (Exception e)
-                    {
-                        try
-                        {
-                            return ClassLoader.getSystemClassLoader().loadClass("sun.misc.Unsafe");
-                        }
-                        catch (ClassNotFoundException e1)
-                        {
-                            throw new IllegalStateException("Cannot get sun.misc.Unsafe",
e);
-                        }
-                    }
-                }
-            });
-        }
-        catch (Exception e)
-        {
-            throw new IllegalStateException("Cannot get sun.misc.Unsafe class", e);
-        }
+        final Class<?> unsafeClass = getUnsafeClass();
 
         Object unsafe = AccessController.doPrivileged(new PrivilegedAction<Object>()
         {
@@ -772,7 +774,7 @@ public abstract class AbstractProxyFacto
                 }
                 catch (Exception e)
                 {
-                    logger.warning("ATTENTION: Cannot get sun.misc.Unsafe - will use newInstance()
instead! Intended for GAE only!");
+                    logger.info("Cannot get sun.misc.Unsafe - will use newInstance() instead!");
                     return null;
                 }
             }
@@ -795,27 +797,46 @@ public abstract class AbstractProxyFacto
                     }
                     catch (Exception e)
                     {
-                        throw new IllegalStateException("Cannot get sun.misc.Unsafe.allocateInstance",
e);
+                        return null; // use newInstance()
                     }
                 }
             });
-            unsafeDefineClass = AccessController.doPrivileged(new PrivilegedAction<Method>()
+        }
+    }
+
+    private Class<?> getUnsafeClass()
+    {
+        Class<?> unsafeClass;
+        try
+        {
+            unsafeClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>()
             {
                 @Override
-                public Method run()
+                public Class<?> run()
                 {
                     try
                     {
-                        return unsafeClass.getDeclaredMethod("defineClass",
-                                String.class, byte[].class, int.class, int.class, ClassLoader.class,
ProtectionDomain.class);
+                        return Thread.currentThread().getContextClassLoader().loadClass("sun.misc.Unsafe");
                     }
                     catch (Exception e)
                     {
-                        throw new IllegalStateException("Cannot get Unsafe.defineClass",
e);
+                        try
+                        {
+                            return ClassLoader.getSystemClassLoader().loadClass("sun.misc.Unsafe");
+                        }
+                        catch (ClassNotFoundException e1)
+                        {
+                            throw new IllegalStateException("Cannot get sun.misc.Unsafe",
e);
+                        }
                     }
                 }
             });
         }
+        catch (final Exception e)
+        {
+            throw new IllegalStateException("Cannot get sun.misc.Unsafe class", e);
+        }
+        return unsafeClass;
     }
 
     /**



Mime
View raw message