You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sirona.apache.org by rm...@apache.org on 2013/12/09 00:40:15 UTC
svn commit: r1549355 - in /incubator/sirona/trunk/agent/javaagent/src:
main/java/org/apache/sirona/javaagent/AgentContext.java
test/java/org/apache/test/sirona/javaagent/AgentContextTest.java
Author: rmannibucau
Date: Sun Dec 8 23:40:15 2013
New Revision: 1549355
URL: http://svn.apache.org/r1549355
Log:
allowing to get class/method from AgentContext, can be very useful for listener accept methods + hasListeners is useless now we only instrument when needed
Added:
incubator/sirona/trunk/agent/javaagent/src/test/java/org/apache/test/sirona/javaagent/AgentContextTest.java
Modified:
incubator/sirona/trunk/agent/javaagent/src/main/java/org/apache/sirona/javaagent/AgentContext.java
Modified: incubator/sirona/trunk/agent/javaagent/src/main/java/org/apache/sirona/javaagent/AgentContext.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/agent/javaagent/src/main/java/org/apache/sirona/javaagent/AgentContext.java?rev=1549355&r1=1549354&r2=1549355&view=diff
==============================================================================
--- incubator/sirona/trunk/agent/javaagent/src/main/java/org/apache/sirona/javaagent/AgentContext.java (original)
+++ incubator/sirona/trunk/agent/javaagent/src/main/java/org/apache/sirona/javaagent/AgentContext.java Sun Dec 8 23:40:15 2013
@@ -24,6 +24,7 @@ import org.apache.sirona.javaagent.spi.I
import org.apache.sirona.javaagent.spi.Order;
import org.apache.sirona.spi.SPI;
+import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -112,18 +113,15 @@ public class AgentContext {
private final String key;
private final Object reference;
private final InvocationListener[] listeners;
- private final boolean hasListeners;
private final Map<Integer, Object> context = new HashMap<Integer, Object>();
+ private Method method = null;
public AgentContext(final String key, final Object that, final InvocationListener[] listeners) {
this.key = key;
this.reference = that;
this.listeners = listeners;
- this.hasListeners = listeners.length > 0;
- if (this.hasListeners) {
- for (final InvocationListener listener : listeners) {
- listener.before(this);
- }
+ for (final InvocationListener listener : this.listeners) {
+ listener.before(this);
}
}
@@ -135,6 +133,36 @@ public class AgentContext {
return key;
}
+ public Class<?> keyAsClass() {
+ try {
+ return keyAsMethod().getDeclaringClass();
+ } catch (final Throwable th) {
+ return null;
+ }
+ }
+
+ public Method keyAsMethod() {
+ if (method != null) {
+ return method;
+ }
+
+ final int lastDot = key.lastIndexOf('.');
+ try {
+ method = tccl().loadClass(key.substring(0, lastDot)).getDeclaredMethod(key.substring(lastDot + 1));
+ } catch (final Throwable th) {
+ return null;
+ }
+ return method;
+ }
+
+ private static ClassLoader tccl() {
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+ if (contextClassLoader == null) {
+ contextClassLoader = ClassLoader.getSystemClassLoader();
+ }
+ return contextClassLoader;
+ }
+
public <T> T get(final Integer key, final Class<T> clazz) {
return clazz.cast(context.get(key));
}
@@ -152,17 +180,11 @@ public class AgentContext {
}
private void stopListeners(final Object result, final Throwable error) {
- if (hasListeners) {
- for (final InvocationListener listener : listeners) {
- listener.after(this, result, error);
- }
+ for (final InvocationListener listener : listeners) {
+ listener.after(this, result, error);
}
}
- public static void touch() {
- // no-op
- }
-
private static class ListenerComparator implements Comparator<InvocationListener> {
private static final ListenerComparator INSTANCE = new ListenerComparator();
Added: incubator/sirona/trunk/agent/javaagent/src/test/java/org/apache/test/sirona/javaagent/AgentContextTest.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/agent/javaagent/src/test/java/org/apache/test/sirona/javaagent/AgentContextTest.java?rev=1549355&view=auto
==============================================================================
--- incubator/sirona/trunk/agent/javaagent/src/test/java/org/apache/test/sirona/javaagent/AgentContextTest.java (added)
+++ incubator/sirona/trunk/agent/javaagent/src/test/java/org/apache/test/sirona/javaagent/AgentContextTest.java Sun Dec 8 23:40:15 2013
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.test.sirona.javaagent;
+
+import org.apache.sirona.javaagent.AgentContext;
+import org.apache.sirona.javaagent.spi.InvocationListener;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+
+import static org.junit.Assert.assertEquals;
+
+public class AgentContextTest {
+ @Test
+ public void keyClass() {
+ final Class<?> clazz = new AgentContext("org.apache.test.sirona.javaagent.AgentContextTest.keyClass", this, new InvocationListener[0]).keyAsClass();
+ assertEquals(AgentContextTest.class, clazz);
+ }
+
+ @Test
+ public void keyMethod() throws NoSuchMethodException {
+ final Method mtd = new AgentContext("org.apache.test.sirona.javaagent.AgentContextTest.keyMethod", this, new InvocationListener[0]).keyAsMethod();
+ assertEquals(AgentContextTest.class.getMethod("keyMethod"), mtd);
+ }
+}