private Class<?> injectToBootstrapClassLoader(String className) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { if (injectedToRoot.compareAndSet(false, true)) { instrumentation.appendToBootstrapClassLoaderSearch(pluginJar); classPool.appendToBootstrapClassPath(pluginJar.getName()); } return Class.forName(className, false, null); }
private static void addJarToClassPath(final Instrumentation instrProxy, final JarFile jarfile) { instrProxy.appendToBootstrapClassLoaderSearch(jarfile); }
public void start() { // 1st find boot-strap.jar final ClassPathResolver classPathResolver = new ClassPathResolver(); boolean agentJarNotFound = classPathResolver.findAgentJar(); if (!agentJarNotFound) { logger.severe("pinpoint-bootstrap-x.x.x(-SNAPSHOT).jar Fnot found."); logPinpointAgentLoadFail(); return; } // 2st find boot-strap-core.jar final String bootStrapCoreJar = classPathResolver.getBootStrapCoreJar(); if (bootStrapCoreJar == null) { logger.severe("pinpoint-bootstrap-core-x.x.x(-SNAPSHOT).jar not found"); logPinpointAgentLoadFail(); return; } JarFile bootStrapCoreJarFile = getBootStrapJarFile(bootStrapCoreJar); if (bootStrapCoreJarFile == null) { logger.severe("pinpoint-bootstrap-core-x.x.x(-SNAPSHOT).jar not found"); logPinpointAgentLoadFail(); return; } logger.info("load pinpoint-bootstrap-core-x.x.x(-SNAPSHOT).jar :" + bootStrapCoreJar); instrumentation.appendToBootstrapClassLoaderSearch(bootStrapCoreJarFile); if (!isValidId("pinpoint.agentId", PinpointConstants.AGENT_NAME_MAX_LEN)) { logPinpointAgentLoadFail(); return; } if (!isValidId("pinpoint.applicationName", PinpointConstants.APPLICATION_NAME_MAX_LEN)) { logPinpointAgentLoadFail(); return; } URL[] pluginJars = classPathResolver.resolvePlugins(); TraceMetadataLoaderService typeLoaderService = new DefaultTraceMetadataLoaderService(pluginJars); ServiceTypeRegistryService serviceTypeRegistryService = new DefaultServiceTypeRegistryService(typeLoaderService); AnnotationKeyRegistryService annotationKeyRegistryService = new DefaultAnnotationKeyRegistryService(typeLoaderService); String configPath = getConfigPath(classPathResolver); if (configPath == null) { logPinpointAgentLoadFail(); return; } // set the path of log file as a system property saveLogFilePath(classPathResolver); savePinpointVersion(); try { // Is it right to load the configuration in the bootstrap? ProfilerConfig profilerConfig = ProfilerConfig.load(configPath); // this is the library list that must be loaded List<URL> libUrlList = resolveLib(classPathResolver); AgentClassLoader agentClassLoader = new AgentClassLoader(libUrlList.toArray(new URL[libUrlList.size()])); String bootClass = argMap.containsKey("bootClass") ? argMap.get("bootClass") : BOOT_CLASS; agentClassLoader.setBootClass(bootClass); logger.info("pinpoint agent [" + bootClass + "] starting..."); AgentOption option = createAgentOption( agentArgs, instrumentation, profilerConfig, pluginJars, bootStrapCoreJar, serviceTypeRegistryService, annotationKeyRegistryService); Agent pinpointAgent = agentClassLoader.boot(option); pinpointAgent.start(); registerShutdownHook(pinpointAgent); logger.info("pinpoint agent started normally."); } catch (Exception e) { // unexpected exception that did not be checked above logger.log(Level.SEVERE, ProductInfo.NAME + " start failed. Error:" + e.getMessage(), e); logPinpointAgentLoadFail(); } }
public static void addMixinInterfacesToBootstrap( final JarResource agentJarResource, final URL agentJarUrl, final Instrumentation inst) { boolean succeeded = false; final Pattern packageSearchPattern = Pattern.compile("com/newrelic/agent/instrumentation/pointcuts/(.*).class"); final String interfaceMixinAnnotation = "Lcom/newrelic/agent/instrumentation/pointcuts/InterfaceMixin;"; final String loadOnBootstrapAnnotation = "Lcom/newrelic/agent/instrumentation/pointcuts/LoadOnBootstrap;"; final String interfaceMapperAnnotation = "Lcom/newrelic/agent/instrumentation/pointcuts/InterfaceMapper;"; final String methodMapperAnnotation = "Lcom/newrelic/agent/instrumentation/pointcuts/MethodMapper;"; final String fieldAccessorAnnotation = "Lcom/newrelic/agent/instrumentation/pointcuts/FieldAccessor;"; final List<String> bootstrapAnnotations = Arrays.asList( "Lcom/newrelic/agent/instrumentation/pointcuts/InterfaceMixin;", "Lcom/newrelic/agent/instrumentation/pointcuts/InterfaceMapper;", "Lcom/newrelic/agent/instrumentation/pointcuts/MethodMapper;", "Lcom/newrelic/agent/instrumentation/pointcuts/FieldAccessor;", "Lcom/newrelic/agent/instrumentation/pointcuts/LoadOnBootstrap;"); File generatedFile = null; JarOutputStream outputJarStream = null; try { generatedFile = File.createTempFile("newrelic-bootstrap", ".jar"); final Manifest manifest = createManifest(); outputJarStream = createJarOutputStream(generatedFile, manifest); final long modTime = System.currentTimeMillis(); final Collection<String> fileNames = AgentJarHelper.findJarFileNames(agentJarUrl, packageSearchPattern); for (final String fileName : fileNames) { final int size = (int) agentJarResource.getSize(fileName); final ByteArrayOutputStream out = new ByteArrayOutputStream(size); Streams.copy(agentJarResource.getInputStream(fileName), out, size, true); final byte[] classBytes = out.toByteArray(); final ClassReader cr = new ClassReader(classBytes); final ClassStructure structure = ClassStructure.getClassStructure(cr, 4); final Collection<String> annotations = structure.getClassAnnotations().keySet(); if (containsAnyOf(bootstrapAnnotations, annotations)) { final JarEntry entry = new JarEntry(fileName); entry.setTime(modTime); outputJarStream.putNextEntry(entry); outputJarStream.write(classBytes); } } outputJarStream.closeEntry(); succeeded = true; } catch (IOException iox) { logIfNRDebug("generating mixin jar file", iox); try { outputJarStream.close(); } catch (Throwable th) { logIfNRDebug("closing outputJarStream", th); } } finally { try { outputJarStream.close(); } catch (Throwable th2) { logIfNRDebug("closing outputJarStream", th2); } } if (succeeded) { JarFile jarFile = null; try { jarFile = new JarFile(generatedFile); inst.appendToBootstrapClassLoaderSearch(jarFile); generatedFile.deleteOnExit(); } catch (IOException iox2) { logIfNRDebug("adding dynamic mixin jar to bootstrap", iox2); try { jarFile.close(); } catch (Throwable th3) { logIfNRDebug("closing generated jar file", th3); } } finally { try { jarFile.close(); } catch (Throwable th4) { logIfNRDebug("closing generated jar file", th4); } } } }