Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * 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  org.jboss.logging.Logger;
$Id: JavaSerializationManager.java 4519 2008-08-30 06:20:07Z ron.sigal@jboss.com $

Author(s):
Clebert Suconic
 
 {
    protected static final Logger log = Logger.getLogger(JavaSerializationManager.class);
 
    public ObjectInputStream createInput(InputStream inputClassLoader loaderthrows IOException
    {
       if(.isTraceEnabled())
       {
          .trace("Creating ObjectInputStreamWithClassLoader");
       }
       return new ObjectInputStreamWithClassLoader(inputloader);
    }
 
    public ObjectOutputStream createOutput(OutputStream outputthrows IOException
    {
       if(.isTraceEnabled())
       {
          .trace("Creating ObjectOutputStream");
       }
       return new ClearableObjectOutputStream(output); 
    }

   
Creates a MarshalledValue that does lazy serialization.
 
    public IMarshalledValue createdMarshalledValue(Object sourcethrows IOException
    {
       if(source instanceof IMarshalledValue)
       {
          return (IMarshalledValuesource;
       }
       else
       {
          return new JavaMarshalledValue(source);
       }
    }
 
     public IMarshalledValue createMarshalledValueForClone(Object originalthrows IOException {
         return createdMarshalledValue(original);
     }
    
    public void sendObject(ObjectOutputStream oosObject dataObjectint versionthrows IOException
    {
       switch (version)
       {
          case .:
          case .:
             sendObjectVersion1_2(oosdataObject);
             break;
             
          case .:
             sendObjectVersion2_2(oosdataObject);
             break;
             
          default:
             throw new IOException("Can not process version " + version + ". " +
                  "Supported versions: " + . + ", " + . + ", " + .);
      }
   }
   
   protected void sendObjectVersion1_2(ObjectOutputStream oosObject dataObjectthrows 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(.);
      oos.flush();
      oos.reset();
   }
   
   protected void sendObjectVersion2_2(ObjectOutputStream oosObject dataObjectthrows IOException
   {
      oos.reset();
      oos.writeObject(dataObject);
      oos.flush();
      
      if (oos instanceof ClearableObjectOutputStream)
      {
         ((ClearableObjectOutputStreamoos).clear();
      }
   }
   
   public Object receiveObject(InputStream inputStreamClassLoader customClassLoaderint versionthrows IOExceptionClassNotFoundException
   {
      switch (version)
      {
         case .:
         case .:
            return receiveObjectVersion1_2(inputStreamcustomClassLoader);
            
         case .:
            return receiveObjectVersion2_2(inputStreamcustomClassLoader);
            
         default:
            throw new IOException("Can not process version " + version + ". " +
                  "Supported versions: " + . + ", " + . + ", " + .);
      }
   }
   
   protected Object receiveObjectVersion1_2(InputStream inputStreamClassLoader customClassLoaderthrows IOExceptionClassNotFoundException
   {
      ObjectInputStream objInputStream = null;
      Object obj = null;
      if(inputStream instanceof ObjectInputStreamWithClassLoader)
      {
         ((ObjectInputStreamWithClassLoaderinputStream).setClassLoader(customClassLoader);
         objInputStream = (ObjectInputStreaminputStream;
      }
      /*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 = (ObjectInputStreaminputStream;
      }
      else
      {
         if(customClassLoader != null)
         {
            objInputStream = SerializationStreamFactory.getManagerInstance(.).createInput(inputStreamcustomClassLoader);
         }
         else
         {
            objInputStream = SerializationStreamFactory.getManagerInstance(.).createRegularInput(inputStream);
         }
      }
      try
      {
      obj = objInputStream.readObject();
      }
      catch (IOException e)
      {
         .debug(""e);
         throw e;
      }
      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;
   }
   
   protected Object receiveObjectVersion2_2(InputStream inputStreamClassLoader customClassLoaderthrows IOExceptionClassNotFoundException
   {
      ObjectInputStream objInputStream = null;
      Object obj = null;
      if(inputStream instanceof ObjectInputStreamWithClassLoader)
      {
         ((ObjectInputStreamWithClassLoaderinputStream).setClassLoader(customClassLoader);
         objInputStream = (ObjectInputStreaminputStream;
      }
      /*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 = (ObjectInputStreaminputStream;
      }
      else
      {
         if(customClassLoader != null)
         {
            objInputStream = SerializationStreamFactory.getManagerInstance(.).createInput(inputStreamcustomClassLoader);
         }
         else
         {
            objInputStream = SerializationStreamFactory.getManagerInstance(.).createRegularInput(inputStream);
         }
      }
      obj = objInputStream.readObject();
      if(inputStream instanceof ObjectInputStreamWithClassLoader)
      {
         ((ObjectInputStreamWithClassLoaderinputStream).clearCache();
      }
      
      return obj;
   }
New to GrepCode? Check out our FAQ X