JBoss Remoting SVN: r4295 - remoting2/branches/2.2/src/main/org/jboss/remoting/marshal.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2008-06-19 21:23:12 -0400 (Thu, 19 Jun 2008)
New Revision: 4295
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java
Log:
JBREM-962: Can look in HeirarchicalLoaderRepository3s for scoped classes.
Modified: remoting2/branches/2.2/src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java
===================================================================
--- remoting2/branches/2.2/src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java 2008-06-20 01:18:25 UTC (rev 4294)
+++ remoting2/branches/2.2/src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java 2008-06-20 01:23:12 UTC (rev 4295)
@@ -22,9 +22,25 @@
package org.jboss.remoting.marshal;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectOutputStream;
+import java.net.URL;
+import java.util.Iterator;
import java.util.Map;
+import java.util.Set;
+
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanInfo;
+import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
import org.jboss.logging.Logger;
+import org.jboss.mx.loading.LoaderRepository;
+import org.jboss.mx.loading.RepositoryClassLoader;
import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.ServerInvocationHandler;
@@ -43,10 +59,15 @@
{
private ServerInvoker invoker = null;
private MBeanServer server = null;
+ private Set repositories;
protected final static Logger log = Logger.getLogger(MarshallerLoaderHandler.class);
-
+ public MarshallerLoaderHandler(Set repositories)
+ {
+ this.repositories = repositories;
+ }
+
/**
* set the mbean server that the handler can reference
*
@@ -102,9 +123,11 @@
else if(LOAD_CLASS_METHOD.equals(param))
{
String className = (String) metadMap.get(CLASSNAME);
+ log.debug("MarshallerLoaderHandler: loading class: " + className);
if(className != null)
{
ret = loadClassBytes(className, invoker.getClassLoader());
+ log.debug("MarshallerLoaderHandler: returning class: " + className + ": " + ret);
}
else
{
@@ -155,6 +178,33 @@
if(className != null)
{
byte[] classDefinition = ClassUtil.getClassBytes(className, classLoader);
+
+ if (classDefinition == null && repositories != null)
+ {
+ Iterator it = repositories.iterator();
+ while (it.hasNext())
+ {
+ ObjectName name = (ObjectName) it.next();
+
+ try
+ {
+ Object o = server.getAttribute(name, "Instance");
+ LoaderRepository repository = (LoaderRepository) o;
+ Class c = repository.getCachedClass(className);
+ RepositoryClassLoader cl = (RepositoryClassLoader) c.getClassLoader();
+ classDefinition = loadByteCode(cl, className);
+ }
+ catch (Exception e)
+ {
+ log.debug("unable to get class from " + name + ": " + e.getMessage(), e);
+ }
+ }
+ }
+
+ if (classDefinition == null)
+ {
+ log.debug("unable to load class " + className);
+ }
classBytes = new ClassBytes(className, classDefinition);
}
return classBytes;
@@ -179,4 +229,62 @@
{
//NO OP as don't won't allow listeners
}
+
+ /**
+ * Adapted from org.jboss.mx.loading.RepositoryClassLoader.
+ *
+ * @param cl
+ * @param classname
+ * @return
+ * @throws ClassNotFoundException
+ * @throws IOException
+ */
+ protected byte[] loadByteCode(ClassLoader cl, String classname)
+ throws ClassNotFoundException, IOException
+ {
+ byte[] bytecode = null;
+ URL classURL = getClassURL(cl, classname);
+
+ // Load the class bytecode
+ InputStream is = null;
+ try
+ {
+ is = classURL.openStream();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] tmp = new byte[1024];
+ int read = 0;
+ while( (read = is.read(tmp)) > 0 )
+ {
+ baos.write(tmp, 0, read);
+ }
+ bytecode = baos.toByteArray();
+ }
+ finally
+ {
+ if( is != null )
+ is.close();
+ }
+
+ return bytecode;
+ }
+
+ /**
+ * Adapted from org.jboss.mx.loading.RepositoryClassLoader.
+ *
+ * @param cl
+ * @param classname
+ * @return
+ * @throws ClassNotFoundException
+ */
+ private URL getClassURL(ClassLoader cl, String classname) throws ClassNotFoundException
+ {
+ String classRsrcName = classname.replace('.', '/') + ".class";
+ URL classURL = cl.getResource(classRsrcName);
+ if( classURL == null )
+ {
+ String msg = "Failed to find: "+classname+" as resource: "+classRsrcName;
+ throw new ClassNotFoundException(msg);
+ }
+ return classURL;
+ }
}