Author: clebert.suconic(a)jboss.com
Date: 2011-10-06 16:27:03 -0400 (Thu, 06 Oct 2011)
New Revision: 11481
Added:
branches/Branch_2_2_EAP/src/main/org/hornetq/utils/ClassloadingUtil.java
Modified:
branches/Branch_2_2_EAP/src/main/org/hornetq/core/logging/Logger.java
Log:
Back porting HORNETQ-681
Modified: branches/Branch_2_2_EAP/src/main/org/hornetq/core/logging/Logger.java
===================================================================
--- branches/Branch_2_2_EAP/src/main/org/hornetq/core/logging/Logger.java 2011-10-06
19:51:38 UTC (rev 11480)
+++ branches/Branch_2_2_EAP/src/main/org/hornetq/core/logging/Logger.java 2011-10-06
20:27:03 UTC (rev 11481)
@@ -19,6 +19,7 @@
import org.hornetq.core.logging.impl.JULLogDelegateFactory;
import org.hornetq.spi.core.logging.LogDelegate;
import org.hornetq.spi.core.logging.LogDelegateFactory;
+import org.hornetq.utils.ClassloadingUtil;
/**
*
@@ -80,16 +81,7 @@
if (className != null)
{
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- try
- {
- Class<?> clz = loader.loadClass(className);
- delegateFactory = (LogDelegateFactory)clz.newInstance();
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Error instantiating transformer
class \"" + className + "\"", e);
- }
+ delegateFactory = (LogDelegateFactory)
ClassloadingUtil.safeInitNewInstance(className);
}
else
{
Added: branches/Branch_2_2_EAP/src/main/org/hornetq/utils/ClassloadingUtil.java
===================================================================
--- branches/Branch_2_2_EAP/src/main/org/hornetq/utils/ClassloadingUtil.java
(rev 0)
+++ branches/Branch_2_2_EAP/src/main/org/hornetq/utils/ClassloadingUtil.java 2011-10-06
20:27:03 UTC (rev 11481)
@@ -0,0 +1,79 @@
+package org.hornetq.utils;
+
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+* A ClassloadingUtil *
+* @author <a href="mailto:hgao@redhat.com">Howard Gao</a>
+*/
+
+public class ClassloadingUtil
+{
+ public static Object safeInitNewInstance(final String className)
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<Object>()
+ {
+ public Object run()
+ {
+ ClassLoader loader = ClassloadingUtil.class.getClassLoader();
+ try
+ {
+ Class<?> clazz = loader.loadClass(className);
+ return clazz.newInstance();
+ }
+ catch (Throwable t)
+ {
+ try
+ {
+ loader = Thread.currentThread().getContextClassLoader();
+ if (loader != null)
+ return loader.loadClass(className).newInstance();
+ }
+ catch (RuntimeException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ }
+
+ throw new IllegalArgumentException("Could not find class " +
className);
+ }
+ }
+ });
+ }
+
+ public static URL findResource(final String resourceName)
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<URL>()
+ {
+ public URL run()
+ {
+ ClassLoader loader = ClassloadingUtil.class.getClassLoader();
+ try
+ {
+ URL resource = loader.getResource(resourceName);
+ if (resource != null)
+ return resource;
+ }
+ catch (Throwable t)
+ {
+ }
+
+ loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null)
+ return null;
+
+ URL resource = loader.getResource(resourceName);
+ if (resource != null)
+ return resource;
+
+ return null;
+ }
+ });
+ }
+
+}
+
Show replies by date