Beispiel #1
0
        public object defaultAdapt(ReferenceCache refCache)
        {
            // server side support for javascript class aliasing
            if (properties.Contains(ORBConstants.CLASS_NAME_FIELD))
            {
                string clientClassName = (string)((IAdaptingType)properties[ORBConstants.CLASS_NAME_FIELD]).defaultAdapt();
                Type   type            = ORBConfig.GetInstance().getTypeMapper()._getServerTypeForClientClass(clientClassName);
                if (type != null)
                {
                    return(adapt(type));
                }
            }

#if (FULL_BUILD)
            Hashtable hashtable = new Hashtable();
#else
            Dictionary <Object, Object> hashtable = new Dictionary <Object, Object>();
#endif

            if (refCache.HasObject(this))
            {
                return(refCache.GetObject(this));
            }
            else
            {
                refCache.AddObject(this, hashtable);
            }

            ICollection keys = properties.Keys;

            foreach (object key in keys)
            {
                object obj = properties[key];

                if (obj != null)
                {
                    if (obj is IAdaptingType && refCache.HasObject((IAdaptingType)obj))
                    {
                        obj = refCache.GetObject((IAdaptingType)obj);
                    }
                    else if (obj is ICacheableAdaptingType)
                    {
                        ICacheableAdaptingType cacheableType = (ICacheableAdaptingType)obj;
                        object result = cacheableType.defaultAdapt(refCache);
                        refCache.AddObject((IAdaptingType)obj, result);
                        obj = result;
                    }
                    else if (obj is IAdaptingType)
                    {
                        obj = ((IAdaptingType)obj).defaultAdapt();
                    }
                }

                hashtable[key] = obj;
            }

            return(hashtable);
        }
Beispiel #2
0
    public object adapt( Type type, ReferenceCache refCache )
    {
      if( Object != null )
      {
        bool isCachable = Object is ICacheableAdaptingType;
        IAdaptingType cacheKey = isCachable ? ( Object as ICacheableAdaptingType ).getCacheKey() : Object;

        if( refCache.HasObject( cacheKey, type ) )
          return refCache.GetObject( cacheKey, type );

        return isCachable ? ( Object as ICacheableAdaptingType ).adapt( type, refCache ) : Object.adapt( type );
      }
      
      return null;
    }
Beispiel #3
0
    public object defaultAdapt( ReferenceCache refCache )
    {
      if( Object != null )
      {
        bool isCachable = Object is ICacheableAdaptingType;
        IAdaptingType cacheKey = isCachable ? ( Object as ICacheableAdaptingType ).getCacheKey() : Object;

        if( refCache.HasObject( cacheKey ) )
          return refCache.GetObject( cacheKey );

        return isCachable? (Object as ICacheableAdaptingType).defaultAdapt(refCache) : Object.defaultAdapt();
      }

      return null;
    }
Beispiel #4
0
        public object adapt(Type type, ReferenceCache refCache)
        {
            if (Object != null)
            {
                bool          isCachable = Object is ICacheableAdaptingType;
                IAdaptingType cacheKey   = isCachable ? (Object as ICacheableAdaptingType).getCacheKey() : Object;

                if (refCache.HasObject(cacheKey, type))
                {
                    return(refCache.GetObject(cacheKey, type));
                }

                return(isCachable ? (Object as ICacheableAdaptingType).adapt(type, refCache) : Object.adapt(type));
            }

            return(null);
        }
Beispiel #5
0
        public object defaultAdapt(ReferenceCache refCache)
        {
            if (Object != null)
            {
                bool          isCachable = Object is ICacheableAdaptingType;
                IAdaptingType cacheKey   = isCachable ? (Object as ICacheableAdaptingType).getCacheKey() : Object;

                if (refCache.HasObject(cacheKey))
                {
                    return(refCache.GetObject(cacheKey));
                }

                return(isCachable? (Object as ICacheableAdaptingType).defaultAdapt(refCache) : Object.defaultAdapt());
            }

            return(null);
        }
Beispiel #6
0
        public object adapt(Type type, ReferenceCache refCache)
        {
            if (refCache.HasObject(this, type))
            {
                return(refCache.GetObject(this, type));
            }

            object obj = ObjectFactories.CreateArgumentObject(type, this);

            if (obj != null)
            {
                refCache.AddObject(this, type, obj);
                return(obj);
            }

            if (type.Equals(typeof(IAdaptingType)))
            {
                refCache.AddObject(this, type, obj);
                return(this);
            }

            if (!type.IsArray)
            {
                obj = ObjectFactories.CreateServiceObject(type);
                refCache.AddObject(this, type, obj);
            }

            //refCache[ this ] = obj;

            if (obj is IDictionary)
            {
                IDictionary dictionary = (IDictionary)obj;
                ICollection keys       = properties.Keys;

                foreach (object key in keys)
                {
                    object valueObj = properties[key];

                    if (valueObj != null)
                    {
                        if (type.IsGenericType)
                        {
                            Type[] args = type.GetGenericArguments();

                            if (valueObj is ICacheableAdaptingType)
                            {
                                valueObj = ((ICacheableAdaptingType)valueObj).adapt(args[1]);
                            }
                            else if (valueObj is IAdaptingType)
                            {
                                valueObj = ((IAdaptingType)valueObj).adapt(args[1]);
                            }
                        }
                        else
                        {
                            if (valueObj is ICacheableAdaptingType)
                            {
                                valueObj = ((ICacheableAdaptingType)valueObj).defaultAdapt(refCache);
                            }
                            else if (valueObj is IAdaptingType)
                            {
                                valueObj = ((IAdaptingType)valueObj).defaultAdapt();
                            }
                        }
                    }

                    object keyValue = key;

                    if (type.IsGenericType && key is IAdaptingType)
                    {
                        Type[] args = type.GetGenericArguments();
                        keyValue = ((IAdaptingType)key).adapt(args[0]);
                    }

                    dictionary.Add(keyValue, valueObj);
                }
            }
            else if (type.IsArray && canBeArray())
            {
                Type  elementType = type.GetElementType();
                Array newArray    = Array.CreateInstance(elementType, properties.Count);
                refCache.AddObject(this, type, newArray);

                foreach (String key in properties.Keys)
                {
                    int index;
                    int.TryParse(key, out index);
                    object valueObj = properties[key];

                    if (valueObj is ICacheableAdaptingType)
                    {
                        valueObj = ((ICacheableAdaptingType)valueObj).adapt(elementType, refCache);
                    }
                    else if (valueObj is IAdaptingType)
                    {
                        valueObj = ((IAdaptingType)valueObj).adapt(elementType);
                    }

                    newArray.SetValue(valueObj, index);
                }

                obj = newArray;
            }
            else if (type.BaseType == null)
            {
                return(defaultAdapt(refCache));
            }
#if (FULL_BUILD)
            else if (obj is StringDictionary || typeof(StringDictionary).IsAssignableFrom(type))
            {
                StringDictionary strDict = (StringDictionary)obj;
                ICollection      keys    = properties.Keys;

                foreach (object key in keys)
                {
                    string mappedValue;
                    object valueObj = properties[key];

                    if (valueObj is ICacheableAdaptingType)
                    {
                        mappedValue = (string)((ICacheableAdaptingType)valueObj).adapt(typeof(string), refCache);
                    }
                    else if (valueObj is IAdaptingType)
                    {
                        mappedValue = (string)((IAdaptingType)valueObj).adapt(typeof(string));
                    }
                    else
                    {
                        mappedValue = valueObj.ToString();
                    }

                    strDict.Add(key.ToString(), mappedValue);
                }
            }
#endif
            else
            {
                //if( accessCheckSuppressed )
                setFieldsDirect(obj, properties, refCache);
                //else
                //    setFieldsAsBean( obj, properties );
            }

            return(obj);
        }
    public object defaultAdapt( ReferenceCache refCache )
      {      
      // server side support for javascript class aliasing
      if ( properties.Contains( ORBConstants.CLASS_NAME_FIELD ) )
        {
        string clientClassName = (string) ( (IAdaptingType) properties[ ORBConstants.CLASS_NAME_FIELD ] ).defaultAdapt();
        Type type = ORBConfig.GetInstance().getTypeMapper()._getServerTypeForClientClass( clientClassName );
        if ( type != null )
          return adapt( type );
        }

#if (FULL_BUILD)
      Hashtable hashtable = new Hashtable();
#else
      Dictionary<Object, Object> hashtable = new Dictionary<Object, Object>();
#endif

      if( refCache.HasObject( this ) )
        return refCache.GetObject( this );
      else
        refCache.AddObject( this, hashtable );

      ICollection keys = properties.Keys;

      foreach ( object key in keys )
        {
        object obj = properties[ key ];

        if ( obj != null )
          {
          if ( obj is IAdaptingType && refCache.HasObject( (IAdaptingType)obj ) )
            {
            obj = refCache.GetObject( (IAdaptingType)obj );
            }
          else if ( obj is ICacheableAdaptingType )
            {
              ICacheableAdaptingType cacheableType = (ICacheableAdaptingType) obj;
              object result = cacheableType.defaultAdapt( refCache );
              refCache.AddObject( (IAdaptingType) obj, result );
              obj = result;
            }
          else if ( obj is IAdaptingType )
            {
            obj = ( (IAdaptingType)obj ).defaultAdapt();
            }
          }

        hashtable[ key ] = obj;
        }

      return hashtable;
      }
    public object adapt( Type type, ReferenceCache refCache )
      {
      if ( refCache.HasObject( this, type ) )
        return refCache.GetObject( this, type );

      object obj = ObjectFactories.CreateArgumentObject( type, this );

      if ( obj != null )
        {
        refCache.AddObject( this, type, obj );
        return obj;
        }

      if ( type.Equals( typeof( IAdaptingType ) ) )
        {
        refCache.AddObject( this, type, obj );
        return this;
        }

      if ( !type.IsArray )
        {
        obj = ObjectFactories.CreateServiceObject( type );
        refCache.AddObject( this, type, obj );
        }

      //refCache[ this ] = obj;

      if ( obj is IDictionary )
        {
        IDictionary dictionary = (IDictionary)obj;
        ICollection keys = properties.Keys;

        foreach ( object key in keys )
          {
          object valueObj = properties[ key ];

          if ( valueObj != null )
            {
            if ( type.IsGenericType )
              {
              Type[] args = type.GetGenericArguments();

              if ( valueObj is ICacheableAdaptingType )
                valueObj = ( (ICacheableAdaptingType)valueObj ).adapt( args[ 1 ] );
              else if ( valueObj is IAdaptingType )
                valueObj = ( (IAdaptingType)valueObj ).adapt( args[ 1 ] );
              }
            else
              {
              if ( valueObj is ICacheableAdaptingType )
                valueObj = ( (ICacheableAdaptingType)valueObj ).defaultAdapt( refCache );
              else if ( valueObj is IAdaptingType )
                valueObj = ( (IAdaptingType)valueObj ).defaultAdapt();
              }
            }

          object keyValue = key;

          if ( type.IsGenericType && key is IAdaptingType )
            {
            Type[] args = type.GetGenericArguments();
            keyValue = ( (IAdaptingType)key ).adapt( args[ 0 ] );
            }

          dictionary.Add( keyValue, valueObj );
          }
        }
      else if ( type.IsArray && canBeArray() )
        {
        Type elementType = type.GetElementType();
        Array newArray = Array.CreateInstance( elementType, properties.Count );
        refCache.AddObject( this, type, newArray );

        foreach ( String key in properties.Keys )
          {
          int index;
          int.TryParse( key, out index );
          object valueObj = properties[ key ];

          if ( valueObj is ICacheableAdaptingType )
            valueObj = ( (ICacheableAdaptingType)valueObj ).adapt( elementType, refCache );
          else if ( valueObj is IAdaptingType )
            valueObj = ( (IAdaptingType)valueObj ).adapt( elementType );

          newArray.SetValue( valueObj, index );
          }

        obj = newArray;
        }
      else if ( type.BaseType == null )
        {
        return defaultAdapt( refCache );
        }
#if( FULL_BUILD)
      else if ( obj is StringDictionary || typeof( StringDictionary ).IsAssignableFrom( type ) )
        {
        StringDictionary strDict = (StringDictionary)obj;
        ICollection keys = properties.Keys;

        foreach ( object key in keys )
          {
          string mappedValue;
          object valueObj = properties[ key ];

          if ( valueObj is ICacheableAdaptingType )
            mappedValue = (string)( (ICacheableAdaptingType)valueObj ).adapt( typeof( string ), refCache );
          else if ( valueObj is IAdaptingType )
            mappedValue = (string)( (IAdaptingType)valueObj ).adapt( typeof( string ) );
          else
            mappedValue = valueObj.ToString();

          strDict.Add( key.ToString(), mappedValue );
          }
        }
#endif
      else
        {
        //if( accessCheckSuppressed )
        setFieldsDirect( obj, properties, refCache );
        //else
        //    setFieldsAsBean( obj, properties );
        }

      return obj;
      }
Beispiel #9
0
        public object defaultAdapt(ReferenceCache refCache)
        {
            if (refCache.HasObject(this))
            {
                return(refCache.GetObject(this));
            }

            int size = arrayObject.Length;

            if (isHomogeneous())
            {
                Array array = Array.CreateInstance(getComponentType(), size);
                //refCache[ this ] = array;

                refCache.AddObject(this, array);
                for (int i = 0; i < size; i++)
                {
                    object obj = arrayObject[i];

                    if (obj != null)
                    {
                        if (obj is IAdaptingType && refCache.HasObject((IAdaptingType)obj))
                        {
                            obj = refCache.GetObject((IAdaptingType)obj);
                        }
                        else if (obj is ICacheableAdaptingType)
                        {
                            object value = ((ICacheableAdaptingType)obj).defaultAdapt(refCache);
                            //refCache[ obj ] = value;
                            refCache.AddObject((IAdaptingType)obj, value);
                            obj = value;
                        }
                        else if (obj is IAdaptingType)
                        {
                            obj = ((IAdaptingType)obj).defaultAdapt();
                        }
                    }

                    array.SetValue(obj, i);
                }

                return(array);
            }
            else
            {
                object[] array = new object[size];
                //refCache[ this ] = array;

                refCache.AddObject(this, array);

                for (int i = 0; i < size; i++)
                {
                    object obj = arrayObject[i];

                    if (obj != null)
                    {
                        if (obj is IAdaptingType && refCache.HasObject((IAdaptingType)obj))
                        {
                            //obj = refCache[ obj ];

                            obj = refCache.GetObject((IAdaptingType)obj);
                        }
                        else if (obj is ICacheableAdaptingType)
                        {
                            object val = ((ICacheableAdaptingType)obj).defaultAdapt();
                            //refCache[ obj ] = val;

                            refCache.AddObject((IAdaptingType)obj, val);

                            obj = val;
                        }
                        else if (obj is IAdaptingType)
                        {
                            obj = ((IAdaptingType)obj).defaultAdapt();
                        }
                    }

                    array[i] = obj;
                }

                return(array);
            }
        }
Beispiel #10
0
        public object adapt(Type type, ReferenceCache refCache)
        {
            if (refCache.HasObject(this, type))
            {
                /*object obj = refCache[ this ];
                 *
                 * if( type.IsAssignableFrom( obj.GetType() ) )
                 *  return obj;*/

                return(refCache.GetObject(this, type));
            }

            if (Log.isLogging(LoggingConstants.DEBUG))
            {
                Log.log(LoggingConstants.DEBUG, "ArrayType.adapt, adapting type: " + type.FullName);
            }

            int size = arrayObject.Length;

            if (type == typeof(IAdaptingType))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is an adapting type");
                }

                return(this);
            }
            else if (type.IsArray)
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is array");
                }

                Type componentType = type.GetElementType();

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    if (componentType != null)
                    {
                        Log.log(LoggingConstants.DEBUG, "array element type is " + componentType.FullName);
                    }
                    else
                    {
                        Log.log(LoggingConstants.DEBUG, "array element type is null");
                    }
                }

                Array newArray = Array.CreateInstance(componentType, size);
                //refCache[ this ] = newArray;

                refCache.AddObject(this, type, newArray);

                for (int i = 0; i < size; i++)
                {
                    newArray.SetValue(adaptArrayComponent(arrayObject[i], componentType, refCache), i);
                }

                return(newArray);
            }
            else if (type.IsGenericType && type.GetGenericArguments().Length == 2)
            {
                Type constructedType = Types.Types.GetAbstractClassMapping(type);

                Type   genericTypeDef = type.GetGenericTypeDefinition();
                Type[] genericArgs    = type.GetGenericArguments();

                if (constructedType == null)
                {
                    constructedType = genericTypeDef.MakeGenericType(genericArgs);
                }

                object newDictionary = Activator.CreateInstance(constructedType);
                refCache.AddObject(this, type, newDictionary);
                Type       keyValuePairType = typeof(KeyValuePair);
                MethodInfo addMethod        = constructedType.GetMethod("Add", genericArgs);
                object[]   args             = new object[2];

                for (int i = 0; i < arrayObject.Length; i++)
                {
                    KeyValuePair kvPair = (KeyValuePair)((IAdaptingType)arrayObject[i]).adapt(keyValuePairType);

                    args[0] = kvPair.key.adapt(genericArgs[0]);
                    args[1] = kvPair.value.adapt(genericArgs[1]);

                    addMethod.Invoke(newDictionary, args);
                }

                return(newDictionary);
            }
            else if (type.GetInterface("System.Collections.Generic.ICollection`1") != null)
            {
                Type   genericCollection = type.GetInterface("System.Collections.Generic.ICollection`1");
                Type   elementType       = genericCollection.GetGenericArguments()[0];
                object collection        = ObjectFactories.CreateServiceObject(type);

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "created service object which is a collection");
                }

                refCache.AddObject(this, type, collection);
                MethodInfo addMethod = type.GetMethod("Add", new Type[] { elementType });

                if (addMethod == null)
                {
                    addMethod = collection.GetType().GetMethod("Add", new Type[] { elementType });
                }

                object[] args = new object[1];

                for (int i = 0; i < arrayObject.Length; i++)
                {
                    //args[ 0 ] = ((IAdaptingType) arrayObject[ i ]).adapt( elementType );
                    args[0] = adaptArrayComponent(arrayObject[i], elementType, refCache);

                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "invoking method Add to add item to collection");
                    }

                    addMethod.Invoke(collection, args);

                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "item has been added");
                    }
                }

                return(collection);
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
#if FULL_BUILD
                if (!(type is WebORBArrayCollection) && this is ArrayCollectionType)
                {
                    return(((ArrayCollectionType)this).GetArrayType().adapt(type));
                }
#endif
                IList listObject = (IList)ObjectFactories.CreateServiceObject(type);
                //refCache[ this ] = listObject;

                refCache.AddObject(this, type, listObject);

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "will populate service objects with elements. got " + size + " items");
                }

                Type componentType = null;

                if (type.IsGenericType)
                {
                    componentType = type.GetGenericArguments()[0];
                }

                for (int i = 0; i < size; i++)
                {
                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "element " + i + "   " + arrayObject[i]);
                    }

                    listObject.Add(adaptArrayComponent(arrayObject[i], componentType, refCache));
                }

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "returning list. list size is " + listObject.Count);
                }

                return(listObject);
            }
#if (FULL_BUILD)
            else if (typeof(Queue).IsAssignableFrom(type))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is Queue");
                }

                Queue qObject = (Queue)ObjectFactories.CreateServiceObject(type);
                //refCache[ this ] = qObject;

                refCache.AddObject(this, type, qObject);

                for (int i = 0; i < size; i++)
                {
                    qObject.Enqueue(adaptArrayComponent(arrayObject[i], null, refCache));
                }

                return(qObject);
            }
            else if (typeof(Stack).IsAssignableFrom(type))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is Stack");
                }

                Stack stackObject = (Stack)ObjectFactories.CreateServiceObject(type);
                //refCache[ this ] = stackObject;

                refCache.AddObject(this, type, stackObject);

                for (int i = 0; i < size; i++)
                {
                    stackObject.Push(adaptArrayComponent(arrayObject[i], null, refCache));
                }

                return(stackObject);
            }
            else if (typeof(SortedList).IsAssignableFrom(type))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is SortedList");
                }

                SortedList sortedList = (SortedList)ObjectFactories.CreateServiceObject(type);
                //refCache[ this ] = sortedList;

                refCache.AddObject(this, type, sortedList);

                for (int i = 0; i < size; i++)
                {
                    object obj = adaptArrayComponent(arrayObject[i], null, refCache);
                    sortedList.Add(obj, obj);
                }

                return(sortedList);
            }
#endif
            else if (type.GetInterface("Iesi.Collections.ISet") != null || type.FullName.Equals("Iesi.Collections.ISet"))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is Iesi.Collections.ISet");
                }

                Object iSet = ObjectFactories.CreateServiceObject(type);

                refCache.AddObject(this, type, iSet);
                MethodInfo addMethod = iSet.GetType().GetMethod("Add", new Type[] { typeof(Object) });
                Object[]   args      = new Object[1];

                for (int i = 0; i < size; i++)
                {
                    args[0] = adaptArrayComponent(arrayObject[i], null, refCache);
                    addMethod.Invoke(iSet, args);
                }

                return(iSet);
            }
#if (FULL_BUILD)
            // this is a hack for Flash Builder 4
            else if (typeof(IDictionary).IsAssignableFrom(type) && (arrayObject == null || arrayObject.Length == 0))
            {
                return(new Hashtable());
            }
#endif
            else
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "will do default adaptation");
                }

                return(defaultAdapt(refCache));
            }
        }
Beispiel #11
0
    public object defaultAdapt( ReferenceCache refCache )
      {

      if ( refCache.HasObject( this ) )
        return refCache.GetObject( this );

      int size = arrayObject.Length;

      if ( isHomogeneous() )
        {
        Array array = Array.CreateInstance( getComponentType(), size );
        //refCache[ this ] = array;

        refCache.AddObject( this, array );
        for ( int i = 0; i < size; i++ )
          {
          object obj = arrayObject[ i ];

          if ( obj != null )
            {
            if ( obj is IAdaptingType && refCache.HasObject( (IAdaptingType)obj ) )
              {
              obj = refCache.GetObject( (IAdaptingType)obj );
              }
            else if ( obj is ICacheableAdaptingType )
              {
              object value = ( (ICacheableAdaptingType)obj ).defaultAdapt( refCache );
              //refCache[ obj ] = value;
              refCache.AddObject( (IAdaptingType)obj, value );
              obj = value;
              }
            else if ( obj is IAdaptingType )
              {
              obj = ( (IAdaptingType)obj ).defaultAdapt();
              }
            }

          array.SetValue( obj, i );
          }

        return array;
        }
      else
        {
        object[] array = new object[ size ];
        //refCache[ this ] = array;

        refCache.AddObject( this, array );

        for ( int i = 0; i < size; i++ )
          {
          object obj = arrayObject[ i ];

          if ( obj != null )
            {
            if ( obj is IAdaptingType && refCache.HasObject( (IAdaptingType)obj ) )
              {
              //obj = refCache[ obj ];

              obj = refCache.GetObject( (IAdaptingType)obj );
              }
            else if ( obj is ICacheableAdaptingType )
              {
              object val = ( (ICacheableAdaptingType)obj ).defaultAdapt();
              //refCache[ obj ] = val;

              refCache.AddObject( (IAdaptingType)obj, val );

              obj = val;
              }
            else if ( obj is IAdaptingType )
              {
              obj = ( (IAdaptingType)obj ).defaultAdapt();
              }
            }

          array[ i ] = obj;
          }

        return array;
        }
      }
Beispiel #12
0
    public object adapt( Type type, ReferenceCache refCache )
      {
      if ( refCache.HasObject( this, type ) )
        {
        /*object obj = refCache[ this ];

        if( type.IsAssignableFrom( obj.GetType() ) )
            return obj;*/

        return refCache.GetObject( this, type );
        }

      if ( Log.isLogging( LoggingConstants.DEBUG ) )
        Log.log( LoggingConstants.DEBUG, "ArrayType.adapt, adapting type: " + type.FullName );

      int size = arrayObject.Length;

      if ( type.Equals( typeof( IAdaptingType ) ) )
        {
        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "type is an adapting type" );

        return this;
        }
      else if ( type.IsArray )
        {
        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "type is array" );

        Type componentType = type.GetElementType();

        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          {
          if ( componentType != null )
            Log.log( LoggingConstants.DEBUG, "array element type is " + componentType.FullName );
          else
            Log.log( LoggingConstants.DEBUG, "array element type is null" );
          }

        Array newArray = Array.CreateInstance( componentType, size );
        //refCache[ this ] = newArray;

        refCache.AddObject( this, type, newArray );

        for ( int i = 0; i < size; i++ )
          newArray.SetValue( adaptArrayComponent( arrayObject[ i ], componentType, refCache ), i );

        return newArray;
        }
      else if ( type.IsGenericType && type.GetGenericArguments().Length == 2 )
        {
        Type constructedType = Types.Types.GetAbstractClassMapping( type );

        Type genericTypeDef = type.GetGenericTypeDefinition();
        Type[] genericArgs = type.GetGenericArguments();

        if ( constructedType == null )
          constructedType = genericTypeDef.MakeGenericType( genericArgs );

        object newDictionary = Activator.CreateInstance( constructedType );
        refCache.AddObject( this, type, newDictionary );
        Type keyValuePairType = typeof( KeyValuePair );
        MethodInfo addMethod = constructedType.GetMethod( "Add", genericArgs );
        object[] args = new object[ 2 ];

        for ( int i = 0; i < arrayObject.Length; i++ )
          {
          KeyValuePair kvPair = (KeyValuePair)( (IAdaptingType)arrayObject[ i ] ).adapt( keyValuePairType );

          args[ 0 ] = kvPair.key.adapt( genericArgs[ 0 ] );
          args[ 1 ] = kvPair.value.adapt( genericArgs[ 1 ] );

          addMethod.Invoke( newDictionary, args );
          }

        return newDictionary;
        }
      else if ( type.GetInterface( "ICollection`1", true ) != null )
        {
        Type genericCollection = type.GetInterface( "ICollection`1", true );
        Type elementType = genericCollection.GetGenericArguments()[ 0 ];
        object collection = ObjectFactories.CreateServiceObject( type );

        if( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "created service object which is a collection" );

        refCache.AddObject( this, type, collection );
        MethodInfo addMethod = type.GetMethod( "Add", new Type[] { elementType } );

        if ( addMethod == null )
          addMethod = collection.GetType().GetMethod( "Add", new Type[] { elementType } );

        object[] args = new object[ 1 ];

        for ( int i = 0; i < arrayObject.Length; i++ )
          {
          //args[ 0 ] = ((IAdaptingType) arrayObject[ i ]).adapt( elementType );
          args[ 0 ] = adaptArrayComponent( arrayObject[ i ], elementType, refCache );

          if( Log.isLogging( LoggingConstants.DEBUG ) )
            Log.log( LoggingConstants.DEBUG, "invoking method Add to add item to collection" );

          addMethod.Invoke( collection, args );

          if( Log.isLogging( LoggingConstants.DEBUG ) )
            Log.log( LoggingConstants.DEBUG, "item has been added" );
          }

        return collection;
        }
      else if ( typeof( IList ).IsAssignableFrom( type ) )
        {
#if FULL_BUILD
        if(!(type is WebORBArrayCollection) && this is ArrayCollectionType)
        {
          return ((ArrayCollectionType) this).GetArrayType().adapt(type);
        }
#endif
        IList listObject = (IList)ObjectFactories.CreateServiceObject( type );
        //refCache[ this ] = listObject;

        refCache.AddObject( this, type, listObject );

        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "will populate service objects with elements. got " + size + " items" );

        Type componentType = null;

        if ( type.IsGenericType )
          componentType = type.GetGenericArguments()[ 0 ];

        for ( int i = 0; i < size; i++ )
          {
          if ( Log.isLogging( LoggingConstants.DEBUG ) )
            Log.log( LoggingConstants.DEBUG, "element " + i + "   " + arrayObject[ i ] );

          listObject.Add( adaptArrayComponent( arrayObject[ i ], componentType, refCache ) );
          }

        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "returning list. list size is " + listObject.Count );

        return listObject;
        }
#if (FULL_BUILD)
      else if ( typeof( Queue ).IsAssignableFrom( type ) )
        {
        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "type is Queue" );

        Queue qObject = (Queue)ObjectFactories.CreateServiceObject( type );
        //refCache[ this ] = qObject;

        refCache.AddObject( this, type, qObject );

        for ( int i = 0; i < size; i++ )
          qObject.Enqueue( adaptArrayComponent( arrayObject[ i ], null, refCache ) );

        return qObject;
        }
      else if ( typeof( Stack ).IsAssignableFrom( type ) )
        {
        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "type is Stack" );

        Stack stackObject = (Stack)ObjectFactories.CreateServiceObject( type );
        //refCache[ this ] = stackObject;

        refCache.AddObject( this, type, stackObject );

        for ( int i = 0; i < size; i++ )
          stackObject.Push( adaptArrayComponent( arrayObject[ i ], null, refCache ) );

        return stackObject;
        }
      else if ( typeof( SortedList ).IsAssignableFrom( type ) )
        {
        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "type is SortedList" );

        SortedList sortedList = (SortedList)ObjectFactories.CreateServiceObject( type );
        //refCache[ this ] = sortedList;

        refCache.AddObject( this, type, sortedList );

        for ( int i = 0; i < size; i++ )
          {
          object obj = adaptArrayComponent( arrayObject[ i ], null, refCache );
          sortedList.Add( obj, obj );
          }

        return sortedList;
        }
#endif
      else if ( type.GetInterface( "Iesi.Collections.ISet", true ) != null || type.FullName.Equals( "Iesi.Collections.ISet" ) )
        {
        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "type is Iesi.Collections.ISet" );

        Object iSet = ObjectFactories.CreateServiceObject( type );

        refCache.AddObject( this, type, iSet );
        MethodInfo addMethod = iSet.GetType().GetMethod( "Add", new Type[] { typeof( Object ) } );
        Object[] args = new Object[ 1 ];

        for ( int i = 0; i < size; i++ )
          {
          args[ 0 ] = adaptArrayComponent( arrayObject[ i ], null, refCache );
          addMethod.Invoke( iSet, args );
          }

        return iSet;
        }
#if (FULL_BUILD)
      // this is a hack for Flash Builder 4
      else if ( typeof( IDictionary ).IsAssignableFrom( type ) && ( arrayObject == null || arrayObject.Length == 0 ) )
        {
        return new Hashtable();
        }
#endif
      else
        {
        if ( Log.isLogging( LoggingConstants.DEBUG ) )
          Log.log( LoggingConstants.DEBUG, "will do default adaptation" );

        return defaultAdapt( refCache );
        }
      }