Browse files

Fixing classLoading

  • Loading branch information...
1 parent 852f1b4 commit a440641176b407d2e21eb904ca1355ad44cfcb7b @clebertsuconic clebertsuconic committed Nov 18, 2011
View
2 mac-build.sh
@@ -0,0 +1,2 @@
+#you need to define this variable on mac
+./build.sh -Djdk5.home=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/ "$@"
View
14 src/main/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java
@@ -59,6 +59,7 @@
import org.hornetq.spi.core.remoting.ConnectionLifeCycleListener;
import org.hornetq.spi.core.remoting.Connector;
import org.hornetq.spi.core.remoting.ConnectorFactory;
+import org.hornetq.utils.ClassloadingUtil;
import org.hornetq.utils.ConcurrentHashSet;
import org.hornetq.utils.ConfigurationHelper;
import org.hornetq.utils.ExecutorFactory;
@@ -1351,18 +1352,7 @@ private ConnectorFactory instantiateConnectorFactory(final String connectorFacto
{
public ConnectorFactory run()
{
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- try
- {
- Class<?> clazz = loader.loadClass(connectorFactoryClassName);
- return (ConnectorFactory)clazz.newInstance();
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Error instantiating connector factory \"" + connectorFactoryClassName +
- "\"",
- e);
- }
+ return (ConnectorFactory) ClassloadingUtil.newInstanceFromClassLoader(connectorFactoryClassName);
}
});
}
View
35 src/main/org/hornetq/core/logging/Logger.java
@@ -21,6 +21,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;
/**
*
@@ -202,46 +203,16 @@ public void trace(final Object message, final Throwable t)
delegate.trace(message, t);
}
- /** This seems duplicate code all over the place, but for security reasons we can't let something like this to be open in a
- * utility class, as it would be a door to load anything you like in a safe VM.
- * For that reason any class trying to do a privileged block should do with the AccessController directly.
- * @param className
- * @return
- */
+
private static Object safeInitNewInstance(final String className)
{
return AccessController.doPrivileged(new PrivilegedAction<Object>()
{
public Object run()
{
- ClassLoader loader = this.getClass().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);
- }
+ return ClassloadingUtil.newInstanceFromClassLoader(className);
}
});
}
-
-
}
View
25 src/main/org/hornetq/core/remoting/impl/ssl/SSLSupport.java
@@ -32,6 +32,8 @@
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
+import org.hornetq.utils.ClassloadingUtil;
+
/**
* @author <a href="mailto:jmesnil@redhat.com">Jeff Mesnil</a>
*
@@ -197,7 +199,7 @@ private static URL validateStoreURL(final String storePath) throws Exception
throw new Exception("Failed to find a store at " + storePath);
}
-
+
/** This seems duplicate code all over the place, but for security reasons we can't let something like this to be open in a
* utility class, as it would be a door to load anything you like in a safe VM.
* For that reason any class trying to do a privileged block should do with the AccessController directly.
@@ -208,26 +210,7 @@ private static URL findResource(final String resourceName)
{
public URL run()
{
- ClassLoader loader = getClass().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;
+ return ClassloadingUtil.findResource(resourceName);
}
});
}
View
32 src/main/org/hornetq/core/remoting/server/impl/RemotingServiceImpl.java
@@ -52,6 +52,7 @@
import org.hornetq.spi.core.remoting.BufferHandler;
import org.hornetq.spi.core.remoting.Connection;
import org.hornetq.spi.core.remoting.ConnectionLifeCycleListener;
+import org.hornetq.utils.ClassloadingUtil;
import org.hornetq.utils.ConfigurationHelper;
import org.hornetq.utils.HornetQThreadFactory;
@@ -640,44 +641,15 @@ public void run()
}
}
-
- /** This seems duplicate code all over the place, but for security reasons we can't let something like this to be open in a
- * utility class, as it would be a door to load anything you like in a safe VM.
- * For that reason any class trying to do a privileged block should do with the AccessController directly.
- */
private static Object safeInitNewInstance(final String className)
{
return AccessController.doPrivileged(new PrivilegedAction<Object>()
{
public Object run()
{
- ClassLoader loader = getClass().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);
- }
+ return ClassloadingUtil.newInstanceFromClassLoader(className);
}
});
}
-
}
View
26 src/main/org/hornetq/core/server/impl/HornetQServerImpl.java
@@ -121,6 +121,7 @@
import org.hornetq.spi.core.protocol.RemotingConnection;
import org.hornetq.spi.core.protocol.SessionCallback;
import org.hornetq.spi.core.security.HornetQSecurityManager;
+import org.hornetq.utils.ClassloadingUtil;
import org.hornetq.utils.ExecutorFactory;
import org.hornetq.utils.HornetQThreadFactory;
import org.hornetq.utils.OrderedExecutorFactory;
@@ -2109,30 +2110,7 @@ private static Object safeInitNewInstance(final String className)
{
public Object run()
{
- ClassLoader loader = getClass().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);
- }
+ return ClassloadingUtil.newInstanceFromClassLoader(className);
}
});
}
View
28 src/main/org/hornetq/jms/bridge/impl/JMSBridgeImpl.java
@@ -56,6 +56,7 @@
import org.hornetq.jms.bridge.QualityOfServiceMode;
import org.hornetq.jms.client.HornetQMessage;
import org.hornetq.jms.client.HornetQSession;
+import org.hornetq.utils.ClassloadingUtil;
/**
*
@@ -2003,6 +2004,7 @@ public void onException(final JMSException e)
}
}
}
+
/** This seems duplicate code all over the place, but for security reasons we can't let something like this to be open in a
* utility class, as it would be a door to load anything you like in a safe VM.
@@ -2014,33 +2016,9 @@ private static Object safeInitNewInstance(final String className)
{
public Object run()
{
- ClassLoader loader = getClass().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);
- }
+ return ClassloadingUtil.newInstanceFromClassLoader(className);
}
});
}
-
}
View
68 src/main/org/hornetq/utils/ClassloadingUtil.java
@@ -0,0 +1,68 @@
+package org.hornetq.utils;
+
+import java.net.URL;
+
+/**
+* This class will be used to perform generic class-loader operations,
+* such as load a class first using TCCL, and then the classLoader used by HornetQ (ClassloadingUtil.getClass().getClassLoader()).
+*
+* Is't required to use a Security Block on any calls to this class.
+*
+* @author <a href="mailto:hgao@redhat.com">Howard Gao</a>
+*/
+
+public class ClassloadingUtil
+{
+ public static Object newInstanceFromClassLoader(final String className)
+ {
+ 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)
+ {
+ 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;
+ }
+
+}
View
38 src/main/org/hornetq/utils/XMLUtil.java
@@ -18,8 +18,6 @@
import java.io.StringReader;
import java.lang.reflect.Method;
import java.net.URL;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
@@ -483,7 +481,7 @@ public static void validate(final Node node, final String schemaFile) throws Exc
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
- Schema schema = factory.newSchema(findResource(schemaFile));
+ Schema schema = factory.newSchema(ClassloadingUtil.findResource(schemaFile));
Validator validator = schema.newValidator();
// validate the DOM tree
@@ -531,40 +529,6 @@ private static List filter(final NodeList nl, final short[] typesToFilter)
return nodes;
}
- /** This seems duplicate code all over the place, but for security reasons we can't let something like this to be open in a
- * utility class, as it would be a door to load anything you like in a safe VM.
- * For that reason any class trying to do a privileged block should do with the AccessController directly.
- */
- private static URL findResource(final String resourceName)
- {
- return AccessController.doPrivileged(new PrivilegedAction<URL>()
- {
- public URL run()
- {
- ClassLoader loader = getClass().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;
- }
- });
- }
-
// Inner classes --------------------------------------------------------------------------------
}

0 comments on commit a440641

Please sign in to comment.