Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags.
   * See the copyright.txt in the distribution for a full listing
   * of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * 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,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 package com.arjuna.mwlabs.wst.util;
 
 
Helper class for persisting participants.

Author(s):
kevin
 
 {

    
Save the resource state.

Parameters:
os The output object stream.
resource The resource to persist.
Returns:
true if successful, false otherwise.
 
     public static boolean save_state(final OutputObjectState osfinal Object resource)
     {
         if (resource != null)
         {
             try
             {
                 if (resource instanceof Serializable)
                 {
                     os.packBoolean(true) ;
                     final ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
                     final ObjectOutputStream oos = new ObjectOutputStream(baos) ;
                     oos.writeObject(resource) ;
                     oos.flush() ;
                     os.packBytes(baos.toByteArray()) ;
                 }
                 else if (resource instanceof PersistableParticipant)
                 {
                     final PersistableParticipant participant = (PersistableParticipant)resource ;
                     os.packBoolean(false) ;
                     os.packString(resource.getClass().getName()) ;
                     participant.saveState(os) ;
                 }
                 else
                 {
                     return false ;
                 }
                 return true ;
             }
             catch(final Throwable th) {
                 return false;
             }
         }
         else
             return false;
     }

    
Restore the resource state.

Parameters:
is The input object stream.
Returns:
The resource if successful, null otherwise.
 
     public static Object restore_state(final InputObjectState ios)
     {
         try
         {
             final boolean serializable = ios.unpackBoolean() ;
             if (serializable)
             {
                 final ByteArrayInputStream bais = new ByteArrayInputStream(ios.unpackBytes()) ;
                 final ObjectInputStream ois = new ObjectInputStream(bais) ;
                return ois.readObject() ;
            }
            else
            {
                final String className = ios.unpackString() ;
                final Class resourceClass = ClassLoaderHelper.forName(PersistableParticipantHelper.classclassName) ; // returns Class not instance
                final Object resource = resourceClass.newInstance();
                ((PersistableParticipant)resource).restoreState(ios) ;
                return resource ;
            }
        }
        catch (final Throwable th) {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X