public IForkState CreateForkState() { ForkStateEntry[] forkStateEntries = GetForkStateEntries(); IForkedValueResolver[] oldValues = new IForkedValueResolver[forkStateEntries.Length]; for (int a = 0, size = forkStateEntries.Length; a < size; a++) { ForkStateEntry forkStateEntry = forkStateEntries[a]; IForkProcessor forkProcessor = forkStateEntry.forkProcessor; if (forkProcessor != null) { Object value = forkProcessor.ResolveOriginalValue(forkStateEntry.tlBean, forkStateEntry.fieldName, forkStateEntry.valueTL); oldValues[a] = new ForkProcessorValueResolver(value, forkProcessor); continue; } { Object value = forkStateEntry.getValueMI.Invoke(forkStateEntry.valueTL, ForkStateEntry.EMPTY_ARGS); if (value != null && ForkableType.SHALLOW_COPY.Equals(forkStateEntry.forkableType)) { throw new Exception("Could not clone " + value); } else { oldValues[a] = new ReferenceValueResolver(value, value); } } } return(new ForkState(forkStateEntries, oldValues)); }
protected ForkStateEntry[] GetForkStateEntries() { ForkStateEntry[] cachedForkStateEntries = this.cachedForkStateEntries; if (cachedForkStateEntries != null) { return(cachedForkStateEntries); } Lock writeLock = listeners.GetWriteLock(); writeLock.Lock(); try { // check again: concurrent thread might have been faster cachedForkStateEntries = this.cachedForkStateEntries; if (cachedForkStateEntries != null) { return(cachedForkStateEntries); } IThreadLocalCleanupBean[] extensions = listeners.GetExtensions(); List <ForkStateEntry> forkStateEntries = new List <ForkStateEntry>(extensions.Length); for (int a = 0, size = extensions.Length; a < size; a++) { IThreadLocalCleanupBean extension = extensions[a]; FieldInfo[] fields = ReflectUtil.GetDeclaredFieldsInHierarchy(extension.GetType()); foreach (FieldInfo field in fields) { Forkable forkable = AnnotationUtil.GetAnnotation <Forkable>(field, false); if (forkable == null) { continue; } Object valueTL = field.GetValue(extension); if (valueTL == null) { continue; } Type forkProcessorType = forkable.Processor; IForkProcessor forkProcessor = null; if (forkProcessorType != null && !typeof(IForkProcessor).Equals(forkProcessorType)) { WeakReference beanContextOfExtensionR = extensionToContextMap.Get(extension); IServiceContext beanContextOfExtension = beanContextOfExtensionR != null ? (IServiceContext)beanContextOfExtensionR.Target : null; if (beanContextOfExtension == null) { beanContextOfExtension = BeanContext; } forkProcessor = beanContextOfExtension.RegisterBean <IForkProcessor>(forkProcessorType).Finish(); } forkStateEntries.Add(new ForkStateEntry(extension, field.Name, valueTL, forkable.Value, forkProcessor)); } } cachedForkStateEntries = forkStateEntries.ToArray(); this.cachedForkStateEntries = cachedForkStateEntries; return(cachedForkStateEntries); } finally { writeLock.Unlock(); } }
public ForkStateEntry(IThreadLocalCleanupBean tlBean, String fieldName, Object valueTL, ForkableType forkableType, IForkProcessor forkProcessor) { this.tlBean = tlBean; this.fieldName = fieldName; this.valueTL = valueTL; this.forkableType = forkableType; this.forkProcessor = forkProcessor; getValueMI = valueTL.GetType().GetMethod("get_Value"); setValueMI = valueTL.GetType().GetMethod("set_Value", new Type[] { getValueMI.ReturnType }); }
public ForkProcessorValueResolver(Object originalValue, IForkProcessor forkProcessor) { this.originalValue = originalValue; this.forkProcessor = forkProcessor; }