Example #1
0
    internal static ITypeWriter getWriter( object obj, IProtocolFormatter formatter )
      {
      if ( obj == null || obj is DBNull )
        return nullWriter;

      Type objectType = obj.GetType();
      ITypeWriter writer = formatter.getCachedWriter( objectType );

      if ( writer == null )
        {
        // none of the interfaces matched a writer.
        // perform a lookup for the object class hierarchy
        writer = getWriter( objectType, formatter, true );

        if ( writer == null )
          {
          if ( typeof( IEnumerable ).IsInstanceOfType( obj ) )
            {
            writer = enumerableWriter;
            }
          else
            {
            if ( logSer )
              Log.log( LoggingConstants.SERIALIZATION, "cannot find a writer for the object, will use default writer - " + defaultWriter );

            writer = defaultWriter;
            }
          }

        formatter.addCachedWriter( objectType, writer );
        }

      ITypeWriter referenceWriter = writer.getReferenceWriter();

      if ( referenceWriter != null )
        {
        // if the return object implements IHTTPSessionObject
        // keep it in the http session. The same object will be retrieved
        // on the subsequent invocations
#if (FULL_BUILD)
        if ( obj is IHttpSessionObject )
          {
          IHttpSessionObject httpSessionObject = (IHttpSessionObject)obj;
          string id = httpSessionObject.getID();
          object objectInSession = httpSessionObject.getObject();

          if ( Log.isLogging( LoggingConstants.DEBUG ) )
            Log.log( LoggingConstants.DEBUG, "placing object into HTTP session. ID - " + id + " object " + obj );

          //TODO: check for acuracy of Add method here
          HttpSessionState session = (HttpSessionState)ThreadContext.currentSession();

          if ( session != null )
            session.Add( id, objectInSession );
          }
#endif

        formatter.setContextWriter( writer );
        writer = referenceWriter;
        }

      // if a writer is found, use it, otherwise use the default writer
      return writer;
      }
Example #2
0
        public void write(object obj, IProtocolFormatter writer)
        {
            Array arrayObj = null;

            if (obj is IWebORBArray)
            {
                IEnumerator en = ((IWebORBArray)obj).GetEnumerator();
                arrayObj = new object[((IWebORBArray)obj).Count];

                int i = 0;
                while (en.MoveNext())
                {
                    arrayObj.SetValue(en.Current, i++);
                }
            }
            else
            {
                //TODO: test out this cast!
                arrayObj = (Array)obj;
            }

            if (obj.GetType().IsAssignableFrom(typeof(Byte[])))
            {
                Byte[] byteArray = new byte[arrayObj.Length];

                for (int i = 0; i < arrayObj.Length; i++)
                {
                    byteArray[i] = (Byte)arrayObj.GetValue(i);
                }

                writer.WriteByteArray(byteArray);
                return;
            }

            if (arrayObj.Rank > 1)
            {
                multiDimArrayWriter.write(arrayObj, writer);
                return;
            }

            writer.BeginWriteArray(arrayObj.Length);

            bool genericArray = arrayObj.GetType().GetElementType().BaseType == null;

            if (genericArray)
            {
                for (int i = 0; i < arrayObj.Length; i++)
                {
                    //Log.log( "REFCACHE", "WRITING ARRAY ELEMENT " + i );
                    object      value      = arrayObj.GetValue(i);
                    ITypeWriter typeWriter = MessageWriter.getWriter(value, writer);
                    typeWriter.write(value, writer);
                    //Log.log( "REFCACHE", "DONE WRITING ARRAY ELEMENT " + i );
                }
            }
            else
            {
                ITypeWriter typeWriter    = null;
                ITypeWriter contextWriter = null;


                for (int i = 0; i < arrayObj.Length; i++)
                {
                    object value = arrayObj.GetValue(i);

                    if (contextWriter == null)
                    {
                        typeWriter    = MessageWriter.getWriter(value, writer);
                        contextWriter = writer.getContextWriter();
                    }
                    else
                    {
                        writer.setContextWriter(contextWriter);
                    }

                    typeWriter.write(value, writer);
                }
            }

            writer.EndWriteArray();
        }
Example #3
0
        internal static ITypeWriter getWriter(object obj, IProtocolFormatter formatter)
        {
            if (obj == null || obj is DBNull)
            {
                return(nullWriter);
            }

            Type        objectType = obj.GetType();
            ITypeWriter writer     = formatter.getCachedWriter(objectType);

            if (writer == null)
            {
                // none of the interfaces matched a writer.
                // perform a lookup for the object class hierarchy
                writer = getWriter(objectType, formatter, true);

                if (writer == null)
                {
                    if (typeof(IEnumerable).IsInstanceOfType(obj))
                    {
                        writer = enumerableWriter;
                    }
                    else
                    {
                        if (logSer)
                        {
                            Log.log(LoggingConstants.SERIALIZATION, "cannot find a writer for the object, will use default writer - " + defaultWriter);
                        }

                        writer = defaultWriter;
                    }
                }

                formatter.addCachedWriter(objectType, writer);
            }

            ITypeWriter referenceWriter = writer.getReferenceWriter();

            if (referenceWriter != null)
            {
                // if the return object implements IHTTPSessionObject
                // keep it in the http session. The same object will be retrieved
                // on the subsequent invocations
#if (FULL_BUILD)
                if (obj is IHttpSessionObject)
                {
                    IHttpSessionObject httpSessionObject = (IHttpSessionObject)obj;
                    string             id = httpSessionObject.getID();
                    object             objectInSession = httpSessionObject.getObject();

                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "placing object into HTTP session. ID - " + id + " object " + obj);
                    }

                    //TODO: check for acuracy of Add method here
                    HttpSessionState session = (HttpSessionState)ThreadContext.currentSession();

                    if (session != null)
                    {
                        session.Add(id, objectInSession);
                    }
                }
#endif

                formatter.setContextWriter(writer);
                writer = referenceWriter;
            }

            // if a writer is found, use it, otherwise use the default writer
            return(writer);
        }
Example #4
0
    public void write( object obj, IProtocolFormatter writer )
    {
      Array arrayObj = null;

      if ( obj is IWebORBArray )
      {
        IEnumerator en = ( (IWebORBArray)obj ).GetEnumerator();
        arrayObj = new object[( (IWebORBArray)obj ).Count];

        int i = 0;
        while ( en.MoveNext() )
          arrayObj.SetValue( en.Current, i++ );
      }
      else
      {
        //TODO: test out this cast!
        arrayObj = (Array)obj;
      }

      if ( obj.GetType().IsAssignableFrom(typeof(Byte[])))
      {
        Byte[] byteArray = new byte[arrayObj.Length];
        
        for ( int i = 0; i < arrayObj.Length; i++ )
          byteArray[i] = (Byte)arrayObj.GetValue( i );

        writer.WriteByteArray(byteArray);
        return;
      }

      if ( arrayObj.Rank > 1 )
      {
        multiDimArrayWriter.write( arrayObj, writer );
        return;
      }

      writer.BeginWriteArray( arrayObj.Length );

      bool genericArray = arrayObj.GetType().GetElementType().BaseType == null;

      if ( genericArray )
      {
        for ( int i = 0; i < arrayObj.Length; i++ )
        {
          //Log.log( "REFCACHE", "WRITING ARRAY ELEMENT " + i );
          object value = arrayObj.GetValue( i );
          ITypeWriter typeWriter = MessageWriter.getWriter( value, writer );
          typeWriter.write( value, writer );
          //Log.log( "REFCACHE", "DONE WRITING ARRAY ELEMENT " + i );
        }
      }
      else
      {
        ITypeWriter typeWriter = null;
        ITypeWriter contextWriter = null;


        for ( int i = 0; i < arrayObj.Length; i++ )
        {
          object value = arrayObj.GetValue( i );

          if ( contextWriter == null )
          {
            typeWriter = MessageWriter.getWriter( value, writer );
            contextWriter = writer.getContextWriter();
          }
          else
          {
            writer.setContextWriter( contextWriter );
          }

          typeWriter.write( value, writer );
        }
      }

      writer.EndWriteArray();
    }