[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jboss-cvs-commits
Subject:    [jboss-cvs] jboss-aop/src/main/org/jboss/aop MarshalledValue.java MarshalledValueInputStream.java Ma
From:       Bill Burke <patriot1burke () users ! sourceforge ! net>
Date:       2003-01-30 21:29:15
[Download RAW message or body]

  User: patriot1burke
  Date: 03/01/30 13:29:12

  Modified:    src/main/org/jboss/aop SimpleMetaData.java
  Added:       src/main/org/jboss/aop MarshalledValue.java
                        MarshalledValueInputStream.java
                        MarshalledValueOutputStream.java
  Log:
  marshalling capability for SimpleMetaData.  Basically moved logic and classes from \
Marshalled Invocation to AOP.  
  Revision  Changes    Path
  1.4       +95 -5     jboss-aop/src/main/org/jboss/aop/SimpleMetaData.java
  
  Index: SimpleMetaData.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-aop/src/main/org/jboss/aop/SimpleMetaData.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SimpleMetaData.java	28 Jan 2003 23:16:48 -0000	1.3
  +++ SimpleMetaData.java	30 Jan 2003 21:29:12 -0000	1.4
  @@ -10,10 +10,12 @@
   package org.jboss.aop;
   import java.util.HashMap;
   import java.util.Iterator;
  +import java.io.IOException;
  +import org.jboss.util.NestedRuntimeException;
   /**
    *
    * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
    *
    */
   public class SimpleMetaData implements MetaDataResolver
  @@ -35,6 +37,17 @@
            this.type = type;
            this.value = value;
         }
  +
  +      public Object get() 
  +         throws java.io.IOException, ClassNotFoundException
  +      {
  +         if (value instanceof MarshalledValue)
  +         {
  +            return ((MarshalledValue)value).get();
  +         }
  +         return value;
  +      }
  +
      }
      
   
  @@ -57,10 +70,21 @@
   
      public synchronized Object getMetaData(String group, String attr)
      {
  -      HashMap groupData = (HashMap)metaData.get(group);
  -      if (groupData == null) return null;
  -      MetaDataValue val = (MetaDataValue)groupData.get(attr);
  -      return val.value;
  +      try
  +      {
  +         HashMap groupData = (HashMap)metaData.get(group);
  +         if (groupData == null) return null;
  +         MetaDataValue val = (MetaDataValue)groupData.get(attr);
  +         return val.get();
  +      }
  +      catch (IOException ioex)
  +      {
  +         throw new NestedRuntimeException("failed on MarshalledValue", ioex);
  +      }
  +      catch (ClassNotFoundException ex)
  +      {
  +         throw new NestedRuntimeException("failed on MarshalledValue", ex);
  +      }
      }
   
      public synchronized void removeMetaData(String group, String attr)
  @@ -103,5 +127,71 @@
      {
         return getMetaData(group, attr);
      }
  +
  +   public void writeExternal(java.io.ObjectOutput out)
  +      throws IOException
  +   {
  +      Iterator it = metaData.keySet().iterator();
  +      while (it.hasNext())
  +      {
  +         String group = (String)it.next();
  +         HashMap map = (HashMap)metaData.get(group);
  +         if (map != null && map.size() > 0)
  +         {
  +            boolean groupWritten = false;
  +            Iterator attrs = map.keySet().iterator();
  +            while (it.hasNext())
  +            {
  +               String attr = (String)attrs.next();
  +               MetaDataValue value = (MetaDataValue)map.get(attr);
  +               if (value.type == TRANSIENT) continue;
  +               if (!groupWritten)
  +               {
  +                  groupWritten = true;
  +                  out.writeObject(group);
  +               }
  +               out.writeObject(attr);
  +               if (value.type == AS_IS)
  +               {
  +                  out.writeObject(value.value);
  +               }
  +               else
  +               {
  +                  out.writeObject(new MarshalledValue(value.value));
  +               }
  +            }
  +            if (groupWritten) out.writeObject(null); // placeholder for end of \
attributes  +         }
  +      }
  +      out.writeObject(null); // place holder for end of marshall
  +   }
  +
  +   public void readExternal(java.io.ObjectInput in)
  +      throws IOException, ClassNotFoundException
  +   {
  +      metaData = new HashMap();
  +      String group;
  +      while ((group = (String)in.readObject()) != null)
  +      {
  +         HashMap map = new HashMap();
  +         metaData.put(group, map);
  +         String attr;
  +         while ((attr = (String)in.readObject()) != null)
  +         {
  +            Object obj = in.readObject();
  +            if (obj instanceof MarshalledValue)
  +            {
  +               map.put(attr, new MetaDataValue(MARSHALLED, obj));
  +            }
  +            else
  +            {
  +               map.put(attr, new MetaDataValue(AS_IS, obj));
  +            }
  +         }
  +      }
  +   }
  +
  +
  +
   
   }
  
  
  
  1.1                  jboss-aop/src/main/org/jboss/aop/MarshalledValue.java
  
  Index: MarshalledValue.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.aop;
  
  import java.io.DataOutputStream;
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.ObjectInput;
  import java.io.ObjectOutput;
  import java.io.OutputStream;
  
  import java.util.Arrays;
  
  /**
   * A simple replacement for the RMI MarshalledObject that uses the thread
   * context class loader for resolving classes and proxies. This currently does
   * not support class annotations and dynamic class loading.
   *
   * @author Scott.Stark@jboss.org
   * @version $Revision: 1.1 $
   */
  public class MarshalledValue
     implements java.io.Externalizable
  {
     /** Serial Version Identifier. */
     private static final long serialVersionUID = -1527598981234110311L;
  
     /**
      * The serialized form of the value. If <code>serializedForm</code> is
      * <code>null</code> then the object marshalled was a <code>null</code>
      * reference.
      */
     private byte[] serializedForm = null;
     
     /**
      * The RMI MarshalledObject hash of the serializedForm array
      */
     private int hashCode;
     private boolean isHashComputed = false;
     
     private ByteArrayOutputStream baos = null;
  
     /**
      * Exposed for externalization.
      */
     public MarshalledValue()
     {
        super();
     }
  
     public MarshalledValue(Object obj) throws IOException
     {
        baos = new ByteArrayOutputStream();
        MarshalledValueOutputStream mvos = new MarshalledValueOutputStream(baos);
        mvos.writeObject(obj);
        mvos.flush();
        
        isHashComputed = false;
     }
  
     public Object get() throws IOException, ClassNotFoundException
     {
        if (serializedForm == null)
           return null;
  
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedForm);
        MarshalledValueInputStream mvis = new MarshalledValueInputStream(bais);
        return mvis.readObject();
     }
  
     public byte[] toByteArray()
     {
        return serializedForm;
     }
  
     public int size()
     {
        int size = serializedForm != null ? serializedForm.length : 0;
        return size;
     }
  
     /**
      * Return a hash code for the serialized form of the value.
      *
      * @return the serialized form value hash.
      */
     public int hashCode()
     {
        // lazy computing of hash: we don't need it most of the time
        //
        if (!isHashComputed)
        {
           int hash = 0;
           for (int i = 0; i < serializedForm.length; i++)
           {
              hash = 31 * hash + serializedForm[i];
           }
           
           hashCode = hash;
        }
  
        return hashCode;
     }
  
     public boolean equals(Object obj)
     {
        if( this == obj )
           return true;
  
        boolean equals = false;
        if( obj instanceof MarshalledValue )
        {
           MarshalledValue mv = (MarshalledValue) obj;
           if( serializedForm == mv.serializedForm )
           {
              equals = true;
           }
           else
           {
              equals = Arrays.equals(serializedForm, mv.serializedForm);
           }
        }
        return equals;
     }
     
     /**
      * The object implements the readExternal method to restore its
      * contents by calling the methods of DataInput for primitive
      * types and readObject for objects, strings and arrays.  The
      * readExternal method must read the values in the same sequence
      * and with the same types as were written by writeExternal.
      *
      * @param in the stream to read data from in order to restore the object
      * 
      * @throws IOException              if I/O errors occur
      * @throws ClassNotFoundException   If the class for an object being
      *                                  restored cannot be found.
      */
     public void readExternal(ObjectInput in) throws IOException, \
ClassNotFoundException  {
        int length = in.readInt();
        serializedForm = null;
        if( length > 0 )
        {
           serializedForm = new byte[length];
           in.readFully(serializedForm);
        }
        isHashComputed = false;
     }
  
     /**
      * The object implements the writeExternal method to save its contents
      * by calling the methods of DataOutput for its primitive values or
      * calling the writeObject method of ObjectOutput for objects, strings,
      * and arrays.
      *
      * @serialData Overriding methods should use this tag to describe
      *            the data layout of this Externalizable object.
      *            List the sequence of element types and, if possible,
      *            relate the element to a public/protected field and/or
      *            method of this Externalizable class.
      *
      * @param out    the stream to write the object to
      * 
      * @throws IOException   Includes any I/O exceptions that may occur
      */
     public void writeExternal(ObjectOutput out) throws IOException
     {
        out.writeInt(baos.size());
        baos.writeTo((OutputStream)out);
     }
  }
  
  
  
  1.1                  \
jboss-aop/src/main/org/jboss/aop/MarshalledValueInputStream.java  
  Index: MarshalledValueInputStream.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.aop;
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectStreamClass;
  
  import org.jboss.logging.Logger;
  
  /**
   * An ObjectInputStream subclass used by the MarshalledValue class to
   * ensure the classes and proxies are loaded using the thread context
   * class loader.
   *
   * @author Scott.Stark@jboss.org
   * @version $Revision: 1.1 $
   */
  public class MarshalledValueInputStream
     extends ObjectInputStream
  {
     private static Logger log = Logger.getLogger(MarshalledValueInputStream.class);
  
     /**
      * Creates a new instance of MarshalledValueOutputStream
      */
     public MarshalledValueInputStream(InputStream is) throws IOException
     {
        super(is);
     }
  
     /**
      * Use the thread context class loader to resolve the class
      * 
      * @throws IOException   Any exception thrown by the underlying OutputStream.
      */
     protected Class resolveClass(ObjectStreamClass v)
        throws IOException, ClassNotFoundException
     {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        String className = v.getName();
        return loader.loadClass(className);
     }
  
     protected Class resolveProxyClass(String[] interfaces)
        throws IOException, ClassNotFoundException
     {
        if( log.isDebugEnabled() )
        {
           StringBuffer tmp = new StringBuffer("[");
           for(int i = 0; i < interfaces.length; i ++)
           {
              if( i > 0 )
                 tmp.append(',');
              tmp.append(interfaces[i]);
           }
           tmp.append(']');
           log.debug("resolveProxyClass called, ifaces="+tmp.toString());
        }
        
        // Load the interfaces from the thread context class loader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class[] ifaceClasses = new Class[interfaces.length];
        for (int i = 0; i < interfaces.length; i++)
        {
           ifaceClasses[i] = loader.loadClass(interfaces[i]);
        }
        
        return java.lang.reflect.Proxy.getProxyClass(loader, ifaceClasses);
     }
  }
  
  
  
  1.1                  \
jboss-aop/src/main/org/jboss/aop/MarshalledValueOutputStream.java  
  Index: MarshalledValueOutputStream.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.aop;
  
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.ObjectOutputStream;
  import java.rmi.Remote;
  import java.rmi.server.RemoteObject;
  import java.rmi.server.RemoteStub;
  
  /**
   * An ObjectOutputStream subclass used by the MarshalledValue class to
   * ensure the classes and proxies are loaded using the thread context
   * class loader. Currently this does not do anything as neither class or
   * proxy annotations are used.
   *
   * @author Scott.Stark@jboss.org
   * @version $Revision: 1.1 $
   */
  public class MarshalledValueOutputStream
     extends ObjectOutputStream
  {
     /** Creates a new instance of MarshalledValueOutputStream
      If there is a security manager installed, this method requires a
      SerializablePermission("enableSubstitution") permission to ensure it's
      ok to enable the stream to do replacement of objects in the stream.
      */
     public MarshalledValueOutputStream(OutputStream os) throws IOException
     {
        super(os);
        enableReplaceObject(true);
     }
  
     /**
      * @throws IOException   Any exception thrown by the underlying OutputStream.
      */
     protected void annotateClass(Class cl) throws IOException
     {
        super.annotateClass(cl);
     }
     
     /**
      * @throws IOException   Any exception thrown by the underlying OutputStream.
      */
     protected void annotateProxyClass(Class cl) throws IOException
     {
        super.annotateProxyClass(cl);
     }
  
     /** Override replaceObject to check for Remote objects that are
      not RemoteStubs.
     */
     protected Object replaceObject(Object obj) throws IOException
     {
        if( (obj instanceof Remote) && !(obj instanceof RemoteStub) )
        {
           Remote remote = (Remote) obj;
           try
           {
              obj = RemoteObject.toStub(remote);
           }
           catch(IOException ignore)
           {
              // Let the Serialization layer try with the orignal obj
           }
        }
        return obj;
     }
  }
  
  
  


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
jboss-cvs-commits mailing list
jboss-cvs-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-cvs-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic