portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r906988 - in /portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed: container/JetspeedContainerServlet.java util/JetspeedLoggerUtil.java
Date Fri, 05 Feb 2010 16:13:36 GMT
Author: woonsan
Date: Fri Feb  5 16:13:35 2010
New Revision: 906988

URL: http://svn.apache.org/viewvc?rev=906988&view=rev
Log:
JS2-1099: Making PA's error logs left in PA's log file.

Modified:
    portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/container/JetspeedContainerServlet.java
    portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/JetspeedLoggerUtil.java

Modified: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/container/JetspeedContainerServlet.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/container/JetspeedContainerServlet.java?rev=906988&r1=906987&r2=906988&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/container/JetspeedContainerServlet.java
(original)
+++ portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/container/JetspeedContainerServlet.java
Fri Feb  5 16:13:35 2010
@@ -61,8 +61,12 @@
 public class JetspeedContainerServlet extends HttpServlet 
 {
     private static final long serialVersionUID = -7900846019170204195L;
+    
+    private JetspeedLogger paLogger;
+    
     private boolean started = false;
     private PortalSessionsManager psm;
+    
     // default visibility for more optimal access by the startTimer
     Timer startTimer = null;
     String contextName;
@@ -82,6 +86,8 @@
     
     public synchronized final void init(ServletConfig config) throws ServletException
     {
+        paLogger = null;
+        
         synchronized (this.getClass())
         {            
             super.init(config);
@@ -109,21 +115,20 @@
                 throw new ServletException(JCS + "Portlet Application contextPath must start
with a  \"/\"."); 
             }
             
-            
             String paDir = context.getRealPath("/");
             if ( paDir == null )
-                {
-              throw new ServletException(JCS + " Initialization of PortletApplication at
"+contextName+" without access to its real path not supported");
-                }
-
-            JetspeedLogger jsLogger = JetspeedLoggerUtil.getLogger(getClass());
+            {
+                throw new ServletException(JCS + " Initialization of PortletApplication at
"+contextName+" without access to its real path not supported");
+            }
+            
+            JetspeedLogger jsLogger = JetspeedLoggerUtil.getSharedLogger(getClass());
             jsLogger.info(INIT_START_MSG + contextName);
             context.log(INIT_START_MSG + contextName);            
             System.out.println(INIT_START_MSG + contextName);            
 
             try
             {                
-              startPortletApplication(context, paDir, Thread.currentThread().getContextClassLoader());
+                startPortletApplication(context, paDir, Thread.currentThread().getContextClassLoader());
             }
             catch (Exception e)
             {
@@ -133,7 +138,7 @@
                 System.err.println(message);
                 throw new ServletException(message, e);
             }
-
+            
             jsLogger.info(INIT_DONE_MSG + contextName);
             context.log(INIT_DONE_MSG + contextName);
             System.out.println(INIT_DONE_MSG + contextName);
@@ -155,7 +160,7 @@
         }
 */
         final String START_DELAYED_MSG = JCS + "Could not yet start portlet application at:
"+contextName+". Starting back ground thread to start when the portal comes online.";
-        JetspeedLogger jsLogger = JetspeedLoggerUtil.getLogger(getClass());
+        JetspeedLogger jsLogger = JetspeedLoggerUtil.getSharedLogger(getClass());
         jsLogger.info(START_DELAYED_MSG);
         context.log(START_DELAYED_MSG);
         startTimer = new Timer(true);
@@ -174,7 +179,7 @@
                         }
                         else
                         {
-                            JetspeedLogger jsLogger = JetspeedLoggerUtil.getLogger(getClass());
+                            JetspeedLogger jsLogger = JetspeedLoggerUtil.getSharedLogger(getClass());
                             jsLogger.info(START_DELAYED_MSG);
                             context.log(START_DELAYED_MSG);
                         }
@@ -186,7 +191,7 @@
 
     boolean attemptStart(ServletContext context, String contextName, String contextPath,
String paDir, ClassLoader paClassLoader) 
     {
-        JetspeedLogger jsLogger = JetspeedLoggerUtil.getLogger(getClass());
+        JetspeedLogger jsLogger = JetspeedLoggerUtil.getSharedLogger(getClass());
         
         try
         {
@@ -325,7 +330,15 @@
         }
         catch (Throwable t)
         {
-            JetspeedLogger jsLogger = JetspeedLoggerUtil.getLogger(getClass());
+            if (paLogger == null)
+            {
+                paLogger = JetspeedLoggerUtil.getLocalLogger(getClass());
+                
+                if (paLogger == null)
+                {
+                    paLogger = JetspeedLoggerUtil.getSharedLogger(getClass());
+                }
+            }
             
             if ( t instanceof UnavailableException )
             {
@@ -336,7 +349,7 @@
             if (PortletWindow.Action.RENDER.equals(window.getAction())|| PortletWindow.Action.RESOURCE.equals(window.getAction()))
             {
                 ServletContext context = getServletContext();
-                jsLogger.error(JCS + "Error rendering portlet \"" + window.getPortletDefinition().getUniqueName()
+ "\": " + t.toString(), t);
+                paLogger.error(JCS + "Error rendering portlet \"" + window.getPortletDefinition().getUniqueName()
+ "\": " + t.toString(), t);
                 PrintWriter writer = ((PortletMimeResponseContext)window.getPortletResponseContext()).getWriter();
                 if (writer != null)
                 {
@@ -423,7 +436,7 @@
                         PortletApplicationManagement pam = (PortletApplicationManagement)
services.getService("PAM");
                         if ((pam != null) && pam.isStarted())
                         {
-                            JetspeedLogger jsLogger = JetspeedLoggerUtil.getLogger(getClass());
+                            JetspeedLogger jsLogger = JetspeedLoggerUtil.getSharedLogger(getClass());
                             jsLogger.info(STOP_MSG + contextName);
                             getServletContext().log(STOP_MSG + contextName);
                             try

Modified: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/JetspeedLoggerUtil.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/JetspeedLoggerUtil.java?rev=906988&r1=906987&r2=906988&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/JetspeedLoggerUtil.java
(original)
+++ portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/JetspeedLoggerUtil.java
Fri Feb  5 16:13:35 2010
@@ -16,27 +16,107 @@
  */
 package org.apache.jetspeed.util;
 
+import java.io.Serializable;
+import java.lang.reflect.Method;
+
 import org.apache.jetspeed.logger.JetspeedLogger;
 import org.apache.jetspeed.logger.JetspeedLoggerFactory;
 import org.apache.jetspeed.services.JetspeedPortletServices;
 import org.apache.jetspeed.services.PortletServices;
 
 /**
- * JetspeedLoggerUtil
+ * JetspeedLoggerUtil to get access to portlet web application's logger or shared portal
logger.
  * 
  * @version $Id$
  */
 public class JetspeedLoggerUtil
 {
+    
+    /**
+     * Default logger factory class name
+     */
+    public static final String DEFAULT_LOGGER_FACTORY = "org.slf4j.LoggerFactory";
+    
+    /**
+     * Default logger factory method to create a logger object.
+     */
+    public static final String DEFAULT_LOGGER_FACTORY_METHOD = "getLogger";
+    
+    private static JetspeedLogger noopLogger = new NOOPJetspeedLogger();
+    
     private JetspeedLoggerUtil()
     {
     }
-
+    
+    /**
+     * Returns a JetspeedLogger from the portlet application.
+     * <P>
+     * <EM>Note: This method tries to retrieve slf4j Logger by using current context
classloader
+     *           to get a portlet application specific logger.</EM>
+     * </P>
+     * <P>
+     * If no portlet application specific slf4j Logger is available, then it returns null.
+     * </P>
+     * @param clazz
+     * @return
+     */
+    public static JetspeedLogger getLocalLogger(Class<?> clazz)
+    {
+        try
+        {
+            Class<?> factoryClazz = Thread.currentThread().getContextClassLoader().loadClass(DEFAULT_LOGGER_FACTORY);
+            Object logger = invokeDeclaredMethod(factoryClazz, DEFAULT_LOGGER_FACTORY_METHOD,
new Class [] { Class.class }, new Object [] { clazz });
+            
+            if (logger != null)
+            {
+                return new DelegatingByReflectionJetspeedLogger(logger);
+            }
+        }
+        catch (Exception e)
+        {
+            getSharedLogger(clazz).warn("Failed to create PA logger: " + e);
+        }
+        
+        return null;
+    }
+    
+    /**
+     * Returns a JetspeedLogger from the portlet application.
+     * <P>
+     * <EM>Note: This method tries to retrieve slf4j Logger by using current context
classloader
+     *           to get a portlet application specific logger.</EM>
+     * </P>
+     * <P>
+     * If no portlet application specific slf4j Logger is available, then it returns null.
+     * </P>
+     * @param name
+     * @return
+     */
+    public static JetspeedLogger getLocalLogger(String name)
+    {
+        try
+        {
+            Class<?> factoryClazz = Thread.currentThread().getContextClassLoader().loadClass(DEFAULT_LOGGER_FACTORY);
+            Object logger = invokeDeclaredMethod(factoryClazz, DEFAULT_LOGGER_FACTORY_METHOD,
new Class [] { String.class }, new Object [] { name });
+            
+            if (logger != null)
+            {
+                return new DelegatingByReflectionJetspeedLogger(logger);
+            }
+        }
+        catch (Exception e)
+        {
+            getSharedLogger(name).warn("Failed to create PA logger: " + e);
+        }
+        
+        return null;
+    }
+    
     /**
      * Returns a JetspeedLogger from the portal services component.
      * <P>
-     * <EM>Note: A component which wants to use <CODE>JetspeedLogger</CODE>
must invoke this method
-     * whenever it tries to leave logs. The retrieved logger instance must not be kept for
later use.
+     * <EM>Note: A component which wants to use the shared <CODE>JetspeedLogger</CODE>
should invoke this method
+     * whenever it tries to leave logs. The retrieved logger instance should not be kept
for later use.
      * Jetspeed container can be reloaded any time and it can make the old logger instances
invalid.</EM>
      * </P>
      * <P>
@@ -46,7 +126,7 @@
      * @param clazz
      * @return
      */
-    public static JetspeedLogger getLogger(Class<?> clazz)
+    public static JetspeedLogger getSharedLogger(Class<?> clazz)
     {
         PortletServices ps = JetspeedPortletServices.getSingleton();
         
@@ -62,12 +142,12 @@
         
         return noopLogger;
     }
-
+    
     /**
      * Returns a JetspeedLogger from the portal services component.
      * <P>
-     * <EM>Note: A component which wants to use <CODE>JetspeedLogger</CODE>
must invoke this method
-     * whenever it tries to leave logs. The retrieved logger instance must not be kept for
later use.
+     * <EM>Note: A component which wants to use the shared <CODE>JetspeedLogger</CODE>
should invoke this method
+     * whenever it tries to leave logs. The retrieved logger instance should not be kept
for later use.
      * Jetspeed container can be reloaded any time and it can make the old logger instances
invalid.</EM>
      * </P>
      * <P>
@@ -77,7 +157,7 @@
      * @param name
      * @return
      */
-    public static JetspeedLogger getLogger(String name)
+    public static JetspeedLogger getSharedLogger(String name)
     {
         PortletServices ps = JetspeedPortletServices.getSingleton();
         
@@ -94,86 +174,229 @@
         return noopLogger;
     }
     
-    private static JetspeedLogger noopLogger = new JetspeedLogger()
+    private static Object invokeDeclaredMethod(Class<?> targetClazz, String methodName,
Class<?> [] argTypes, Object ... args)
     {
+        try
+        {
+            Method method = targetClazz.getDeclaredMethod(methodName, argTypes);
+            return method.invoke(targetClazz, args);
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("Failed to invoke logging method. " + e);
+        }
+    }
+    
+    private static class DelegatingByReflectionJetspeedLogger implements JetspeedLogger,
Serializable
+    {
+        private static final long serialVersionUID = 1L;
+        
+        private Class<?> targetClazz;
+        private Object targetLogger;
+        
+        public DelegatingByReflectionJetspeedLogger(Object targetLogger)
+        {
+            this.targetLogger = targetLogger;
+            this.targetClazz = targetLogger.getClass();
+        }
+        
+        public boolean isDebugEnabled()
+        {
+            return ((Boolean) invokeLoggerMethod("isDebugEnabled", null)).booleanValue();
+        }
+        
+        public boolean isInfoEnabled()
+        {
+            return ((Boolean) invokeLoggerMethod("isInfoEnabled", null)).booleanValue();
+        }
+        
+        public boolean isWarnEnabled()
+        {
+            return ((Boolean) invokeLoggerMethod("isWarnEnabled", null)).booleanValue();
+        }
+        
+        public boolean isErrorEnabled()
+        {
+            return ((Boolean) invokeLoggerMethod("isErrorEnabled", null)).booleanValue();
+        }
+        
         public void debug(String msg)
         {
+            invokeLoggerMethod("debug", new Class [] { String.class }, msg);
         }
 
         public void debug(String format, Object arg)
         {
+            invokeLoggerMethod("debug", new Class [] { String.class, Object.class }, format,
arg);
         }
 
         public void debug(String format, Object arg1, Object arg2)
         {
+            invokeLoggerMethod("debug", new Class [] { String.class, Object.class, Object.class
}, format, arg1, arg2);
         }
 
         public void debug(String format, Object[] argArray)
         {
+            invokeLoggerMethod("debug", new Class [] { String.class, Object [].class }, format,
argArray);
         }
 
         public void debug(String msg, Throwable t)
         {
+            invokeLoggerMethod("debug", new Class [] { String.class, Throwable.class }, msg,
t);
+        }
+
+        public void info(String msg)
+        {
+            invokeLoggerMethod("info", new Class [] { String.class }, msg);
+        }
+
+        public void info(String format, Object arg)
+        {
+            invokeLoggerMethod("info", new Class [] { String.class, Object.class }, format,
arg);
+        }
+
+        public void info(String format, Object arg1, Object arg2)
+        {
+            invokeLoggerMethod("info", new Class [] { String.class, Object.class, Object.class
}, format, arg1, arg2);
+        }
+
+        public void info(String format, Object[] argArray)
+        {
+            invokeLoggerMethod("info", new Class [] { String.class, Object [].class }, format,
argArray);
         }
 
+        public void info(String msg, Throwable t)
+        {
+            invokeLoggerMethod("info", new Class [] { String.class, Throwable.class }, msg,
t);
+        }
+        
+        public void warn(String msg)
+        {
+            invokeLoggerMethod("warn", new Class [] { String.class }, msg);
+        }
+
+        public void warn(String format, Object arg)
+        {
+            invokeLoggerMethod("warn", new Class [] { String.class, Object.class }, format,
arg);
+        }
+
+        public void warn(String format, Object arg1, Object arg2)
+        {
+            invokeLoggerMethod("warn", new Class [] { String.class, Object.class, Object.class
}, format, arg1, arg2);
+        }
+
+        public void warn(String format, Object[] argArray)
+        {
+            invokeLoggerMethod("warn", new Class [] { String.class, Object [].class }, format,
argArray);
+        }
+
+        public void warn(String msg, Throwable t)
+        {
+            invokeLoggerMethod("warn", new Class [] { String.class, Throwable.class }, msg,
t);
+        }
+        
         public void error(String msg)
         {
+            invokeLoggerMethod("error", new Class [] { String.class }, msg);
         }
 
         public void error(String format, Object arg)
         {
+            invokeLoggerMethod("error", new Class [] { String.class, Object.class }, format,
arg);
         }
 
         public void error(String format, Object arg1, Object arg2)
         {
+            invokeLoggerMethod("error", new Class [] { String.class, Object.class, Object.class
}, format, arg1, arg2);
         }
 
         public void error(String format, Object[] argArray)
         {
+            invokeLoggerMethod("error", new Class [] { String.class, Object [].class }, format,
argArray);
         }
 
         public void error(String msg, Throwable t)
         {
+            invokeLoggerMethod("error", new Class [] { String.class, Throwable.class }, msg,
t);
+        }
+        
+        private Object invokeLoggerMethod(String methodName, Class<?> [] argTypes,
Object ... args)
+        {
+            try
+            {
+                Method method = targetClazz.getMethod(methodName, argTypes);
+                return method.invoke(targetLogger, args);
+            }
+            catch (Exception e)
+            {
+                throw new RuntimeException("Failed to invoke logger method. " + e);
+            }
+        }
+        
+    };
+    
+    private static class NOOPJetspeedLogger implements JetspeedLogger, Serializable
+    {
+        private static final long serialVersionUID = 1L;
+        
+        public boolean isDebugEnabled()
+        {
+            return false;
+        }
+        
+        public boolean isInfoEnabled()
+        {
+            return false;
         }
 
-        public void info(String msg)
+        public boolean isWarnEnabled()
+        {
+            return false;
+        }
+        
+        public boolean isErrorEnabled()
+        {
+            return false;
+        }
+        
+        public void debug(String msg)
+        {
+        }
+        
+        public void debug(String format, Object arg)
         {
         }
 
-        public void info(String format, Object arg)
+        public void debug(String format, Object arg1, Object arg2)
         {
         }
 
-        public void info(String format, Object arg1, Object arg2)
+        public void debug(String format, Object[] argArray)
         {
         }
 
-        public void info(String format, Object[] arg1)
+        public void debug(String msg, Throwable t)
         {
         }
 
-        public void info(String msg, Throwable t)
+        public void info(String msg)
         {
         }
 
-        public boolean isDebugEnabled()
+        public void info(String format, Object arg)
         {
-            return false;
         }
 
-        public boolean isErrorEnabled()
+        public void info(String format, Object arg1, Object arg2)
         {
-            return false;
         }
 
-        public boolean isInfoEnabled()
+        public void info(String format, Object[] argArray)
         {
-            return false;
         }
 
-        public boolean isWarnEnabled()
+        public void info(String msg, Throwable t)
         {
-            return false;
         }
 
         public void warn(String msg)
@@ -195,5 +418,27 @@
         public void warn(String msg, Throwable t)
         {
         }
+        
+        public void error(String msg)
+        {
+        }
+
+        public void error(String format, Object arg)
+        {
+        }
+
+        public void error(String format, Object arg1, Object arg2)
+        {
+        }
+
+        public void error(String format, Object[] argArray)
+        {
+        }
+
+        public void error(String msg, Throwable t)
+        {
+        }
+
     };
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message