コード例 #1
0
        private void DoGetObjectsOfType(Type type, bool includeFactoryObjects, bool includePrototypes, IDictionary collector)
        {
            bool isFactoryType = (type != null && typeof(IFactoryObject).IsAssignableFrom(type));

            foreach (string name in objects.Keys)
            {
                object instance = objects[name];
                if (instance is IFactoryObject && includeFactoryObjects)
                {
                    IFactoryObject factory    = (IFactoryObject)instance;
                    Type           objectType = factory.ObjectType;
                    if ((objectType == null && factory.IsSingleton) ||
                        ((factory.IsSingleton || includePrototypes) &&
                         objectType != null && type.IsAssignableFrom(objectType)))
                    {
                        object createdObject = GetObject(name);
                        if (type.IsInstanceOfType(createdObject))
                        {
                            collector[name] = createdObject;
                        }
                    }
                }
                else if (type.IsAssignableFrom(instance.GetType()))
                {
                    if (isFactoryType)
                    {
                        collector[ObjectFactoryUtils.BuildFactoryObjectName(name)] = instance;
                    }
                    else
                    {
                        collector[name] = instance;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Determines whether the specified object qualifies as an autowire candidate,
        /// to be injected into other beans which declare a dependency of matching type.
        /// This method checks ancestor factories as well.
        /// </summary>
        /// <param name="objectName">Name of the object to check.</param>
        /// <param name="descriptor">The descriptor of the dependency to resolve.</param>
        /// <returns>
        ///     <c>true</c> if the object should be considered as an autowire candidate; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="NoSuchObjectDefinitionException">if there is no object with the given name.</exception>
        public bool IsAutowireCandidate(string objectName, DependencyDescriptor descriptor)
        {
            //Consider FactoryObjects as autowiring candidates.
            bool isFactoryObject = (descriptor != null && descriptor.DependencyType != null &&
                                    typeof(IFactoryObject).IsAssignableFrom(descriptor.DependencyType));

            if (isFactoryObject)
            {
                objectName = ObjectFactoryUtils.TransformedObjectName(objectName);
            }

            if (!ContainsObjectDefinition(objectName))
            {
                if (ContainsSingleton(objectName))
                {
                    return(true);
                }
                else if (ParentObjectFactory is IConfigurableListableObjectFactory)
                {
                    // No object definition found in this factory -> delegate to parent
                    return
                        (((IConfigurableListableObjectFactory)ParentObjectFactory).IsAutowireCandidate(objectName, descriptor));
                }
            }
            return(IsAutowireCandidate(objectName, GetMergedObjectDefinition(objectName, true), descriptor));
        }
コード例 #3
0
        /// <summary> Add all global interceptors and pointcuts.</summary>
        private void AddGlobalAdvisor(IListableObjectFactory objectFactory, string prefix)
        {
            var globalAspectNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvisors));
            var globalAdvisorNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvisor));
            var globalInterceptorNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IInterceptor));

            List <object> objects             = new List <object>();
            Dictionary <object, string> names = new Dictionary <object, string>();

            for (int i = 0; i < globalAspectNames.Count; i++)
            {
                string name = globalAspectNames[i];
                if (name.StartsWith(prefix))
                {
                    IAdvisors advisors = (IAdvisors)objectFactory.GetObject(name);
                    foreach (object advisor in advisors.Advisors)
                    {
                        // exclude introduction advisors from interceptor list
                        if (!(advisor is IIntroductionAdvisor))
                        {
                            objects.Add(advisor);
                            names[advisor] = name;
                        }
                    }
                }
            }
            for (int i = 0; i < globalAdvisorNames.Count; i++)
            {
                string name = globalAdvisorNames[i];
                if (name.StartsWith(prefix))
                {
                    object obj = objectFactory.GetObject(name);
                    // exclude introduction advisors from interceptor list
                    if (!(obj is IIntroductionAdvisor))
                    {
                        objects.Add(obj);
                        names[obj] = name;
                    }
                }
            }
            for (int i = 0; i < globalInterceptorNames.Count; i++)
            {
                string name = globalInterceptorNames[i];
                if (name.StartsWith(prefix))
                {
                    object obj = objectFactory.GetObject(name);
                    objects.Add(obj);
                    names[obj] = name;
                }
            }
            objects.Sort(OrderComparator.Instance);
            foreach (object obj in objects)
            {
                string name = names[obj];
                AddAdvisorOnChainCreation(obj, name);
            }
        }
コード例 #4
0
        private IDictionary FindAutowireCandidates(string objectName, Type requiredType, DependencyDescriptor descriptor)
        {
            string[] candidateNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(this, requiredType, true, descriptor.Eager);
#if NET_1_0 || NET_1_1
            IDictionary result = new Hashtable();
#else
            IDictionary result = new OrderedDictionary(candidateNames.Length);
#endif
            foreach (DictionaryEntry entry in resolvableDependencies)
            {
                Type autoWiringType = (Type)entry.Key;
                if (autoWiringType.IsAssignableFrom(requiredType))
                {
                    object autowiringValue = this.resolvableDependencies[autoWiringType];
                    if (requiredType.IsInstanceOfType(autowiringValue))
                    {
                        result.Add(ObjectUtils.IdentityToString(autowiringValue), autowiringValue);
                        break;
                    }
                }
            }
            for (int i = 0; i < candidateNames.Length; i++)
            {
                string candidateName = candidateNames[i];
                if (!candidateName.Equals(objectName) && IsAutowireCandidate(candidateName, descriptor))
                {
                    result.Add(candidateName, GetObject(candidateName));
                }
            }
            return(result);
        }
コード例 #5
0
        /// <summary> Add all global interceptors and pointcuts.</summary>
        private void AddGlobalAdvisor(IListableObjectFactory objectFactory, string prefix)
        {
            string[] globalAspectNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvisors));
            string[] globalAdvisorNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvisor));
            string[] globalInterceptorNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IInterceptor));
            ArrayList objects = new ArrayList();
            Hashtable names   = new Hashtable();

            for (int i = 0; i < globalAspectNames.Length; i++)
            {
                string name = globalAspectNames[i];
                if (name.StartsWith(prefix))
                {
                    IAdvisors advisors = (IAdvisors)objectFactory.GetObject(name);
                    foreach (object advisor in advisors.Advisors)
                    {
                        // exclude introduction advisors from interceptor list
                        if (!(advisor is IIntroductionAdvisor))
                        {
                            objects.Add(advisor);
                            names[advisor] = name;
                        }
                    }
                }
            }
            for (int i = 0; i < globalAdvisorNames.Length; i++)
            {
                string name = globalAdvisorNames[i];
                if (name.StartsWith(prefix))
                {
                    object obj = objectFactory.GetObject(name);
                    // exclude introduction advisors from interceptor list
                    if (!(obj is IIntroductionAdvisor))
                    {
                        objects.Add(obj);
                        names[obj] = name;
                    }
                }
            }
            for (int i = 0; i < globalInterceptorNames.Length; i++)
            {
                string name = globalInterceptorNames[i];
                if (name.StartsWith(prefix))
                {
                    object obj = objectFactory.GetObject(name);
                    objects.Add(obj);
                    names[obj] = name;
                }
            }
            ((ArrayList)objects).Sort(new OrderComparator());
            foreach (object obj in objects)
            {
                string name = (string)names[obj];
                AddAdvisorOnChainCreation(obj, name);
            }
        }
コード例 #6
0
        /// <summary>
        /// Determine the type of the object with the given name.
        /// </summary>
        /// <remarks>
        /// <p>
        /// More specifically, checks the type of object that
        /// <see cref="Spring.Objects.Factory.IObjectFactory.GetObject(string)"/> would return.
        /// For an <see cref="Spring.Objects.Factory.IFactoryObject"/>, returns the type
        /// of object that the <see cref="Spring.Objects.Factory.IFactoryObject"/> creates.
        /// </p>
        /// </remarks>
        /// <param name="name">The name of the object to query.</param>
        /// <returns>
        /// The <see cref="System.Type"/> of the object or <see langword="null"/> if
        /// not determinable.
        /// </returns>
        public Type GetType(string name)
        {
            string objectName = ObjectFactoryUtils.TransformedObjectName(name);
            object instance   = objects[objectName];

            if (instance == null)
            {
                throw new NoSuchObjectDefinitionException(name, GrabDefinedObjectsString());
            }
            if (instance is IFactoryObject && !ObjectFactoryUtils.IsFactoryDereference(name))
            {
                return(((IFactoryObject)instance).ObjectType);
            }
            return(instance.GetType());
        }
コード例 #7
0
 /// <summary>
 /// Gets the names of advisor candidates
 /// </summary>
 /// <param name="targetType">the type of the object to be advised</param>
 /// <param name="targetName">the name of the object to be advised</param>
 /// <returns>a non-null string array of advisor candidate names</returns>
 protected virtual IList <string> GetAdvisorCandidateNames(Type targetType, string targetName)
 {
     if (_cachedObjectNames == null)
     {
         lock (this)
         {
             if (_cachedObjectNames == null)
             {
                 List <string>  candidateNameList     = new List <string>();
                 IList <string> advisorCandidateNames = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(_objectFactory, typeof(IAdvisor), true, false);
                 candidateNameList.AddRange(advisorCandidateNames);
                 IList <string> advisorsCandidateNames = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(_objectFactory, typeof(IAdvisors), true, false);
                 candidateNameList.AddRange(advisorsCandidateNames);
                 _cachedObjectNames = candidateNameList;
             }
         }
     }
     return(_cachedObjectNames);
 }
コード例 #8
0
 /// <summary>
 /// Gets the names of advisor candidates
 /// </summary>
 /// <param name="targetType">the type of the object to be advised</param>
 /// <param name="targetName">the name of the object to be advised</param>
 /// <returns>a non-null string array of advisor candidate names</returns>
 protected virtual string[] GetAdvisorCandidateNames(Type targetType, string targetName)
 {
     if (_cachedObjectNames == null)
     {
         lock (this)
         {
             if (_cachedObjectNames == null)
             {
                 ArrayList candidateNameList     = new ArrayList();
                 string[]  advisorCandidateNames = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(_objectFactory, typeof(IAdvisor), true, false);
                 candidateNameList.AddRange(advisorCandidateNames);
                 string[] advisorsCandidateNames = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(_objectFactory, typeof(IAdvisors), true, false);
                 candidateNameList.AddRange(advisorsCandidateNames);
                 _cachedObjectNames = (string[])candidateNameList.ToArray(typeof(string));
             }
         }
     }
     return(_cachedObjectNames);
 }
        /// <summary>
        /// Detects the petsistence exception translators in the given object factory.
        /// </summary>
        /// <param name="objectFactory">The object factory for obtaining all IPersistenceExceptionTranslators.</param>
        /// <returns>A chained IPersistenceExceptionTranslator, combining all PersistenceExceptionTranslators found in the factory
        /// </returns>
        /// <seealso cref="ChainedPersistenceExceptionTranslator"/>
        protected IPersistenceExceptionTranslator DetectPersistenceExceptionTranslators(IListableObjectFactory objectFactory)
        {
            // Find all translators, being careful not to activate FactoryObjects.
            IDictionary <string, object> pets =
                ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(objectFactory,
                                                                   typeof(IPersistenceExceptionTranslator), false,
                                                                   false);

            if (pets.Count == 0)
            {
                throw new InvalidOperationException("No persistence exception translators found in container. Cannot perform exception translation.");
            }

            ChainedPersistenceExceptionTranslator cpet = new ChainedPersistenceExceptionTranslator();

            foreach (KeyValuePair <string, object> pet in pets)
            {
                cpet.AddTranslator((IPersistenceExceptionTranslator)pet.Value);
            }
            return(cpet);
        }
コード例 #10
0
        /// <summary> Add all global introductions.</summary>
        private void AddGlobalIntroduction(IListableObjectFactory objectFactory, string prefix)
        {
            IList <string> globalAspectNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvisors));
            IList <string> globalAdvisorNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvisor));
            IList <string> globalIntroductionNames =
                ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvice));
            ArrayList objects = new ArrayList();
            Dictionary <object, string> names = new Dictionary <object, string>();

            for (int i = 0; i < globalAspectNames.Count; i++)
            {
                string name = globalAspectNames[i];
                if (name.StartsWith(prefix))
                {
                    IAdvisors advisors = (IAdvisors)objectFactory.GetObject(name);
                    foreach (object advisor in advisors.Advisors)
                    {
                        // only include introduction advisors
                        if (advisor is IIntroductionAdvisor)
                        {
                            objects.Add(advisor);
                            names[advisor] = name;
                        }
                    }
                }
            }
            for (int i = 0; i < globalAdvisorNames.Count; i++)
            {
                string name = globalAdvisorNames[i];
                if (name.StartsWith(prefix))
                {
                    object obj = objectFactory.GetObject(name);
                    // only include introduction advisors
                    if (obj is IIntroductionAdvisor)
                    {
                        objects.Add(obj);
                        names[obj] = name;
                    }
                }
            }
            for (int i = 0; i < globalIntroductionNames.Count; i++)
            {
                string name = globalIntroductionNames[i];
                if (name.StartsWith(prefix))
                {
                    object obj = objectFactory.GetObject(name);
                    // exclude other advice types
                    if (!(obj is IInterceptor || obj is IBeforeAdvice || obj is IAfterReturningAdvice))
                    {
                        objects.Add(obj);
                        names[obj] = name;
                    }
                }
            }
            objects.Sort(new OrderComparator());
            foreach (object obj in objects)
            {
                string name = names[obj];
                AddIntroductionOnChainCreation(obj, name);
            }
        }
コード例 #11
0
        /// <summary>
        /// Return the object instances that match the given object
        /// <see cref="System.Type"/> (including subclasses).
        /// </summary>
        /// <param name="type">
        /// The <see cref="System.Type"/> (class or interface) to match.
        /// </param>
        /// <param name="includeNonSingletons">
        /// Whether to include prototype objects too or just singletons (also applies to
        /// <see cref="Spring.Objects.Factory.IFactoryObject"/>s).
        /// </param>
        /// <param name="allowEagerInit">
        /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/>s too
        /// or just normal objects.
        /// </param>
        /// <returns>
        /// An <see cref="System.Collections.IDictionary"/> of the matching objects,
        /// containing the object names as keys and the corresponding object instances
        /// as values.
        /// </returns>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// If any of the objects could not be created.
        /// </exception>
        /// <seealso cref="Spring.Objects.Factory.IListableObjectFactory.GetObjectsOfType(Type, bool, bool)"/>
        protected IList DoGetObjectNamesForType(Type type, bool includeNonSingletons, bool allowEagerInit)
        {
            IList result = new ArrayList();

            string[] objectNames = GetObjectDefinitionNames();
            foreach (string s in objectNames)
            {
                string objectName = s;
                if (!IsAlias(objectName))
                {
                    try
                    {
                        RootObjectDefinition mod = GetMergedObjectDefinition(objectName, false);
                        // Only check object definition if it is complete
                        if (!mod.IsAbstract &&
                            (allowEagerInit || (mod.HasObjectType || !mod.IsLazyInit /*|| this.AllowEagerTypeLoading*/) &&
                             !RequiresEagerInitForType(mod.FactoryObjectName)))
                        {
                            bool isFactoryObject = IsFactoryObject(objectName, mod);
                            bool matchFound      =
                                (allowEagerInit || !isFactoryObject || ContainsSingleton(objectName)) &&
                                (includeNonSingletons || IsSingleton(objectName)) && IsTypeMatch(objectName, type);
                            if (!matchFound && isFactoryObject)
                            {
                                // in case of a FactoryObject, try to match FactoryObject instance itself next
                                objectName = ObjectFactoryUtils.BuildFactoryObjectName(objectName);
                                matchFound = (includeNonSingletons || mod.IsSingleton) && IsTypeMatch(objectName, type);
                            }
                            if (matchFound)
                            {
                                result.Add(objectName);
                            }
                        }
                    }
                    catch (CannotLoadObjectTypeException ex)
                    {
                        if (allowEagerInit)
                        {
                            throw;
                        }
                        // Probably contains a placeholder; lets ignore it for type matching purposes.
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Ignoring object class loading failure for object '" + objectName + "'", ex);
                        }
                    }
                    catch (ObjectDefinitionStoreException ex)
                    {
                        if (allowEagerInit)
                        {
                            throw;
                        }
                        // Probably contains a placeholder; lets ignore it for type matching purposes.
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Ignoring unresolvable metadata in object definition '" + objectName + "'", ex);
                        }
                    }
                }
            }

            // check singletons too, to catch manually registered singletons...
            string[] singletonNames = GetSingletonNames();
            foreach (string s in singletonNames)
            {
                string objectName = s;
                // only check if manually registered...
                if (!ContainsObjectDefinition(objectName))
                {
                    // in the case of an IFactoryObject, match the object created by the IFactoryObject...
                    if (IsFactoryObject(objectName))
                    {
                        if ((includeNonSingletons || IsSingleton(objectName)) && IsTypeMatch(objectName, type))
                        {
                            result.Add(objectName);
                            continue;
                        }
                        objectName = ObjectFactoryUtils.BuildFactoryObjectName(objectName);
                    }
                    if (IsTypeMatch(objectName, type))
                    {
                        result.Add(objectName);
                    }
                }
            }
            return(result);
        }
コード例 #12
0
        /// <summary>
        /// Ensure that all non-lazy-init singletons are instantiated, also
        /// considering <see cref="Spring.Objects.Factory.IFactoryObject"/>s.
        /// </summary>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// If one of the singleton objects could not be created.
        /// </exception>
        /// <seealso cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory.PreInstantiateSingletons"/>
        public void PreInstantiateSingletons()
        {
            #region Instrumentation

            if (log.IsDebugEnabled)
            {
                log.Debug("Pre-instantiating singletons in factory [" + this + "]");
            }

            #endregion

            try
            {
                int definitionCount = objectDefinitionNames.Count;
                for (int i = 0; i < definitionCount; i++)
                {
                    string name = (string)objectDefinitionNames[i];
                    if (!ContainsSingleton(name) && ContainsObjectDefinition(name))
                    {
                        RootObjectDefinition definition
                            = GetMergedObjectDefinition(name, false);
                        if (!definition.IsAbstract &&
                            definition.IsSingleton &&
                            !definition.IsLazyInit)
                        {
                            Type objectType = ResolveObjectType(definition, name);
                            if (objectType != null &&
                                typeof(IFactoryObject).IsAssignableFrom(definition.ObjectType))
                            {
                                IFactoryObject factoryObject = (IFactoryObject)GetObject(
                                    ObjectFactoryUtils.
                                    BuildFactoryObjectName(name));
                                if (factoryObject.IsSingleton)
                                {
                                    GetObject(name);
                                }
                            }
                            else
                            {
                                GetObject(name);
                            }
                        }
                    }
                }
            }
            catch (ObjectsException)
            {
                // destroy already created singletons to avoid dangling resources...
                try
                {
                    Dispose();
                }
                catch (Exception ex)
                {
                    log.Error(
                        "PreInstantiateSingletons failed but couldn't destroy any already-created singletons.",
                        ex);
                }
                throw;
            }
        }
コード例 #13
0
 /// <summary>
 /// Find object instances that match the <paramref name="requiredType"/>.
 /// </summary>
 /// <remarks>
 /// <p>
 /// Called by autowiring. If a subclass cannot obtain information about object
 /// names by <see cref="System.Type"/>, a corresponding exception should be thrown.
 /// </p>
 /// </remarks>
 /// <param name="requiredType">
 /// The type of the objects to look up.
 /// </param>
 /// <returns>
 /// An <see cref="System.Collections.IDictionary"/> of object names and object
 /// instances that match the <paramref name="requiredType"/>, or
 /// <see langword="null"/> if none is found.
 /// </returns>
 /// <exception cref="Spring.Objects.ObjectsException">
 /// In case of errors.
 /// </exception>
 protected override IDictionary FindMatchingObjects(Type requiredType)
 {
     return(ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(
                this, requiredType, true, true));
 }