[richfaces-svn-commits] JBoss Rich Faces SVN: r18557 - in trunk/core/impl/src/main/java/org: richfaces/application and 1 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Wed Aug 11 11:02:44 EDT 2010


Author: nbelaevski
Date: 2010-08-11 11:02:44 -0400 (Wed, 11 Aug 2010)
New Revision: 18557

Modified:
   trunk/core/impl/src/main/java/org/ajax4jsf/context/ContextInitParameters.java
   trunk/core/impl/src/main/java/org/richfaces/application/InitializationListener.java
   trunk/core/impl/src/main/java/org/richfaces/resource/ResourceHandlerImpl.java
Log:
https://jira.jboss.org/browse/RF-4471

Modified: trunk/core/impl/src/main/java/org/ajax4jsf/context/ContextInitParameters.java
===================================================================
--- trunk/core/impl/src/main/java/org/ajax4jsf/context/ContextInitParameters.java	2010-08-11 14:59:57 UTC (rev 18556)
+++ trunk/core/impl/src/main/java/org/ajax4jsf/context/ContextInitParameters.java	2010-08-11 15:02:44 UTC (rev 18557)
@@ -73,6 +73,8 @@
     
     public static final String STATIC_RESOURCE_LOCATION_VARIABLE = "resourceLocation";
 
+    public static final String EXECUTE_AWT_INITIALIZER = "org.richfaces.executeAWTInitializer";
+    
     private static final String[] RESOURCES_TTL_ARRAY = { RESOURCES_TTL };
     private static final String[] RESOURCES_CACHE_SIZE_ARRAY = { RESOURCES_CACHE_SIZE };
     
@@ -151,6 +153,11 @@
         return (String) evaluateInitParameter(context, STATIC_RESOURCE_LOCATION);
     }
     
+    public static boolean isExecuteAWTInitializer(FacesContext context) {
+        Object paramValue = evaluateInitParameter(context, EXECUTE_AWT_INITIALIZER);
+        return getBooleanValue(paramValue, true);
+    }
+    
     static int getInteger(FacesContext context, String[] paramNames, int defaultValue) {
         String initParameter = getInitParameter(context, paramNames);
 

Modified: trunk/core/impl/src/main/java/org/richfaces/application/InitializationListener.java
===================================================================
--- trunk/core/impl/src/main/java/org/richfaces/application/InitializationListener.java	2010-08-11 14:59:57 UTC (rev 18556)
+++ trunk/core/impl/src/main/java/org/richfaces/application/InitializationListener.java	2010-08-11 15:02:44 UTC (rev 18557)
@@ -21,16 +21,23 @@
  */
 package org.richfaces.application;
 
+import java.awt.Toolkit;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.text.MessageFormat;
 import java.util.ArrayList;
 
 import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
 import javax.faces.event.AbortProcessingException;
 import javax.faces.event.PostConstructApplicationEvent;
 import javax.faces.event.PreDestroyApplicationEvent;
 import javax.faces.event.SystemEvent;
 import javax.faces.event.SystemEventListener;
+import javax.imageio.ImageIO;
+import javax.imageio.stream.ImageInputStream;
 
+import org.ajax4jsf.context.ContextInitParameters;
 import org.richfaces.VersionBean;
 import org.richfaces.log.RichfacesLogger;
 import org.slf4j.Logger;
@@ -43,6 +50,53 @@
 
     private static final Logger LOGGER = RichfacesLogger.APPLICATION.getLogger();
     
+    private static final class AWTInitializer {
+
+        private static boolean checkGetSystemClassLoaderAccess() {
+            try {
+                ClassLoader.getSystemClassLoader();
+
+                return true;
+            } catch (SecurityException e) {
+                return false;
+            }
+        }
+        
+        public static void initialize() {
+            if (!checkGetSystemClassLoaderAccess()) {
+                LOGGER.warn("Access to system class loader restricted - AWTInitializer won't be run");
+                return;
+            }
+
+            Thread thread = Thread.currentThread();
+            ClassLoader initialTCCL = thread.getContextClassLoader();
+            ImageInputStream testStream = null;
+            
+            try {
+                ClassLoader systemCL = ClassLoader.getSystemClassLoader();
+                thread.setContextClassLoader(systemCL);
+                // set in-memory caching ImageIO
+                ImageIO.setUseCache(false);
+                
+                //force Disposer/AWT threads initialization
+                testStream = ImageIO.createImageInputStream(new ByteArrayInputStream(new byte[0]));
+                Toolkit.getDefaultToolkit().getSystemEventQueue();
+            } catch (IOException e) {
+                LOGGER.error(e.getMessage(), e);
+            } finally {
+                if (testStream != null) {
+                    try {
+                        testStream.close();
+                    } catch (IOException e) {
+                        LOGGER.error(e.getMessage(), e);
+                    }
+                }
+
+                thread.setContextClassLoader(initialTCCL);
+            }
+        }
+    }
+
     /* (non-Javadoc)
       * @see javax.faces.event.SystemEventListener#isListenerForSource(java.lang.Object)
       */
@@ -60,6 +114,15 @@
                 LOGGER.info(versionString);
             }
         }
+
+        FacesContext context = FacesContext.getCurrentInstance();
+        if (ContextInitParameters.isExecuteAWTInitializer(context)) {
+            try {
+                AWTInitializer.initialize();
+            } catch (NoClassDefFoundError e) {
+                LOGGER.warn(MessageFormat.format("There were problems loading class: {0} - AWTInitializer won't be run", e.getMessage()));
+            }
+        }
     }
 
     protected ServicesFactory createFactory() {

Modified: trunk/core/impl/src/main/java/org/richfaces/resource/ResourceHandlerImpl.java
===================================================================
--- trunk/core/impl/src/main/java/org/richfaces/resource/ResourceHandlerImpl.java	2010-08-11 14:59:57 UTC (rev 18556)
+++ trunk/core/impl/src/main/java/org/richfaces/resource/ResourceHandlerImpl.java	2010-08-11 15:02:44 UTC (rev 18557)
@@ -35,7 +35,6 @@
 import javax.faces.application.ResourceHandlerWrapper;
 import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
-import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -58,25 +57,6 @@
 
     private ResourceFactory resourceFactory;
     
-    // TODO - review - do we need this?
-    static {
-
-        // set in-memory caching ImageIO
-        Thread thread = Thread.currentThread();
-        ClassLoader initialTCCL = thread.getContextClassLoader();
-
-        try {
-            ClassLoader systemCL = ClassLoader.getSystemClassLoader();
-
-            thread.setContextClassLoader(systemCL);
-            ImageIO.setUseCache(false);
-        } catch (Exception e) {
-            LOGGER.error(e.getMessage(), e);
-        } finally {
-            thread.setContextClassLoader(initialTCCL);
-        }
-    }
-
     private ResourceHandler defaultHandler;
 
     public ResourceHandlerImpl(ResourceHandler defaultHandler) {



More information about the richfaces-svn-commits mailing list