From: <fab...@us...> - 2011-03-20 17:31:20
|
Revision: 5477 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5477&view=rev Author: fabiomaulo Date: 2011-03-20 17:31:14 +0000 (Sun, 20 Mar 2011) Log Message: ----------- Artifacts for default bytecode provider Modified Paths: -------------- trunk/nhibernate/src/NHibernate/NHibernate.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate/Bytecode/DefaultProxyFactoryFactory.cs trunk/nhibernate/src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs trunk/nhibernate/src/NHibernate/Proxy/DefaultLazyInitializer.cs trunk/nhibernate/src/NHibernate/Proxy/DefaultProxyFactory.cs Added: trunk/nhibernate/src/NHibernate/Bytecode/DefaultProxyFactoryFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Bytecode/DefaultProxyFactoryFactory.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Bytecode/DefaultProxyFactoryFactory.cs 2011-03-20 17:31:14 UTC (rev 5477) @@ -0,0 +1,31 @@ +using NHibernate.Proxy; + +namespace NHibernate.Bytecode +{ + public class DefaultProxyFactoryFactory : IProxyFactoryFactory + { + #region IProxyFactoryFactory Members + + public IProxyFactory BuildProxyFactory() + { + return new DefaultProxyFactory(); + } + + public IProxyValidator ProxyValidator + { + get { return new DynProxyTypeValidator(); } + } + + public bool IsInstrumented(System.Type entityClass) + { + return true; + } + + public bool IsProxy(object entity) + { + return entity is INHibernateProxy; + } + + #endregion + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs 2011-03-20 17:31:14 UTC (rev 5477) @@ -0,0 +1,64 @@ +using System; +using NHibernate.Proxy.DynamicProxy; +using NHibernate.Util; + +namespace NHibernate.Intercept +{ + public class DefaultDynamicLazyFieldInterceptor : IFieldInterceptorAccessor, Proxy.DynamicProxy.IInterceptor + { + public DefaultDynamicLazyFieldInterceptor(object targetInstance) + { + if (targetInstance == null) + { + throw new ArgumentNullException("targetInstance"); + } + TargetInstance = targetInstance; + } + + public IFieldInterceptor FieldInterceptor { get; set; } + public object TargetInstance { get; private set; } + + public object Intercept(InvocationInfo info) + { + var methodName = info.TargetMethod.Name; + if (FieldInterceptor != null) + { + if (ReflectHelper.IsPropertyGet(info.TargetMethod)) + { + if("get_FieldInterceptor".Equals(methodName)) + { + return FieldInterceptor; + } + object propValue = info.TargetMethod.Invoke(TargetInstance, info.Arguments); + + var result = FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), propValue); + + if (result != AbstractFieldInterceptor.InvokeImplementation) + { + return result; + } + } + else if (ReflectHelper.IsPropertySet(info.TargetMethod)) + { + if ("set_FieldInterceptor".Equals(methodName)) + { + FieldInterceptor = (IFieldInterceptor)info.Arguments[0]; + return null; + } + FieldInterceptor.MarkDirty(); + FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), info.Arguments[0]); + } + } + else + { + if ("set_FieldInterceptor".Equals(methodName)) + { + FieldInterceptor = (IFieldInterceptor)info.Arguments[0]; + return null; + } + } + + return info.TargetMethod.Invoke(TargetInstance, info.Arguments); + } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj =================================================================== --- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2011-03-20 17:03:52 UTC (rev 5476) +++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2011-03-20 17:31:14 UTC (rev 5477) @@ -91,6 +91,7 @@ <Compile Include="ADOException.cs" /> <Compile Include="AssemblyInfo.cs" /> <Compile Include="AssertionFailure.cs" /> + <Compile Include="Bytecode\DefaultProxyFactoryFactory.cs" /> <Compile Include="Cache\Access\ISoftLock.cs" /> <Compile Include="Cache\CachedItem.cs" /> <Compile Include="Cache\CacheException.cs" /> @@ -252,6 +253,7 @@ <Compile Include="Impl\SqlQueryImpl.cs" /> <Compile Include="Engine\Status.cs" /> <Compile Include="InstantiationException.cs" /> + <Compile Include="Intercept\DefaultDynamicLazyFieldInterceptor.cs" /> <Compile Include="IQuery.cs" /> <Compile Include="ISession.cs" /> <Compile Include="ISessionFactory.cs" /> @@ -326,6 +328,8 @@ <Compile Include="Properties\PascalCaseMUnderscoreStrategy.cs" /> <Compile Include="Properties\PascalCaseUnderscoreStrategy.cs" /> <Compile Include="Properties\PropertyAccessorFactory.cs" /> + <Compile Include="Proxy\DefaultLazyInitializer.cs" /> + <Compile Include="Proxy\DefaultProxyFactory.cs" /> <Compile Include="Proxy\DynamicProxy\DefaultArgumentHandler.cs" /> <Compile Include="Proxy\DynamicProxy\DefaultMethodEmitter.cs" /> <Compile Include="Proxy\DynamicProxy\DefaultProxyMethodBuilder.cs" /> Added: trunk/nhibernate/src/NHibernate/Proxy/DefaultLazyInitializer.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Proxy/DefaultLazyInitializer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Proxy/DefaultLazyInitializer.cs 2011-03-20 17:31:14 UTC (rev 5477) @@ -0,0 +1,48 @@ +using System; +using System.Reflection; +using NHibernate.Engine; +using NHibernate.Proxy.DynamicProxy; +using NHibernate.Proxy.Poco; +using NHibernate.Type; + +namespace NHibernate.Proxy +{ + public class DefaultLazyInitializer : BasicLazyInitializer, DynamicProxy.IInterceptor + { + private static readonly MethodInfo exceptionInternalPreserveStackTrace = + typeof (Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic); + + public DefaultLazyInitializer(string entityName, System.Type persistentClass, object id, MethodInfo getIdentifierMethod, + MethodInfo setIdentifierMethod, IAbstractComponentType componentIdType, + ISessionImplementor session) + : base(entityName, persistentClass, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session) {} + + #region Implementation of IInterceptor + + public object Intercept(InvocationInfo info) + { + object returnValue; + try + { + returnValue = base.Invoke(info.TargetMethod, info.Arguments, info.Target); + + // Avoid invoking the actual implementation, if possible + if (returnValue != InvokeImplementation) + { + return returnValue; + } + + returnValue = info.TargetMethod.Invoke(GetImplementation(), info.Arguments); + } + catch (TargetInvocationException ex) + { + exceptionInternalPreserveStackTrace.Invoke(ex.InnerException, new Object[] {}); + throw ex.InnerException; + } + + return returnValue; + } + + #endregion + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate/Proxy/DefaultProxyFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Proxy/DefaultProxyFactory.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Proxy/DefaultProxyFactory.cs 2011-03-20 17:31:14 UTC (rev 5477) @@ -0,0 +1,40 @@ +using System; +using NHibernate.Engine; +using NHibernate.Intercept; +using NHibernate.Proxy.DynamicProxy; + +namespace NHibernate.Proxy +{ + public class DefaultProxyFactory : AbstractProxyFactory + { + private static readonly ProxyFactory factory = new ProxyFactory(); + protected static readonly IInternalLogger log = LoggerProvider.LoggerFor(typeof (DefaultProxyFactory)); + + public override INHibernateProxy GetProxy(object id, ISessionImplementor session) + { + try + { + var initializer = new DefaultLazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod, SetIdentifierMethod, + ComponentIdType, session); + + object proxyInstance = IsClassProxy + ? factory.CreateProxy(PersistentClass, initializer, Interfaces) + : factory.CreateProxy(Interfaces[0], initializer, Interfaces); + + return (INHibernateProxy) proxyInstance; + } + catch (Exception ex) + { + log.Error("Creating a proxy instance failed", ex); + throw new HibernateException("Creating a proxy instance failed", ex); + } + } + + public override object GetFieldInterceptionProxy() + { + object targetInstance = Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(PersistentClass, true); + var interceptor = new DefaultDynamicLazyFieldInterceptor(targetInstance); + return factory.CreateProxy(PersistentClass, interceptor, new[] { typeof(IFieldInterceptorAccessor) }); + } + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |