You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2010/03/12 13:46:05 UTC

svn commit: r922243 - in /myfaces/core/trunk: api/src/main/java/javax/faces/context/ExternalContext.java api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java impl/src/main/java/org/apache/myfaces/context/FacesContextFactoryImpl.java

Author: jakobk
Date: Fri Mar 12 12:46:05 2010
New Revision: 922243

URL: http://svn.apache.org/viewvc?rev=922243&view=rev
Log:
MYFACES-2601 java.lang.NoSuchFieldException when MyFaces and Mojarra are on the classpath

Added:
    myfaces/core/trunk/api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java   (with props)
Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/context/ExternalContext.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/FacesContextFactoryImpl.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/context/ExternalContext.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/context/ExternalContext.java?rev=922243&r1=922242&r2=922243&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/context/ExternalContext.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/context/ExternalContext.java Fri Mar 12 12:46:05 2010
@@ -41,22 +41,6 @@ public abstract class ExternalContext
     public static final String FORM_AUTH = "FORM";
     
     /**
-     * This variable holds the firstInstance where all ExternalContext
-     * objects should call when new jsf 2.0 methods are called.
-     * 
-     * This variable is an implementation detail and should be 
-     * initialized and released on FacesContextFactoryImpl (because
-     * this is the place where ExternalContextFactory.getExternalContext()
-     * is called).
-     * 
-     * The objective of this is keep compatibility of libraries that wrap 
-     * ExternalContext objects before 2.0. It is similar as FacesContext._firstInstace,
-     * but since we don't have any place to init and release this variable properly
-     * we should do it using reflection.
-     */
-    private static ThreadLocal<ExternalContext> _firstInstance = new ThreadLocal<ExternalContext>();
-
-    /**
      *
      * @param name
      * @param value
@@ -66,7 +50,7 @@ public abstract class ExternalContext
      */
     public void addResponseCookie(String name, String value, Map<String, Object> properties)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -85,7 +69,7 @@ public abstract class ExternalContext
      */
     public void addResponseHeader(String name, String value)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -108,7 +92,7 @@ public abstract class ExternalContext
      */
     public String encodeBookmarkableURL(String baseUrl, Map<String,List<String>> parameters)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -127,7 +111,7 @@ public abstract class ExternalContext
     public String encodePartialActionURL(String url)
     {
         // TODO: IMPLEMENT IMPL
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -146,7 +130,7 @@ public abstract class ExternalContext
      */
     public String encodeRedirectURL(String baseUrl, Map<String,List<String>> parameters)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -173,7 +157,7 @@ public abstract class ExternalContext
      */
     public String getContextName()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -188,7 +172,7 @@ public abstract class ExternalContext
      */
     public Flash getFlash()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -208,7 +192,7 @@ public abstract class ExternalContext
      */
     public String getMimeType(String file)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -223,7 +207,7 @@ public abstract class ExternalContext
      */
     public String getRealPath(String path)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -239,7 +223,7 @@ public abstract class ExternalContext
 
     public String getRequestCharacterEncoding()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -257,7 +241,7 @@ public abstract class ExternalContext
      */
     public int getRequestContentLength()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -269,7 +253,7 @@ public abstract class ExternalContext
 
     public String getRequestContentType()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -306,7 +290,7 @@ public abstract class ExternalContext
      */
     public String getRequestScheme()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -321,7 +305,7 @@ public abstract class ExternalContext
      */
     public String getRequestServerName()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -336,7 +320,7 @@ public abstract class ExternalContext
      */
     public int getRequestServerPort()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -364,7 +348,7 @@ public abstract class ExternalContext
      */
     public int getResponseBufferSize()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -376,7 +360,7 @@ public abstract class ExternalContext
 
     public String getResponseCharacterEncoding()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -393,7 +377,7 @@ public abstract class ExternalContext
      */
     public String getResponseContentType()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -408,7 +392,7 @@ public abstract class ExternalContext
      */
     public OutputStream getResponseOutputStream() throws IOException
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -423,7 +407,7 @@ public abstract class ExternalContext
      */
     public Writer getResponseOutputWriter() throws IOException
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -444,7 +428,7 @@ public abstract class ExternalContext
      */
     public void invalidateSession()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -459,7 +443,7 @@ public abstract class ExternalContext
      */
     public boolean isResponseCommitted()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -491,7 +475,7 @@ public abstract class ExternalContext
      */
     public void responseFlushBuffer() throws IOException
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -507,7 +491,7 @@ public abstract class ExternalContext
      */
     public void responseReset()
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -527,7 +511,7 @@ public abstract class ExternalContext
      */
     public void responseSendError(int statusCode, String message) throws IOException
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -545,7 +529,7 @@ public abstract class ExternalContext
      */
     public void setRequest(Object request)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -565,7 +549,7 @@ public abstract class ExternalContext
     public void setRequestCharacterEncoding(java.lang.String encoding)
             throws java.io.UnsupportedEncodingException
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -583,7 +567,7 @@ public abstract class ExternalContext
      */
     public void setResponse(Object response)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -601,7 +585,7 @@ public abstract class ExternalContext
      */
     public void setResponseBufferSize(int size)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -619,7 +603,7 @@ public abstract class ExternalContext
      */
     public void setResponseCharacterEncoding(String encoding)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -637,7 +621,7 @@ public abstract class ExternalContext
      */
     public void setResponseContentLength(int length)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -655,7 +639,7 @@ public abstract class ExternalContext
      */
     public void setResponseContentType(String contentType)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -674,7 +658,7 @@ public abstract class ExternalContext
      */
     public void setResponseHeader(String name, String value)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {
@@ -692,7 +676,7 @@ public abstract class ExternalContext
      */
     public void setResponseStatus(int statusCode)
     {
-        ExternalContext ctx = _firstInstance.get();
+        ExternalContext ctx = _MyFacesExternalContextHelper._firstInstance.get();
         
         if (ctx == null)
         {

Added: myfaces/core/trunk/api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java?rev=922243&view=auto
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java (added)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java Fri Mar 12 12:46:05 2010
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package javax.faces.context;
+
+/**
+ * Stores the first instance of the ExternalContext in the
+ * field _firstInstance. We cannot put this field directly
+ * into ExternalContext, because this can cause classloading
+ * problems when accessing it from myfaces-impl when Mojarra is
+ * also on the classpath.
+ * 
+ * @author Jakob Korherr (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+final class _MyFacesExternalContextHelper
+{
+
+    /**
+     * This variable holds the firstInstance where all ExternalContext
+     * objects should call when new jsf 2.0 methods are called.
+     * 
+     * This variable is an implementation detail and should be 
+     * initialized and released on FacesContextFactoryImpl (because
+     * this is the place where ExternalContextFactory.getExternalContext()
+     * is called).
+     * 
+     * The objective of this is keep compatibility of libraries that wrap 
+     * ExternalContext objects before 2.0. It is similar as FacesContext._firstInstace,
+     * but since we don't have any place to init and release this variable properly
+     * we should do it using reflection.
+     */
+    static ThreadLocal<ExternalContext> _firstInstance = new ThreadLocal<ExternalContext>();
+    
+    /**
+     * this class should not be instantiated.
+     */
+    private _MyFacesExternalContextHelper()
+    {
+    }
+    
+}

Propchange: myfaces/core/trunk/api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/trunk/api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: myfaces/core/trunk/api/src/main/java/javax/faces/context/_MyFacesExternalContextHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/FacesContextFactoryImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/FacesContextFactoryImpl.java?rev=922243&r1=922242&r2=922243&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/FacesContextFactoryImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/FacesContextFactoryImpl.java Fri Mar 12 12:46:05 2010
@@ -33,6 +33,7 @@ import javax.faces.lifecycle.Lifecycle;
 import javax.servlet.ServletContext;
 
 import org.apache.myfaces.context.servlet.FacesContextImpl;
+import org.apache.myfaces.shared_impl.util.ClassUtils;
 
 /**
  * DOCUMENT ME!
@@ -71,7 +72,8 @@ public class FacesContextFactoryImpl ext
         ThreadLocal<ExternalContext> firstExternalContextInstance = null;
         try
         {
-            Field externalContextFirstInstance = ExternalContext.class.getDeclaredField("_firstInstance");
+            Class clazz = ClassUtils.classForName("javax.faces.context._MyFacesExternalContextHelper");
+            Field externalContextFirstInstance = clazz.getDeclaredField("_firstInstance");
             externalContextFirstInstance.setAccessible(true);
             
             if (externalContextFirstInstance != null)
@@ -87,13 +89,14 @@ public class FacesContextFactoryImpl ext
         {
             // It could happen, but we can ignore it.
             if (log.isLoggable(Level.FINE))
-                log.log(Level.FINE, "Cannot access private field _firstInstance from ExternalContext ",e);
+                log.log(Level.FINE, "Cannot access field _firstInstance"
+                        + "from _MyFacesExternalContextHelper ", e);
         }
         catch (Exception e)
         {
-            //It should not happen if we have only myfaces on classpath
             if (log.isLoggable(Level.SEVERE))
-                log.log(Level.SEVERE, "Cannot found private field _firstInstance from ExternalContext ",e);
+                log.log(Level.SEVERE, "Cannot find field _firstInstance"
+                        + "from _MyFacesExternalContextHelper ", e);
         }
         
         _firstExternalContextInstance = firstExternalContextInstance;