Permalink
Browse files

Add utils.

  • Loading branch information...
1 parent d6502ba commit ed4290ec6c892dbf3af3539b7a4503b3d823479d @NogbadTheBad NogbadTheBad committed Nov 21, 2010
View
@@ -0,0 +1 @@
+target
@@ -0,0 +1,53 @@
+package org.harrison.insight.plugin.mongodb;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.bson.types.ObjectId;
+
+import com.mongodb.DBObject;
+
+/**
+ * Utilities for converting method arguments for MongoDB-related operations
+ *
+ * @author stephen harrison
+ */
+public class ArgUtils {
+ private static Set<Class<?>> WRAPPER_TYPES = new HashSet<Class<?>>(
+ Arrays.asList(Boolean.class, Byte.class, Character.class,
+ Short.class, Integer.class, Long.class, Float.class,
+ Double.class));
+
+ private ArgUtils() {
+ // empty OK
+ }
+
+ public static List<String> toString(final Object[] args) {
+ final List<String> argsAsString = new ArrayList<String>();
+
+ for (final Object arg : args) {
+ argsAsString.add(toString(arg));
+ }
+
+ return argsAsString;
+ }
+
+ public static String toString(final Object object) {
+ if (object == null) {
+ return "null";
+ }
+
+ final Class<? extends Object> cls = object.getClass();
+
+ if (cls.isPrimitive() || object instanceof String
+ || WRAPPER_TYPES.contains(cls) || object instanceof ObjectId
+ || object instanceof DBObject) {
+ return object.toString();
+ }
+
+ return object.getClass().getSimpleName();
+ }
+}
@@ -0,0 +1,28 @@
+package org.harrison.insight.plugin.mongodb;
+
+import com.springsource.insight.intercept.operation.BasicOperation;
+import com.springsource.insight.intercept.operation.OperationType;
+import com.springsource.insight.intercept.operation.SourceCodeLocation;
+
+/**
+ * A Spring Insight {@link BasicOperation} that collects MongoDB DBCollection
+ * operations
+ *
+ * @author stephen harrison
+ */
+public class MongoCursorOperation extends BasicOperation {
+ public static final String NAME = "mongo_cursor_operation";
+ public static final OperationType TYPE = OperationType.valueOf(NAME);
+
+ public MongoCursorOperation(final SourceCodeLocation scl) {
+ super(scl);
+ }
+
+ public String getLabel() {
+ return "MongoDB: DBCursor.next()";
+ }
+
+ public OperationType getType() {
+ return TYPE;
+ }
+}
@@ -0,0 +1,29 @@
+package org.harrison.insight.plugin.mongodb;
+
+import org.aspectj.lang.JoinPoint;
+
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.springsource.insight.collection.AbstractOperationCollectionAspect;
+import com.springsource.insight.intercept.operation.Operation;
+
+public aspect MongoCursorOperationCollectionAspect extends
+ AbstractOperationCollectionAspect {
+
+ public pointcut nextExecute()
+ : execution(DBObject DBCursor.next());
+
+ /**
+ * Many of the MongoDB Java driver methods are chained, so we use cflowbelow
+ * to cull subsequent calls.
+ */
+ public pointcut collectionPoint()
+:
+ (nextExecute() && !cflowbelow(nextExecute()))
+;
+
+ @Override
+ protected Operation createOperation(final JoinPoint joinPoint) {
+ return new MongoCursorOperation(getSourceCodeLocation(joinPoint));
+ }
+}
@@ -0,0 +1,44 @@
+package org.harrison.insight.plugin.mongodb;
+
+import java.util.List;
+
+import com.springsource.insight.intercept.operation.BasicOperation;
+import com.springsource.insight.intercept.operation.OperationType;
+import com.springsource.insight.intercept.operation.SourceCodeLocation;
+
+/**
+ * A Spring Insight {@link BasicOperation} that collects MongoDB DB operations
+ *
+ * @author stephen harrison
+ */
+public class MongoDbOperation extends BasicOperation {
+ public static final String NAME = "mongo_db_operation";
+ public static final OperationType TYPE = OperationType.valueOf(NAME);
+
+ private final List<String> args;
+ private final String signature;
+
+ public MongoDbOperation(final SourceCodeLocation scl,
+ final List<String> args, final String signature) {
+ super(scl);
+
+ this.args = args;
+ this.signature = signature;
+ }
+
+ public String getLabel() {
+ return "MongoDB: " + signature;
+ }
+
+ public OperationType getType() {
+ return TYPE;
+ }
+
+ public String getSignature() {
+ return signature;
+ }
+
+ public List<String> getArgs() {
+ return args;
+ }
+}
@@ -0,0 +1,34 @@
+package org.harrison.insight.plugin.mongodb;
+
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+
+import com.mongodb.CommandResult;
+import com.mongodb.DB;
+import com.springsource.insight.collection.AbstractOperationCollectionAspect;
+import com.springsource.insight.intercept.operation.Operation;
+
+public aspect MongoDbOperationCollectionAspect extends
+ AbstractOperationCollectionAspect {
+
+ public pointcut commandExecute()
+ : execution(CommandResult DB.command(..));
+
+ /**
+ * Many of the MongoDB Java driver methods are chained, so we use cflowbelow
+ * to cull subsequent calls.
+ */
+ public pointcut collectionPoint()
+:
+ commandExecute()
+;
+
+ @Override
+ protected Operation createOperation(final JoinPoint joinPoint) {
+ final Signature signature = joinPoint.getSignature();
+
+ return new MongoDbOperation(getSourceCodeLocation(joinPoint),
+ ArgUtils.toString(joinPoint.getArgs()),
+ signature.toShortString());
+ }
+}
@@ -0,0 +1,11 @@
+<#ftl strip_whitespace=true>
+<#import "/insight-1.0.ftl" as insight />
+
+<@insight.group label="MongoDB DBCursor">
+ <@insight.entry name="Collection">
+ <em>unknown</em>
+ </@insight.entry>
+ <@insight.entry name="Return">
+ ${operation.returnValue?html}
+ </@insight.entry>
+</@insight.group>
@@ -0,0 +1,11 @@
+<#ftl strip_whitespace=true>
+<#import "/insight-1.0.ftl" as insight />
+
+<@insight.group label="MongoDB DB">
+ <@insight.entry name="Params" if=operation.args?has_content>
+ <@insight.list type="ordered" collection=operation.args />
+ </@insight.entry>
+ <@insight.entry name="Return">
+ ${operation.returnValue?html}
+ </@insight.entry>
+</@insight.group>

0 comments on commit ed4290e

Please sign in to comment.