use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class WeavingClassLoader method processClassPath.
private void processClassPath() throws IOException {
// scan for @Weave or @SkipIfPresent classes and the classes they shadow
Set<String> weaveResourceNames = new HashSet<>();
List<byte[]> weavePackageClasses = new ArrayList<>();
Set<URL> weavePackageURLs = new HashSet<>();
// also scan for NewRelic classes and AgentBridge class
// we use this later to prevent the API JAR's NewRelic class from shadowing the bridge JAR's NewRelic class
Set<ClassResource> newRelicApiClasses = new HashSet<>();
URL agentBridgeSourceURL = null;
List<ClassResource> classResources = ClassResource.fromClassLoader((URLClassLoader) getParent());
for (ClassResource classResource : classResources) {
String resourceName = classResource.resourceName;
String className = resourceName.replace('/', '.').replace(".class", "");
if (weaveIncludes.isIncluded(className)) {
byte[] bytes = classResource.read();
if (usesWeaver(bytes) || isInstrumentationUtilityClass(classResource)) {
weaveResourceNames.add(resourceName);
weavePackageClasses.add(bytes);
weavePackageURLs.add(classResource.sourceURL);
} else {
// Everything else
shadowedClassResources.put(resourceName, classResource);
}
} else if (weaveIncludes.classUnderTestName.startsWith(className)) {
weavePackageURLs.add(classResource.sourceURL);
} else if (className.equals(NEWRELIC_API_CLASS)) {
newRelicApiClasses.add(classResource);
} else if (className.equals(AGENT_BRIDGE_CLASS)) {
agentBridgeSourceURL = classResource.sourceURL;
}
}
// remove resources that aren't actually shadowed
shadowedClassResources.keySet().retainAll(weaveResourceNames);
// this is necessary to support @SkipIfPresent
for (String name : weaveResourceNames) {
if (!shadowedClassResources.containsKey(name)) {
shadowedClassResources.put(name, NO_RESOURCE);
}
}
// create weave package for testing
// @formatter:off
WeavePackageConfig config = WeavePackageConfig.builder().name(TEST_WEAVE_PACKAGE_NAME).source(TEST_WEAVE_PACKAGE_SOURCE).errorHandleClassNode(errorTrapClassNode).weavePreprocessor(weavePreprocessor).weavePostprocessor(weavePostprocessor).build();
// @formatter:on
WeavePackage weavePackage = new WeavePackage(config, weavePackageClasses);
weavePackageManager.register(weavePackage);
// handle NewRelic API class shadowing
if (agentBridgeSourceURL != null) {
for (ClassResource newRelicApiClass : newRelicApiClasses) {
if (agentBridgeSourceURL.equals(newRelicApiClass.sourceURL)) {
shadowedClassResources.put(newRelicApiClass.resourceName, newRelicApiClass);
}
}
}
}
use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class EnumNoNewFieldsTest method testEnumInvalidNewFields.
@Test
public void testEnumInvalidNewFields() throws IOException {
List<byte[]> weaveBytes = new ArrayList<>();
WeavePackageConfig config = WeavePackageConfig.builder().name("weave_unittest").source("com.newrelic.weave.weavepackage.testclasses").build();
weaveBytes.add(WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumNoNewFieldsTest$Day_Weave"));
WeavePackage weavePackage = new WeavePackage(config, weaveBytes);
ClassCache cache = new ClassCache(new ClassLoaderFinder(EnumNoNewFieldsTest.class.getClassLoader()));
PackageValidationResult result = weavePackage.validate(cache);
WeaveTestUtils.expectViolations(result, new WeaveViolation(WeaveViolationType.ENUM_NEW_FIELD, "com/newrelic/weave/EnumNoNewFieldsTest$Day"));
}
use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class WeavePackageVerifier method getWeavePackage.
/**
* Create a {@link WeavePackage} for the specified instrumentation JAR
*
* @param instrumentationJar instrumentation JAR
* @return {@link WeavePackage} for the specified instrumentation JAR
*/
private WeavePackage getWeavePackage(String instrumentationJar) throws Exception {
URL instrumentationJarUrl = new File(instrumentationJar).toURI().toURL();
JarInputStream jarStream = null;
try {
WeavePackageConfig config = WeavePackageConfig.builder().url(instrumentationJarUrl).build();
jarStream = new JarInputStream(instrumentationJarUrl.openStream());
return CachedWeavePackage.createWeavePackage(jarStream, config);
} finally {
if (null != jarStream) {
jarStream.close();
}
}
}
use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class ClassTransformerConfigImpl method isWeavePackageEnabled.
@Override
public boolean isWeavePackageEnabled(WeavePackageConfig weavePackageConfig) {
// user can override with traditional instrumentation module name
String moduleName = weavePackageConfig.getName();
Config instrumentationConfig = getInstrumentationConfig(moduleName);
Boolean moduleNameEnabled = instrumentationConfig.getProperty("enabled");
if (moduleNameEnabled == null) {
if (DEFAULT_DISABLED_WEAVE_PACKAGES.contains(moduleName)) {
moduleNameEnabled = false;
}
}
// ..or, an alias for backwards compatibility with legacy pointcut configurations, if available
String aliasName = weavePackageConfig.getAlias();
Boolean aliasEnabled = null;
if (aliasName != null) {
Config aliasConfig = getInstrumentationConfig(aliasName);
if (aliasConfig.getProperty("enabled") != null) {
Agent.LOG.log(Level.INFO, "Using deprecated configuration setting {0} for instrumentation {1}", aliasName, moduleName);
aliasEnabled = aliasConfig.getProperty("enabled");
}
}
Agent.LOG.log(Level.FINEST, " ### Considering instrumentation: {0}({1}) enabled?{2}({3})", moduleName, aliasName, moduleNameEnabled, aliasEnabled);
if (moduleNameEnabled == null && aliasEnabled == null && !isDefaultInstrumentationEnabled) {
Agent.LOG.log(Level.FINEST, " Instrumentation is disabled by default. Skipping: {0} because it is not explicitly enabled.", moduleName);
// no configuration and instrumentation_disabled is true
return false;
} else if (moduleNameEnabled == null && aliasEnabled == null && isDefaultInstrumentationEnabled) {
// no configuration, return default from weave package
return weavePackageConfig.isEnabled();
} else if (moduleNameEnabled == null) {
// only alias was configured
return aliasEnabled;
} else if (aliasEnabled == null) {
// only module name was configured
return moduleNameEnabled;
} else {
// both were configured
return aliasEnabled && moduleNameEnabled;
}
}
use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class ClassLoaderClassTransformer method buildClassLoaderPatcherPackage.
private WeavePackage buildClassLoaderPatcherPackage() {
WeavePackageConfig classloaderPatcher = WeavePackageConfig.builder().name("agent-classloader-patcher").errorHandleClassNode(LogAndReturnOriginal.ERROR_HANDLER_NODE).extensionClassTemplate(extensionTemplate).build();
List<byte[]> weavePackageBytes = new ArrayList<>();
try {
// Grab the bytes of our Agent Classloader instrumentation class. Note: This call uses "findResource" but
// this is ok because we are not under a classloader lock at this point, we are still in the premain()
byte[] classloaderPatcherInstrumentationBytes = WeaveUtils.getClassBytesFromClassLoaderResource(AgentClassLoaderInstrumentation.class.getName(), ClassLoaderClassTransformer.class.getClassLoader());
weavePackageBytes.add(classloaderPatcherInstrumentationBytes);
} catch (IOException e) {
Agent.LOG.log(Level.FINE, e, "Unable to initialize agent classloader instrumentation");
}
return new WeavePackage(classloaderPatcher, weavePackageBytes);
}
Aggregations