fixed ConfigSectionResource error handling
polishing added DelegateObjectFactoryConfigurer
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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, "name", false, null);
|
||||
/// ctx.AddObjectFactoryPostProcessor(new DelegateObjectFactoryConfigurer( of =>
|
||||
/// {
|
||||
/// of.RegisterSingleton("someObject", 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -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"
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user