You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mb...@apache.org on 2007/03/04 01:15:01 UTC

svn commit: r514285 - in /myfaces/core/branches/jsf12/impl/src: main/java/org/apache/myfaces/webapp/ test/java/org/apache/myfaces/webapp/

Author: mbr
Date: Sat Mar  3 16:15:00 2007
New Revision: 514285

URL: http://svn.apache.org/viewvc?view=rev&rev=514285
Log:
Add ELContextListener to initialize created ELContext from Jsp
+Tests

Added:
    myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/FacesELContextListener.java   (with props)
    myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/
    myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/DefaultFacesInitializerTest.java   (with props)
    myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/FacesELContextListenerTest.java   (with props)
Modified:
    myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/DefaultFacesInitializer.java

Modified: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/DefaultFacesInitializer.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/DefaultFacesInitializer.java?view=diff&rev=514285&r1=514284&r2=514285
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/DefaultFacesInitializer.java (original)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/DefaultFacesInitializer.java Sat Mar  3 16:15:00 2007
@@ -18,6 +18,17 @@
  */
 package org.apache.myfaces.webapp;
 
+import java.util.Iterator;
+import java.util.List;
+
+import javax.faces.FactoryFinder;
+import javax.faces.context.ExternalContext;
+import javax.faces.event.PhaseListener;
+import javax.faces.lifecycle.LifecycleFactory;
+import javax.servlet.ServletContext;
+import javax.servlet.jsp.JspApplicationContext;
+import javax.servlet.jsp.JspFactory;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.myfaces.application.ApplicationImpl;
@@ -33,17 +44,6 @@
 import org.apache.myfaces.shared_impl.util.StateUtils;
 import org.apache.myfaces.shared_impl.webapp.webxml.WebXml;
 
-import javax.faces.FactoryFinder;
-import javax.faces.context.ExternalContext;
-import javax.faces.event.PhaseListener;
-import javax.faces.lifecycle.LifecycleFactory;
-import javax.servlet.ServletContext;
-import javax.servlet.jsp.JspApplicationContext;
-import javax.servlet.jsp.JspFactory;
-
-import java.util.Iterator;
-import java.util.List;
-
 /**
  * @author Mathias Broekelmann (latest modification by $Author$)
  * @version $Revision$ $Date$
@@ -51,6 +51,7 @@
 public class DefaultFacesInitializer implements FacesInitializer
 {
     private static final Log log = LogFactory.getLog(DefaultFacesInitializer.class);
+    private JspFactory _jspFactory;
 
     protected ELResolverBuilder createResolverBuilderForJSP(RuntimeConfig runtimeConfig)
     {
@@ -72,18 +73,23 @@
             {
                 Class.forName("org.apache.jasper.compiler.JspRuntimeContext");
             }
-            catch (Exception e)
+            catch (ClassNotFoundException e)
             {
-                e.printStackTrace();
+                // ignore
+            }
+            catch (Exception e) {
+                log.debug(e.getMessage(), e);
             }
 
-            JspFactory jspFactory = JspFactory.getDefaultFactory();
+            JspFactory jspFactory = getJspFactory();
             if (log.isDebugEnabled())
             {
                 log.debug("jspfactory = " + jspFactory);
             }
             JspApplicationContext appCtx = jspFactory.getJspApplicationContext(servletContext);
 
+            appCtx.addELContextListener(new FacesELContextListener());
+
             RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(externalContext);
             runtimeConfig.setExpressionFactory(appCtx.getExpressionFactory());
 
@@ -110,6 +116,23 @@
         {
             log.error("Error initializing MyFaces: " + ex.getMessage(), ex);
         }
+    }
+
+    protected JspFactory getJspFactory()
+    {
+        if (_jspFactory == null)
+        {
+            return JspFactory.getDefaultFactory();
+        }
+        return _jspFactory;
+    }
+    
+    /**
+     * @param jspFactory the jspFactory to set
+     */
+    public void setJspFactory(JspFactory jspFactory)
+    {
+        _jspFactory = jspFactory;
     }
 
     /**

Added: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/FacesELContextListener.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/FacesELContextListener.java?view=auto&rev=514285
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/FacesELContextListener.java (added)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/FacesELContextListener.java Sat Mar  3 16:15:00 2007
@@ -0,0 +1,48 @@
+/*
+ * 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 org.apache.myfaces.webapp;
+
+import javax.el.ELContextEvent;
+import javax.el.ELContextListener;
+import javax.faces.context.FacesContext;
+
+/**
+ * EL context listener which installs the faces context (if present) into el context and dispatches el context events to
+ * faces application el context listeners.
+ * 
+ * @author Mathias Broekelmann (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class FacesELContextListener implements ELContextListener
+{
+    public void contextCreated(ELContextEvent ece)
+    {
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        if (facesContext != null)
+        {
+            ece.getELContext().putContext(FacesContext.class, facesContext);
+
+            for (ELContextListener listener : facesContext.getApplication().getELContextListeners())
+            {
+                listener.contextCreated(ece);
+            }
+        }
+    }
+
+}

Propchange: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/FacesELContextListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/FacesELContextListener.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/DefaultFacesInitializerTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/DefaultFacesInitializerTest.java?view=auto&rev=514285
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/DefaultFacesInitializerTest.java (added)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/DefaultFacesInitializerTest.java Sat Mar  3 16:15:00 2007
@@ -0,0 +1,97 @@
+/*
+ * 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 org.apache.myfaces.webapp;
+
+import static org.easymock.EasyMock.*;
+
+import javax.el.ExpressionFactory;
+import javax.faces.webapp.FacesServlet;
+import javax.servlet.ServletContext;
+import javax.servlet.jsp.JspApplicationContext;
+import javax.servlet.jsp.JspFactory;
+
+import junit.framework.TestCase;
+
+import org.apache.myfaces.config.RuntimeConfig;
+import org.apache.myfaces.el.DefaultPropertyResolver;
+import org.apache.myfaces.el.VariableResolverImpl;
+import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver;
+import org.easymock.IAnswer;
+import org.easymock.classextension.EasyMock;
+import org.easymock.classextension.IMocksControl;
+
+/**
+ * @author Mathias Broekelmann (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class DefaultFacesInitializerTest extends TestCase
+{
+
+    /**
+     * Test method for {@link org.apache.myfaces.webapp.DefaultFacesInitializer#initFaces(javax.servlet.ServletContext)}.
+     * @throws Exception 
+     */
+    public void testInitFaces() throws Exception
+    {
+        DefaultFacesInitializer initializer = new DefaultFacesInitializer();
+        IMocksControl control = EasyMock.createControl();
+        
+        JspFactory jspFactory = control.createMock(JspFactory.class);
+        initializer.setJspFactory(jspFactory);
+        
+        RuntimeConfig runtimeConfig = control.createMock(RuntimeConfig.class);
+        
+        ServletContext context = control.createMock(ServletContext.class);
+        ExpressionFactory expressionFactory = control.createMock(ExpressionFactory.class);
+        runtimeConfig.setExpressionFactory(expressionFactory);
+        runtimeConfig.setPropertyResolverChainHead(isA(DefaultPropertyResolver.class));
+        runtimeConfig.setVariableResolverChainHead(isA(VariableResolverImpl.class));
+        
+        expect(context.getAttribute(eq(RuntimeConfig.class.getName()))).andReturn(runtimeConfig).anyTimes();
+        
+        expect(context.getInitParameter(eq(FacesServlet.CONFIG_FILES_ATTR))).andReturn(null);
+        expect(context.getResourceAsStream(eq("/WEB-INF/faces-config.xml"))).andReturn(null);
+        expect(context.getInitParameter(eq(FacesServlet.LIFECYCLE_ID_ATTR))).andReturn(null);
+
+        // TODO: add myfaces specific tests
+        expect(context.getResource(isA(String.class))).andReturn(null);
+        expect(context.getResourceAsStream(isA(String.class))).andReturn(null);
+        expect(context.getInitParameter(isA(String.class))).andReturn(null).anyTimes();
+        expect(context.getAttribute(isA(String.class))).andReturn(null).anyTimes();
+        context.setAttribute(isA(String.class), anyObject());
+        expectLastCall().anyTimes();
+        expect(context.getRealPath(isA(String.class))).andAnswer(new IAnswer<String>() {
+            public String answer() throws Throwable
+            {
+                return (String) org.easymock.EasyMock.getCurrentArguments()[0];
+            }
+        });
+        
+        JspApplicationContext jspAppCtx = control.createMock(JspApplicationContext.class);
+        expect(jspAppCtx.getExpressionFactory()).andReturn(expressionFactory);
+        jspAppCtx.addELContextListener(isA(FacesELContextListener.class));
+        expect(jspFactory.getJspApplicationContext(eq(context))).andReturn(jspAppCtx);
+        jspAppCtx.addELResolver(isA(FacesCompositeELResolver.class));
+
+        control.replay();
+        initializer.initFaces(context);
+        control.verify();
+    }
+
+}

Propchange: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/DefaultFacesInitializerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/DefaultFacesInitializerTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/FacesELContextListenerTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/FacesELContextListenerTest.java?view=auto&rev=514285
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/FacesELContextListenerTest.java (added)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/FacesELContextListenerTest.java Sat Mar  3 16:15:00 2007
@@ -0,0 +1,64 @@
+/*
+ * 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 org.apache.myfaces.webapp;
+
+import static org.easymock.EasyMock.*;
+
+import javax.el.ELContext;
+import javax.el.ELContextEvent;
+import javax.el.ELContextListener;
+import javax.faces.application.Application;
+import javax.faces.context.FacesContext;
+
+import junit.framework.TestCase;
+
+import org.apache.shale.test.mock.MockFacesContext12;
+import org.easymock.IMocksControl;
+import org.easymock.classextension.EasyMock;
+
+/**
+ * @author Mathias Broekelmann (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class FacesELContextListenerTest extends TestCase
+{
+
+    /**
+     * Test method for {@link org.apache.myfaces.webapp.FacesELContextListener#contextCreated(javax.el.ELContextEvent)}.
+     */
+    public void testContextCreated()
+    {
+        FacesELContextListener listener = new FacesELContextListener();
+        IMocksControl mockControl = EasyMock.createControl();
+        ELContext elctx = mockControl.createMock(ELContext.class);
+        MockFacesContext12 facesctx = new MockFacesContext12();
+        Application app = mockControl.createMock(Application.class);
+        facesctx.setApplication(app);
+        ELContextEvent event = mockControl.createMock(ELContextEvent.class);
+        expect(event.getELContext()).andReturn(elctx);
+        elctx.putContext(eq(FacesContext.class), same(facesctx));
+        ELContextListener elctxListener = mockControl.createMock(ELContextListener.class);
+        expect(app.getELContextListeners()).andReturn(new ELContextListener[] { elctxListener });
+        elctxListener.contextCreated(same(event));
+        mockControl.replay();
+        listener.contextCreated(event);
+        mockControl.verify();
+    }
+
+}

Propchange: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/FacesELContextListenerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/webapp/FacesELContextListenerTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL