Fix WebServiceExporter to allow type-based auto-wiring and auto-proxying [SPRNET-1323]

This commit is contained in:
bbaia
2010-04-12 17:34:25 +00:00
parent 060f6561c2
commit 097bda93bd
4 changed files with 55 additions and 76 deletions

View File

@@ -95,7 +95,17 @@ namespace Spring.Web.Script.Services
if (nod != null)
{
Type serviceType = appContext.GetType(nod.Name);
Type serviceType = null;
if (appContext.IsTypeMatch(nod.Name, typeof(WebServiceExporter)))
{
WebServiceExporter wse = (WebServiceExporter)appContext.GetObject(nod.Name);
serviceType = wse.GetExportedType();
}
else
{
serviceType = appContext.GetType(nod.Name);
}
object[] attrs = serviceType.GetCustomAttributes(typeof(ScriptServiceAttribute), false);
if (attrs.Length > 0)
{

View File

@@ -57,7 +57,7 @@ namespace Spring.Web.Services
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
public class WebServiceExporter : IInitializingObject, IObjectFactoryAware, IFactoryObject, IObjectNameAware, IDisposable
public class WebServiceExporter : IInitializingObject, IObjectFactoryAware, IObjectNameAware, IDisposable
{
/// <summary>
/// Holds EXPORTER_ID to WebServiceExporter instance mappings.
@@ -330,58 +330,6 @@ namespace Spring.Web.Services
#endregion
#region IFactoryObject Members
private object GetTargetInstance()
{
return objectFactory.GetObject( TargetName );
}
/// <summary>
/// Return an instance (possibly shared or independent) of the object
/// managed by this factory.
/// </summary>
/// <remarks>
/// <note type="caution">
/// If this method is being called in the context of an enclosing IoC container and
/// returns <see langword="null"/>, the IoC container will consider this factory
/// object as not being fully initialized and throw a corresponding (and most
/// probably fatal) exception.
/// </note>
/// </remarks>
/// <returns>
/// An instance (possibly shared or independent) of the object managed by
/// this factory.
/// </returns>
public virtual object GetObject()
{
// no sense to call this method, because the web service type
// will be instantiated by the .NET infrastructure. (ObjectType is used instead)
// Users should use GetObject("TargetName") instead.
return new InvalidOperationException(
"The web service instance is created and managed by the .NET infrastructure.");
}
/// <summary>
/// Return the <see cref="System.Type"/> of object that this
/// <see cref="Spring.Objects.Factory.IFactoryObject"/> creates, or
/// <see langword="null"/> if not known in advance.
/// </summary>
public virtual Type ObjectType
{
get { return (proxyType != null ? proxyType : objectFactory.GetType(TargetName)); }
}
/// <summary>
/// Is the object managed by this factory a singleton or a prototype?
/// </summary>
public virtual bool IsSingleton
{
get { return false; }
}
#endregion
#region IObjectNameAware Members
/// <summary>
@@ -422,7 +370,16 @@ namespace Spring.Web.Services
#endregion
#region Protected Methods
#region Methods
/// <summary>
/// Returns the Web Service wrapper type for the object that is to be exposed.
/// </summary>
/// <returns></returns>
public virtual Type GetExportedType()
{
return (proxyType != null ? proxyType : objectFactory.GetType(TargetName));
}
/// <summary>
/// Validates the configuration.
@@ -458,6 +415,11 @@ namespace Spring.Web.Services
proxyType = builder.BuildProxyType();
}
private object GetTargetInstance()
{
return objectFactory.GetObject(TargetName);
}
#endregion
#region WebServiceProxyTypeBuilder inner class implementation
@@ -466,7 +428,7 @@ namespace Spring.Web.Services
{
#region Fields
private static readonly MethodInfo WebServiceExporter_GetTargetInstance = typeof( WebServiceExporter ).GetMethod( "GetTarget", new Type[] { typeof( string ) } );
private static readonly MethodInfo WebServiceExporter_GetTargetInstance = typeof(WebServiceExporter).GetMethod( "GetTarget", new Type[] { typeof( string ) } );
private WebServiceExporter exporter;
private CustomAttributeBuilder webServiceAttribute;
#if NET_2_0

View File

@@ -83,14 +83,21 @@ namespace Spring.Web.Services
Type serviceType = null;
if (nod != null)
{
serviceType = appContext.GetType(nod.Name);
// check if the type defines a Web Service
object[] wsAttribute = serviceType.GetCustomAttributes(typeof(WebServiceAttribute), true);
if (wsAttribute.Length == 0)
if (appContext.IsTypeMatch(nod.Name, typeof(WebServiceExporter)))
{
serviceType = null;
}
WebServiceExporter wse = (WebServiceExporter)appContext.GetObject(nod.Name);
serviceType = wse.GetExportedType();
}
else
{
serviceType = appContext.GetType(nod.Name);
// check if the type defines a Web Service
object[] wsAttribute = serviceType.GetCustomAttributes(typeof(WebServiceAttribute), true);
if (wsAttribute.Length == 0)
{
serviceType = null;
}
}
}
if (serviceType == null)

View File

@@ -78,7 +78,7 @@ namespace Spring.Web.Services
wse.TargetName = "noDecoratedService";
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
Assert.IsTrue(typeof(IService).IsAssignableFrom(proxyType));
}
@@ -89,7 +89,7 @@ namespace Spring.Web.Services
wse.TargetName = "noDecoratedService";
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceAttribute), true);
Assert.IsNotEmpty(attrs);
Assert.AreEqual(1, attrs.Length);
@@ -110,7 +110,7 @@ namespace Spring.Web.Services
wse.Namespace = "http://www.springframework.net";
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceAttribute), true);
Assert.IsNotEmpty(attrs);
Assert.AreEqual(1, attrs.Length);
@@ -129,7 +129,7 @@ namespace Spring.Web.Services
wse.TargetName = "noDecoratedService";
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceBindingAttribute), true);
Assert.IsNotEmpty(attrs);
Assert.AreEqual(1, attrs.Length);
@@ -146,7 +146,7 @@ namespace Spring.Web.Services
wse.WsiProfile = WsiProfiles.None;
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceBindingAttribute), true);
Assert.IsNotEmpty(attrs);
Assert.AreEqual(1, attrs.Length);
@@ -162,7 +162,7 @@ namespace Spring.Web.Services
wse.TargetName = "decoratedService";
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceBindingAttribute), true);
Assert.IsNotEmpty(attrs);
Assert.AreEqual(1, attrs.Length);
@@ -179,7 +179,7 @@ namespace Spring.Web.Services
wse.TargetName = "noDecoratedService";
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
MethodInfo method = proxyType.GetMethod("SomeMethod");
Assert.IsNotNull(method);
@@ -196,7 +196,7 @@ namespace Spring.Web.Services
wse.MemberAttributes.Add("SomeMethod", new WebMethodAttribute(true)); // default value is false
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
MethodInfo method = proxyType.GetMethod("SomeMethod");
Assert.IsNotNull(method);
@@ -215,7 +215,7 @@ namespace Spring.Web.Services
wse.TargetName = "decoratedService";
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceAttribute), true);
Assert.IsNotEmpty(attrs);
Assert.AreEqual(1, attrs.Length);
@@ -233,7 +233,7 @@ namespace Spring.Web.Services
wse.TargetName = "decoratedService";
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
MethodInfo method = proxyType.GetMethod("SomeMethod");
Assert.IsNotNull(method);
@@ -255,7 +255,7 @@ namespace Spring.Web.Services
wse.MemberAttributes.Add("SomeMethod", new WebMethodAttribute(true)); // default value is false
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
MethodInfo method = proxyType.GetMethod("SomeMethod");
Assert.IsNotNull(method);
@@ -277,7 +277,7 @@ namespace Spring.Web.Services
new CustomAttributeBuilder(typeof(WebServiceAttribute).GetConstructor(Type.EmptyTypes), ObjectUtils.EmptyObjects) };
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceAttribute), true);
Assert.IsNotEmpty(attrs);
@@ -295,7 +295,7 @@ namespace Spring.Web.Services
wse.WebServiceBaseType = typeof(CustomWebService);
wse.AfterPropertiesSet();
Type proxyType = wse.ObjectType;
Type proxyType = wse.GetExportedType();
Assert.IsTrue(typeof(CustomWebService).IsAssignableFrom(proxyType));
}