/// <summary> /// DO NOT USE - this is subject to change! /// </summary> /// <param name="appRelativeVirtualPath"></param> /// <param name="objectFactory"></param> /// <returns> /// This method requires registrars to follow the convention of registering web object definitions using their /// application relative urls (~/mypath/mypage.aspx). /// </returns> /// <remarks> /// Resolve an object definition by url. /// </remarks> protected internal static NamedObjectDefinition FindWebObjectDefinition(string appRelativeVirtualPath, IConfigurableListableObjectFactory objectFactory) { ILog Log = LogManager.GetLogger(typeof(AbstractHandlerFactory)); bool isDebug = Log.IsDebugEnabled; // lookup definition using app-relative url if (isDebug) { Log.Debug(string.Format("GetHandler():looking up definition for app-relative url '{0}'", appRelativeVirtualPath)); } string objectDefinitionName = appRelativeVirtualPath; IObjectDefinition pageDefinition = objectFactory.GetObjectDefinition(appRelativeVirtualPath, true); if (pageDefinition == null) { // try using pagename+extension and pagename only string pageExtension = Path.GetExtension(appRelativeVirtualPath); string pageName = WebUtils.GetPageName(appRelativeVirtualPath); // only looks in the specified object factory -- it will *not* search parent contexts pageDefinition = objectFactory.GetObjectDefinition(pageName + pageExtension, false); if (pageDefinition == null) { pageDefinition = objectFactory.GetObjectDefinition(pageName, false); if (pageDefinition != null) { objectDefinitionName = pageName; } } else { objectDefinitionName = pageName + pageExtension; } if (pageDefinition != null) { if (isDebug) { Log.Debug(string.Format("GetHandler():found definition for page-name '{0}'", objectDefinitionName)); } } else { if (isDebug) { Log.Debug(string.Format("GetHandler():no definition found for page-name '{0}'", pageName)); } } } else { if (isDebug) { Log.Debug(string.Format("GetHandler():found definition for page-url '{0}'", appRelativeVirtualPath)); } } return((pageDefinition == null) ? (NamedObjectDefinition)null : new NamedObjectDefinition(objectDefinitionName, pageDefinition)); }
public void ReplacesNamedCtorArgument() { RootObjectDefinition def = new RootObjectDefinition(); def.ObjectType = typeof(TestObject); ConstructorArgumentValues args = new ConstructorArgumentValues(); args.AddNamedArgumentValue("name", "${hope.floats}"); def.ConstructorArgumentValues = args; NameValueCollection properties = new NameValueCollection(); const string expectedName = "Rick"; properties.Add("hope.floats", expectedName); IConfigurableListableObjectFactory mock = mocks.StrictMock <IConfigurableListableObjectFactory>(); Expect.Call(mock.GetObjectDefinitionNames(false)).Return(new string[] { "foo" }); Expect.Call(mock.GetObjectDefinition(null, false)).IgnoreArguments().Return(def); Expect.Call(delegate { mock.AddEmbeddedValueResolver(null); }).IgnoreArguments(); mocks.ReplayAll(); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); cfg.Properties = properties; cfg.PostProcessObjectFactory(mock); mocks.VerifyAll(); Assert.AreEqual(expectedName, def.ConstructorArgumentValues.GetNamedArgumentValue("name").Value, "Named argument placeholder value was not replaced."); }
public void ChokesOnCircularReferenceToPlaceHolder() { RootObjectDefinition def = new RootObjectDefinition(); def.ObjectType = typeof(TestObject); ConstructorArgumentValues args = new ConstructorArgumentValues(); args.AddNamedArgumentValue("name", "${foo}"); def.ConstructorArgumentValues = args; NameValueCollection properties = new NameValueCollection(); const string expectedName = "ba${foo}r"; properties.Add("foo", expectedName); IConfigurableListableObjectFactory mock = A.Fake <IConfigurableListableObjectFactory>(); A.CallTo(() => mock.GetObjectDefinitionNames(false)).Returns(new string[] { "foo" }); A.CallTo(() => mock.GetObjectDefinition(null, false)).WithAnyArguments().Returns(def); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); cfg.Properties = properties; try { cfg.PostProcessObjectFactory(mock); Assert.Fail("Should have raised an ObjectDefinitionStoreException by this point."); } catch (ObjectDefinitionStoreException) { } }
public void UsingCustomMarkers() { RootObjectDefinition def = new RootObjectDefinition(); def.ObjectType = typeof(TestObject); ConstructorArgumentValues args = new ConstructorArgumentValues(); args.AddNamedArgumentValue("name", "#hope.floats#"); def.ConstructorArgumentValues = args; NameValueCollection properties = new NameValueCollection(); const string expectedName = "Rick"; properties.Add("hope.floats", expectedName); IConfigurableListableObjectFactory mock = A.Fake <IConfigurableListableObjectFactory>(); A.CallTo(() => mock.GetObjectDefinitionNames(false)).Returns(new string[] { "foo" }); A.CallTo(() => mock.GetObjectDefinition(null, false)).WithAnyArguments().Returns(def); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); cfg.PlaceholderPrefix = cfg.PlaceholderSuffix = "#"; cfg.Properties = properties; cfg.PostProcessObjectFactory(mock); A.CallTo(() => mock.AddEmbeddedValueResolver(null)).WithAnyArguments().MustHaveHappened(); Assert.AreEqual(expectedName, def.ConstructorArgumentValues.GetNamedArgumentValue("name").Value, "Named argument placeholder value was not replaced."); }
public void UsingCustomMarkers() { RootObjectDefinition def = new RootObjectDefinition(); def.ObjectType = typeof(TestObject); ConstructorArgumentValues args = new ConstructorArgumentValues(); args.AddNamedArgumentValue("name", "#hope.floats#"); def.ConstructorArgumentValues = args; NameValueCollection properties = new NameValueCollection(); const string expectedName = "Rick"; properties.Add("hope.floats", expectedName); IConfigurableListableObjectFactory mock = (IConfigurableListableObjectFactory)mocks.CreateMock(typeof(IConfigurableListableObjectFactory)); Expect.Call(mock.GetObjectDefinitionNames()).Return(new string[] { "foo" }); Expect.Call(mock.GetObjectDefinition(null)).IgnoreArguments().Return(def); mocks.ReplayAll(); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); cfg.PlaceholderPrefix = cfg.PlaceholderSuffix = "#"; cfg.Properties = properties; cfg.PostProcessObjectFactory(mock); mocks.VerifyAll(); Assert.AreEqual(expectedName, def.ConstructorArgumentValues.GetNamedArgumentValue("name").Value, "Named argument placeholder value was not replaced."); }
/// <summary> /// Apply the property replacement using the specified <see cref="IVariableSource"/>s for all /// object in the supplied /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>. /// </summary> /// <param name="factory"> /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/> /// used by the application context. /// </param> /// <exception cref="Spring.Objects.ObjectsException"> /// If an error occured. /// </exception> protected virtual void ProcessProperties(IConfigurableListableObjectFactory factory) { CompositeVariableSource compositeVariableSource = new CompositeVariableSource(variableSourceList); TextProcessor tp = new TextProcessor(this, compositeVariableSource); ObjectDefinitionVisitor visitor = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(tp.ParseAndResolveVariables)); var objectDefinitionNames = factory.GetObjectDefinitionNames(includeAncestors); for (int i = 0; i < objectDefinitionNames.Count; ++i) { string name = objectDefinitionNames[i]; IObjectDefinition definition = factory.GetObjectDefinition(name, includeAncestors); if (definition == null) { continue; } try { visitor.VisitObjectDefinition(definition); } catch (ObjectDefinitionStoreException ex) { throw new ObjectDefinitionStoreException( definition.ResourceDescription, name, ex.Message); } } }
/// <summary> /// Allows for custom modification of an application context's object definitions. /// </summary> /// <remarks> /// <p> /// If the object name matches, replaces the type by a AOP proxied type based on inheritance. /// </p> /// </remarks> public void PostProcessObjectFactory(IConfigurableListableObjectFactory factory) { IList <string> objectDefinitionNames = factory.GetObjectDefinitionNames(); for (int i = 0; i < objectDefinitionNames.Count; ++i) { string name = objectDefinitionNames[i]; if (IsObjectNameMatch(name)) { IConfigurableObjectDefinition definition = factory.GetObjectDefinition(name) as IConfigurableObjectDefinition; if (definition == null || IsInfrastructureType(definition.ObjectType, name)) { continue; } ProxyFactory pf = CreateProxyFactory(definition.ObjectType, name); InheritanceAopProxyTypeBuilder iaptb = new InheritanceAopProxyTypeBuilder(pf); iaptb.ProxyDeclaredMembersOnly = this.ProxyDeclaredMembersOnly; Type type = iaptb.BuildProxyType(); definition.ObjectType = type; definition.ConstructorArgumentValues.AddIndexedArgumentValue(definition.ConstructorArgumentValues.ArgumentCount, pf); } } }
public void IncludingAncestors() { const string defName = "foo"; const string placeholder = "${name}"; MutablePropertyValues pvs = new MutablePropertyValues(); const string theProperty = "name"; pvs.Add(theProperty, placeholder); RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), pvs); IConfigurableListableObjectFactory mock = mocks.StrictMock <IConfigurableListableObjectFactory>(); Expect.Call(mock.GetObjectDefinitionNames(true)).Return(new string[] { defName }); Expect.Call(mock.GetObjectDefinition(defName, true)).Return(def); Expect.Call(delegate { mock.AddEmbeddedValueResolver(null); }).IgnoreArguments(); mocks.ReplayAll(); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); cfg.IncludeAncestors = true; NameValueCollection defaultProperties = new NameValueCollection(); const string expectedName = "Rick Evans"; defaultProperties.Add(theProperty, expectedName); cfg.Properties = defaultProperties; cfg.PostProcessObjectFactory(mock); Assert.AreEqual(expectedName, def.PropertyValues.GetPropertyValue(theProperty).Value, "Property placeholder value was not replaced with the resolved value."); mocks.VerifyAll(); }
public void PostProcessObjectFactory(IConfigurableListableObjectFactory factory) { IList<string> objectDefinitionNames = factory.GetObjectDefinitionNames(); if (Logger.IsInfoEnabled) Logger.Info("Iniciando proceso para establecer el alcance de los services y controllers"); foreach (string objectDefinitionName in objectDefinitionNames) { IObjectDefinition objectDefinition = factory.GetObjectDefinition(objectDefinitionName); if (objectDefinition is ScannedGenericObjectDefinition) { var componentAttribute = Attribute.GetCustomAttribute(objectDefinition.ObjectType, typeof (ComponentAttribute), true) as ComponentAttribute; if ( (componentAttribute is ControllerAttribute) || (WcfServices && componentAttribute is ServiceAttribute) ) { if (Logger.IsInfoEnabled) Logger.InfoFormat("Estableciendo alcance para la definición {0}", objectDefinitionName); objectDefinition.Scope = "prototype"; } } } }
public void InlcludeAncestors() { const string defName = "foo"; const string placeholder = "${name}"; MutablePropertyValues pvs = new MutablePropertyValues(); const string theProperty = "name"; pvs.Add(theProperty, placeholder); RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), pvs); IConfigurableListableObjectFactory mock = mocks.StrictMock <IConfigurableListableObjectFactory>(); Expect.Call(mock.GetObjectDefinitionNames(true)).Return(new string[] { defName }); Expect.Call(mock.GetObjectDefinition(defName, true)).Return(def); mocks.ReplayAll(); VariablePlaceholderConfigurer vpc = new VariablePlaceholderConfigurer(); vpc.IgnoreUnresolvablePlaceholders = true; vpc.VariableSource = new DictionaryVariableSource(new string[] { "name", "Erich" }); vpc.IncludeAncestors = true; vpc.PostProcessObjectFactory(mock); mocks.VerifyAll(); }
public void WithIgnoreUnresolvablePlaceholder() { const string defName = "foo"; const string placeholder = "${name}"; TestObject foo = new TestObject(placeholder, 30); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add("name", placeholder); RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), pvs); IConfigurableListableObjectFactory mock = mocks.StrictMock <IConfigurableListableObjectFactory>(); Expect.Call(mock.GetObjectDefinitionNames(false)).Return(new string [] { defName }); Expect.Call(mock.GetObjectDefinition(defName, false)).Return(def); Expect.Call(() => mock.AddEmbeddedValueResolver(null)).IgnoreArguments(); mocks.ReplayAll(); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); cfg.IgnoreUnresolvablePlaceholders = true; cfg.PostProcessObjectFactory(mock); Assert.AreEqual(placeholder, foo.Name); mocks.VerifyAll(); }
private void EnhanceConfigurationClasses(IConfigurableListableObjectFactory objectFactory) { ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer(objectFactory); IList <string> objectNames = objectFactory.GetObjectDefinitionNames(); foreach (string name in objectNames) { IObjectDefinition objDef = objectFactory.GetObjectDefinition(name); if (((AbstractObjectDefinition)objDef).HasObjectType) { if (Attribute.GetCustomAttribute(objDef.ObjectType, typeof(ConfigurationAttribute)) != null) { //TODO check type of object isn't infrastructure type. Type configClass = objDef.ObjectType; Type enhancedClass = enhancer.Enhance(configClass); Logger.Debug(m => m("Replacing object definition '{0}' existing class '{1}' with enhanced class", name, configClass.FullName)); ((IConfigurableObjectDefinition)objDef).ObjectType = enhancedClass; } } } }
public void ChokesOnCircularReferenceToPlaceHolder() { RootObjectDefinition def = new RootObjectDefinition(); def.ObjectType = typeof(TestObject); ConstructorArgumentValues args = new ConstructorArgumentValues(); args.AddNamedArgumentValue("name", "${foo}"); def.ConstructorArgumentValues = args; NameValueCollection properties = new NameValueCollection(); const string expectedName = "ba${foo}r"; properties.Add("foo", expectedName); IConfigurableListableObjectFactory mock = (IConfigurableListableObjectFactory)mocks.CreateMock(typeof(IConfigurableListableObjectFactory)); Expect.Call(mock.GetObjectDefinitionNames()).Return(new string[] { "foo" }); Expect.Call(mock.GetObjectDefinition(null)).IgnoreArguments().Return(def); mocks.ReplayAll(); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); cfg.Properties = properties; try { cfg.PostProcessObjectFactory(mock); Assert.Fail("Should have raised an ObjectDefinitionStoreException by this point."); } catch (ObjectDefinitionStoreException) { } mocks.VerifyAll(); }
public void WithDefaultProperties() { const string defName = "foo"; const string placeholder = "${name}"; MutablePropertyValues pvs = new MutablePropertyValues(); const string theProperty = "name"; pvs.Add(theProperty, placeholder); RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), pvs); IConfigurableListableObjectFactory mock = (IConfigurableListableObjectFactory)mocks.CreateMock(typeof(IConfigurableListableObjectFactory)); Expect.Call(mock.GetObjectDefinitionNames()).Return(new string [] { defName }); Expect.Call(mock.GetObjectDefinition(defName)).Return(def); mocks.ReplayAll(); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); NameValueCollection defaultProperties = new NameValueCollection(); const string expectedName = "Rick Evans"; defaultProperties.Add(theProperty, expectedName); cfg.Properties = defaultProperties; cfg.PostProcessObjectFactory(mock); Assert.AreEqual(expectedName, def.PropertyValues.GetPropertyValue(theProperty).Value, "Property placeholder value was not replaced with the resolved value."); mocks.VerifyAll(); }
public void IncludingAncestors() { const string defName = "foo"; const string placeholder = "${name}"; MutablePropertyValues pvs = new MutablePropertyValues(); const string theProperty = "name"; pvs.Add(theProperty, placeholder); RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), pvs); IConfigurableListableObjectFactory mock = A.Fake <IConfigurableListableObjectFactory>(); A.CallTo(() => mock.GetObjectDefinitionNames(true)).Returns(new string[] { defName }); A.CallTo(() => mock.GetObjectDefinition(defName, true)).Returns(def); PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); cfg.IncludeAncestors = true; NameValueCollection defaultProperties = new NameValueCollection(); const string expectedName = "Rick Evans"; defaultProperties.Add(theProperty, expectedName); cfg.Properties = defaultProperties; cfg.PostProcessObjectFactory(mock); Assert.AreEqual(expectedName, def.PropertyValues.GetPropertyValue(theProperty).Value, "Property placeholder value was not replaced with the resolved value."); A.CallTo(() => mock.AddEmbeddedValueResolver(A <IStringValueResolver> ._)).MustHaveHappened(); }
/// <summary> /// Apply the given properties to the supplied /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>. /// </summary> /// <param name="factory"> /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/> /// used by the application context. /// </param> /// <param name="props">The properties to apply.</param> /// <exception cref="Spring.Objects.ObjectsException"> /// If an error occured. /// </exception> protected override void ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props) { PlaceholderResolveHandlerAdapter resolveAdapter = new PlaceholderResolveHandlerAdapter(this, props); ObjectDefinitionVisitor visitor = new ObjectDefinitionVisitor(resolveAdapter.ParseAndResolveVariables); IList <string> objectDefinitionNames = factory.GetObjectDefinitionNames(includeAncestors); for (int i = 0; i < objectDefinitionNames.Count; ++i) { string name = objectDefinitionNames[i]; IObjectDefinition definition = factory.GetObjectDefinition(name, includeAncestors); if (definition == null) { logger.ErrorFormat("'{0}' can't be found in factorys' '{1}' object definition (includeAncestor {2})", name, factory, includeAncestors); continue; } try { visitor.VisitObjectDefinition(definition); } catch (ObjectDefinitionStoreException ex) { throw new ObjectDefinitionStoreException( definition.ResourceDescription, name, ex.Message); } } factory.AddEmbeddedValueResolver(resolveAdapter); }
private static bool IsLazyInit(IConfigurableListableObjectFactory configurableObjectFactory, string name) { if (configurableObjectFactory == null) { return false; } return configurableObjectFactory.GetObjectDefinition(name).IsLazyInit; }
private static bool IsLazyInit(IConfigurableListableObjectFactory configurableObjectFactory, string name) { if (configurableObjectFactory == null) { return(false); } return(configurableObjectFactory.GetObjectDefinition(name).IsLazyInit); }
/// <summary> /// Process the given key as 'name.property' entry. /// </summary> /// <param name="factory"> /// The object factory containing the object definitions that are to be /// processed. /// </param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <exception cref="Spring.Objects.ObjectsException"> /// If an error occurs. /// </exception> /// <exception cref="Spring.Objects.FatalObjectException"> /// If the property was not well formed (i.e. not in the format "name.property"). /// </exception> protected virtual void ProcessKey( IConfigurableListableObjectFactory factory, string key, string value) { int dotIndex = key.IndexOf('.'); if (dotIndex == -1) { throw new FatalObjectException( string.Format(CultureInfo.InvariantCulture, "Invalid key '{0}': expected 'objectName.property' form.", key)); } string name = key.Substring(0, dotIndex); string objectProperty = key.Substring(dotIndex + 1); IObjectDefinition definition = factory.GetObjectDefinition(name); if (definition != null) { PropertyValue pv = definition.PropertyValues.GetPropertyValue(objectProperty); if (pv != null && pv.Value is RuntimeObjectReference) { definition.PropertyValues.Add(objectProperty, new RuntimeObjectReference(value)); } else if (pv != null && pv.Value is ExpressionHolder) { definition.PropertyValues.Add(objectProperty, new ExpressionHolder(value)); } else { definition.PropertyValues.Add(objectProperty, value); } } else { #region Instrumentation if (_logger.IsWarnEnabled) { _logger.Warn(string.Format(CultureInfo.InvariantCulture, "Cannot find object '{0}' when overriding properties; check configuration.", name)); } #endregion } #region Instrumentation if (_logger.IsDebugEnabled) { _logger.Debug(string.Format(CultureInfo.InvariantCulture, "Property '{0}' set to '{1}'.", key, value)); } #endregion }
public void MissingObjectDefinitionDoesntRaiseFatalException() { const string valueTo_NOT_BeOveridden = "Jenny Lewis"; TestObject foo = new TestObject(valueTo_NOT_BeOveridden, 30); IConfigurableListableObjectFactory objectFactory = A.Fake <IConfigurableListableObjectFactory>(); A.CallTo(() => objectFactory.GetObjectDefinition("rubbish")).Returns(null); IConfigurableListableObjectFactory fac = objectFactory; PropertyOverrideConfigurer cfg = new PropertyOverrideConfigurer(); NameValueCollection defaultProperties = new NameValueCollection(); defaultProperties.Add("rubbish.Name", "Rick Evans"); cfg.Properties = defaultProperties; cfg.PostProcessObjectFactory(fac); Assert.AreEqual(valueTo_NOT_BeOveridden, foo.Name, "Property value was overridden, but a rubbish objectName root was supplied."); }
/// <summary> /// Apply the given properties to the supplied /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>. /// </summary> /// <param name="factory"> /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/> /// used by the application context. /// </param> /// <param name="props">The properties to apply.</param> /// <exception cref="Spring.Objects.ObjectsException"> /// If an error occured. /// </exception> protected override void ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props) { PlaceholderResolveHandlerAdapter resolveAdapter = new PlaceholderResolveHandlerAdapter(this, props); ObjectDefinitionVisitor visitor = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(resolveAdapter.ParseAndResolveVariables)); string[] objectDefinitionNames = factory.GetObjectDefinitionNames(); for (int i = 0; i < objectDefinitionNames.Length; ++i) { string name = objectDefinitionNames[i]; IObjectDefinition definition = factory.GetObjectDefinition(name); try { visitor.VisitObjectDefinition(definition); } catch (ObjectDefinitionStoreException ex) { throw new ObjectDefinitionStoreException( definition.ResourceDescription, name, ex.Message); } } }
public void MissingObjectDefinitionDoesntRaiseFatalException() { const string valueTo_NOT_BeOveridden = "Jenny Lewis"; TestObject foo = new TestObject(valueTo_NOT_BeOveridden, 30); IConfigurableListableObjectFactory objectFactory = (IConfigurableListableObjectFactory)mocks.CreateMock(typeof(IConfigurableListableObjectFactory)); Expect.Call(objectFactory.GetObjectDefinition("rubbish")).Return(null); IConfigurableListableObjectFactory fac = (IConfigurableListableObjectFactory)objectFactory; PropertyOverrideConfigurer cfg = new PropertyOverrideConfigurer(); NameValueCollection defaultProperties = new NameValueCollection(); defaultProperties.Add("rubbish.Name", "Rick Evans"); cfg.Properties = defaultProperties; mocks.ReplayAll(); cfg.PostProcessObjectFactory(fac); Assert.AreEqual(valueTo_NOT_BeOveridden, foo.Name, "Property value was overridden, but a rubbish objectName root was supplied."); mocks.VerifyAll(); }
/// <summary> /// Apply the given properties to the supplied /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>. /// </summary> /// <param name="factory"> /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/> /// used by the application context. /// </param> /// <param name="props">The properties to apply.</param> /// <exception cref="Spring.Objects.ObjectsException"> /// If an error occured. /// </exception> protected override void ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props) { PlaceholderResolveHandlerAdapter resolveAdapter = new PlaceholderResolveHandlerAdapter(this, props); ObjectDefinitionVisitor visitor = new ObjectDefinitionVisitor(resolveAdapter.ParseAndResolveVariables); IList<string> objectDefinitionNames = factory.GetObjectDefinitionNames(); for (int i = 0; i < objectDefinitionNames.Count; ++i) { string name = objectDefinitionNames[i]; IObjectDefinition definition = factory.GetObjectDefinition(name); try { visitor.VisitObjectDefinition(definition); } catch (ObjectDefinitionStoreException ex) { throw new ObjectDefinitionStoreException( definition.ResourceDescription, name, ex.Message); } } factory.AddEmbeddedValueResolver(resolveAdapter); }
/// <summary> /// Apply the given properties to the supplied /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>. /// </summary> /// <param name="factory"> /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/> /// used by the application context. /// </param> /// <param name="props">The properties to apply.</param> /// <exception cref="Spring.Objects.ObjectsException"> /// If an error occured. /// </exception> protected override void ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props) { PlaceholderResolveHandlerAdapter resolveAdapter = new PlaceholderResolveHandlerAdapter(this, props); ObjectDefinitionVisitor visitor = new ObjectDefinitionVisitor(resolveAdapter.ParseAndResolveVariables); IList<string> objectDefinitionNames = factory.GetObjectDefinitionNames(includeAncestors); for (int i = 0; i < objectDefinitionNames.Count; ++i) { string name = objectDefinitionNames[i]; IObjectDefinition definition = factory.GetObjectDefinition(name, includeAncestors); if (definition == null) { logger.ErrorFormat("'{0}' can't be found in factorys' '{1}' object definition (includeAncestor {2})", name, factory, includeAncestors); continue; } try { visitor.VisitObjectDefinition(definition); } catch (ObjectDefinitionStoreException ex) { throw new ObjectDefinitionStoreException( definition.ResourceDescription, name, ex.Message); } } factory.AddEmbeddedValueResolver(resolveAdapter); }
/// <summary> /// Allows for custom modification of an application context's object definitions. /// </summary> /// <remarks> /// <p> /// If the object name matches, replaces the type by a AOP proxied type based on inheritance. /// </p> /// </remarks> public void PostProcessObjectFactory(IConfigurableListableObjectFactory factory) { IList<string> objectDefinitionNames = factory.GetObjectDefinitionNames(); for (int i = 0; i < objectDefinitionNames.Count; ++i) { string name = objectDefinitionNames[i]; if (IsObjectNameMatch(name)) { var definition = factory.GetObjectDefinition(name) as IConfigurableObjectDefinition; if (definition == null || IsInfrastructureType(definition.ObjectType, name)) { continue; } ProxyFactory pf = CreateProxyFactory(definition.ObjectType, name); InheritanceAopProxyTypeBuilder iaptb = new InheritanceAopProxyTypeBuilder(pf); iaptb.ProxyDeclaredMembersOnly = this.ProxyDeclaredMembersOnly; Type type = iaptb.BuildProxyType(); definition.ObjectType = type; definition.ConstructorArgumentValues.AddIndexedArgumentValue(definition.ConstructorArgumentValues.ArgumentCount, pf); } } }
private void EnhanceConfigurationClasses(IConfigurableListableObjectFactory objectFactory) { string[] objectNames = objectFactory.GetObjectDefinitionNames(); foreach (string t in objectNames) { IObjectDefinition objDef = objectFactory.GetObjectDefinition(t); if (((AbstractObjectDefinition)objDef).HasObjectType) { if (Attribute.GetCustomAttribute(objDef.ObjectType, typeof(ConfigurationAttribute)) != null) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.ProxyTargetAttributes = true; proxyFactory.Interfaces = Type.EmptyTypes; proxyFactory.TargetSource = new ObjectFactoryTargetSource(t, objectFactory); SpringObjectMethodInterceptor methodInterceptor = new SpringObjectMethodInterceptor(objectFactory); proxyFactory.AddAdvice(methodInterceptor); //TODO check type of object isn't infrastructure type. InheritanceAopProxyTypeBuilder iaptb = new InheritanceAopProxyTypeBuilder(proxyFactory); //iaptb.ProxyDeclaredMembersOnly = true; // make configurable. ((IConfigurableObjectDefinition)objDef).ObjectType = iaptb.BuildProxyType(); objDef.ConstructorArgumentValues.AddIndexedArgumentValue(objDef.ConstructorArgumentValues.ArgumentCount, proxyFactory); } } } }
/// <summary> /// Process the given key as 'name.property' entry. /// </summary> /// <param name="factory"> /// The object factory containing the object definitions that are to be /// processed. /// </param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <exception cref="Spring.Objects.ObjectsException"> /// If an error occurs. /// </exception> /// <exception cref="Spring.Objects.FatalObjectException"> /// If the property was not well formed (i.e. not in the format "name.property"). /// </exception> protected virtual void ProcessKey( IConfigurableListableObjectFactory factory, string key, string value) { int dotIndex = key.IndexOf('.'); if (dotIndex == -1) { throw new FatalObjectException( string.Format(CultureInfo.InvariantCulture, "Invalid key '{0}': expected 'objectName.property' form.", key)); } string name = key.Substring(0, dotIndex); string objectProperty = key.Substring(dotIndex + 1); IObjectDefinition definition = factory.GetObjectDefinition(name); if(definition != null) { PropertyValue pv = definition.PropertyValues.GetPropertyValue(objectProperty); if (pv != null && pv.Value is RuntimeObjectReference) { definition.PropertyValues.Add(objectProperty, new RuntimeObjectReference(value)); } else if (pv != null && pv.Value is ExpressionHolder) { definition.PropertyValues.Add(objectProperty, new ExpressionHolder(value)); } else { definition.PropertyValues.Add(objectProperty, value); } } else { #region Instrumentation if (_logger.IsWarnEnabled) { _logger.Warn(string.Format(CultureInfo.InvariantCulture, "Cannot find object '{0}' when overriding properties; check configuration.", name)); } #endregion } #region Instrumentation if (_logger.IsDebugEnabled) { _logger.Debug(string.Format(CultureInfo.InvariantCulture, "Property '{0}' set to '{1}'.", key, value)); } #endregion }
/// <summary> /// DO NOT USE - this is subject to change! /// </summary> /// <param name="appRelativeVirtualPath"></param> /// <param name="objectFactory"></param> /// <returns> /// This method requires registrars to follow the convention of registering web object definitions using their /// application relative urls (~/mypath/mypage.aspx). /// </returns> /// <remarks> /// Resolve an object definition by url. /// </remarks> protected internal static NamedObjectDefinition FindWebObjectDefinition(string appRelativeVirtualPath, IConfigurableListableObjectFactory objectFactory) { ILog Log = LogManager.GetLogger(typeof(AbstractHandlerFactory)); bool isDebug = Log.IsDebugEnabled; // lookup definition using app-relative url if (isDebug) Log.Debug(string.Format("GetHandler():looking up definition for app-relative url '{0}'", appRelativeVirtualPath)); string objectDefinitionName = appRelativeVirtualPath; IObjectDefinition pageDefinition = objectFactory.GetObjectDefinition(appRelativeVirtualPath, true); if (pageDefinition == null) { // try using pagename+extension and pagename only string pageExtension = Path.GetExtension(appRelativeVirtualPath); string pageName = WebUtils.GetPageName(appRelativeVirtualPath); // only looks in the specified object factory -- it will *not* search parent contexts pageDefinition = objectFactory.GetObjectDefinition(pageName + pageExtension, false); if (pageDefinition == null) { pageDefinition = objectFactory.GetObjectDefinition(pageName, false); if (pageDefinition != null) objectDefinitionName = pageName; } else { objectDefinitionName = pageName + pageExtension; } if (pageDefinition != null) { if (isDebug) Log.Debug(string.Format("GetHandler():found definition for page-name '{0}'", objectDefinitionName)); } else { if (isDebug) Log.Debug(string.Format("GetHandler():no definition found for page-name '{0}'", pageName)); } } else { if (isDebug) Log.Debug(string.Format("GetHandler():found definition for page-url '{0}'", appRelativeVirtualPath)); } return (pageDefinition == null) ? (NamedObjectDefinition)null : new NamedObjectDefinition(objectDefinitionName, pageDefinition); }
private void EnhanceConfigurationClasses(IConfigurableListableObjectFactory objectFactory) { ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer(objectFactory); IList<string> objectNames = objectFactory.GetObjectDefinitionNames(); foreach (string name in objectNames) { IObjectDefinition objDef = objectFactory.GetObjectDefinition(name); if (((AbstractObjectDefinition)objDef).HasObjectType) { if (Attribute.GetCustomAttribute(objDef.ObjectType, typeof(ConfigurationAttribute)) != null) { //TODO check type of object isn't infrastructure type. Type configClass = objDef.ObjectType; Type enhancedClass = enhancer.Enhance(configClass); Logger.Debug(m => m("Replacing object definition '{0}' existing class '{1}' with enhanced class", name, configClass.FullName)); ((IConfigurableObjectDefinition)objDef).ObjectType = enhancedClass; } } } }
/// <summary> /// Apply the property replacement using the specified <see cref="IVariableSource"/>s for all /// object in the supplied /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>. /// </summary> /// <param name="factory"> /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/> /// used by the application context. /// </param> /// <exception cref="Spring.Objects.ObjectsException"> /// If an error occured. /// </exception> protected virtual void ProcessProperties( IConfigurableListableObjectFactory factory ) { CompositeVariableSource compositeVariableSource = new CompositeVariableSource(variableSourceList); TextProcessor tp = new TextProcessor(this, compositeVariableSource); ObjectDefinitionVisitor visitor = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(tp.ParseAndResolveVariables)); IList<string> objectDefinitionNames = factory.GetObjectDefinitionNames(includeAncestors); for (int i = 0; i < objectDefinitionNames.Count; ++i) { string name = objectDefinitionNames[i]; IObjectDefinition definition = factory.GetObjectDefinition( name, includeAncestors ); if (definition == null) continue; try { visitor.VisitObjectDefinition( definition ); } catch (ObjectDefinitionStoreException ex) { throw new ObjectDefinitionStoreException( definition.ResourceDescription, name, ex.Message ); } } }