You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2009/10/20 19:53:37 UTC

svn commit: r827745 - in /tuscany/java/sca/modules/monitor/src: main/java/org/apache/tuscany/sca/monitor/ main/java/org/apache/tuscany/sca/monitor/impl/ test/java/org/apache/tuscany/sca/monitor/

Author: rfeng
Date: Tue Oct 20 17:53:37 2009
New Revision: 827745

URL: http://svn.apache.org/viewvc?rev=827745&view=rev
Log:
Enable DefaultMonitorFactory to create new Monitor instances
Add more convenient methods onto the Monitor

Modified:
    tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java
    tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java
    tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java
    tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java
    tuscany/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java

Modified: tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java?rev=827745&r1=827744&r2=827745&view=diff
==============================================================================
--- tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java (original)
+++ tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java Tue Oct 20 17:53:37 2009
@@ -29,10 +29,8 @@
 public class DefaultMonitorFactory implements MonitorFactory {
     private ThreadLocal<Monitor> contextMonitor = new InheritableThreadLocal<Monitor>();
 
-    // [HACK] This is a hack to reuse the same monitor on the thread 
     public Monitor createMonitor() {
-        return getContextMonitor(true);
-        // return new MonitorImpl();
+        return new MonitorImpl();
     }
 
     public Monitor getContextMonitor() {

Modified: tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java?rev=827745&r1=827744&r2=827745&view=diff
==============================================================================
--- tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java (original)
+++ tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java Tue Oct 20 17:53:37 2009
@@ -20,6 +20,7 @@
 package org.apache.tuscany.sca.monitor;
 
 import java.util.List;
+import java.util.logging.Logger;
 
 import org.apache.tuscany.sca.monitor.Problem.Severity;
 
@@ -29,103 +30,133 @@
  * @version $Rev$ $Date$
  */
 public abstract class Monitor {
-    /**
-     * Reports a build problem.
-     * 
-     * @param problem
-     */
-    public abstract void problem(Problem problem);
+    private static class ContextFinder extends SecurityManager {
+        private final static ContextFinder instance = new ContextFinder();
+        
+        // This is sensitive to the calling stack
+        private static Class<?> getContextClass() {
+            Class[] classes = instance.getClassContext();
+            // 0: ContextFinder (getClassContext)
+            // 1: ContextFinder (getContextClass)
+            // 2: Monitor (getSourceClassName)
+            // 3: Monitor (error/warning)
+            return classes[4];
+        }
+        
+    }
+    private final static Logger logger = Logger.getLogger(Monitor.class.getName());
 
-    /** 
-     * Returns a list of reported problems. 
+    /**
+     * A utility function for raising an error. It creates the problem and 
+     * adds it to the monitor
      * 
-     * @return the list of problems. The list may be empty
+     * @param monitor
+     * @param reportingObject
+     * @param messageBundle
+     * @param messageId
+     * @param messageParameters
      */
-    public abstract List<Problem> getProblems();
+    public static void error (Monitor monitor, 
+                              Object reportingObject,
+                              String messageBundle,
+                              String messageId, 
+                              Object... messageParameters){
+        String contextClassName = getSourceClassName(reportingObject);
+        if (monitor != null) {
+            Problem problem =
+                monitor.createProblem(contextClassName,
+                                      messageBundle,
+                                      Severity.ERROR,
+                                      reportingObject,
+                                      messageId,
+                                      messageParameters);
+            monitor.problem(problem);
+        } else {
+            logNullMonitor(messageId, contextClassName);
+        }
+    }
 
     /**
-     * Returns the last logged problem.
+     * A utility function for raising an error. It creates the problem and 
+     * adds it to the monitor
      * 
-     * @return
+     * @param monitor
+     * @param reportingObject
+     * @param messageBundle
+     * @param messageId
+     * @param exception
      */
-    public abstract Problem getLastProblem();
+    public static void error (Monitor monitor, 
+                              Object reportingObject,
+                              String messageBundle,
+                              String messageId, 
+                              Throwable cause){
+        String contextClassName = getSourceClassName(reportingObject);
+        if (monitor != null) {
+            Problem problem =
+                monitor.createProblem(contextClassName,
+                                      messageBundle,
+                                      Severity.ERROR,
+                                      reportingObject,
+                                      messageId,
+                                      cause);
+            monitor.problem(problem);
+        } else {
+            logNullMonitor(messageId, contextClassName);
+        }
+    }
 
     /**
-     * Create a new problem.
+     * A utility function for raising an error. It creates the problem and 
+     * adds it to the monitor
      * 
-     * @param sourceClassName   the class name reporting the problem
-     * @param bundleName        the name of the message bundle to use
-     * @param severity          the severity of the problem
-     * @param problemObject     the model object for which the problem is being reported
-     * @param messageId         the id of the problem message
-     * @param cause             the exception which caused the problem
-     * @return
+     * @param monitor
+     * @param reportingObject
+     * @param messageBundle
+     * @param messageId
+     * @param exception
      */
-    public abstract Problem createProblem(String sourceClassName,
-                                          String bundleName,
-                                          Severity severity,
-                                          Object problemObject,
-                                          String messageId,
-                                          Throwable cause);
+    public static void error (Monitor monitor, 
+                              Object reportingObject,
+                              String messageBundle,
+                              String messageId, 
+                              Throwable cause,
+                              Object... messageParameters) {
+        String contextClassName = getSourceClassName(reportingObject);
+        if (monitor != null) {
+            Problem problem =
+                monitor.createProblem(contextClassName,
+                                      messageBundle,
+                                      Severity.ERROR,
+                                      reportingObject,
+                                      messageId,
+                                      cause,
+                                      messageParameters);
+            monitor.problem(problem);
+        } else {
+            logNullMonitor(messageId, contextClassName);
+        }
+    }
 
-    /**
-     * Create a new problem.
-     *  
-     * @param sourceClassName   the class name reporting the problem
-     * @param bundleName        the name of the message bundle to use
-     * @param severity          the severity of the problem
-     * @param problemObject     the model object for which the problem is being reported
-     * @param messageId         the id of the problem message
-     * @param messageParams     the parameters of the problem message
-     * @return
-     */
-    public abstract Problem createProblem(String sourceClassName,
-                                   String bundleName,
-                                   Severity severity,
-                                   Object problemObject,
-                                   String messageId,
-                                   Object... messageParams);
+    private static String getSourceClassName(Object reportingObject) {
+        String contextClassName = null;
+        if (reportingObject != null) {
+            contextClassName = reportingObject.getClass().getName();
+        } else {
+            contextClassName = ContextFinder.getContextClass().getName();
+        }
+        return contextClassName;
+    }
 
-    /**
-     * Set the name of an artifact for which errors are Monitored
-     * @param artifactName the artifact name
-     */
-    public abstract void setArtifactName(String artifactName);
+    private static void logNullMonitor(String messageId, String contextClassName) {
+        logger.warning("Attempt to report error with id " + 
+                messageId + 
+                " from class " + 
+                contextClassName +
+                " but the monitor object was null");
+    }
 
     /**
-     * Get the name of the artifact for which errors are Monitored
-     * @return the name of the Artifact or null if no artifact name has been set
-     */
-    public abstract String getArtifactName();
-    
-    // =====================================================
-    // TUSCANY-3132 - new approach to monitoring errors
-    //           
-    
-    /**
-     * Add a context string to the context stack
-     * @param context the context string to add
-     */
-    public abstract void pushContext(String context);
-    
-    /**
-     * Remove the most recent context string from the 
-     * context stack
-     */
-    public abstract void popContext();
-    
-    /**
-     * Remove all of the context strings from the 
-     * context stack
-     */
-    public abstract void clearContext();
-    
-    /**
-     * Clear context and problems
-     */
-    public abstract void reset();
-    
-    /**
      * A utility function for raising a warning. It creates the problem and 
      * adds it to the monitor
      * 
@@ -140,24 +171,25 @@
                                 String messageBundle,
                                 String messageId, 
                                 Object... messageParameters){
+        String contextClassName = getSourceClassName(reportingObject);
         if (monitor != null) {
             Problem problem =
-                monitor.createProblem(reportingObject.getClass().getName(),
+                monitor.createProblem(contextClassName,
                                       messageBundle,
                                       Severity.WARNING,
-                                      null,
+                                      reportingObject,
                                       messageId,
                                       messageParameters);
             monitor.problem(problem);
         } else {
-            System.out.println("Attempt to report warning with id " + 
-                               messageId + 
-                               " from class " + 
-                               reportingObject.getClass().getName() +
-                               " but the monitor object was null");
+            logNullMonitor(messageId, contextClassName);
         }
     }
-   
+    
+    // =====================================================
+    // TUSCANY-3132 - new approach to monitoring errors
+    //           
+    
     /**
      * A utility function for raising an error. It creates the problem and 
      * adds it to the monitor
@@ -166,28 +198,25 @@
      * @param reportingObject
      * @param messageBundle
      * @param messageId
-     * @param messageParameters
+     * @param exception
      */
-    public static void error (Monitor monitor, 
+    public static void warning (Monitor monitor, 
                               Object reportingObject,
                               String messageBundle,
                               String messageId, 
-                              Object... messageParameters){
+                              Throwable cause){
+        String contextClassName = getSourceClassName(reportingObject);
         if (monitor != null) {
             Problem problem =
-                monitor.createProblem(reportingObject.getClass().getName(),
+                monitor.createProblem(contextClassName,
                                       messageBundle,
                                       Severity.ERROR,
-                                      null,
+                                      reportingObject,
                                       messageId,
-                                      messageParameters);
+                                      cause);
             monitor.problem(problem);
         } else {
-            System.out.println("Attempt to report error with id " + 
-                    messageId + 
-                    " from class " + 
-                    reportingObject.getClass().getName() +
-                    " but the monitor object was null");
+            logNullMonitor(messageId, contextClassName);
         }
     }
     
@@ -201,28 +230,113 @@
      * @param messageId
      * @param exception
      */
-    public static void error (Monitor monitor, 
+    public static void warning (Monitor monitor, 
                               Object reportingObject,
                               String messageBundle,
                               String messageId, 
-                              Throwable cause){
+                              Throwable cause,
+                              Object... messageParameters) {
+        String contextClassName = getSourceClassName(reportingObject);
         if (monitor != null) {
             Problem problem =
-                monitor.createProblem(reportingObject.getClass().getName(),
+                monitor.createProblem(contextClassName,
                                       messageBundle,
                                       Severity.ERROR,
-                                      null,
+                                      reportingObject,
                                       messageId,
-                                      cause);
+                                      cause,
+                                      messageParameters);
             monitor.problem(problem);
         } else {
-            System.out.println("Attempt to report error with id " + 
-                    messageId + 
-                    " from class " + 
-                    reportingObject.getClass().getName() +
-                    " but the monitor object was null");
+            logNullMonitor(messageId, contextClassName);
         }
-    }    
+    }
+    
+    /**
+     * Create a new problem.
+     *  
+     * @param sourceClassName   the class name reporting the problem
+     * @param bundleName        the name of the message bundle to use
+     * @param severity          the severity of the problem
+     * @param problemObject     the model object for which the problem is being reported
+     * @param messageId         the id of the problem message
+     * @param messageParams     the parameters of the problem message
+     * @return
+     */
+    public abstract Problem createProblem(String sourceClassName,
+                                   String bundleName,
+                                   Severity severity,
+                                   Object problemObject,
+                                   String messageId,
+                                   Object... messageParams);
+    
+    /**
+     * Create a new problem.
+     * 
+     * @param sourceClassName   the class name reporting the problem
+     * @param bundleName        the name of the message bundle to use
+     * @param severity          the severity of the problem
+     * @param problemObject     the model object for which the problem is being reported
+     * @param messageId         the id of the problem message
+     * @param cause             the exception which caused the problem
+     * @return
+     */
+    public abstract Problem createProblem(String sourceClassName,
+                                          String bundleName,
+                                          Severity severity,
+                                          Object problemObject,
+                                          String messageId,
+                                          Throwable cause);
+
+    /**
+     * Get the name of the artifact for which errors are Monitored
+     * @return the name of the Artifact or null if no artifact name has been set
+     */
+    public abstract String getArtifactName();
+
+    /**
+     * Returns the last logged problem.
+     * 
+     * @return
+     */
+    public abstract Problem getLastProblem();
+   
+    /** 
+     * Returns a list of reported problems. 
+     * 
+     * @return the list of problems. The list may be empty
+     */
+    public abstract List<Problem> getProblems();
+
+    /**
+     * Remove the most recent context string from the 
+     * context stack
+     * @return The object popped 
+     */
+    public abstract Object popContext();
+
+    /**
+     * Reports a build problem.
+     * 
+     * @param problem
+     */
+    public abstract void problem(Problem problem);
+    
+    /**
+     * Add a context string to the context stack
+     * @param context the context string to add
+     */
+    public abstract void pushContext(Object context);
+    
+    /**
+     * Clear context and problems
+     */
+    public abstract void reset();    
+    /**
+     * Set the name of an artifact for which errors are Monitored
+     * @param artifactName the artifact name
+     */
+    public abstract void setArtifactName(String artifactName);
     
     // =====================================================
 }

Modified: tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java?rev=827745&r1=827744&r2=827745&view=diff
==============================================================================
--- tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java (original)
+++ tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java Tue Oct 20 17:53:37 2009
@@ -38,7 +38,7 @@
     private static final Logger logger = Logger.getLogger(MonitorImpl.class.getName());
     
     // stack of context information that is printed alongside each problem
-    private Stack<String> contextStack = new Stack<String>();
+    private Stack<Object> contextStack = new Stack<Object>();
 
     // Cache all the problem reported to monitor for further analysis
     private List<Problem> problemCache = new ArrayList<Problem>();
@@ -106,6 +106,16 @@
                                  Object... messageParams) {
         return new ProblemImpl(sourceClassName, bundleName, severity, contextStack.toString(), problemObject, messageId, messageParams);
     }
+    
+    public Problem createProblem(String sourceClassName,
+                                 String bundleName,
+                                 Severity severity,
+                                 Object problemObject,
+                                 String messageId,
+                                 Throwable cause,
+                                 Object... messageParams) {
+        return new ProblemImpl(sourceClassName, bundleName, severity, contextStack.toString(), problemObject, messageId, cause, messageParams);
+    }
 
     public String getArtifactName() {
         return artifactName;
@@ -116,23 +126,18 @@
     }
     
     @Override
-    public void pushContext(String context) {
+    public void pushContext(Object context) {
         contextStack.push(context);
     }
     
     @Override
-    public void popContext() {
-        contextStack.pop();
-    }
-    
-    @Override
-    public void clearContext() {
-        contextStack.clear();  
+    public Object popContext() {
+        return contextStack.pop();
     }
     
     @Override
     public void reset() {
-        clearContext();
+        contextStack.clear();
         problemCache.clear();
         artifactName = null;
     }

Modified: tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java?rev=827745&r1=827744&r2=827745&view=diff
==============================================================================
--- tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java (original)
+++ tuscany/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java Tue Oct 20 17:53:37 2009
@@ -79,6 +79,34 @@
      * @param context           the string indicating where the error occurred
      * @param problemObject     the model object for which the problem is being reported
      * @param messageId         the id of the problem message
+     * @param messageParams     the parameters of the problem message
+     */
+    public ProblemImpl(String sourceClassName,
+                       String bundleName,
+                       Severity severity,
+                       String context,
+                       Object problemObject,
+                       String messageId,
+                       Throwable cause,
+                       Object... messageParams) {
+        this.sourceClassName = sourceClassName;
+        this.bundleName = bundleName;
+        this.severity = severity;
+        this.context = context;
+        this.problemObject = problemObject;
+        this.messageId = messageId;
+        this.cause = cause;
+        this.messageParams = messageParams;
+    }    
+    /**
+     * Construct a new problem
+     * 
+     * @param sourceClassName   the class name reporting the problem
+     * @param bundleName        the name of the message bundle to use
+     * @param severity          the severity of the problem
+     * @param context           the string indicating where the error occurred
+     * @param problemObject     the model object for which the problem is being reported
+     * @param messageId         the id of the problem message
      * @param cause             the exception which caused the problem
      */
     public ProblemImpl(String sourceClassName,

Modified: tuscany/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java?rev=827745&r1=827744&r2=827745&view=diff
==============================================================================
--- tuscany/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java (original)
+++ tuscany/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java Tue Oct 20 17:53:37 2009
@@ -94,4 +94,5 @@
         monitor.problem(problem);
 
     }
+
 }