/** * {@inheritDoc} * <p> * We need this method due to the class loading delegation. As we are instrumenting the * {@link ClassLoader#loadClass(String)} with delegation byte code, without this method we * would enter the loop if any our class is loaded via this class loader. Thus, I delegate * this to our {@link #loadClass(String, boolean)} method with <code>false</code> resolve. * Thus, escaping the further delegation checks. */ @Override public Class<?> loadClass(String name) throws ClassNotFoundException { return loadClass(name, false); }
/** {@inheritDoc} */ @Override public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class<?> result = findLoadedClass(name); if (null != result) { if (resolve) { resolveClass(result); } return result; } boolean selfFirst = false; if (!ignoreClasses.contains(name)) { if (selfFirstClasses.contains(name) || ClassLoadingUtil.isInspectITClass(name)) { selfFirst = true; } } // Override parent-first behavior into self-first only for specified classes if (selfFirst) { Class<?> c = findClass(name); if (resolve) { resolveClass(c); } return c; } else { return super.loadClass(name, resolve); } }
super(urls, getParentClassLoader()); File agentFile = getInspectItAgentJarFileLocation(); if (isJar(agentFile.getAbsolutePath())) { addJarResource(agentFile); } else { LOGGER.severe("There was a problem in retrieving the root jar name!");
checkForCorrectSetup(); InspectItClassLoader classLoader = new InspectItClassLoader(new URL[0]); for (String jarPath : classLoader.getBootClassLoaderJarFiles()) { inst.appendToBootstrapClassLoaderSearch(new JarFile(jarPath)); Class<?> agentClazz = classLoader.loadClass(INSPECTIT_AGENT); Constructor<?> constructor = agentClazz.getConstructor(File.class, Instrumentation.class); Object realAgent = constructor.newInstance(getInspectItAgentJarFileLocation(), inst); preloadClasses(); boolean useRetransformation = inst.isRetransformClassesSupported() && Agent.agent.isUsingRetransformation(); inst.addTransformer(new JavaAgent(), useRetransformation); analyzeAlreadyLoadedClasses(useRetransformation); } else { LOGGER.info("inspectIT Agent: Already loaded classes will not be instrumented because redefinition/retransformation is not supported or turned off...");
/** * Analyze this jar file for containing jar files and classes to be used in our own * classloader. * * @param file * the file to analyze * @throws IOException * if something happens on file access. */ private void addJarResource(File file) throws IOException { JarFile jarFile = new JarFile(file); addURL(file.toURI().toURL()); analyzeFile(file); Enumeration<JarEntry> jarEntries = jarFile.entries(); while (jarEntries.hasMoreElements()) { JarEntry jarEntry = jarEntries.nextElement(); if (!jarEntry.isDirectory() && isJar(jarEntry.getName())) { File jar = jarEntryAsFile(jarFile, jarEntry); if (jar.getName().contains("io.opentracing")) { bootClassLoaderJarFiles.add(jar.getAbsolutePath()); } else { addJarResource(jar); } } } }
/** * Checks for the correct setup of the jvm parameters or tries to append the inspectit agent to * the bootstrap class loader search automatically (Java 6+ required). */ private static void checkForCorrectSetup() { try { // we can utilize the mechanism to add the inspectit-agent to the bootstrap classloader // through the instrumentation api. instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(getInspectItAgentJarFileLocation())); instrumentCoreClasses = true; } catch (SecurityException e) { LOGGER.info("inspectIT Agent: Advanced instrumentation capabilities not detected due to security constraints..."); } catch (Exception e) { LOGGER.severe("Something unexpected happened while trying to get advanced instrumentation capabilities!"); e.printStackTrace(); // NOPMD } if (!instrumentCoreClasses) { // 2. try // find out if the bootclasspath option is set List<String> inputArgs = ManagementFactory.getRuntimeMXBean().getInputArguments(); for (String arg : inputArgs) { if (arg.contains("Xbootclasspath") && arg.contains("inspectit-agent.jar")) { instrumentCoreClasses = true; LOGGER.info("inspectIT Agent: Xbootclasspath setting found, activating core class instrumentation..."); break; } } } }