Author: ron.sigal(a)jboss.com
Date: 2008-08-01 01:52:08 -0400 (Fri, 01 Aug 2008)
New Revision: 4455
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java
Log:
JBREM-1000: Can look in HeirarchicalLoaderRepository3s for scoped classes.
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java
===================================================================
---
remoting2/branches/2.x/src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java 2008-08-01
05:50:12 UTC (rev 4454)
+++
remoting2/branches/2.x/src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java 2008-08-01
05:52:08 UTC (rev 4455)
@@ -22,9 +22,19 @@
package org.jboss.remoting.marshal;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import javax.management.MBeanServer;
+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 +53,15 @@
{
private ServerInvoker invoker = null;
private MBeanServer server = null;
+ private List repositories;
protected final static Logger log = Logger.getLogger(MarshallerLoaderHandler.class);
-
+ public MarshallerLoaderHandler(List repositories)
+ {
+ this.repositories = repositories;
+ }
+
/**
* set the mbean server that the handler can reference
*
@@ -102,9 +117,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 +172,39 @@
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();
+ log.debug("searching repository " + name);
+
+ try
+ {
+ Object o = server.getAttribute(name, "Instance");
+ LoaderRepository repository = (LoaderRepository) o;
+ Class c = repository.getCachedClass(className);
+ if (c == null)
+ {
+ continue;
+ }
+ log.debug("found class in repository " + name);
+ 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;
+ }
}
Show replies by date