fixed ConfigSectionResource error handling

polishing
added DelegateObjectFactoryConfigurer
This commit is contained in:
eeichinger
2009-10-15 18:07:04 +00:00
parent ccc64cbf73
commit 5c3a40204b
11 changed files with 191 additions and 3 deletions

View File

@@ -523,9 +523,9 @@ namespace Spring.Context.Support
#endregion
}
private void ProcessObjectFactoryPostProcessors(IList orderedFactoryProcessors)
private void ProcessObjectFactoryPostProcessors(IList objectFactoryPostProcessors)
{
foreach (IObjectFactoryPostProcessor processor in orderedFactoryProcessors)
foreach (IObjectFactoryPostProcessor processor in objectFactoryPostProcessors)
{
processor.PostProcessObjectFactory(ObjectFactory);
}

View File

@@ -177,7 +177,14 @@ namespace Spring.Core.IO
/// <seealso cref="Spring.Core.IO.IInputStreamSource"/>
public override Stream InputStream
{
get { return new MemoryStream(Encoding.UTF8.GetBytes(configElement.OuterXml)); }
get
{
if (configElement == null)
{
throw new FileNotFoundException(string.Format("Configuration Section '{0}' does not exist", this.sectionName), this.sectionName);
}
return new MemoryStream(Encoding.UTF8.GetBytes(configElement.OuterXml));
}
}
#endregion

View File

@@ -0,0 +1,72 @@
#region License
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
namespace Spring.Objects.Factory.Config
{
/// <summary>
/// A generic implementation of an <see cref="IObjectFactoryPostProcessor"/>, that delegates post processing to a passed delegate
/// </summary>
/// <remarks>
/// This comes in handy when you want to perform specific tasks on an object factory, e.g. doing special initialization.
/// </remarks>
/// <example>
/// The example below is taken from a unit test. The snippet causes 'someObject' to be registered each time <see cref="Spring.Context.Support.AbstractApplicationContext.Refresh"/> is called on
/// the context instance:
/// <code>
/// IConfigurableApplicationContext ctx = new XmlApplicationContext(false, &quot;name&quot;, false, null);
/// ctx.AddObjectFactoryPostProcessor(new DelegateObjectFactoryConfigurer( of =&gt;
/// {
/// of.RegisterSingleton(&quot;someObject&quot;, someObject);
/// }));
/// </code>
/// </example>
/// <author>Erich Eichinger</author>
public class DelegateObjectFactoryConfigurer : IObjectFactoryPostProcessor
{
public delegate void ObjectFactoryConfigurationHandler(IConfigurableListableObjectFactory objectFactory);
private ObjectFactoryConfigurationHandler _configurationHandler;
/// <summary>
/// Get or Set the handler to delegate configuration to
/// </summary>
public ObjectFactoryConfigurationHandler ConfigurationHandler
{
get { return _configurationHandler; }
set { _configurationHandler = value; }
}
public DelegateObjectFactoryConfigurer()
{ }
public DelegateObjectFactoryConfigurer(ObjectFactoryConfigurationHandler configurationHandler)
{
_configurationHandler = configurationHandler;
}
public void PostProcessObjectFactory(IConfigurableListableObjectFactory factory)
{
if (_configurationHandler != null)
{
_configurationHandler(factory);
}
}
}
}

View File

@@ -2070,6 +2070,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Config\DelegateObjectFactoryConfigurer.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Config\ConstructorArgumentValues.cs"
SubType = "Code"

View File

@@ -653,6 +653,7 @@
<Compile Include="Objects\Factory\Config\CommandLineArgsVariableSource.cs" />
<Compile Include="Objects\Factory\Config\ConfigSectionVariableSource.cs" />
<Compile Include="Objects\Factory\Config\ConnectionStringsVariableSource.cs" />
<Compile Include="Objects\Factory\Config\DelegateObjectFactoryConfigurer.cs" />
<Compile Include="Objects\Factory\Config\DependencyDescriptor.cs" />
<Compile Include="Objects\Factory\Config\DictionaryVariableSource.cs" />
<Compile Include="Objects\Factory\Config\IConfigurableFactoryObject.cs" />

View File

@@ -665,6 +665,7 @@
<Compile Include="Objects\Factory\Config\CommandLineArgsVariableSource.cs" />
<Compile Include="Objects\Factory\Config\ConfigSectionVariableSource.cs" />
<Compile Include="Objects\Factory\Config\ConnectionStringsVariableSource.cs" />
<Compile Include="Objects\Factory\Config\DelegateObjectFactoryConfigurer.cs" />
<Compile Include="Objects\Factory\Config\DependencyDescriptor.cs" />
<Compile Include="Objects\Factory\Config\DictionaryVariableSource.cs" />
<Compile Include="Objects\Factory\Config\IConfigurableFactoryObject.cs" />

View File

@@ -70,5 +70,20 @@ namespace Spring.Core.IO
{
new ConfigSectionResource((XmlElement)null);
}
[Test]
public void ThrowsIoExceptionIfConfigSectionDoesNotExist()
{
IResource res = new ConfigSectionResource("DOES NOT EXIST");
try
{
Stream istm = res.InputStream;
Assert.Fail();
}
catch(IOException ioex)
{
Console.WriteLine(ioex);
}
}
}
}

View File

@@ -0,0 +1,80 @@
#region License
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
#if NET_2_0
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
namespace Spring.Objects.Factory.Config
{
/// <summary>
/// </summary>
/// <author>Erich Eichinger</author>
[TestFixture]
public class DelegateObjectFactoryConfigurerIntegrationTests
{
private class MockObjectFactoryPostProcessor : IObjectFactoryPostProcessor
{
public bool Called;
public void PostProcessObjectFactory(IConfigurableListableObjectFactory factory)
{
Called = true;
}
}
[Test]
public void ExecutesBeforeObjectFactoryPostProcessing()
{
MockObjectFactoryPostProcessor mofp = new MockObjectFactoryPostProcessor();
IConfigurableApplicationContext ctx = new XmlApplicationContext(false, "name", false, null);
ctx.AddObjectFactoryPostProcessor(new DelegateObjectFactoryConfigurer(delegate(IConfigurableListableObjectFactory of)
{
of.RegisterSingleton("mofp", mofp);
}));
ctx.Refresh();
Assert.IsTrue(mofp.Called);
}
[Test]
public void CanBeUsedToReconfigureAnApplicationContextOnRefresh()
{
MockObjectFactoryPostProcessor mofp = new MockObjectFactoryPostProcessor();
IConfigurableApplicationContext ctx = new XmlApplicationContext(false, "name", false, null);
ctx.AddObjectFactoryPostProcessor(new DelegateObjectFactoryConfigurer(of =>
{
of.RegisterSingleton("mofp", mofp);
}));
ctx.Refresh();
mofp.Called = false;
ctx.Refresh();
Assert.IsTrue(mofp.Called);
}
}
}
#endif

View File

@@ -1347,6 +1347,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Config\DelegateObjectFactoryConfigurerTests.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Config\ConstructorArgumentValuesTests.cs"
SubType = "Code"

View File

@@ -309,6 +309,7 @@
<Compile Include="Objects\Factory\Config\CommandLineArgsVariableSourceTests.cs" />
<Compile Include="Objects\Factory\Config\ConfigSectionVariableSourceTests.cs" />
<Compile Include="Objects\Factory\Config\ConnectionStringsVariableSourceTests.cs" />
<Compile Include="Objects\Factory\Config\DelegateObjectFactoryConfigurerTests.cs" />
<Compile Include="Objects\Factory\Config\ObjectDefinitionVisitorTests.cs" />
<Compile Include="Objects\Factory\Config\ResourceHandlerConfigurerTests.cs" />
<Compile Include="Objects\Factory\Config\PropertyFileVariableSourceTests.cs" />

View File

@@ -320,6 +320,7 @@
<Compile Include="Objects\Factory\Config\CommandLineArgsVariableSourceTests.cs" />
<Compile Include="Objects\Factory\Config\ConfigSectionVariableSourceTests.cs" />
<Compile Include="Objects\Factory\Config\ConnectionStringsVariableSourceTests.cs" />
<Compile Include="Objects\Factory\Config\DelegateObjectFactoryConfigurerTests.cs" />
<Compile Include="Objects\Factory\Config\ObjectDefinitionVisitorTests.cs" />
<Compile Include="Objects\Factory\Config\ResourceHandlerConfigurerTests.cs" />
<Compile Include="Objects\Factory\Config\PropertyFileVariableSourceTests.cs" />