Merge pull request #119 from emeroad/scope_api_refactoring

Scope api refactoring
This commit is contained in:
Woonduk Kang
2015-01-19 20:18:25 +09:00
4 changed files with 42 additions and 21 deletions
@@ -29,6 +29,8 @@ public interface ByteCodeInstrumentor {
Scope getScope(String scopeName);
Scope getScope(ScopeDefinition scopeDefinition);
Class<?> defineClass(ClassLoader classLoader, String defineClass, ProtectionDomain protectedDomain) throws InstrumentException;
Interceptor newInterceptor(ClassLoader classLoader, ProtectionDomain protectedDomain, String interceptorFQCN) throws InstrumentException;
@@ -30,4 +30,24 @@ public class DefaultScopeDefinition implements ScopeDefinition {
public Type getType() {
return type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DefaultScopeDefinition that = (DefaultScopeDefinition) o;
if (!name.equals(that.name)) return false;
if (type != that.type) return false;
return true;
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + type.hashCode();
return result;
}
}
@@ -24,10 +24,7 @@ import java.net.URLClassLoader;
import java.security.ProtectionDomain;
import com.navercorp.pinpoint.bootstrap.Agent;
import com.navercorp.pinpoint.bootstrap.instrument.ByteCodeInstrumentor;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.instrument.Scope;
import com.navercorp.pinpoint.bootstrap.instrument.*;
import com.navercorp.pinpoint.bootstrap.interceptor.Interceptor;
import com.navercorp.pinpoint.bootstrap.interceptor.TargetClassLoader;
import com.navercorp.pinpoint.profiler.util.ScopePool;
@@ -81,12 +78,17 @@ public class JavaAssistByteCodeInstrumentor implements ByteCodeInstrumentor {
@Override
public Scope getScope(String scopeName) {
return getScope(scopeName, false);
final ScopeDefinition scopeDefinition = new DefaultScopeDefinition(scopeName, ScopeDefinition.Type.SIMPLE);
return getScope(scopeDefinition);
}
public Scope getScope(String scopeName, boolean attachment) {
return this.scopePool.getScope(scopeName, attachment);
public Scope getScope(ScopeDefinition scopeDefinition) {
if (scopeDefinition == null) {
throw new NullPointerException("scopeDefinition must not be null");
}
return this.scopePool.getScope(scopeDefinition);
}
private NamedClassPool createClassPool(String[] pathNames, String classPoolName) {
@@ -20,42 +20,39 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.navercorp.pinpoint.bootstrap.instrument.Scope;
import com.navercorp.pinpoint.bootstrap.instrument.ScopeDefinition;
/**
* @author emeroad
*/
public class ScopePool {
private final ConcurrentMap<String, Scope> pool = new ConcurrentHashMap<String, Scope>();
private final ConcurrentMap<ScopeDefinition, Scope> pool = new ConcurrentHashMap<ScopeDefinition, Scope>();
public Scope getScope(String scopeName) {
return getScope(scopeName, false);
}
public Scope getScope(String scopeName, boolean attachment) {
if (scopeName == null) {
public Scope getScope(ScopeDefinition scopeDefinition) {
if (scopeDefinition == null) {
throw new NullPointerException("scopeName must not be null");
}
final Scope scope = this.pool.get(scopeName);
final Scope scope = this.pool.get(scopeDefinition);
if (scope != null) {
return scope;
}
final ScopeFactory factory = createScopeFactory(scopeName, attachment);
final ScopeFactory factory = createScopeFactory(scopeDefinition);
final Scope newScope = new ThreadLocalScope(factory);
final Scope exist = this.pool.putIfAbsent(scopeName, newScope);
final Scope exist = this.pool.putIfAbsent(scopeDefinition, newScope);
if (exist != null) {
return exist;
}
return newScope;
}
private ScopeFactory createScopeFactory(String scopeName, boolean attachment) {
if (attachment) {
return new AttachmentSimpleScopeFactory<Object>(scopeName);
private ScopeFactory createScopeFactory(ScopeDefinition scopeDefinition) {
if (scopeDefinition.getType() == ScopeDefinition.Type.ATTACHMENT) {
return new AttachmentSimpleScopeFactory<Object>(scopeDefinition.getName());
}
return new SimpleScopeFactory(scopeName);
return new SimpleScopeFactory(scopeDefinition.getName());
}
@Override