Skip to content

Commit

Permalink
[#noissue] Cleanup Bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 5, 2024
1 parent 8132e25 commit 7b638bd
Show file tree
Hide file tree
Showing 38 changed files with 177 additions and 184 deletions.
4 changes: 4 additions & 0 deletions agent-module/bootstraps/bootstrap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.navercorp.pinpoint.bootstrap;
final class VersionTemplate {
static final String VERSION_TEMPLATE = "${project.version}";

private VersionTemplate() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.bootstrap.util.StringUtils;
import com.navercorp.pinpoint.common.util.AgentUuidUtils;
import com.navercorp.pinpoint.common.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
Expand All @@ -31,15 +29,12 @@
public class AgentIdResolver {
private final BootLogger logger = BootLogger.getLogger(this.getClass());

private final List<AgentProperties> reverseList;
private final List<AgentProperties> agentPropertyList;

private final IdValidator idValidator = new IdValidator();

public AgentIdResolver(List<AgentProperties> agentPropertyList) {
Objects.requireNonNull(agentPropertyList, "agentPropertyList");

this.reverseList = new ArrayList<>(agentPropertyList);
Collections.reverse(reverseList);
this.agentPropertyList = Objects.requireNonNull(agentPropertyList, "agentPropertyList");
}

public AgentIds resolve() {
Expand Down Expand Up @@ -70,7 +65,7 @@ private String newRandomAgentId() {
}

private String getAgentId() {
for (AgentProperties agentProperty : reverseList) {
for (AgentProperties agentProperty : agentPropertyList) {
final String agentId = agentProperty.getAgentId();
if (StringUtils.isEmpty(agentId)) {
continue;
Expand All @@ -84,7 +79,7 @@ private String getAgentId() {
}

private String getAgentName() {
for (AgentProperties agentProperty : reverseList) {
for (AgentProperties agentProperty : agentPropertyList) {
final String agentName = agentProperty.getAgentName();
if (idValidator.validateAgentName(agentProperty.getType(), agentName)) {
logger.info(agentProperty.getType().getDesc() + " " + agentProperty.getAgentNameKey() + "=" + agentName);
Expand All @@ -95,7 +90,7 @@ private String getAgentName() {
}

private String getApplicationName() {
for (AgentProperties agentProperty : reverseList) {
for (AgentProperties agentProperty : agentPropertyList) {
final String applicationName = agentProperty.getApplicationName();
if (StringUtils.isEmpty(applicationName)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,15 @@
public class AgentIdResolverBuilder {
private final List<AgentProperties> agentProperties = new ArrayList<>();

public void addSystemProperties(Function<String, String> system) {
Objects.requireNonNull(system, "system");

AgentProperties properties = new AgentProperties(AgentIdSourceType.SYSTEM, system);
this.agentProperties.add(properties);
}

public void addEnvProperties(Function<String, String> env) {
public void addProperties(AgentIdSourceType sourceType, Function<String, String> env) {
Objects.requireNonNull(sourceType, "sourceType");
Objects.requireNonNull(env, "env");

AgentProperties properties = new AgentProperties(AgentIdSourceType.SYSTEM_ENV, env);
this.agentProperties.add(properties);
}

public void addAgentArgument(Function<String, String> agentArguments) {
Objects.requireNonNull(agentArguments, "agentArguments");

AgentProperties properties = new AgentProperties(AgentIdSourceType.AGENT_ARGUMENT, agentArguments);
AgentProperties properties = new AgentProperties(sourceType, env);
this.agentProperties.add(properties);
}


public AgentIdResolver build() {
List<AgentProperties> copy = new ArrayList<>(this.agentProperties);
return new AgentIdResolver(copy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public interface AgentOption {

String getApplicationName();

boolean isContainer();

ProfilerConfig getProfilerConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,31 @@

package com.navercorp.pinpoint.bootstrap;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
* @author Woonduk Kang(emeroad)
*/
public enum AgentType {
DEFAULT_AGENT("DEFAULT_AGENT", "com.navercorp.pinpoint.profiler.DefaultAgent"),
PLUGIN_TEST("PLUGIN_TEST", "com.navercorp.pinpoint.profiler.test.PluginTestAgent");
DEFAULT_AGENT("com.navercorp.pinpoint.profiler.DefaultAgent"),
PLUGIN_TEST("com.navercorp.pinpoint.profiler.test.PluginTestAgent");

public static final String AGENT_TYPE_KEY = "AGENT_TYPE";

private final String type;
private final String className;

AgentType(String type, String className) {
this.type = Objects.requireNonNull(type, "type");
AgentType(String className) {
this.className = Objects.requireNonNull(className, "className");
}

public String getType() {
return type;
}

public String getClassName() {
return className;
}

@Override
public String toString() {
return "AgentType{" +
"type='" + type + '\'' +
", className='" + className + '\'' +
'}';
}
Expand All @@ -65,8 +58,11 @@ public static AgentType getAgentType(String agentTypeName) {
throw new IllegalArgumentException("Unknown AgentType:" + agentTypeName);
}

public static AgentType of(Map<String, String> agentArgs) {
String agentTypeString = agentArgs.get(AGENT_TYPE_KEY);
public static AgentType of(Function<String, String> agentArgs) {
String agentTypeString = agentArgs.apply(AGENT_TYPE_KEY);
if (agentTypeString == null) {
agentTypeString = AgentType.DEFAULT_AGENT.name();
}
return getAgentType(agentTypeString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,19 @@ public class DefaultAgentOption implements AgentOption {
private final String agentId;
private final String agentName;
private final String applicationName;
private final boolean isContainer;

private final ProfilerConfig profilerConfig;
private final List<String> pluginJars;
private final List<String> bootstrapJarPaths;

public DefaultAgentOption(final Instrumentation instrumentation,
String agentId, String agentName, String applicationName, final boolean isContainer,
final ProfilerConfig profilerConfig, final List<String> pluginJars, final List<String> bootstrapJarPaths) {
String agentId, String agentName, String applicationName,
final ProfilerConfig profilerConfig, final List<String> pluginJars,
final List<String> bootstrapJarPaths) {
this.instrumentation = Objects.requireNonNull(instrumentation, "instrumentation");
this.agentId = Objects.requireNonNull(agentId, "agentId");
this.agentName = Objects.requireNonNull(agentName, "agentName");
this.applicationName = Objects.requireNonNull(applicationName, "applicationName");
this.isContainer = isContainer;
this.profilerConfig = Objects.requireNonNull(profilerConfig, "profilerConfig");
this.pluginJars = Objects.requireNonNull(pluginJars, "pluginJars");
this.bootstrapJarPaths = Objects.requireNonNull(bootstrapJarPaths, "bootstrapJarPaths");
Expand All @@ -71,11 +70,6 @@ public String getApplicationName() {
return applicationName;
}

@Override
public boolean isContainer() {
return isContainer;
}

@Override
public List<String> getPluginJars() {
return this.pluginJars;
Expand All @@ -98,7 +92,6 @@ public String toString() {
sb.append(", agentId='").append(agentId).append('\'');
sb.append(", agentName='").append(agentName).append('\'');
sb.append(", applicationName='").append(applicationName).append('\'');
sb.append(", isContainer=").append(isContainer);
sb.append(", profilerConfig=").append(profilerConfig);
sb.append(", pluginJars=").append(pluginJars);
sb.append(", bootstrapJarPaths=").append(bootstrapJarPaths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.bootstrap.util.StringUtils;
import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.util.IdValidateUtils;
import com.navercorp.pinpoint.common.util.StringUtils;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@

package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.common.util.CollectionUtils;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -42,24 +38,12 @@ public static URL toURL(Path path) {
}

public static URL[] toURLs(List<Path> pathList) {
if (CollectionUtils.isEmpty(pathList)) {
return new URL[0];
}
Objects.requireNonNull(pathList, "pathList");

List<URL> list = new ArrayList<>(pathList.size());
for (Path path : pathList) {
list.add(PathUtils.toURL(path));
}
return list.toArray(new URL[0]);
}

public static Path toPath(URL url) {
Objects.requireNonNull(url, "url");

try {
return Paths.get(url.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException("URISyntax error", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import com.navercorp.pinpoint.bootstrap.config.AgentSystemConfig;
import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig;
import com.navercorp.pinpoint.bootstrap.config.LogConfig;
import com.navercorp.pinpoint.bootstrap.config.OsEnvSimpleProperty;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfigLoader;
import com.navercorp.pinpoint.bootstrap.config.PropertyLoader;
import com.navercorp.pinpoint.bootstrap.config.PropertyLoaderFactory;
import com.navercorp.pinpoint.common.Version;
import com.navercorp.pinpoint.common.util.OsEnvSimpleProperty;
import com.navercorp.pinpoint.common.util.StringUtils;

import java.lang.instrument.Instrumentation;
import java.net.URL;
Expand Down Expand Up @@ -64,7 +62,7 @@ public PinpointStarter(Instrumentation instrumentation,
// throw new NullPointerException("bootstrapClassLoader");
// }
this.agentArgs = Objects.requireNonNull(agentArgs, "agentArgs");
this.agentType = AgentType.of(agentArgs);
this.agentType = AgentType.of(agentArgs::get);
this.parentClassLoader = parentClassLoader;
this.agentDirectory = Objects.requireNonNull(agentDirectory, "agentDirectory");
this.instrumentation = Objects.requireNonNull(instrumentation, "instrumentation");
Expand Down Expand Up @@ -104,9 +102,6 @@ boolean start() {
return false;
}

final ContainerResolver containerResolver = new ContainerResolver();
final boolean isContainer = containerResolver.isContainer();

try {
final Properties properties = loadProperties();

Expand Down Expand Up @@ -140,7 +135,7 @@ boolean start() {

final List<Path> pluginJars = agentDirectory.getPlugins();
final String agentName = agentIds.getAgentName();
AgentOption option = createAgentOption(agentId, agentName, applicationName, isContainer,
AgentOption option = createAgentOption(agentId, agentName, applicationName,
profilerConfig,
instrumentation,
pluginJars,
Expand Down Expand Up @@ -170,9 +165,9 @@ private List<String> getAgentClassloaderLibs(ProfilerConfig profilerConfig) {

private AgentIds resolveAgentIds() {
AgentIdResolverBuilder builder = new AgentIdResolverBuilder();
builder.addAgentArgument(agentArgs::get);
builder.addEnvProperties(System.getenv()::get);
builder.addSystemProperties(System.getProperties()::getProperty);
builder.addProperties(AgentIdSourceType.SYSTEM, System.getProperties()::getProperty);
builder.addProperties(AgentIdSourceType.SYSTEM_ENV, System.getenv()::get);
builder.addProperties(AgentIdSourceType.AGENT_ARGUMENT, agentArgs::get);
AgentIdResolver agentIdResolver = builder.build();
return agentIdResolver.resolve();
}
Expand All @@ -194,7 +189,7 @@ private Properties loadProperties() {
if (this.agentType == AgentType.PLUGIN_TEST) {
properties.put(DefaultProfilerConfig.PROFILER_INTERCEPTOR_EXCEPTION_PROPAGATE, "true");
}
final String importPluginIds = StringUtils.defaultString(this.agentArgs.get(AgentParameter.IMPORT_PLUGIN), "");
final String importPluginIds = this.agentArgs.getOrDefault(AgentParameter.IMPORT_PLUGIN, "");
properties.put(DefaultProfilerConfig.IMPORT_PLUGIN, importPluginIds);

return properties;
Expand All @@ -205,22 +200,22 @@ private Properties copyJavaSystemProperty() {
}

private Properties copyOSEnvVariables() {
return OsEnvSimpleProperty.copy(System.getenv());
return new OsEnvSimpleProperty().toProperties(System.getenv());
}

private ClassLoader createClassLoader(final String name, final URL[] urls, final ClassLoader parentClassLoader, List<String> libClass) {
return PinpointClassLoaderFactory.createClassLoader(name, urls, parentClassLoader, libClass);
}


private AgentOption createAgentOption(String agentId, String agentName, String applicationName, boolean isContainer,
private AgentOption createAgentOption(String agentId, String agentName, String applicationName,
ProfilerConfig profilerConfig,
Instrumentation instrumentation,
List<Path> pluginJars,
List<Path> bootstrapJarPaths) {
List<String> pluginJarStrPath = toPathList(pluginJars);
List<String> bootstrapJarPathStrPath = toPathList(bootstrapJarPaths);
return new DefaultAgentOption(instrumentation, agentId, agentName, applicationName, isContainer, profilerConfig, pluginJarStrPath, bootstrapJarPathStrPath);
return new DefaultAgentOption(instrumentation, agentId, agentName, applicationName, profilerConfig, pluginJarStrPath, bootstrapJarPathStrPath);
}

private List<String> toPathList(List<Path> paths) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.navercorp.pinpoint.bootstrap;

public class Version {
public static final String VERSION = VersionTemplate.VERSION_TEMPLATE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.navercorp.pinpoint.bootstrap.agentdir;

import com.navercorp.pinpoint.bootstrap.BootLogger;
import com.navercorp.pinpoint.common.annotations.VisibleForTesting;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
Expand Down Expand Up @@ -88,7 +87,6 @@ public Path getPath() {
return Paths.get(URI.create(jarLocation));
}

@VisibleForTesting
String getJarLocation(String className) {
final String internalClassName = className.replace('.', '/') + ".class";
final URL classURL = getResource(internalClassName);
Expand Down Expand Up @@ -137,7 +135,6 @@ public Path getPath() {
return null;
}

@VisibleForTesting
List<String> getInputArguments() {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
return runtimeMXBean.getInputArguments();
Expand Down
Loading

0 comments on commit 7b638bd

Please sign in to comment.