diff options
Diffstat (limited to '')
5 files changed, 271 insertions, 125 deletions
diff --git a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java index a92bb89084..779b92e27a 100644 --- a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java +++ b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/DefaultMonitorFactory.java @@ -29,10 +29,8 @@ import org.apache.tuscany.sca.monitor.impl.MonitorImpl; 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() { diff --git a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java index 581d3d3c38..0f4e7d71ff 100644 --- a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java +++ b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java @@ -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 @@ import org.apache.tuscany.sca.monitor.Problem.Severity; * @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 @@ public abstract class Monitor { 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 @@ public abstract class Monitor { * @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 @@ public abstract class Monitor { * @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); // ===================================================== } diff --git a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java index 523a63b819..c81581aae0 100644 --- a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java +++ b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/MonitorImpl.java @@ -38,7 +38,7 @@ public class MonitorImpl extends Monitor { 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 @@ public class MonitorImpl extends Monitor { 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 @@ public class MonitorImpl extends Monitor { } @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; } diff --git a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java index e1847e7b5f..acfc409856 100644 --- a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java +++ b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/impl/ProblemImpl.java @@ -79,6 +79,34 @@ public class ProblemImpl implements 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 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, diff --git a/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java b/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java index b0d9ffb8ce..6e44b2d5d9 100644 --- a/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java +++ b/java/sca/modules/monitor/src/test/java/org/apache/tuscany/sca/monitor/MonitorTestCase.java @@ -94,4 +94,5 @@ public class MonitorTestCase { monitor.problem(problem); } + } |