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 void RegisterThreadLocalCleanupBean(IThreadLocalCleanupBean threadLocalCleanupBean)
        {
            Lock writeLock = listeners.GetWriteLock();

            writeLock.Lock();
            try
            {
                listeners.Register(threadLocalCleanupBean);
                cachedForkStateEntries = null;
                IServiceContext currentBeanContext = BeanContextInitializer.GetCurrentBeanContext();
                if (currentBeanContext != null)
                {
                    extensionToContextMap.Put(threadLocalCleanupBean, new WeakReference(currentBeanContext));
                    if (alreadyHookedContextSet.PutIfNotExists(currentBeanContext, null))
                    {
                        ParamHolder <bool?> inactive = new ParamHolder <bool?>();

                        currentBeanContext.RegisterDisposeHook(new IBackgroundWorkerParamDelegate <IServiceContext>(delegate(IServiceContext state)
                        {
                            if (inactive.Value.HasValue && inactive.Value.Value)
                            {
                                return;
                            }
                            foreignContextHook(state);
                        }));
                        alreadyHookedContextSet.Put(currentBeanContext, inactive);
                    }
                }
            }
            finally
            {
                writeLock.Unlock();
            }
        }
Beispiel #3
0
        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 void UnregisterThreadLocalCleanupBean(IThreadLocalCleanupBean threadLocalCleanupBean)
        {
            Lock writeLock = listeners.GetWriteLock();

            writeLock.Lock();
            try
            {
                listeners.Unregister(threadLocalCleanupBean);
                cachedForkStateEntries = null;
                extensionToContextMap.Remove(threadLocalCleanupBean);
            }
            finally
            {
                writeLock.Unlock();
            }
            // clear this threadlocal a last time before letting the bean alone...
            threadLocalCleanupBean.CleanupThreadLocal();
        }