diff options
author | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2009-07-22 20:28:03 +0000 |
---|---|---|
committer | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2009-07-22 20:28:03 +0000 |
commit | ecf9531818c689e51c9354902f35572e0becef31 (patch) | |
tree | 82b5188494c3b8be0b506a4b06cb6ce83f129908 | |
parent | 396bdfd297b947cb7c665cecda3d9ef01948554e (diff) |
TUSCANY-3150 TUSCANY-3176 updates to show how we can accumulate context and how we can make the code simpler in terms of introducing the monitor to a module. I would like to think there is a more automatic way of determining the bundle name.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@796871 13f79535-47bb-0310-9956-ffa450edef68
5 files changed, 117 insertions, 28 deletions
diff --git a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/ComponentReferenceWireBuilderImpl.java b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/ComponentReferenceWireBuilderImpl.java index 35876fa01f..dd62cbb11a 100644 --- a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/ComponentReferenceWireBuilderImpl.java +++ b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/ComponentReferenceWireBuilderImpl.java @@ -66,6 +66,8 @@ public class ComponentReferenceWireBuilderImpl extends BaseBuilderImpl implement */ protected void wireComponentReferences(Composite composite, Monitor monitor) { + monitor.pushContext(composite.getName().toString()); + // Wire nested composites recursively for (Component component : composite.getComponents()) { Implementation implementation = component.getImplementation(); @@ -108,7 +110,14 @@ public class ComponentReferenceWireBuilderImpl extends BaseBuilderImpl implement componentReference.getName()); } } else { - warning(monitor, "TooManyReferenceTargets", composite, componentReference.getName()); +// --------------------------- +// TUSCANY-3132 first example of updated error handling + Monitor.error(monitor, + this, + "assembly-validation-messages", + "TooManyReferenceTargets", + componentReference.getName()); +// --------------------------- } } } @@ -118,6 +127,8 @@ public class ComponentReferenceWireBuilderImpl extends BaseBuilderImpl implement // for (ComponentReference componentReference : componentReferences.values()) { // componentReference.getTargets().clear(); // } + + monitor.popContext(); } /** 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 5b09f0116c..946e785b8c 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 @@ -28,27 +28,27 @@ import org.apache.tuscany.sca.monitor.Problem.Severity; * * @version $Rev$ $Date$ */ -public interface Monitor { +public abstract class Monitor { /** * Reports a build problem. * * @param problem */ - void problem(Problem problem); + public abstract void problem(Problem problem); /** * Returns a list of reported problems. * * @return the list of problems. The list may be empty */ - List<Problem> getProblems(); + public abstract List<Problem> getProblems(); /** * Returns the last logged problem. * * @return */ - public Problem getLastProblem(); + public abstract Problem getLastProblem(); /** * Create a new problem. @@ -61,12 +61,12 @@ public interface Monitor { * @param cause the exception which caused the problem * @return */ - Problem createProblem(String sourceClassName, - String bundleName, - Severity severity, - Object problemObject, - String messageId, - Exception cause); + public abstract Problem createProblem(String sourceClassName, + String bundleName, + Severity severity, + Object problemObject, + String messageId, + Exception cause); /** * Create a new problem. @@ -79,23 +79,74 @@ public interface Monitor { * @param messageParams the parameters of the problem message * @return */ - Problem createProblem(String sourceClassName, - String bundleName, - Severity severity, - Object problemObject, - String messageId, - Object... messageParams); + public abstract Problem createProblem(String sourceClassName, + String bundleName, + Severity severity, + Object problemObject, + String messageId, + Object... messageParams); /** * Set the name of an artifact for which errors are Monitored * @param artifactName the artifact name */ - void setArtifactName(String artifactName); + public abstract void setArtifactName(String artifactName); /** * 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 */ - String getArtifactName(); - + 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(); + + public static void warning (Monitor monitor, + Object reportingObject, + String messageBundle, + String messageId, + String... messageParameters){ + if (monitor != null) { + Problem problem = + monitor.createProblem(reportingObject.getClass().getName(), + messageBundle, + Severity.WARNING, + null, + messageId, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + public static void error (Monitor monitor, + Object reportingObject, + String messageBundle, + String messageId, + String... messageParameters){ + if (monitor != null) { + Problem problem = + monitor.createProblem(reportingObject.getClass().getName(), + messageBundle, + Severity.ERROR, + null, + messageId, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + // ===================================================== } diff --git a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Problem.java b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Problem.java index d495569e73..61a90333ab 100644 --- a/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Problem.java +++ b/java/sca/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Problem.java @@ -35,6 +35,8 @@ public interface Problem { String getBundleName(); Severity getSeverity(); + + String getContext(); Object getProblemObject(); 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 27070fcc40..e4bb2a2201 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 @@ -21,6 +21,7 @@ package org.apache.tuscany.sca.monitor.impl; import java.util.ArrayList; import java.util.List; +import java.util.Stack; import java.util.logging.Level; import java.util.logging.Logger; @@ -33,8 +34,11 @@ import org.apache.tuscany.sca.monitor.Problem.Severity; * * @version $Rev$ $Date$ */ -public class MonitorImpl implements Monitor { +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>(); // Cache all the problem reported to monitor for further analysis private List<Problem> problemCache = new ArrayList<Problem>(); @@ -54,21 +58,21 @@ public class MonitorImpl implements Monitor { if (problem.getSeverity() == Severity.INFO) { problemCache.add(problem); - problemLogger.logp(Level.INFO, problem.getSourceClassName(), null, problem.getMessageId(), problem + problemLogger.logp(Level.INFO, problem.getSourceClassName(), problem.getContext(), problem.getMessageId(), problem .getMessageParams()); } else if (problem.getSeverity() == Severity.WARNING) { problemCache.add(problem); - problemLogger.logp(Level.WARNING, problem.getSourceClassName(), null, problem.getMessageId(), problem + problemLogger.logp(Level.WARNING, problem.getSourceClassName(), problem.getContext(), problem.getMessageId(), problem .getMessageParams()); } else if (problem.getSeverity() == Severity.ERROR) { if (problem.getCause() != null) { problemCache.add(problem); - problemLogger.logp(Level.SEVERE, problem.getSourceClassName(), null, problem.getMessageId(), problem + problemLogger.logp(Level.SEVERE, problem.getSourceClassName(), problem.getContext(), problem.getMessageId(), problem .getCause().toString()); } else { problemCache.add(problem); - problemLogger.logp(Level.SEVERE, problem.getSourceClassName(), null, problem.getMessageId(), problem + problemLogger.logp(Level.SEVERE, problem.getSourceClassName(), problem.getContext(), problem.getMessageId(), problem .getMessageParams()); } } @@ -91,7 +95,7 @@ public class MonitorImpl implements Monitor { Object problemObject, String messageId, Exception cause) { - return new ProblemImpl(sourceClassName, bundleName, severity, problemObject, messageId, cause); + return new ProblemImpl(sourceClassName, bundleName, severity, contextStack.toString(), problemObject, messageId, cause); } public Problem createProblem(String sourceClassName, @@ -100,7 +104,7 @@ public class MonitorImpl implements Monitor { Object problemObject, String messageId, Object... messageParams) { - return new ProblemImpl(sourceClassName, bundleName, severity, problemObject, messageId, messageParams); + return new ProblemImpl(sourceClassName, bundleName, severity, contextStack.toString(), problemObject, messageId, messageParams); } public String getArtifactName() { @@ -110,4 +114,14 @@ public class MonitorImpl implements Monitor { public void setArtifactName(String artifactName) { this.artifactName = artifactName; } + + @Override + public void pushContext(String context) { + contextStack.push(context); + } + + @Override + public void popContext() { + contextStack.pop(); + } } 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 f00071bc51..f5534963df 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 @@ -37,6 +37,7 @@ public class ProblemImpl implements Problem { private String sourceClassName; private String bundleName; private Severity severity; + private String context; private Object problemObject; private String messageId; private Object[] messageParams; @@ -48,6 +49,7 @@ public class ProblemImpl implements 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 messageParams the parameters of the problem message @@ -55,12 +57,14 @@ public class ProblemImpl implements Problem { public ProblemImpl(String sourceClassName, String bundleName, Severity severity, + String context, Object problemObject, String messageId, Object... messageParams) { this.sourceClassName = sourceClassName; this.bundleName = bundleName; this.severity = severity; + this.context = context; this.problemObject = problemObject; this.messageId = messageId; this.messageParams = messageParams; @@ -72,6 +76,7 @@ public class ProblemImpl implements 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 @@ -79,12 +84,14 @@ public class ProblemImpl implements Problem { public ProblemImpl(String sourceClassName, String bundleName, Severity severity, + String context, Object problemObject, String messageId, Exception cause) { this.sourceClassName = sourceClassName; this.bundleName = bundleName; this.severity = severity; + this.context = context; this.problemObject = problemObject; this.messageId = messageId; this.cause = cause; @@ -101,6 +108,10 @@ public class ProblemImpl implements Problem { public Severity getSeverity() { return severity; } + + public String getContext() { + return context; + } public Object getProblemObject() { return problemObject; @@ -137,6 +148,6 @@ public class ProblemImpl implements Problem { Formatter formatter = new SimpleFormatter(); - return formatter.formatMessage(record); + return context + " - " + formatter.formatMessage(record); } } |