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/08 19:59:59 UTC

svn commit: r920454 - in /myfaces/core/trunk: ./ implee6/ implee6/src/ implee6/src/main/ implee6/src/main/java/ implee6/src/main/java/org/ implee6/src/main/java/org/apache/ implee6/src/main/java/org/apache/myfaces/ implee6/src/main/java/org/apache/myfa...

Author: jakobk
Date: Mon Mar  8 18:59:58 2010
New Revision: 920454

URL: http://svn.apache.org/viewvc?rev=920454&view=rev
Log:
MYFACES-2579 Support the dynamic adding of servlets and servlet-mappings on Servlet API 3.0 to make web.xml entries for Faces Servlet not mandatory

Added:
    myfaces/core/trunk/implee6/pom.xml   (with props)
    myfaces/core/trunk/implee6/src/
    myfaces/core/trunk/implee6/src/main/
    myfaces/core/trunk/implee6/src/main/java/
    myfaces/core/trunk/implee6/src/main/java/org/
    myfaces/core/trunk/implee6/src/main/java/org/apache/
    myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/
    myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/
    myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/MyFacesContainerInitializer.java   (with props)
    myfaces/core/trunk/implee6/src/main/resources/
    myfaces/core/trunk/implee6/src/main/resources/META-INF/
    myfaces/core/trunk/implee6/src/main/resources/META-INF/services/
    myfaces/core/trunk/implee6/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
    myfaces/core/trunk/implee6/src/test/
    myfaces/core/trunk/implee6/src/test/java/
    myfaces/core/trunk/implee6/src/test/java/org/
    myfaces/core/trunk/implee6/src/test/java/org/apache/
    myfaces/core/trunk/implee6/src/test/java/org/apache/myfaces/
    myfaces/core/trunk/implee6/src/test/resources/
Modified:
    myfaces/core/trunk/pom.xml

Added: myfaces/core/trunk/implee6/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/implee6/pom.xml?rev=920454&view=auto
==============================================================================
--- myfaces/core/trunk/implee6/pom.xml (added)
+++ myfaces/core/trunk/implee6/pom.xml Mon Mar  8 18:59:58 2010
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <artifactId>myfaces-core-project</artifactId>
+    <groupId>org.apache.myfaces.core</groupId>
+    <version>2.0.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.myfaces.core</groupId>
+  <artifactId>myfaces-impl-ee6</artifactId>
+  <packaging>jar</packaging>
+  <name>Apache MyFaces JSF-2.0 Core Impl for Java EE 6</name>
+  <description>
+    The private implementation classes of the Apache MyFaces Core JSF-2.0 Implementation which require Java EE 6
+  </description>
+  <url>http://myfaces.apache.org/core20/myfaces-impl</url>
+  <scm>
+    <connection>scm:svn:http://svn.apache.org/repos/asf/myfaces/core/trunk/implee6</connection>
+    <developerConnection>scm:svn:https://svn.apache.org/repos/asf/myfaces/core/trunk/implee6</developerConnection>
+    <url>http://svn.apache.org/repos/asf/myfaces/core/trunk/implee6</url>
+  </scm>
+  
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-source-plugin</artifactId>
+        <version>2.0.4</version>
+        <executions>
+          <execution>
+            <id>attach-source</id>
+            <goals>
+              <goal>jar</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.myfaces.core</groupId>
+      <artifactId>myfaces-api</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <!-- this dependency will be packed together with the main artifact of this pom -->
+      <groupId>org.apache.myfaces.shared</groupId>
+      <artifactId>myfaces-shared-impl</artifactId>
+      <version>${myfaces-shared.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax</groupId>
+      <artifactId>javaee-web-api</artifactId>
+      <version>6.0</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+  
+  <properties>
+    <myfaces-shared.version>4.0.1-SNAPSHOT</myfaces-shared.version>
+  </properties>
+  
+</project>

Propchange: myfaces/core/trunk/implee6/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/trunk/implee6/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: myfaces/core/trunk/implee6/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/MyFacesContainerInitializer.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/MyFacesContainerInitializer.java?rev=920454&view=auto
==============================================================================
--- myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/MyFacesContainerInitializer.java (added)
+++ myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/MyFacesContainerInitializer.java Mon Mar  8 18:59:58 2010
@@ -0,0 +1,166 @@
+/*
+ * 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.ee6;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.faces.application.ResourceDependencies;
+import javax.faces.application.ResourceDependency;
+import javax.faces.bean.ApplicationScoped;
+import javax.faces.bean.CustomScoped;
+import javax.faces.bean.ManagedBean;
+import javax.faces.bean.ManagedProperty;
+import javax.faces.bean.NoneScoped;
+import javax.faces.bean.ReferencedBean;
+import javax.faces.bean.RequestScoped;
+import javax.faces.bean.SessionScoped;
+import javax.faces.bean.ViewScoped;
+import javax.faces.component.FacesComponent;
+import javax.faces.component.behavior.FacesBehavior;
+import javax.faces.convert.FacesConverter;
+import javax.faces.event.ListenerFor;
+import javax.faces.event.ListenersFor;
+import javax.faces.event.NamedEvent;
+import javax.faces.render.FacesBehaviorRenderer;
+import javax.faces.render.FacesRenderer;
+import javax.faces.validator.FacesValidator;
+import javax.faces.webapp.FacesServlet;
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRegistration;
+import javax.servlet.annotation.HandlesTypes;
+
+import org.apache.myfaces.shared_impl.webapp.webxml.DelegatedFacesServlet;
+
+/**
+ * This class is called by any Java EE 6 complaint container at startup.
+ * It checks if the current webapp is a JSF-webapp by checking if some of 
+ * the JSF related annotations are specified in the webapp classpath or if
+ * the faces-config.xml file is present. If so, the listener checks if 
+ * the FacesServlet has already been defined in web.xml and if not, it adds
+ * the FacesServlet with the mappings (/faces/*, *.jsf, *.faces) dynamically.
+ * 
+ * @author Jakob Korherr (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+@HandlesTypes({
+        ApplicationScoped.class,
+        CustomScoped.class,
+        FacesBehavior.class,
+        FacesBehaviorRenderer.class,
+        FacesComponent.class,
+        FacesConverter.class,
+        FacesRenderer.class,
+        FacesValidator.class,
+        ListenerFor.class,
+        ListenersFor.class,
+        ManagedBean.class,
+        ManagedProperty.class,
+        NamedEvent.class,
+        NoneScoped.class,
+        ReferencedBean.class,
+        RequestScoped.class,
+        ResourceDependencies.class,
+        ResourceDependency.class,
+        SessionScoped.class,
+        ViewScoped.class
+    })
+public class MyFacesContainerInitializer implements ServletContainerInitializer
+{
+
+    /**
+     * If the servlet mapping for the FacesServlet is added dynamically, Boolean.TRUE 
+     * is stored under this key in the ServletContext.
+     * ATTENTION: this constant is duplicate in AbstractFacesInitializer.
+     */
+    private static final String FACES_SERVLET_ADDED_ATTRIBUTE = "org.apache.myfaces.DYNAMICALLY_ADDED_FACES_SERVLET";
+    
+    private static final String FACES_CONFIG_RESOURCE = "/WEB-INF/faces-config.xml";
+    private static final Logger log = Logger.getLogger(MyFacesContainerInitializer.class.getName());
+    
+    public void onStartup(Set<Class<?>> clazzes, ServletContext servletContext) throws ServletException
+    {
+        if ((clazzes != null && !clazzes.isEmpty()) || isFacesConfigPresent(servletContext))
+        {
+            // look for the FacesServlet
+            Map<String, ? extends ServletRegistration> servlets = servletContext.getServletRegistrations();
+            for (Map.Entry<String, ? extends ServletRegistration> servletEntry : servlets.entrySet())
+            {
+                String className = servletEntry.getValue().getClassName();
+                if (FacesServlet.class.getName().equals(className)
+                        || isDelegatedFacesServlet(className))
+                {
+                    // we found a FacesServlet, so we have nothing to do!
+                    return;
+                }
+            }
+            
+            // the FacesServlet is not installed yet - install it
+            ServletRegistration.Dynamic servlet = servletContext.addServlet("FacesServlet", FacesServlet.class);
+            servlet.addMapping("/faces/*", "*.jsf", "*.faces");
+            
+            // now we have to set a field in the ServletContext to indicate that we have
+            // added the mapping dynamically, because MyFaces just parsed the web.xml to
+            // find mappings and thus it would abort initializing
+            servletContext.setAttribute(FACES_SERVLET_ADDED_ATTRIBUTE, Boolean.TRUE);
+            
+            // add a log message
+            log.log(Level.INFO, "Added FacesServlet with mappings /faces/*, *.jsf and *.faces");
+        }
+    }
+    
+    /**
+     * Checks if /WEB-INF/faces-config.xml is present.
+     * @return
+     */
+    private boolean isFacesConfigPresent(ServletContext servletContext)
+    {
+        try
+        {
+            return servletContext.getResource(FACES_CONFIG_RESOURCE) != null;
+        }
+        catch (Exception e)
+        {
+            return false;
+        }
+    }
+    
+    /**
+     * Checks if the class represented by className implements DelegatedFacesServlet.
+     * @param className
+     * @return
+     */
+    private boolean isDelegatedFacesServlet(String className)
+    {
+        try
+        {
+            Class<?> clazz = Class.forName(className);
+            return DelegatedFacesServlet.class.isAssignableFrom(clazz);
+        } 
+        catch (ClassNotFoundException cnfe)
+        {
+            return false;
+        }
+    }
+
+}

Propchange: myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/MyFacesContainerInitializer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/MyFacesContainerInitializer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: myfaces/core/trunk/implee6/src/main/java/org/apache/myfaces/ee6/MyFacesContainerInitializer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: myfaces/core/trunk/implee6/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/implee6/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer?rev=920454&view=auto
==============================================================================
--- myfaces/core/trunk/implee6/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer (added)
+++ myfaces/core/trunk/implee6/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer Mon Mar  8 18:59:58 2010
@@ -0,0 +1 @@
+org.apache.myfaces.ee6.MyFacesContainerInitializer
\ No newline at end of file

Modified: myfaces/core/trunk/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/pom.xml?rev=920454&r1=920453&r2=920454&view=diff
==============================================================================
--- myfaces/core/trunk/pom.xml (original)
+++ myfaces/core/trunk/pom.xml Mon Mar  8 18:59:58 2010
@@ -30,6 +30,7 @@
   <modules>
     <!-- module>build</module -->
     <module>api</module>
+    <module>implee6</module>
     <module>impl</module>
   </modules>
   <repositories>