public void CreateFactorySuccessful() { ObjectFactorySectionHandler objHandler = new ObjectFactorySectionHandler(); IListableObjectFactory factory = (IListableObjectFactory)objHandler.Create(null, null, _xmlElement); Assert.AreEqual(1, factory.ObjectDefinitionCount); }
/// <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); } }
/// <summary> /// Return all objects of the given type or subtypes, also picking up objects /// defined in ancestor object factories if the current object factory is an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>. /// </summary> /// <remarks> /// <p> /// The return list will only contain objects of this type. /// Useful convenience method when we don't care about object names. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <param name="type">The <see cref="System.Type"/> of object to match.</param> /// <param name="includePrototypes"> /// Whether to include prototype objects too or just singletons /// (also applies to <see cref="Spring.Objects.Factory.IFactoryObject"/> instances). /// </param> /// <param name="includeFactoryObjects"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/> instances /// too or just normal objects. /// </param> /// <exception cref="Spring.Objects.ObjectsException"> /// If the objects could not be created. /// </exception> /// <returns> /// The <see cref="System.Collections.IDictionary"/> of object instances, or an /// empty <see cref="System.Collections.IDictionary"/> if none. /// </returns> public static Dictionary <string, object> ObjectsOfTypeIncludingAncestors( IListableObjectFactory factory, Type type, bool includePrototypes, bool includeFactoryObjects) { Dictionary <string, object> result = new Dictionary <string, object>(); foreach (var entry in factory.GetObjectsOfType(type, includePrototypes, includeFactoryObjects)) { result.Add(entry.Key, entry.Value); } IListableObjectFactory pof = GetParentListableObjectFactoryIfAny(factory); if (pof != null) { IHierarchicalObjectFactory hof = (IHierarchicalObjectFactory)factory; Dictionary <string, object> parentResult = ObjectsOfTypeIncludingAncestors(pof, type, includePrototypes, includeFactoryObjects); foreach (string objectName in parentResult.Keys) { if (!result.ContainsKey(objectName) && !hof.ContainsLocalObject(objectName)) { result.Add(objectName, parentResult[objectName]); } } } return(result); }
///<summary> /// Creates a new bytecode Provider instance using the specified object factory ///</summary> ///<param name="listableObjectFactory"></param> public BytecodeProvider(IListableObjectFactory listableObjectFactory) { this.listableObjectFactory = listableObjectFactory; this.objectsFactory = new ObjectsFactory(listableObjectFactory); this.collectionTypefactory = new DefaultCollectionTypeFactory(); this.proxyFactoryFactory = new ProxyFactoryFactory(); }
/// <summary> /// Return a single object of the given type or subtypes, not looking in /// ancestor factories. /// </summary> /// <remarks> /// <p> /// Useful convenience method when we expect a single object and don't care /// about the object name. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <param name="type">The <see cref="System.Type"/> of object to match.</param> /// <param name="includePrototypes"> /// Whether to include prototype objects too or just singletons /// (also applies to <see cref="Spring.Objects.Factory.IFactoryObject"/> instances). /// </param> /// <param name="includeFactoryObjects"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/> instances /// too or just normal objects. /// </param> /// <exception cref="Spring.Objects.ObjectsException"> /// If the object could not be created. /// </exception> /// <exception cref="Spring.Objects.Factory.NoSuchObjectDefinitionException"> /// If not exactly one instance of an object was found. /// </exception> /// <returns> /// A single object of the given type or subtypes. /// </returns> public static object ObjectOfType(IListableObjectFactory factory, Type type, bool includePrototypes, bool includeFactoryObjects) { var objectsOfType = factory.GetObjectsOfType(type, includePrototypes, includeFactoryObjects); return(GrabTheOnlyObject(objectsOfType, type)); }
/// <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); } }
private static IListableObjectFactory GetParentListableObjectFactoryIfAny(IListableObjectFactory factory) { if (factory is IHierarchicalObjectFactory hierFactory) { return(hierFactory.ParentObjectFactory as IListableObjectFactory); } return(null); }
/// <summary> /// Creates a new adapter instance, using the specified <paramref name="factory"/> /// for resolving service instance requests. /// </summary> /// <param name="factory">the actual factory used for resolving service instance requests.</param> public SpringServiceLocatorAdapter(IListableObjectFactory factory) { if (factory == null) { throw new ArgumentNullException("factory", "A factory is mandatory"); } _factory = factory; }
/// <summary> /// Creates a new adapter instance, using the specified <paramref name="factory"/> /// for resolving service instance requests. /// </summary> /// <param name="factory">the actual factory used for resolving service instance requests.</param> public SpringServiceLocatorAdapter(IListableObjectFactory factory) { if (factory == null) { throw new ArgumentNullException("factory", "A factory is mandatory"); } _springFactory = factory; }
/// <summary> /// Return a single object of the given type or subtypes, also picking up objects defined /// in ancestor object factories if the current object factory is an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>. /// </summary> /// <remarks> /// <p> /// Useful convenience method when we expect a single object and don't care /// about the object name. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <param name="type">The <see cref="System.Type"/> of object to match.</param> /// <param name="includePrototypes"> /// Whether to include prototype objects too or just singletons /// (also applies to <see cref="Spring.Objects.Factory.IFactoryObject"/> instances). /// </param> /// <param name="includeFactoryObjects"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/> instances /// too or just normal objects. /// </param> /// <exception cref="Spring.Objects.ObjectsException"> /// If the object could not be created. /// </exception> /// <exception cref="Spring.Objects.Factory.NoSuchObjectDefinitionException"> /// If more than one instance of an object was found. /// </exception> /// <returns> /// A single object of the given type or subtypes. /// </returns> public static object ObjectOfTypeIncludingAncestors( IListableObjectFactory factory, Type type, bool includePrototypes, bool includeFactoryObjects) { var objectsOfType = ObjectsOfTypeIncludingAncestors(factory, type, includePrototypes, includeFactoryObjects); return(GrabTheOnlyObject(objectsOfType, type)); }
public SpringNetControllerFactory(IListableObjectFactory context) { if (context == null) { throw new ArgumentNullException("context"); } this.context = context; }
/// <summary>The get object.</summary> /// <param name="factory">The factory.</param> /// <typeparam name="T"></typeparam> /// <returns>The T.</returns> public static T GetObject <T>(this IListableObjectFactory factory) { var objectsForType = factory.GetObjectNamesForType(typeof(T)); if (objectsForType.Count() > 0) { return((T)factory.GetObject(objectsForType[0])); } // TODO: determine why this behavior is provided as a fall-back to not finding the object in the container... return(default(T)); }
/// <summary> /// Get all object names for the given type, including those defined in ancestor /// factories. /// </summary> /// <remarks> /// <p> /// Will return unique names in case of overridden object definitions. /// </p> /// <p> /// Does consider objects created by <see cref="Spring.Objects.Factory.IFactoryObject"/>s, /// or rather it considers the type of objects created by /// <see cref="Spring.Objects.Factory.IFactoryObject"/> (which means that /// <see cref="Spring.Objects.Factory.IFactoryObject"/>s will be instantiated). /// </p> /// </remarks> /// <param name="factory"> /// If this isn't also an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>, /// this method will return the same as it's own /// <see cref="Spring.Objects.Factory.IListableObjectFactory.GetObjectDefinitionNames"/> /// method. /// </param> /// <param name="type"> /// The <see cref="System.Type"/> that objects must match. /// </param> /// <returns> /// The array of object names, or an empty array if none. /// </returns> public static IList <string> ObjectNamesForTypeIncludingAncestors( IListableObjectFactory factory, Type type) { List <string> result = new List <string>(); result.AddRange(factory.GetObjectNamesForType(type)); IListableObjectFactory pof = GetParentListableObjectFactoryIfAny(factory); if (pof != null) { IHierarchicalObjectFactory hof = (IHierarchicalObjectFactory)factory; IList <string> parentsResult = ObjectNamesForTypeIncludingAncestors(pof, type); foreach (string objectName in parentsResult) { if (!result.Contains(objectName) && !hof.ContainsLocalObject(objectName)) { result.Add(objectName); } } } return(result); }
/// <summary> /// Get all object names for the given type, including those defined in ancestor /// factories. /// </summary> /// <remarks> /// <p> /// Will return unique names in case of overridden object definitions. /// </p> /// <p> /// Does consider objects created by <see cref="Spring.Objects.Factory.IFactoryObject"/>s /// if <paramref name="includeFactoryObjects"/> is set to true, /// which means that <see cref="Spring.Objects.Factory.IFactoryObject"/>s will get initialized. /// </p> /// </remarks> /// <param name="factory"> /// If this isn't also an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>, /// this method will return the same as it's own /// <see cref="Spring.Objects.Factory.IListableObjectFactory.GetObjectDefinitionNames"/> /// method. /// </param> /// <param name="type"> /// The <see cref="System.Type"/> that objects must match. /// </param> /// <param name="includePrototypes"> /// Whether to include prototype objects too or just singletons /// (also applies to <see cref="Spring.Objects.Factory.IFactoryObject"/> instances). /// </param> /// <param name="includeFactoryObjects"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/> instances /// too or just normal objects. /// </param> /// <returns> /// The array of object names, or an empty array if none. /// </returns> public static string[] ObjectNamesForTypeIncludingAncestors( IListableObjectFactory factory, Type type, bool includePrototypes, bool includeFactoryObjects) { ArrayList result = new ArrayList(); result.AddRange(factory.GetObjectNamesForType(type, includePrototypes, includeFactoryObjects)); IListableObjectFactory pof = GetParentListableObjectFactoryIfAny(factory); if (pof != null) { IHierarchicalObjectFactory hof = (IHierarchicalObjectFactory)factory; string[] parentsResult = ObjectNamesForTypeIncludingAncestors(pof, type, includePrototypes, includeFactoryObjects); foreach (string objectName in parentsResult) { if (!result.Contains(objectName) && !hof.ContainsLocalObject(objectName)) { result.Add(objectName); } } } return((string[])result.ToArray(typeof(string))); }
/// <summary> /// Finds the scheduler. /// </summary> /// <param name="schedulerName">Name of the scheduler.</param> /// <returns></returns> protected virtual IScheduler FindScheduler(string schedulerName) { if (objectFactory is IListableObjectFactory) { IListableObjectFactory lbf = (IListableObjectFactory)objectFactory; string[] objectNames = lbf.GetObjectNamesForType(typeof(IScheduler)); for (int i = 0; i < objectNames.Length; i++) { IScheduler schedulerObject = (IScheduler)lbf.GetObject(objectNames[i]); if (schedulerName.Equals(schedulerObject.SchedulerName)) { return(schedulerObject); } } } IScheduler schedulerInRepo = SchedulerRepository.Instance.Lookup(schedulerName); if (schedulerInRepo == null) { throw new InvalidOperationException("No Scheduler named '" + schedulerName + "' found"); } return(schedulerInRepo); }
/// <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 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 (DictionaryEntry pet in pets) { cpet.AddTranslator((IPersistenceExceptionTranslator)pet.Value); } return cpet; }
/// <summary> /// Initializes a new instance of the <see cref="PersistenceExceptionTranslationAdvisor"/> class. /// </summary> /// <param name="objectFactory">The object factory to obtain all IPersistenceExceptionTranslators from.</param> /// <param name="repositoryAttributeType">Type of the repository attribute to check for.</param> public PersistenceExceptionTranslationAdvisor(IListableObjectFactory objectFactory, Type repositoryAttributeType) { this.advice = new PersistenceExceptionTranslationInterceptor(objectFactory); this.pointcut = new AttributeMatchingPointcut(repositoryAttributeType, true); }
/// <summary> /// Return a single object of the given type or subtypes, not looking in /// ancestor factories. /// </summary> /// <remarks> /// <p> /// Useful convenience method when we expect a single object and don't care /// about the object name. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <param name="type">The <see cref="System.Type"/> of object to match.</param> /// <param name="includePrototypes"> /// Whether to include prototype objects too or just singletons /// (also applies to <see cref="Spring.Objects.Factory.IFactoryObject"/> instances). /// </param> /// <param name="includeFactoryObjects"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/> instances /// too or just normal objects. /// </param> /// <exception cref="Spring.Objects.ObjectsException"> /// If the object could not be created. /// </exception> /// <exception cref="Spring.Objects.Factory.NoSuchObjectDefinitionException"> /// If not exactly one instance of an object was found. /// </exception> /// <returns> /// A single object of the given type or subtypes. /// </returns> public static object ObjectOfType(IListableObjectFactory factory, Type type, bool includePrototypes, bool includeFactoryObjects) { IDictionary objectsOfType = factory.GetObjectsOfType(type, includePrototypes, includeFactoryObjects); return GrabTheOnlyObject(objectsOfType, type); }
/// <summary> /// Get all object names for the given type, including those defined in ancestor /// factories. /// </summary> /// <remarks> /// <p> /// Will return unique names in case of overridden object definitions. /// </p> /// <p> /// Does consider objects created by <see cref="Spring.Objects.Factory.IFactoryObject"/>s /// if <paramref name="includeFactoryObjects"/> is set to true, /// which means that <see cref="Spring.Objects.Factory.IFactoryObject"/>s will get initialized. /// </p> /// </remarks> /// <param name="factory"> /// If this isn't also an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>, /// this method will return the same as it's own /// <see cref="Spring.Objects.Factory.IListableObjectFactory.GetObjectDefinitionNames"/> /// method. /// </param> /// <param name="type"> /// The <see cref="System.Type"/> that objects must match. /// </param> /// <param name="includePrototypes"> /// Whether to include prototype objects too or just singletons /// (also applies to <see cref="Spring.Objects.Factory.IFactoryObject"/> instances). /// </param> /// <param name="includeFactoryObjects"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/> instances /// too or just normal objects. /// </param> /// <returns> /// The array of object names, or an empty array if none. /// </returns> public static IList<string> ObjectNamesForTypeIncludingAncestors( IListableObjectFactory factory, Type type, bool includePrototypes, bool includeFactoryObjects) { List<string> result = new List<string>(); result.AddRange(factory.GetObjectNamesForType(type, includePrototypes, includeFactoryObjects)); IListableObjectFactory pof = GetParentListableObjectFactoryIfAny(factory); if (pof != null) { IHierarchicalObjectFactory hof = (IHierarchicalObjectFactory)factory; IList<string> parentsResult = ObjectNamesForTypeIncludingAncestors(pof, type, includePrototypes, includeFactoryObjects); foreach (string objectName in parentsResult) { if (!result.Contains(objectName) && !hof.ContainsLocalObject(objectName)) { result.Add(objectName); } } } return result; }
///<summary> ///</summary> ///<param name="listableObjectFactory"></param> ///<param name="mappedType"></param> ///<param name="getters"></param> ///<param name="setters"></param> public ReflectionOptimizer(IListableObjectFactory listableObjectFactory, Type mappedType, IGetter[] getters, ISetter[] setters) : base(mappedType, getters, setters) { this.listableObjectFactory = listableObjectFactory; }
/// <summary>Create the advisor (interceptor) chain.</summary> /// <remarks> /// The advisors that are sourced from an ObjectFactory will be refreshed each time /// a new prototype instance is added. Interceptors added programmatically through /// the factory API are unaffected by such changes. /// </remarks> private void InitializeAdvisorChain() { if (ObjectUtils.IsEmpty(this.interceptorNames)) { return; } CheckInterceptorNames(); // Globals can't be last unless we specified a targetSource using the property... if (this.interceptorNames[this.interceptorNames.Length - 1] != null && this.interceptorNames[this.interceptorNames.Length - 1].EndsWith(GlobalInterceptorSuffix) && this.targetName == null && this.TargetSource == EmptyTargetSource.Empty) { throw new AopConfigException("Target required after globals"); } // materialize interceptor chain from object names... foreach (string name in this.interceptorNames) { if (name == null) { throw new AopConfigException("Found null interceptor name value in the InterceptorNames list; check your configuration."); } if (name.EndsWith(GlobalInterceptorSuffix)) { IListableObjectFactory lof = this.objectFactory as IListableObjectFactory; if (lof == null) { throw new AopConfigException("Can only use global advisors or interceptors in conjunction with an IListableObjectFactory."); } #region Instrumentation if (logger.IsDebugEnabled) { logger.Debug("Adding global advisor '" + name + "'"); } #endregion AddGlobalAdvisor(lof, name.Substring(0, (name.Length - GlobalInterceptorSuffix.Length))); } else { #region Instrumentation if (logger.IsDebugEnabled) { logger.Debug("resolving advisor name " + "'" + name + "'"); } #endregion // If we get here, we need to add a named interceptor. // We must check if it's a singleton or prototype. object advice; if (this.IsSingleton || this.objectFactory.IsSingleton(name)) { advice = this.objectFactory.GetObject(name); AssertUtils.ArgumentNotNull(advice, "advice", "object factory returned a null object"); } else { advice = new PrototypePlaceholder(name); } AddAdvisorOnChainCreation(advice, name); } } }
/// <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); } }
public static T Resolve <T>(this IListableObjectFactory factory) { return(factory.GetObjectsOfType(typeof(T)) .Values.OfType <T>().Single()); }
/// <summary> /// Get all object names for the given type, including those defined in ancestor /// factories. /// </summary> /// <remarks> /// <p> /// Will return unique names in case of overridden object definitions. /// </p> /// <p> /// Does consider objects created by <see cref="Spring.Objects.Factory.IFactoryObject"/>s, /// or rather it considers the type of objects created by /// <see cref="Spring.Objects.Factory.IFactoryObject"/> (which means that /// <see cref="Spring.Objects.Factory.IFactoryObject"/>s will be instantiated). /// </p> /// </remarks> /// <param name="factory"> /// If this isn't also an <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>, /// this method will return the same as it's own <see cref="IListableObjectFactory.GetObjectDefinitionNames()"/> method. /// </param> /// <param name="type"> /// The <see cref="System.Type"/> that objects must match. /// </param> /// <returns> /// The array of object names, or an empty array if none. /// </returns> public static IReadOnlyList <string> ObjectNamesForTypeIncludingAncestors(IListableObjectFactory factory, Type type) { return(factory.GetObjectNamesForType(type)); }
/// <summary> /// Return all object names in the factory, including ancestor factories. /// </summary> /// <param name="factory">The object factory.</param> /// <returns>The array of object names, or an empty array if none.</returns> public static IReadOnlyList <string> ObjectNamesIncludingAncestors(IListableObjectFactory factory) { return(ObjectNamesForTypeIncludingAncestors(factory, typeof(object))); }
/// <summary> /// Count all object definitions in any hierarchy in which this /// factory participates. /// </summary> /// <remarks> /// <p> /// Includes counts of ancestor object factories. /// </p> /// <p> /// Objects that are "overridden" (specified in a descendant factory /// with the same name) are counted only once. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <returns> /// The count of objects including those defined in ancestor factories. /// </returns> public static int CountObjectsIncludingAncestors(IListableObjectFactory factory) { return(ObjectNamesIncludingAncestors(factory).Count); }
public EnhancedBytecode(IListableObjectFactory listableObjectFactory) { this.listableObjectFactory = listableObjectFactory; objectsFactory = new ObjectsFactory(listableObjectFactory); collectionTypefactory = new DefaultCollectionTypeFactory(); }
/// <summary> /// Initializes a new instance of the <see cref="PersistenceExceptionTranslationInterceptor"/> class, autodetecting /// IPersistenceExceptionTranslators in the given object factory. /// </summary> /// <param name="objectFactory">The object factory to obtain all IPersistenceExceptionTranslators from.</param> public PersistenceExceptionTranslationInterceptor(IListableObjectFactory objectFactory) { this.persistenceExceptionTranslator = DetectPersistenceExceptionTranslators(objectFactory); }
/// <summary> /// TODO: /// </summary> /// <param name="listableObjectFactory"></param> public BytecodeProviderSrlzSupport(IListableObjectFactory listableObjectFactory) : base(listableObjectFactory) { this.proxyFactoryFactory = new ProxyFactoryFactorySrlzSupport(); }
/// <summary> Add all global interceptors and pointcuts.</summary> private void AddGlobalAdvisor(IListableObjectFactory objectFactory, string prefix) { IList<string> globalAspectNames = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvisors)); IList<string> globalAdvisorNames = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(objectFactory, typeof(IAdvisor)); IList<string> 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(new OrderComparator()); foreach (object obj in objects) { string name = names[obj]; AddAdvisorOnChainCreation(obj, name); } }
///<summary> ///</summary> ///<param name="listableObjectFactory"></param> public ObjectsFactory(IListableObjectFactory listableObjectFactory) { this.listableObjectFactory = listableObjectFactory; }
/// <summary> /// Return all object names in the factory, including ancestor factories. /// </summary> /// <param name="factory">The object factory.</param> /// <returns>The array of object names, or an empty array if none.</returns> public static IList<string> ObjectNamesIncludingAncestors(IListableObjectFactory factory) { return ObjectNamesForTypeIncludingAncestors(factory, typeof(object)); }
/// <summary>The get objects of type.</summary> /// <param name="factory">The factory.</param> /// <typeparam name="T"></typeparam> /// <returns>The System.Collections.IDictionary.</returns> public static IDictionary GetObjects <T>(this IListableObjectFactory factory) { return(factory.GetObjectsOfType(typeof(T))); }
/// <summary> /// Return a single object of the given type or subtypes, also picking up objects defined /// in ancestor object factories if the current object factory is an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>. /// </summary> /// <remarks> /// <p> /// Useful convenience method when we expect a single object and don't care /// about the object name. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <param name="type">The <see cref="System.Type"/> of object to match.</param> /// <param name="includePrototypes"> /// Whether to include prototype objects too or just singletons /// (also applies to <see cref="Spring.Objects.Factory.IFactoryObject"/> instances). /// </param> /// <param name="includeFactoryObjects"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/> instances /// too or just normal objects. /// </param> /// <exception cref="Spring.Objects.ObjectsException"> /// If the object could not be created. /// </exception> /// <exception cref="Spring.Objects.Factory.NoSuchObjectDefinitionException"> /// If more than one instance of an object was found. /// </exception> /// <returns> /// A single object of the given type or subtypes. /// </returns> public static object ObjectOfTypeIncludingAncestors( IListableObjectFactory factory, Type type, bool includePrototypes, bool includeFactoryObjects) { IDictionary<string, object> objectsOfType = ObjectsOfTypeIncludingAncestors(factory, type, includePrototypes, includeFactoryObjects); return GrabTheOnlyObject(objectsOfType, type); }
/// <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); } }
private static IListableObjectFactory GetParentListableObjectFactoryIfAny(IListableObjectFactory factory) { IHierarchicalObjectFactory hierFactory = factory as IHierarchicalObjectFactory; if (hierFactory != null) { return hierFactory.ParentObjectFactory as IListableObjectFactory; } return null; }
/// <summary> /// Count all object definitions in any hierarchy in which this /// factory participates. /// </summary> /// <remarks> /// <p> /// Includes counts of ancestor object factories. /// </p> /// <p> /// Objects that are "overridden" (specified in a descendant factory /// with the same name) are counted only once. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <returns> /// The count of objects including those defined in ancestor factories. /// </returns> public static int CountObjectsIncludingAncestors(IListableObjectFactory factory) { return ObjectNamesIncludingAncestors(factory).Count; }
/// <summary> /// Get all object names for the given type, including those defined in ancestor /// factories. /// </summary> /// <remarks> /// <p> /// Will return unique names in case of overridden object definitions. /// </p> /// <p> /// Does consider objects created by <see cref="Spring.Objects.Factory.IFactoryObject"/>s, /// or rather it considers the type of objects created by /// <see cref="Spring.Objects.Factory.IFactoryObject"/> (which means that /// <see cref="Spring.Objects.Factory.IFactoryObject"/>s will be instantiated). /// </p> /// </remarks> /// <param name="factory"> /// If this isn't also an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>, /// this method will return the same as it's own /// <see cref="Spring.Objects.Factory.IListableObjectFactory.GetObjectDefinitionNames"/> /// method. /// </param> /// <param name="type"> /// The <see cref="System.Type"/> that objects must match. /// </param> /// <returns> /// The array of object names, or an empty array if none. /// </returns> public static IList<string> ObjectNamesForTypeIncludingAncestors( IListableObjectFactory factory, Type type) { return factory.GetObjectNamesForType(type); }
/// <summary> /// Get all object names for the given type, including those defined in ancestor /// factories. /// </summary> /// <remarks> /// <p> /// Will return unique names in case of overridden object definitions. /// </p> /// <p> /// Does consider objects created by <see cref="Spring.Objects.Factory.IFactoryObject"/>s, /// or rather it considers the type of objects created by /// <see cref="Spring.Objects.Factory.IFactoryObject"/> (which means that /// <see cref="Spring.Objects.Factory.IFactoryObject"/>s will be instantiated). /// </p> /// </remarks> /// <param name="factory"> /// If this isn't also an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>, /// this method will return the same as it's own /// <see cref="Spring.Objects.Factory.IListableObjectFactory.GetObjectDefinitionNames"/> /// method. /// </param> /// <param name="type"> /// The <see cref="System.Type"/> that objects must match. /// </param> /// <returns> /// The array of object names, or an empty array if none. /// </returns> public static string[] ObjectNamesForTypeIncludingAncestors( IListableObjectFactory factory, Type type) { ArrayList result = new ArrayList(); result.AddRange(factory.GetObjectNamesForType(type)); IListableObjectFactory pof = GetParentListableObjectFactoryIfAny(factory); if (pof != null) { IHierarchicalObjectFactory hof = (IHierarchicalObjectFactory)factory; string[] parentsResult = ObjectNamesForTypeIncludingAncestors(pof, type); foreach (string objectName in parentsResult) { if (!result.Contains(objectName) && !hof.ContainsLocalObject(objectName)) { result.Add(objectName); } } } return (string[])result.ToArray(typeof(string)); }
/// <summary> /// Return a single object of the given type or subtypes, not looking in /// ancestor factories. /// </summary> /// <remarks> /// <p> /// Useful convenience method when we expect a single object and don't care /// about the object name. /// This version of <c>ObjectOfType</c> automatically includes prototypes and /// <see cref="Spring.Objects.Factory.IFactoryObject"/> instances. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <param name="type">The <see cref="System.Type"/> of object to match.</param> /// <exception cref="Spring.Objects.ObjectsException"> /// If the object could not be created. /// </exception> /// <exception cref="Spring.Objects.Factory.NoSuchObjectDefinitionException"> /// If not exactly one instance of an object was found. /// </exception> /// <returns> /// A single object of the given type or subtypes. /// </returns> public static object ObjectOfType(IListableObjectFactory factory, Type type) { return ObjectOfType(factory, type, true, true); }
/// <summary> /// Return a single object of the given type or subtypes, not looking in /// ancestor factories. /// </summary> /// <remarks> /// <p> /// Useful convenience method when we expect a single object and don't care /// about the object name. /// This version of <c>ObjectOfType</c> automatically includes prototypes and /// <see cref="Spring.Objects.Factory.IFactoryObject"/> instances. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <param name="type">The <see cref="System.Type"/> of object to match.</param> /// <exception cref="Spring.Objects.ObjectsException"> /// If the object could not be created. /// </exception> /// <exception cref="Spring.Objects.Factory.NoSuchObjectDefinitionException"> /// If not exactly one instance of an object was found. /// </exception> /// <returns> /// A single object of the given type or subtypes. /// </returns> public static object ObjectOfType(IListableObjectFactory factory, Type type) { return(ObjectOfType(factory, type, true, true)); }
/// <summary> /// Return all objects of the given type or subtypes, also picking up objects /// defined in ancestor object factories if the current object factory is an /// <see cref="Spring.Objects.Factory.IHierarchicalObjectFactory"/>. /// </summary> /// <remarks> /// <p> /// The return list will only contain objects of this type. /// Useful convenience method when we don't care about object names. /// </p> /// </remarks> /// <param name="factory">The object factory.</param> /// <param name="type">The <see cref="System.Type"/> of object to match.</param> /// <param name="includePrototypes"> /// Whether to include prototype objects too or just singletons /// (also applies to <see cref="Spring.Objects.Factory.IFactoryObject"/> instances). /// </param> /// <param name="includeFactoryObjects"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/> instances /// too or just normal objects. /// </param> /// <exception cref="Spring.Objects.ObjectsException"> /// If the objects could not be created. /// </exception> /// <returns> /// The <see cref="System.Collections.IDictionary"/> of object instances, or an /// empty <see cref="System.Collections.IDictionary"/> if none. /// </returns> public static IDictionary<string, object> ObjectsOfTypeIncludingAncestors( IListableObjectFactory factory, Type type, bool includePrototypes, bool includeFactoryObjects) { Dictionary<string, object> result = new Dictionary<string, object>(); foreach (KeyValuePair<string, object> entry in factory.GetObjectsOfType(type, includePrototypes, includeFactoryObjects)) { result.Add(entry.Key, entry.Value); } IListableObjectFactory pof = GetParentListableObjectFactoryIfAny(factory); if (pof != null) { IHierarchicalObjectFactory hof = (IHierarchicalObjectFactory)factory; IDictionary<string, object> parentResult = ObjectsOfTypeIncludingAncestors(pof, type, includePrototypes, includeFactoryObjects); foreach (string objectName in parentResult.Keys) { if (!result.ContainsKey(objectName) && !hof.ContainsLocalObject(objectName)) { result.Add(objectName, parentResult[objectName]); } } } return result; }
/// <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); }