/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.remoting.serialization.impl.java;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import org.jboss.logging.Logger;
import org.jboss.remoting.loading.ObjectInputStreamWithClassLoader;
import org.jboss.remoting.serialization.SerializationManager;
import org.jboss.remoting.serialization.SerializationStreamFactory;
import org.jboss.remoting.serialization.IMarshalledValue;
/**
* $Id: JavaSerializationManager.java,v 1.7 2006/02/27 20:53:50 csuconic Exp $
*
* @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
*/
public class JavaSerializationManager extends SerializationManager
{
protected static final Logger log = Logger.getLogger(JavaSerializationManager.class);
public ObjectInputStream createInput(InputStream input, ClassLoader loader) throws IOException
{
if(log.isTraceEnabled())
{
log.trace("Creating ObjectInputStreamWithClassLoader");
}
return new ObjectInputStreamWithClassLoader(input, loader);
}
public ObjectOutputStream createOutput(OutputStream output) throws IOException
{
if(log.isTraceEnabled())
{
log.trace("Creating ObjectOutputStream");
}
return new ObjectOutputStream(output);
}
/**
* Creates a MarshalledValue that does lazy serialization.
*/
public IMarshalledValue createdMarshalledValue(Object source) throws IOException
{
if(source instanceof IMarshalledValue)
{
return (IMarshalledValue) source;
}
else
{
return new JavaMarshalledValue(source);
}
}
public void sendObject(ObjectOutputStream oos, Object dataObject) throws IOException
{
oos.writeObject(dataObject);
oos.reset();
// to make sure stream gets reset
// Stupid ObjectInputStream holds object graph
// can only be set by the client/server sending a TC_RESET
oos.writeObject(Boolean.TRUE);
oos.flush();
oos.reset();
}
public Object receiveObject(InputStream inputStream, ClassLoader customClassLoader) throws IOException, ClassNotFoundException
{
ObjectInputStream objInputStream = null;
Object obj = null;
if(inputStream instanceof ObjectInputStreamWithClassLoader)
{
if(((ObjectInputStreamWithClassLoader) inputStream).getClassLoader() == null)
{
((ObjectInputStreamWithClassLoader) inputStream).setClassLoader(customClassLoader);
}
objInputStream = (ObjectInputStream) inputStream;
}
/*else if(inputStream instanceof JBossObjectInputStream)
{
if(((JBossObjectInputStream) inputStream).getClassLoader() == null)
{
((JBossObjectInputStream) inputStream).setClassLoader(customClassLoader);
}
objInputStream = (ObjectInputStream) inputStream;
} -- for future reference */
else if(inputStream instanceof ObjectInputStream)
{
objInputStream = (ObjectInputStream) inputStream;
}
else
{
if(customClassLoader != null)
{
objInputStream = SerializationStreamFactory.getManagerInstance(SerializationStreamFactory.JAVA).createInput(inputStream, customClassLoader);
}
else
{
objInputStream = SerializationStreamFactory.getManagerInstance(SerializationStreamFactory.JAVA).createRegularInput(inputStream);
}
}
obj = objInputStream.readObject();
try
{
objInputStream.readObject(); // for stupid ObjectInputStream reset
}
catch(Exception e)
{
/**
* Putting try catch around this because if using servlet sever invoker, the previous
* call is not needed, so will throw EOFException and can ignore.
*/
}
return obj;
}
}