Skip to content

Commit

Permalink
[#3933] Refactor ApplicationContext
Browse files Browse the repository at this point in the history
 - Refactor dependency of the InterceptorRegistryBinder
  • Loading branch information
emeroad committed Apr 2, 2018
1 parent e4492de commit 3487e70
Show file tree
Hide file tree
Showing 37 changed files with 541 additions and 390 deletions.
Expand Up @@ -18,6 +18,7 @@

import com.navercorp.pinpoint.bootstrap.util.NumberUtils;
import com.navercorp.pinpoint.bootstrap.util.spring.PropertyPlaceholderHelper;
import com.navercorp.pinpoint.common.annotations.VisibleForTesting;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.common.util.logger.CommonLogger;
import com.navercorp.pinpoint.common.util.PropertyUtils;
Expand Down Expand Up @@ -98,6 +99,9 @@ public static ProfilerConfig load(String pinpointConfigFileName) throws IOExcept

private int interceptorRegistrySize = 1024 * 8;

@VisibleForTesting
private boolean staticResourceCleanup = false;

private String collectorSpanServerIp = DEFAULT_IP;
private int collectorSpanServerPort = 9996;

Expand Down Expand Up @@ -395,6 +399,15 @@ public long getAgentInfoSendRetryInterval() {
return agentInfoSendRetryInterval;
}

@Override
public boolean getStaticResourceCleanup() {
return staticResourceCleanup;
}

public void setStaticResourceCleanup(boolean staticResourceCleanup) {
this.staticResourceCleanup = staticResourceCleanup;
}


@Override
public Filter<String> getProfilableClassFilter() {
Expand Down
Expand Up @@ -16,6 +16,9 @@

package com.navercorp.pinpoint.bootstrap.config;

import com.navercorp.pinpoint.common.annotations.InterfaceAudience;
import com.navercorp.pinpoint.common.annotations.VisibleForTesting;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -108,6 +111,10 @@ public interface ProfilerConfig {

long getAgentInfoSendRetryInterval();

@InterfaceAudience.Private
@VisibleForTesting
boolean getStaticResourceCleanup();


Filter<String> getProfilableClassFilter();

Expand Down
@@ -0,0 +1,42 @@
/*
* Copyright 2018 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.test;

import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;

/**
* @author Woonduk Kang(emeroad)
*/
public class InterceptorRegistryModule extends AbstractModule {
private final InterceptorRegistryBinder interceptorRegistryBinder;

public static Module wrap(InterceptorRegistryBinder interceptorRegistryBinder) {
return new InterceptorRegistryModule(interceptorRegistryBinder);
}

private InterceptorRegistryModule(InterceptorRegistryBinder interceptorRegistryBinder) {
this.interceptorRegistryBinder = Assert.requireNonNull(interceptorRegistryBinder, "interceptorRegistryBinder must not be null");
}

@Override
protected void configure() {
bind(InterceptorRegistryBinder.class).toInstance(interceptorRegistryBinder);
}
}
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.test;


import com.google.inject.Injector;
import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext;

import com.navercorp.pinpoint.bootstrap.AgentOption;
Expand All @@ -29,24 +30,17 @@
* @author hyungil.jeong
*/
public class MockApplicationContext extends DefaultApplicationContext {
private final InterceptorRegistryBinder interceptorRegistryBinder;


public MockApplicationContext(AgentOption agentOption, InterceptorRegistryBinder binder, ModuleFactory moduleFactory) {
super(agentOption, binder, moduleFactory);

if (binder == null) {
throw new NullPointerException("binder must not be null");
}
this.interceptorRegistryBinder = binder;
public MockApplicationContext(AgentOption agentOption, ModuleFactory moduleFactory) {
super(agentOption, moduleFactory);
}

@Override
public void close() {
super.close();

if (this.interceptorRegistryBinder != null) {
interceptorRegistryBinder.unbind();
}
final Injector injector = this.getInjector();
InterceptorRegistryBinder binder = injector.getInstance(InterceptorRegistryBinder.class);
binder.unbind();
}
}
Expand Up @@ -16,6 +16,7 @@

package com.navercorp.pinpoint.test;

import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.util.Modules;
import com.navercorp.pinpoint.bootstrap.AgentOption;
Expand All @@ -28,6 +29,7 @@
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.profiler.context.module.ApplicationContextModule;
import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext;
import com.navercorp.pinpoint.profiler.context.module.ModuleFactory;
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;

Expand All @@ -43,9 +45,9 @@ public class MockApplicationContextFactory {
public MockApplicationContextFactory() {
}

public MockApplicationContext of(String configPath) {
public DefaultApplicationContext build(String configPath) {
ProfilerConfig profilerConfig = readProfilerConfig(configPath, MockApplicationContext.class.getClassLoader());
return of(profilerConfig);
return build(profilerConfig);
}

private ProfilerConfig readProfilerConfig(String configPath, ClassLoader classLoader) {
Expand All @@ -72,13 +74,18 @@ private String getFilePath(ClassLoader classLoader, String configPath) {
return resource.getPath();
}

public MockApplicationContext of(ProfilerConfig config) {
InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder();
public DefaultApplicationContext build(ProfilerConfig config) {
DefaultApplicationContext context = build(config, newModuleFactory());
bindInterceptorRegistry(context.getInjector());
return context;
}

private void bindInterceptorRegistry(Injector injector) {
InterceptorRegistryBinder binder = injector.getInstance(InterceptorRegistryBinder.class);
binder.bind();
return of(config, binder, newModuleFactory());
}

public MockApplicationContext of(ProfilerConfig config, InterceptorRegistryBinder binder, ModuleFactory moduleFactory) {
public DefaultApplicationContext build(ProfilerConfig config, ModuleFactory moduleFactory) {
Instrumentation instrumentation = new DummyInstrumentation();
String mockAgent = "mockAgent";
String mockApplicationName = "mockApplicationName";
Expand All @@ -88,22 +95,16 @@ public MockApplicationContext of(ProfilerConfig config, InterceptorRegistryBinde

AgentOption agentOption = new DefaultAgentOption(instrumentation, mockAgent, mockApplicationName, config, new URL[0],
null, serviceTypeRegistryService, annotationKeyRegistryService);
return new MockApplicationContext(agentOption, binder, moduleFactory);
return new MockApplicationContext(agentOption, moduleFactory);
}


public ModuleFactory newModuleFactory() {
private ModuleFactory newModuleFactory() {
Module pluginModule = new MockApplicationContextModule();

ModuleFactory moduleFactory = new ModuleFactory() {
@Override
public Module newModule(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) {

Module module = new ApplicationContextModule(agentOption, interceptorRegistryBinder);
Module pluginModule = new MockApplicationContextModule();

return Modules.override(module).with(pluginModule);
}
};
InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder();
Module interceptorRegistryModule = InterceptorRegistryModule.wrap(binder);
ModuleFactory moduleFactory = new OverrideModuleFactory(pluginModule, interceptorRegistryModule);
return moduleFactory;

}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.navercorp.pinpoint.bootstrap.interceptor.registry.InterceptorRegistryAdaptor;
import com.navercorp.pinpoint.profiler.AgentInformation;
import com.navercorp.pinpoint.profiler.context.module.ApplicationContextModule;
import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext;
import com.navercorp.pinpoint.profiler.context.module.ModuleFactory;
import com.navercorp.pinpoint.profiler.context.storage.LogStorageFactory;
import com.navercorp.pinpoint.profiler.context.storage.StorageFactory;
Expand All @@ -39,41 +40,16 @@
public class MockTraceContextFactory {


public static MockApplicationContext newMockApplicationContext(ProfilerConfig profilerConfig) {
public static DefaultApplicationContext newMockApplicationContext(ProfilerConfig profilerConfig) {

ModuleFactory moduleFactory = new ModuleFactory() {
@Override
public Module newModule(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) {
Module module = new ApplicationContextModule(agentOption, interceptorRegistryBinder);
Module loggingModule = new LoggingModule();

LoggingModule loggingModule = new LoggingModule();
return Modules.override(module).with(loggingModule);
}
};
InterceptorRegistryBinder interceptorRegistryBinder = new EmptyInterceptorRegistryBinder();
Module interceptorRegistryModule = InterceptorRegistryModule.wrap(interceptorRegistryBinder);
ModuleFactory moduleFactory = new OverrideModuleFactory(loggingModule, interceptorRegistryModule);

MockApplicationContextFactory factory = new MockApplicationContextFactory();
InterceptorRegistryBinder binder = new InterceptorRegistryBinder() {
@Override
public void bind() {

}

@Override
public void unbind() {

}

@Override
public InterceptorRegistryAdaptor getInterceptorRegistryAdaptor() {
return null;
}

@Override
public String getInterceptorRegistryClassName() {
return null;
}
};
return factory.of(profilerConfig, binder, moduleFactory);
return factory.build(profilerConfig, moduleFactory);
}

public static class LoggingModule extends AbstractModule {
Expand All @@ -85,4 +61,26 @@ protected void configure() {
}
}


public static class EmptyInterceptorRegistryBinder implements InterceptorRegistryBinder {
@Override
public void bind() {

}

@Override
public void unbind() {

}

@Override
public InterceptorRegistryAdaptor getInterceptorRegistryAdaptor() {
return null;
}

@Override
public String getInterceptorRegistryClassName() {
return null;
}
};
}
@@ -0,0 +1,41 @@
/*
* Copyright 2018 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.test;

import com.google.inject.Module;
import com.google.inject.util.Modules;
import com.navercorp.pinpoint.bootstrap.AgentOption;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.profiler.context.module.ApplicationContextModule;
import com.navercorp.pinpoint.profiler.context.module.ModuleFactory;

/**
* @author Woonduk Kang(emeroad)
*/
public class OverrideModuleFactory implements ModuleFactory {
private final Module[] with;

public OverrideModuleFactory(Module... with) {
this.with = Assert.requireNonNull(with, "with must not be null");
}

@Override
public Module newModule(AgentOption agentOption) {
Module module = new ApplicationContextModule(agentOption);
return Modules.override(module).with(with);
}
}
Expand Up @@ -40,7 +40,6 @@
import com.navercorp.pinpoint.profiler.context.module.ApplicationContextModule;
import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext;
import com.navercorp.pinpoint.profiler.context.module.ModuleFactory;
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;

import com.google.common.base.Objects;
import com.navercorp.pinpoint.bootstrap.AgentOption;
Expand All @@ -59,7 +58,7 @@
import com.navercorp.pinpoint.profiler.DefaultAgent;
import com.navercorp.pinpoint.profiler.context.Span;
import com.navercorp.pinpoint.profiler.context.SpanEvent;
import com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.util.JavaAssistUtils;
import com.navercorp.pinpoint.thrift.dto.TAnnotation;
import com.navercorp.pinpoint.thrift.dto.TIntStringStringValue;
Expand All @@ -84,27 +83,17 @@ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier


public PluginTestAgent(AgentOption agentOption) {
super(agentOption, new DefaultInterceptorRegistryBinder());
super(agentOption);
this.annotationKeyRegistryService = agentOption.getAnnotationKeyRegistryService();
PluginTestVerifierHolder.setInstance(this);
}

@Override
protected ApplicationContext newApplicationContext(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) {
protected ApplicationContext newApplicationContext(AgentOption agentOption) {

this.pluginApplicationContextModule = new PluginApplicationContextModule();

final ModuleFactory moduleFactory = new ModuleFactory() {
@Override
public Module newModule(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) {

Module module = new ApplicationContextModule(agentOption, interceptorRegistryBinder);

return Modules.override(module).with(pluginApplicationContextModule);
}
};

ApplicationContext applicationContext = new DefaultApplicationContext(agentOption, interceptorRegistryBinder, moduleFactory);
ModuleFactory moduleFactory = new OverrideModuleFactory(pluginApplicationContextModule);
ApplicationContext applicationContext = new DefaultApplicationContext(agentOption, moduleFactory);
return applicationContext;

}
Expand Down

0 comments on commit 3487e70

Please sign in to comment.