Author: simoneg
Date: Tue Oct 20 23:55:22 2009
New Revision: 827842

URL: http://svn.apache.org/viewvc?rev=827842&view=rev
Log:
New tracing tool domain

Added:
    labs/magma/trunk/devtools-domain/   (with props)
    labs/magma/trunk/devtools-domain/pom.xml
    labs/magma/trunk/devtools-domain/src/
    labs/magma/trunk/devtools-domain/src/main/
    labs/magma/trunk/devtools-domain/src/main/java/
    labs/magma/trunk/devtools-domain/src/main/java/org/
    labs/magma/trunk/devtools-domain/src/main/java/org/apache/
    labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/
    labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/
    
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/
    
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/
    
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/EmptyHandler.java
    
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Trace.java
    
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEvent.java
    
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventHandler.java
    
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventPriority.java
    
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Traced.java
    labs/magma/trunk/devtools-domain/src/main/resources/
    labs/magma/trunk/devtools-domain/src/test/
    labs/magma/trunk/devtools-domain/src/test/java/
    labs/magma/trunk/devtools-domain/src/test/resources/

Propchange: labs/magma/trunk/devtools-domain/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Oct 20 23:55:22 2009
@@ -0,0 +1,7 @@
+.classpath
+.project
+.settings
+bin
+classes
+target
+target-eclipse

Added: labs/magma/trunk/devtools-domain/pom.xml
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/devtools-domain/pom.xml?rev=827842&view=auto
==============================================================================
--- labs/magma/trunk/devtools-domain/pom.xml (added)
+++ labs/magma/trunk/devtools-domain/pom.xml Tue Oct 20 23:55:22 2009
@@ -0,0 +1,21 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <parent>
+    <artifactId>magma-parent</artifactId>
+    <groupId>org.apache.magma</groupId>
+    <version>3</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.magma.tools</groupId>
+  <artifactId>devtools-domain</artifactId>
+  <name>Magma Developer Tools Domain</name>
+  <version>0.0.3-SNAPSHOT</version>
+  <description>Contains classes used by both the client and the server part of 
the Developer Tools</description>
+  <packaging>magma</packaging>
+  <dependencies>
+       <dependency>
+               <groupId>org.apache.magma</groupId>
+               <artifactId>foundation-basics</artifactId>
+               <version>0.0.3-SNAPSHOT</version>
+       </dependency>
+  </dependencies>
+</project>

Added: 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/EmptyHandler.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/EmptyHandler.java?rev=827842&view=auto
==============================================================================
--- 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/EmptyHandler.java
 (added)
+++ 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/EmptyHandler.java
 Tue Oct 20 23:55:22 2009
@@ -0,0 +1,29 @@
+package org.apache.magma.tools.devtools.domain;
+
+import org.aspectj.lang.JoinPoint;
+
+/**
+ * An empty {...@link TraceEventHandler} that returns the event passed
+ * without any modifications.
+ * 
+ *  Can be used as a superclass for other handlers, and is used
+ *  by {...@link EventCollector} in the case that the required handler 
+ *  cannot be instantiated.
+ * 
+ * @author Simone Gianni <[email protected]>
+ */
+public class EmptyHandler implements TraceEventHandler {
+
+       public TraceEvent handleEntering(TraceEvent event, JoinPoint enterjp) {
+               return event;
+       }
+
+       public TraceEvent handleError(TraceEvent event, Throwable 
error,JoinPoint exitjp) {
+               return event;
+       }
+
+       public TraceEvent handleReturning(TraceEvent event, Object retValue, 
JoinPoint exitjp) {
+               return event;
+       }
+
+}

Added: 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Trace.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Trace.java?rev=827842&view=auto
==============================================================================
--- 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Trace.java
 (added)
+++ 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Trace.java
 Tue Oct 20 23:55:22 2009
@@ -0,0 +1,56 @@
+package org.apache.magma.tools.devtools.domain;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * A sequence of events, with a unique id and a user name and eventually
+ * dependent referreds traces. 
+ * 
+ * For example, in a web environment, a page request will generate a
+ * trace, having its own id and having, eventually, the requested
+ * URL as a name.
+ * 
+ * The name can be used both for human presentation and for finding
+ * the trace again to add referred ones, which are for example images
+ * the browser is requesting to display the page. 
+ * 
+ * @author Simone Gianni <[email protected]>
+ */
+public class Trace {
+       
+       private static long lastid = System.currentTimeMillis(); 
+       
+       private long id = lastid++;
+       private long timestamp = System.currentTimeMillis();
+       private String name = null;
+       
+       private List<TraceEvent> events = new LinkedList<TraceEvent>();
+       
+       private List<Trace> referreds = new LinkedList<Trace>();
+       
+       public Trace(String name) {
+               this.name = name;
+       }
+       
+       public String getName() {
+               return name;
+       }
+       public long getId() {
+               return id;
+       }
+       public List<TraceEvent> getEvents() {
+               return events;
+       }
+       public void addReferred(Trace t) {
+               referreds.add(t);
+       }
+       public List<Trace> getReferreds() {
+               return referreds;
+       }
+       public long getTimestamp() {
+               return timestamp;
+       }
+
+       
+}

Added: 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEvent.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEvent.java?rev=827842&view=auto
==============================================================================
--- 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEvent.java
 (added)
+++ 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEvent.java
 Tue Oct 20 23:55:22 2009
@@ -0,0 +1,325 @@
+package org.apache.magma.tools.devtools.domain;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A single event captured during a {...@link Trace}.
+ * 
+ * @author Simone Gianni <[email protected]>
+ */
+public class TraceEvent {
+       
+       private static long lastid = System.currentTimeMillis();
+       
+       /**
+        * Unique id of this event
+        */
+       private long id = lastid++;
+       
+       /**
+        * Location in code of where this event happened.
+        * 
+        * Format is filename>classname:line.
+        */
+       private String codeLocation;
+       
+       /**
+        * String representation of the raw joinpoint
+        */
+       private String rawJoinPoint;
+       
+       /**
+        * String representation of parameters.
+        * 
+        * Format is fqn>toString
+        */
+       private String[] parameters;
+       
+       /**
+        * String representation of return value, if any
+        * 
+        * Format is fwn>toString
+        */
+       private String returnValue;
+       
+       /**
+        * The stack trace of an error if occurred.
+        */
+       private String stackTrace;
+       
+       /**
+        * Subsystem the event is pertaining to, should be a "/" separated path.
+        */
+       private String subSystem = "";
+       
+       /**
+        * Subsystem specific type of event.
+        */
+       private String type = "";
+       
+       /**
+        * Priority of the event, used to gain user attention.
+        */
+       private TraceEventPriority priority = TraceEventPriority.DEBUG;
+       
+       /**
+        * Start time in nanoseconds
+        */
+       private long started = System.nanoTime();
+       
+       /**
+        * End time in nanoseconds, this will include all events happenend 
"inside" this.
+        */
+       private long ended;
+       
+       /**
+        * Whether this events should be located or not in the output.
+        */
+       private boolean located;
+       
+       /**
+        * The parent (or eclosing) event, or null for root events.
+        */
+       private TraceEvent parent;
+       
+       /**
+        * An optional bag of properties
+        */
+       private Map<String, String> dataBag;
+       
+       public TraceEvent(TraceEvent parent, String codeLocation) {
+               this.parent = parent;
+               this.codeLocation = codeLocation;
+       }
+
+       /**
+        * @return the unique id of this event.
+        */
+       public long getId() {
+               return id;
+       }
+       
+       /**
+        * @return the start time of this event in nanoseconds.
+        */
+       public long getStarted() {
+               return started;
+       }
+       /**
+        * Marks this event as ended, to correctly compute execution times.
+        */
+       public void ended() {
+               ended = System.nanoTime();
+       }
+       /**
+        * @return the end time of this event in nanoseconds.
+        */
+       public long getEnded() {
+               return ended;
+       }
+       
+       /**
+        * @return the total execution time of this event (including 
child/enclosed events).
+        */
+       public long getElapsed() {
+               return ended - started;
+       }
+       
+       /**
+        * Adds some data to the optional bag of properties.
+        * @param name The name of the property to add.
+        * @param value The value of the property to add.
+        */
+       public void addData(String name, String value) {
+               if (dataBag == null) dataBag = new HashMap<String, String>();
+               dataBag.put(name, value);
+       }
+       /**
+        * Removes data from the optional bag of properties.
+        * @param name The name of the property to remove.
+        */
+       public void removeData(String name) {
+               if (dataBag == null) return;
+               dataBag.remove(name);
+       }
+       /**
+        * Retrieves data from the optional bag of properties.
+        * @param name The name of the property to retrieve.
+        * @return the value of the property.
+        */
+       public String getData(String name) {
+               if (dataBag == null) return null;
+               return dataBag.get(name);
+       }
+       /**
+        * @return the names of the properties currently in the optional bag of 
properties.
+        */
+       public Set<String> getDataNames() {
+               if (dataBag == null) return Collections.emptySet();
+               return dataBag.keySet();
+       }
+       
+       /**
+        * @return the sub system this event pertains to, a slash ('/') 
separated string like "Magma/I18n".
+        */
+       public String getSubSystem() {
+               return subSystem;
+       }
+
+       /**
+        * @param subSystem the sub system this events pertains to.
+        */
+       public void setSubSystem(String subSystem) {
+               assert(subSystem != null);
+               this.subSystem = subSystem;
+       }
+
+       /**
+        * @return the sub type specific event type.
+        */
+       public String getType() {
+               return type;
+       }
+
+       /**
+        * @param type the sub system specific event type.
+        */
+       public void setType(String type) {
+               assert(type != null);
+               this.type = type;
+       }
+
+       /**
+        * @return the priority of this event.
+        */
+       public TraceEventPriority getPriority() {
+               return priority;
+       }
+
+       /**
+        * @param priority the priority of this event.
+        */
+       public void setPriority(TraceEventPriority priority) {
+               assert(priority != null);
+               this.priority = priority;
+       }
+
+       /**
+        * @return whether this event should be located on the output
+        */
+       public boolean isLocated() {
+               return located;
+       }
+
+       /**
+        * @param located whether this event should be located on the output
+        */
+       public void setLocated(boolean located) {
+               this.located = located;
+       }
+
+       /**
+        * @return the parent (enclosing) event, if any.
+        */
+       public TraceEvent getParent() {
+               return parent;
+       }
+       
+       /**
+        * @return the location in code of this event, format is 
filename>classname:line
+        */
+       public String getCodeLocation() {
+               return codeLocation;
+       }
+
+       /**
+        * @return the raw join point representation, as obtained from AspectJ
+        */
+       public String getRawJoinPoint() {
+               return rawJoinPoint;
+       }
+
+       /**
+        * @param rawJoinPoint the raw join point representation, as obtained 
from AspectJ
+        */
+       public void setRawJoinPoint(String rawJoinPoint) {
+               this.rawJoinPoint = rawJoinPoint;
+       }
+
+       /**
+        * @return String representations of parameters, if any, null or empty 
array otherwise. Format is fqn>toString
+        */
+       public String[] getParameters() {
+               return parameters;
+       }
+
+       /**
+        * @return String representation of the return value, format is 
fqn>toString
+        */
+       public String getReturnValue() {
+               return returnValue;
+       }
+       
+       /**
+        * @param obj An object
+        * @return A string representation fqn>toString
+        */
+       private String objectString(Object obj) {
+               if (obj == null) {
+                       return "null";
+               } else {
+                       return obj.getClass().getName() + ">" + obj.toString();
+               }
+       }
+       
+       /**
+        * Notifies that this event returned a value
+        * @param retVal The returned value
+        */
+       public void returned(Object retVal) {
+               returnValue = objectString(retVal);
+       }
+       
+       /**
+        * Notifies that this event took parameters. 
+        * @param params the parameters.
+        */
+       public void withParameters(Object... params) {
+               if (params == null) return;
+               if (params.length == 0) return;
+               this.parameters = new String[params.length];
+               for (int i = 0; i < params.length; i++) {
+                       this.parameters[i] = objectString(params[i]);
+               }
+       }
+
+       /**
+        * @return The stacktrace of an error, if occurred.
+        */
+       public String getStackTrace() {
+               return stackTrace;
+       }
+       
+       /**
+        * @return true is this event registered an exception stack trace
+        */
+       public boolean hasStackTrace() {
+               return this.stackTrace != null;
+       }
+       
+       public void exception(Throwable t) {
+               StringWriter sw = new StringWriter();
+               PrintWriter pw = new PrintWriter(sw);
+               t.printStackTrace(pw);
+               pw.close();
+               this.stackTrace = pw.toString();
+       }
+       
+       
+       
+}

Added: 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventHandler.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventHandler.java?rev=827842&view=auto
==============================================================================
--- 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventHandler.java
 (added)
+++ 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventHandler.java
 Tue Oct 20 23:55:22 2009
@@ -0,0 +1,71 @@
+package org.apache.magma.tools.devtools.domain;
+
+import org.aspectj.lang.JoinPoint;
+
+/**
+ * Handler for events decoration by sub systems.
+ * 
+ * Each sub system can annotate which methods or fields deserve to 
+ * generate events using the {...@link Traced} annotation, and specify
+ * which handler should handle event decoration.
+ * 
+ * When such a method is executed (or field accessed) a {...@link TraceEvent}
+ * is created and passed to the handler {...@link #handleEntering(TraceEvent, 
JoinPoint)}
+ * method.
+ * 
+ * This method can then setup the event correctly, like setting the sub system
+ * name, the event type and optional properties in the property bag of the 
event.
+ * It can gather data from the {...@link JoinPoint}.
+ * 
+ * It can return the same event modified, or eventually, in the future, return
+ * a specific subclass.
+ * 
+ * Same happens for {...@link #handleReturning(TraceEvent, Object, JoinPoint)} 
and
+ * {...@link #handleError(TraceEvent, Throwable, JoinPoint)} methods.
+ * 
+ * Classes implementing this interface MUST be thread safe and MUST have
+ * a no arg constructor.
+ * 
+ * @author Simone Gianni <[email protected]>
+ */
+public interface TraceEventHandler {
+
+       /**
+        * Called when execution is entering a joinpoint (method execution or
+        * field get/set) annotated with {...@link Traced} having as a value 
this
+        * handler.
+        * @param event The event this handler can modify.
+        * @param enterjp The join point this handler can use to gather data.
+        * @return The same event or, in the future, a specific subclass.
+        */
+       public TraceEvent handleEntering(TraceEvent event, JoinPoint enterjp);
+       
+       /**
+        * Called when execution is exiting correctly from a joinpoint 
+        * (method execution or field get/set) annotated with {...@link Traced} 
+        * having as a value this handler.
+        * 
+        * Depending on the kind of joinpoint, the value can be the value
+        * returned from the method (or null if void), the value of the field
+        * accessed when reading, or null if the field was accessed for writing 
it.
+        * 
+        * @param event The event this handler can modify.
+        * @param retValue The value returned by the join point.
+        * @param exitjp The join point this handler can use to gather data.
+        * @return The same event or, in the future, a specific subclass.
+        */
+       public TraceEvent handleReturning(TraceEvent event, Object retValue, 
JoinPoint exitjp);
+       
+       /**
+        * Called when execution is exiting with an exception from a joinpoint 
+        * (method execution or field get/set) annotated with {...@link Traced} 
+        * having as a value this handler.
+        * 
+        * @param event The event this handler can modify.
+        * @param error The error that happened.
+        * @param exitjp The join point this handler can use to gather data.
+        * @return The same event or, in the future, a specific subclass.
+        */
+       public TraceEvent handleError(TraceEvent event, Throwable error, 
JoinPoint exitjp);
+       
+}

Added: 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventPriority.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventPriority.java?rev=827842&view=auto
==============================================================================
--- 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventPriority.java
 (added)
+++ 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/TraceEventPriority.java
 Tue Oct 20 23:55:22 2009
@@ -0,0 +1,32 @@
+package org.apache.magma.tools.devtools.domain;
+
+/**
+ * {...@link TraceEvent} priority, used to gain user attention.
+ * 
+ * @author Simone Gianni <[email protected]>
+ */
+public enum TraceEventPriority {
+
+       /**
+        * DEBUG is normally used to trace normal events
+        */
+       DEBUG,
+       /**
+        * INFO denotes something the user may seriously be interested in,
+        * but not vital for execution.
+        */
+       INFO,
+       /**
+        * WARN denotes something the user should seriously consider. 
+        */
+       WARN,
+       /**
+        * ERROR denotes that an error has happened and it must be fixed
+        */
+       ERROR,
+       /**
+        * FATAL is for real emergencies
+        */
+       FATAL
+       
+}

Added: 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Traced.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Traced.java?rev=827842&view=auto
==============================================================================
--- 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Traced.java
 (added)
+++ 
labs/magma/trunk/devtools-domain/src/main/java/org/apache/magma/tools/devtools/domain/Traced.java
 Tue Oct 20 23:55:22 2009
@@ -0,0 +1,28 @@
+package org.apache.magma.tools.devtools.domain;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation to be placed on methods, fields and constructors
+ * that generates {...@link TraceEvent}s.
+ * 
+ * Each subsystem can place this annotations where needed, and/or
+ * inject them using aspects. A specific {...@link TraceEventHandler} can
+ * be specified, that will act as a factory/decorator for the event.
+ * 
+ * @author Simone Gianni <[email protected]>
+ */
+...@retention(RetentionPolicy.RUNTIME)
+...@target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR})
+public @interface Traced {
+
+       Class<? extends TraceEventHandler> handler() default EmptyHandler.class;
+       boolean located() default false;
+       String subsystem();
+       String type() default "";
+       TraceEventPriority priority() default TraceEventPriority.DEBUG;
+       
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to