Package org.jboss.mx.remote

Source Code of org.jboss.mx.remote.MethodInvocationResult

/**
* @(#)$Id: MethodInvocationResult.java,v 1.3 2002/11/07 05:44:36 jhaynie Exp $
*/
package org.jboss.mx.remote;

import org.jboss.mx.util.SerializationHelper;
import org.jboss.mx.server.ObjectInputStreamWithClassLoader;
import org.jboss.mx.remote.connector.ConnectorFactory;

import javax.management.loading.DefaultLoaderRepository;
import java.io.Serializable;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

/**
* MethodInvocationResult
*
* @author Jeff Haynie
*/
public class MethodInvocationResult implements Serializable
{
    static final long serialVersionUID = -1892711678044823043L;
    private final boolean isException;
    private final byte data[];
    private final String className;

    public MethodInvocationResult(Object result)
        throws Exception
    {
        this.isException=false;
        if (result!=null)
        {
            data=SerializationHelper.serialize(result);
            className = result.getClass().getName();
        }
        else
        {
            data=null;
            className=null;
        }
    }
    public String toString ()
    {
        return "MethodInvocationResult [className="+className+",isException="+isException+"]";
    }

    public MethodInvocationResult(Throwable exception)
        throws Exception
    {
        this.isException=true;
        if (exception!=null)
        {
            data=SerializationHelper.serialize(exception);
            className = exception.getClass().getName();
        }
        else
        {
            data=null;
            className=null;
        }
    }

    public final boolean hasException()
    {
        return (isException);
    }

    private Class loadClass (String name)
        throws Exception
    {
        try
        {
            //FIRST: try the default MBean class loader
           return DefaultLoaderRepository.loadClass(name);
        }
        catch (Exception ex)   {}

        try
        {
            //SECOND: try the ConnectorFactory
           return ConnectorFactory.loadClass(name);
        }
        catch (Exception ex)   {}

        try
        {
            //THIRD: try this classes classloader
           return this.getClass().getClassLoader().loadClass(name);
        }
        catch (Exception ex )  {}

        throw new ClassNotFoundException(name);
    }
    public final Throwable getException()
        throws Exception
    {
        Class clazz=loadClass(className);
        ClassLoader cl =clazz.getClassLoader();
        //System.err.println(">> getException - clazz ("+className+") returned: "+clazz);
        if (cl==null)
        {
            cl=getClass().getClassLoader();
        }
        ObjectInputStream ois=new ObjectInputStreamWithClassLoader(new ByteArrayInputStream(data), cl);
        return (Throwable)ois.readObject();
    }

    public final Object getResult()
        throws Exception
    {
        if (data==null)
        {
            return null;
        }
        Class clazz=loadClass(className);
        //System.err.println(">> getResult - clazz ("+className+") returned: "+clazz);
        ClassLoader cl =clazz.getClassLoader();
        if (cl==null)
        {
            cl = this.getClass().getClassLoader();
        }
        //System.err.println(">> class classLoader= "+cl);
        ObjectInputStream ois=new ObjectInputStreamWithClassLoader(new ByteArrayInputStream(data), cl);
        return ois.readObject();
    }
}
TOP

Related Classes of org.jboss.mx.remote.MethodInvocationResult

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.