/*
* Copyright 2014 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.bootstrap;
import java.lang.instrument.Instrumentation;
import java.util.List;
import java.util.Map;
import java.util.jar.JarFile;
import com.navercorp.pinpoint.ProductInfo;
/**
* @author emeroad
* @author netspider
*/
public class PinpointBootStrap {
private static final BootLogger logger = BootLogger.getLogger(PinpointBootStrap.class.getName());
private static final LoadState STATE = new LoadState();
public static void premain(String agentArgs, Instrumentation instrumentation) {
if (agentArgs == null) {
agentArgs = "";
}
logger.info(ProductInfo.NAME + " agentArgs:" + agentArgs);
final boolean success = STATE.start();
if (!success) {
logger.warn("pinpoint-bootstrap already started. skipping agent loading.");
return;
}
Map<String, String> agentArgsMap = argsToMap(agentArgs);
final ClassPathResolver classPathResolver = new AgentDirBaseClassPathResolver();
if (!classPathResolver.verify()) {
logger.warn("Agent Directory Verify fail. skipping agent loading.");
logPinpointAgentLoadFail();
return;
}
BootstrapJarFile bootstrapJarFile = classPathResolver.getBootstrapJarFile();
appendToBootstrapClassLoader(instrumentation, bootstrapJarFile);
PinpointStarter bootStrap = new PinpointStarter(agentArgsMap, bootstrapJarFile, classPathResolver, instrumentation);
if (!bootStrap.start()) {
logPinpointAgentLoadFail();
}
}
private static Map<String, String> argsToMap(String agentArgs) {
ArgsParser argsParser = new ArgsParser();
Map<String, String> agentArgsMap = argsParser.parse(agentArgs);
if (!agentArgsMap.isEmpty()) {
logger.info("agentParameter :" + agentArgs);
}
return agentArgsMap;
}
private static void appendToBootstrapClassLoader(Instrumentation instrumentation, BootstrapJarFile agentJarFile) {
List<JarFile> jarFileList = agentJarFile.getJarFileList();
for (JarFile jarFile : jarFileList) {
logger.info("appendToBootstrapClassLoader:" + jarFile.getName());
instrumentation.appendToBootstrapClassLoaderSearch(jarFile);
}
}
private static void logPinpointAgentLoadFail() {
final String errorLog =
"*****************************************************************************\n" +
"* Pinpoint Agent load failure\n" +
"*****************************************************************************";
System.err.println(errorLog);
}
}