Mirror of the lttng-dev mailing list
 help / color / mirror / Atom feed
* [lttng-dev] [PATCH lttng-ust] Fix: Try loading classes using the thread context class loader
@ 2014-10-31 18:03 Jérémie Galarneau
  2014-10-31 18:05 ` Mathieu Desnoyers
  0 siblings, 1 reply; 2+ messages in thread
From: Jérémie Galarneau @ 2014-10-31 18:03 UTC (permalink / raw)


LTTngAgent should load classes using the current thread context's
class loader and then try using the system class loader if a
ClassNotFoundException is thrown.

Signed-off-by: J?r?mie Galarneau <jeremie.galarneau at efficios.com>
---
 .../java/org/lttng/ust/agent/LTTngAgent.java       | 26 +++++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java b/liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java
index 293ac84..47b4519 100644
--- a/liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java
+++ b/liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java
@@ -94,8 +94,7 @@ public class LTTngAgent {
 		Class<?> logging;
 
 		try {
-			ClassLoader loader = ClassLoader.getSystemClassLoader();
-			logging = loader.loadClass("org.apache.log4j.spi.LoggingEvent");
+			logging = loadClass("org.apache.log4j.spi.LoggingEvent");
 		} catch (ClassNotFoundException e) {
 			/* Log4j classes not found, no need to create the relevant objects */
 			return false;
@@ -131,10 +130,26 @@ public class LTTngAgent {
 		return true;
 	}
 
+	private Class<?> loadClass(String className) throws ClassNotFoundException {
+		ClassLoader loader;
+		Class<?> loadedClass;
+
+		try {
+			/* Try to load class using the current thread's context class loader */
+			loader = Thread.currentThread().getContextClassLoader();
+			loadedClass = loader.loadClass(className);
+		} catch (ClassNotFoundException e) {
+			/* Loading failed, try using the system class loader */
+			loader = ClassLoader.getSystemClassLoader();
+			loadedClass = loader.loadClass(className);
+		}
+
+		return loadedClass;
+	}
+
 	private void initAgentJULClasses() {
 		try {
-			ClassLoader loader = ClassLoader.getSystemClassLoader();
-			Class<?> lttngJUL = loader.loadClass("org.lttng.ust.agent.jul.LTTngJUL");
+			Class<?> lttngJUL = loadClass("org.lttng.ust.agent.jul.LTTngJUL");
 			this.julUser = (LogFramework)lttngJUL.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(false);
 			this.julRoot = (LogFramework)lttngJUL.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(true);
 			this.useJUL = true;
@@ -154,8 +169,7 @@ public class LTTngAgent {
 
 	private void initAgentLog4jClasses() {
 		try {
-			ClassLoader loader = ClassLoader.getSystemClassLoader();
-			Class<?> lttngLog4j = loader.loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
+			Class<?> lttngLog4j = loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
 			this.log4jUser = (LogFramework)lttngLog4j.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(false);
 			this.log4jRoot = (LogFramework)lttngLog4j.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(true);
 			this.useLog4j = true;
-- 
2.1.2




^ permalink raw reply	[flat|nested] 2+ messages in thread

* [lttng-dev] [PATCH lttng-ust] Fix: Try loading classes using the thread context class loader
  2014-10-31 18:03 [lttng-dev] [PATCH lttng-ust] Fix: Try loading classes using the thread context class loader Jérémie Galarneau
@ 2014-10-31 18:05 ` Mathieu Desnoyers
  0 siblings, 0 replies; 2+ messages in thread
From: Mathieu Desnoyers @ 2014-10-31 18:05 UTC (permalink / raw)


Merged into master and stable-2.6, thanks!

Mathieu

----- Original Message -----
> From: "J?r?mie Galarneau" <jeremie.galarneau@efficios.com>
> To: lttng-dev at lists.lttng.org
> Cc: "mathieu desnoyers" <mathieu.desnoyers at efficios.com>, "J?r?mie Galarneau" <jeremie.galarneau at efficios.com>
> Sent: Friday, October 31, 2014 2:03:47 PM
> Subject: [PATCH lttng-ust] Fix: Try loading classes using the thread context class loader
> 
> LTTngAgent should load classes using the current thread context's
> class loader and then try using the system class loader if a
> ClassNotFoundException is thrown.
> 
> Signed-off-by: J?r?mie Galarneau <jeremie.galarneau at efficios.com>
> ---
>  .../java/org/lttng/ust/agent/LTTngAgent.java       | 26
>  +++++++++++++++++-----
>  1 file changed, 20 insertions(+), 6 deletions(-)
> 
> diff --git a/liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java
> b/liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java
> index 293ac84..47b4519 100644
> --- a/liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java
> +++ b/liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java
> @@ -94,8 +94,7 @@ public class LTTngAgent {
>  		Class<?> logging;
>  
>  		try {
> -			ClassLoader loader = ClassLoader.getSystemClassLoader();
> -			logging = loader.loadClass("org.apache.log4j.spi.LoggingEvent");
> +			logging = loadClass("org.apache.log4j.spi.LoggingEvent");
>  		} catch (ClassNotFoundException e) {
>  			/* Log4j classes not found, no need to create the relevant objects */
>  			return false;
> @@ -131,10 +130,26 @@ public class LTTngAgent {
>  		return true;
>  	}
>  
> +	private Class<?> loadClass(String className) throws ClassNotFoundException
> {
> +		ClassLoader loader;
> +		Class<?> loadedClass;
> +
> +		try {
> +			/* Try to load class using the current thread's context class loader */
> +			loader = Thread.currentThread().getContextClassLoader();
> +			loadedClass = loader.loadClass(className);
> +		} catch (ClassNotFoundException e) {
> +			/* Loading failed, try using the system class loader */
> +			loader = ClassLoader.getSystemClassLoader();
> +			loadedClass = loader.loadClass(className);
> +		}
> +
> +		return loadedClass;
> +	}
> +
>  	private void initAgentJULClasses() {
>  		try {
> -			ClassLoader loader = ClassLoader.getSystemClassLoader();
> -			Class<?> lttngJUL = loader.loadClass("org.lttng.ust.agent.jul.LTTngJUL");
> +			Class<?> lttngJUL = loadClass("org.lttng.ust.agent.jul.LTTngJUL");
>  			this.julUser = (LogFramework)lttngJUL.getDeclaredConstructor(new Class[]
>  			{Boolean.class}).newInstance(false);
>  			this.julRoot = (LogFramework)lttngJUL.getDeclaredConstructor(new Class[]
>  			{Boolean.class}).newInstance(true);
>  			this.useJUL = true;
> @@ -154,8 +169,7 @@ public class LTTngAgent {
>  
>  	private void initAgentLog4jClasses() {
>  		try {
> -			ClassLoader loader = ClassLoader.getSystemClassLoader();
> -			Class<?> lttngLog4j =
> loader.loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
> +			Class<?> lttngLog4j = loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
>  			this.log4jUser = (LogFramework)lttngLog4j.getDeclaredConstructor(new
>  			Class[] {Boolean.class}).newInstance(false);
>  			this.log4jRoot = (LogFramework)lttngLog4j.getDeclaredConstructor(new
>  			Class[] {Boolean.class}).newInstance(true);
>  			this.useLog4j = true;
> --
> 2.1.2
> 
> 

-- 
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-10-31 18:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-31 18:03 [lttng-dev] [PATCH lttng-ust] Fix: Try loading classes using the thread context class loader Jérémie Galarneau
2014-10-31 18:05 ` Mathieu Desnoyers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox