[jboss-cvs] JBossAS SVN: r76707 - in trunk: testsuite/imports/sections and 4 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Aug 6 08:43:02 EDT 2008


Author: stan.silvert at jboss.com
Date: 2008-08-06 08:43:02 -0400 (Wed, 06 Aug 2008)
New Revision: 76707

Added:
   trunk/tomcat/src/main/org/jboss/web/jsf/integration/config/FaceletsLoggers.java
Modified:
   trunk/component-matrix/pom.xml
   trunk/testsuite/imports/sections/jsf.xml
   trunk/testsuite/src/main/org/jboss/test/jsf/webapp/LoggingTest.java
   trunk/testsuite/src/main/org/jboss/test/web/test/JSFIntegrationUnitTestCase.java
   trunk/thirdparty/pom.xml
   trunk/tomcat/src/main/org/jboss/web/jsf/integration/config/JBossJSFConfigureListener.java
Log:
JBAS-5832 Add Facelets logging bridge to AS


Modified: trunk/component-matrix/pom.xml
===================================================================
--- trunk/component-matrix/pom.xml	2008-08-06 10:56:37 UTC (rev 76706)
+++ trunk/component-matrix/pom.xml	2008-08-06 12:43:02 UTC (rev 76707)
@@ -319,6 +319,12 @@
       </dependency>
       
       <dependency>
+        <groupId>com.sun.facelets</groupId>
+        <artifactId>jsf-facelets</artifactId>
+        <version>1.1.15.B1</version>
+      </dependency>
+      
+      <dependency>
         <groupId>dom4j</groupId>
         <artifactId>dom4j</artifactId>
         <version>1.6.1</version>

Modified: trunk/testsuite/imports/sections/jsf.xml
===================================================================
--- trunk/testsuite/imports/sections/jsf.xml	2008-08-06 10:56:37 UTC (rev 76706)
+++ trunk/testsuite/imports/sections/jsf.xml	2008-08-06 12:43:02 UTC (rev 76707)
@@ -18,6 +18,26 @@
        </webinf>
      </war>
 
+     <!-- make WAR for testing Facelets logging -->
+     <war destfile="${build.lib}/jbosstest-jsf-facelets.war"
+       webxml="${build.resources}/web/WEB-INF/jsftest-web.xml">
+       <fileset dir="${build.resources}/web/html/jsftest">
+         <include name="**/*.jsp"/>
+       </fileset>
+
+       <classes dir="${build.classes}">
+          <include name="org/jboss/test/jsf/webapp/**"/>
+       </classes>
+
+       <lib dir="${com.sun.facelets.lib}">
+          <include name="jsf-facelets.jar"/>
+       </lib>
+       
+       <webinf dir="${build.resources}/web/html/jsftest/WEB-INF">
+         <include name="**/*"/>
+       </webinf>
+     </war>
+     
      <!-- make WAR for testing legacy WARs that bundle MyFaces -->
      <war destfile="${build.lib}/bundled-myfaces-hellojsf.war"
        webxml="${build.resources}/web/WEB-INF/bundled-myfaces-jsf.xml">

Modified: trunk/testsuite/src/main/org/jboss/test/jsf/webapp/LoggingTest.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/jsf/webapp/LoggingTest.java	2008-08-06 10:56:37 UTC (rev 76706)
+++ trunk/testsuite/src/main/org/jboss/test/jsf/webapp/LoggingTest.java	2008-08-06 12:43:02 UTC (rev 76707)
@@ -25,10 +25,12 @@
 import com.sun.faces.util.FacesLogger;
 import org.apache.log4j.*;
 import java.io.ByteArrayOutputStream;
+import java.lang.reflect.Method;
+import java.util.Iterator;
 
 /**
  * This class simulates the kind of logging that the JSF implementation
- * does.  
+ * does.  It also simulates logging through the Facelets loggers.
  *
  * @author Stan Silvert
  */
@@ -46,6 +48,8 @@
       Appender appender = new WriterAppender(new SimpleLayout(), out);
       Logger log4jLogger = Logger.getLogger("javax.enterprise.resource.webcontainer.jsf");
       log4jLogger.addAppender(appender);
+      Logger faceletsLogger = Logger.getLogger("facelets");
+      faceletsLogger.addAppender(appender);
    }
 
    /**
@@ -99,7 +103,34 @@
 
       julLogger = FacesLogger.MANAGEDBEAN.getLogger();
       julLogger.finest("Logged FINEST message in MANAGEDBEAN_LOGGER Logger<br/>"); // logged
+      
+      sendFaceletsLogMessages();
    }
+   
+   private void sendFaceletsLogMessages()
+   {
+      for (Iterator loggers = loadLoggers(); loggers.hasNext();)
+      {
+         java.util.logging.Logger julLogger = (java.util.logging.Logger)loggers.next();
+         julLogger.info("Here is a message for " + julLogger.getName() + "<br/>");
+      }
+   }
+   
+   // Loading FaceletsLoggers by reflection is much easier than trying to
+   // figure out how to link jboss-faces.jar with the testsuite build.
+   private Iterator loadLoggers()
+   {
+      try
+      {
+         Class faceletsLoggersClass = Thread.currentThread().getContextClassLoader().loadClass("org.jboss.web.jsf.integration.config.FaceletsLoggers");
+         Method method = faceletsLoggersClass.getMethod("getLoggers", null);
+         return (Iterator)method.invoke(null, null);
+      }
+      catch (Exception e)
+      {
+         throw new IllegalStateException("Unable to call FaceletsLoggers.getLoggers()", e);
+      }
+   }
 
    /**
     * Return the contents of the in-memory Log4J appender.

Modified: trunk/testsuite/src/main/org/jboss/test/web/test/JSFIntegrationUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/web/test/JSFIntegrationUnitTestCase.java	2008-08-06 10:56:37 UTC (rev 76706)
+++ trunk/testsuite/src/main/org/jboss/test/web/test/JSFIntegrationUnitTestCase.java	2008-08-06 12:43:02 UTC (rev 76707)
@@ -84,19 +84,8 @@
     */
    public void testJSFIntegrated() throws Exception
    {
-      HttpClient client = new HttpClient();
-      client.executeMethod(makeRequest());
-      
-      HttpMethodBase result = makeRequest();
+      String responseBody = getResponseBody("jbosstest-jsf");
 
-      // need to hit it twice with the same session for test to pass
-      client.executeMethod(result);
-
-      String responseBody = result.getResponseBodyAsString();
-      if (responseBody == null) {
-         throw new Exception("Unable to get response from server.");
-      }
-
       assertTrue(contains(responseBody, "@PostConstruct was called."));
       assertTrue(contains(responseBody, "@PreDestroy was called."));
       assertTrue(contains(responseBody, "Datasource was injected."));
@@ -126,14 +115,48 @@
       assertTrue(contains(responseBody, "Logged FINEST message in MANAGEDBEAN_LOGGER"));
       
    }   
+   
+   public void testNoFaceletsLogging() throws Exception 
+   {
+      String responseBody = getResponseBody("jbosstest-jsf");
+      assertFalse(contains(responseBody, "facelets.compiler"));
+   }
+   
+   public void testFaceletsLogging() throws Exception
+   {
+      String responseBody = getResponseBody("jbosstest-jsf-facelets");
+      assertTrue(contains(responseBody, "facelets.compiler"));
+      assertTrue(contains(responseBody, "facelets.factory"));
+      assertTrue(contains(responseBody, "facelets.tag.component"));
+      assertTrue(contains(responseBody, "facelets.viewhandler"));
+      assertTrue(contains(responseBody, "facelets.tag.meta"));
+   }
+           
+   private String getResponseBody(String warName) throws Exception
+   {
+      HttpClient client = new HttpClient();
+      client.executeMethod(makeRequest(warName));
+      
+      HttpMethodBase result = makeRequest(warName);
 
+      // need to hit it twice with the same session for test to pass
+      client.executeMethod(result);
+
+      String responseBody = result.getResponseBodyAsString();
+      if (responseBody == null) {
+         throw new Exception("Unable to get response from server.");
+      }
+      
+      return responseBody;
+   }
+
    private boolean contains(String base, String target) {
       return base.indexOf(target) != -1;
    }
 
-   private GetMethod makeRequest() {
+   private GetMethod makeRequest(String warName) {
       String baseURL = HttpUtils.getBaseURL();
-      return new GetMethod(baseURL+"jbosstest-jsf/index.jsf");
+      return new GetMethod(baseURL + warName + "/index.jsf");
    }
 
    public static Test suite() throws Exception
@@ -150,10 +173,12 @@
          protected void setUp() throws Exception
          {
             super.setUp();
-            deploy("jbosstest-jsf.war");           
+            deploy("jbosstest-jsf.war");   
+            deploy("jbosstest-jsf-facelets.war");
          }
          protected void tearDown() throws Exception
          {
+            undeploy("jbosstest-jsf-facelets.war");
             undeploy("jbosstest-jsf.war");
             super.tearDown();            
          }

Modified: trunk/thirdparty/pom.xml
===================================================================
--- trunk/thirdparty/pom.xml	2008-08-06 10:56:37 UTC (rev 76706)
+++ trunk/thirdparty/pom.xml	2008-08-06 12:43:02 UTC (rev 76707)
@@ -1676,6 +1676,10 @@
       <groupId>javax.faces</groupId>
       <artifactId>jsf-api</artifactId>
     </dependency>
+    <dependency>
+        <groupId>com.sun.facelets</groupId>
+        <artifactId>jsf-facelets</artifactId>
+    </dependency>
     
     <dependency>
       <groupId>sun-jaxws</groupId>

Added: trunk/tomcat/src/main/org/jboss/web/jsf/integration/config/FaceletsLoggers.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/jsf/integration/config/FaceletsLoggers.java	                        (rev 0)
+++ trunk/tomcat/src/main/org/jboss/web/jsf/integration/config/FaceletsLoggers.java	2008-08-06 12:43:02 UTC (rev 76707)
@@ -0,0 +1,167 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.web.jsf.integration.config;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.logging.Logger;
+
+/**
+ * This class provides access to the loggers used by Facelets version 1.1.15.
+ *
+ * @author Stan Silvert
+ * @author Pete Muir
+ */
+public class FaceletsLoggers 
+{
+    
+   // Don't allow an instance of this class
+   private FaceletsLoggers() {}
+    
+   /**
+    * Determine if Facelets is in the classpath.
+    *
+    * @return <code>true</code> if Facelets is available,
+    *         <code>false</code> otherwise.
+    */
+   public static boolean isFaceletsAvailable()
+   {
+      try
+      {
+         classForName("com.sun.facelets.Facelet");
+         return true;
+      } 
+      catch (ClassNotFoundException e)
+      {
+         return false;
+      }
+   }
+   
+   /**
+    * Return an Iterator of all the java.util.logging.Logger objects used
+    * in Facelets.
+    *
+    * @return The Loggers, or an empty Iterator if Facelets is not available.
+    */
+   public static Iterator<Logger> getLoggers() throws Exception
+   {
+      List<Logger> loggers = new ArrayList<Logger>();
+      
+      if (!isFaceletsAvailable()) return loggers.iterator();
+      
+      // Gah have to do this by reflection as the loggers are protected
+         
+      // And some aren't static, so this really is best effort
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.compiler.TagLibraryConfig", "log"));
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.compiler.Compiler", "log"));
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.impl.DefaultFaceletFactory", "log"));
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.tag.jsf.ComponentHandler", "log"));
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.util.Resource", "log"));
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.FaceletViewHandler", "log"));
+
+      // These ones are in a package-scoped class
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.compiler.CompilationManager", "log"));
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.tag.jsf.ComponentRule", "log"));
+      loggers.add(getPrivateStaticLogger("com.sun.facelets.tag.MetaRulesetImpl", "log"));
+         
+      return loggers.iterator();
+   }
+   
+   private static Logger getPrivateStaticLogger(Class clazz, String fieldName) throws Exception 
+   {
+      Field field = getField(clazz, fieldName);
+      field.setAccessible(true);
+      return (Logger) get(field, new Object());
+   }
+   
+   private static Logger getPrivateStaticLogger(String className, String fieldName) throws Exception
+   {
+      return getPrivateStaticLogger(classForName(className), fieldName);
+   }
+   
+   // Code copied from org.jboss.seam.util.Reflctions
+   private static Object get(Field field, Object target) throws Exception
+   {
+      try
+      {
+         return field.get(target);
+      }
+      catch (IllegalArgumentException iae)
+      {
+         String message = "Could not get field value by reflection: " + toString(field) + 
+            " on: " + target.getClass().getName();
+         throw new IllegalArgumentException(message, iae);
+      }
+   }
+   
+   // Code copied from org.jboss.seam.util.Reflctions
+   private static Field getField(Class clazz, String name)
+   {
+      for ( Class superClass = clazz; superClass!=Object.class; superClass=superClass.getSuperclass() )
+      {
+         try
+         {
+            return superClass.getDeclaredField(name);
+         }
+         catch (NoSuchFieldException nsfe) {}
+      }
+      throw new IllegalArgumentException("no such field: " + clazz.getName() + '.' + name);
+   }
+   
+   // Code copied from org.jboss.seam.util.Reflctions
+   private static Class classForName(String name) throws ClassNotFoundException
+   {
+      try 
+      {
+         return Thread.currentThread().getContextClassLoader().loadClass(name);
+      }
+      catch (Exception e)
+      {
+         return Class.forName(name);
+      }
+   }
+   
+   // Code copied from org.jboss.seam.util.Reflctions
+   private static String toString(Member member)
+   {
+      return unqualify( member.getDeclaringClass().getName() ) + 
+            '.' + 
+            member.getName();
+   }
+   
+   // Code copied from org.jboss.seam.util.Strings
+   private static String unqualify(String name)
+   {
+      return unqualify(name, '.');
+   }
+   
+   // Code copied from org.jboss.seam.util.Strings
+   private static String unqualify(String name, char sep)
+   {
+      return name.substring( name.lastIndexOf(sep)+1, name.length() );
+   }
+   
+}

Modified: trunk/tomcat/src/main/org/jboss/web/jsf/integration/config/JBossJSFConfigureListener.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/jsf/integration/config/JBossJSFConfigureListener.java	2008-08-06 10:56:37 UTC (rev 76706)
+++ trunk/tomcat/src/main/org/jboss/web/jsf/integration/config/JBossJSFConfigureListener.java	2008-08-06 12:43:02 UTC (rev 76707)
@@ -24,6 +24,7 @@
 
 import com.sun.faces.config.ConfigureListener;
 import com.sun.faces.util.FacesLogger;
+import java.util.Iterator;
 import java.util.logging.Filter;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
@@ -64,6 +65,7 @@
         if (System.getProperty("org.jboss.logging.Logger.pluginClass") == null) 
         {
             setLog4J();
+            setLog4JForFacelets();
         }
 
         checkForMyFaces();
@@ -140,6 +142,25 @@
             julLogger.setLevel(java.util.logging.Level.ALL);
     }
 
+    private void setLog4JForFacelets()
+    {
+       Filter conversionFilter = new Log4JConversionFilter(logConfigMessages());
+
+       try
+       {  // if Facelets is not used, getLoggers() returns an empty Iterator
+          for (Iterator<java.util.logging.Logger> loggers = FaceletsLoggers.getLoggers(); loggers.hasNext();)
+          {
+             java.util.logging.Logger julLogger = loggers.next();
+             setLevel(julLogger);
+             julLogger.setFilter(conversionFilter);
+          }
+       }
+       catch (Exception e)
+       {
+          LOG.warn("Unable to convert Facelets logging to Log4J", e);
+       }
+    }
+    
     /**
      * If Log4J is being used, set a filter that converts JSF RI java.util.logger
      * messages to Log4J messages.




More information about the jboss-cvs-commits mailing list