Esempio n. 1
0
 public R Use <R>(IResultingBackgroundWorkerDelegate <R> runnable)
 {
     Object[] oldValues = SetThreadLocals();
     try
     {
         return(runnable());
     }
     finally
     {
         RestoreThreadLocals(oldValues);
     }
 }
Esempio n. 2
0
        public R ExecuteBusy <R>(IResultingBackgroundWorkerDelegate <R> busyDelegate)
        {
            IBusyToken token = AcquireBusyToken();

            try
            {
                return(busyDelegate.Invoke());
            }
            finally
            {
                token.Finished();
            }
        }
Esempio n. 3
0
 public R ExecuteWithSecurityScopes <R>(IResultingBackgroundWorkerDelegate <R> runnable, params ISecurityScope[] securityScopes)
 {
     ISecurityScope[] oldSecurityScopes = SecurityScopes;
     try
     {
         SecurityScopes = securityScopes;
         return(runnable());
     }
     finally
     {
         SecurityScopes = oldSecurityScopes;
     }
 }
Esempio n. 4
0
 public R ExecuteWithoutSecurity<R>(IResultingBackgroundWorkerDelegate<R> pausedSecurityRunnable)
 {
     bool? oldSecurityActive = securityActiveTL.Value;
     securityActiveTL.Value = false;
     try
     {
         return pausedSecurityRunnable();
     }
     finally
     {
         securityActiveTL.Value = oldSecurityActive;
     }
 }
Esempio n. 5
0
 public R ExecuteWithoutFiltering<R>(IResultingBackgroundWorkerDelegate<R> noFilterRunnable)
 {
     bool? oldFilterActive = entityActiveTL.Value;
     entityActiveTL.Value = false;
     try
     {
         return noFilterRunnable();
     }
     finally
     {
         entityActiveTL.Value = oldFilterActive;
     }
 }
Esempio n. 6
0
        public IDisposableCache WithParent(ICache parent, IResultingBackgroundWorkerDelegate <IDisposableCache> runnable)
        {
            ICache oldParent = parentTL.Value;

            parentTL.Value = parent;
            try
            {
                return(runnable());
            }
            finally
            {
                parentTL.Value = oldParent;
            }
        }
Esempio n. 7
0
        public R InvokeInGuiAndWait <R>(IResultingBackgroundWorkerDelegate <R> callback)
        {
            if (IsInGuiThread() || SyncContext == null)
            {
                return(callback());
            }
            R result = default(R);

            SyncContext.Send(delegate(Object state)
            {
                result = callback();
            }, null);
            return(result);
        }
Esempio n. 8
0
 public R ExecuteWithFiltering<R>(IResultingBackgroundWorkerDelegate<R> filterRunnable)
 {
     bool? oldFilterActive = entityActiveTL.Value;
     entityActiveTL.Value = true;
     try
     {
         bool? oldSecurityActive = securityActiveTL.Value;
         securityActiveTL.Value = true;
         try
         {
             return filterRunnable();
         }
         finally
         {
             securityActiveTL.Value = oldSecurityActive;
         }
     }
     finally
     {
         entityActiveTL.Value = oldFilterActive;
     }
 }
Esempio n. 9
0
        public R SetScopedAuthentication <R>(IAuthentication authentication, IResultingBackgroundWorkerDelegate <R> runnableScope)
        {
            ISecurityContext securityContext = Context;
            bool             created         = false;

            if (securityContext == null)
            {
                securityContext = GetCreateContext();
                created         = true;
            }
            IAuthorization  oldAuthorization  = securityContext.Authorization;
            IAuthentication oldAuthentication = securityContext.Authentication;

            try
            {
                if (Object.ReferenceEquals(oldAuthentication, authentication))
                {
                    return(runnableScope());
                }
                try
                {
                    securityContext.Authentication = authentication;
                    securityContext.Authorization  = null;
                    return(runnableScope());
                }
                finally
                {
                    securityContext.Authentication = oldAuthentication;
                    securityContext.Authorization  = oldAuthorization;
                }
            }
            finally
            {
                if (created)
                {
                    ClearContext();
                }
            }
        }
Esempio n. 10
0
        public static T SetState <T>(Type originalType, Type currentType, NewType newType, IServiceContext beanContext,
                                     IEnhancementHint context, IResultingBackgroundWorkerDelegate <T> runnable)
        {
            IBytecodeBehaviorState oldState = stateTL.Value;

            stateTL.Value = new BytecodeBehaviorState(currentType, newType, originalType, beanContext, context);
            try
            {
                return(runnable.Invoke());
            }
            finally
            {
                if (oldState != null)
                {
                    stateTL.Value = oldState;
                }
                else
                {
                    stateTL.Value = null;
                }
            }
        }
Esempio n. 11
0
        public R ExecuteWithCache <R>(ICacheProvider cacheProvider, IResultingBackgroundWorkerDelegate <R> runnable)
        {
            ParamChecker.AssertParamNotNull(cacheProvider, "cacheProvider");
            ParamChecker.AssertParamNotNull(runnable, "runnable");
            Stack <ICacheProvider> stack = cacheProviderStackTL.Value;

            if (stack == null)
            {
                stack = new Stack <ICacheProvider>();
                cacheProviderStackTL.Value = stack;
            }
            stack.Push(cacheProvider);
            try
            {
                return(runnable());
            }
            finally
            {
                if (!Object.ReferenceEquals(stack.Pop(), cacheProvider))
                {
                    throw new Exception("Must never happen");
                }
            }
        }
Esempio n. 12
0
 public R ExecuteWithSecurityDirective<R>(SecurityDirective securityDirective, IResultingBackgroundWorkerDelegate<R> runnable)
 {
     bool? securityActive = securityDirective.HasFlag(SecurityDirective.DISABLE_SECURITY) ? false : securityDirective
             .HasFlag(SecurityDirective.ENABLE_SECURITY) ? true : default(bool?);
     bool? entityActive = securityDirective.HasFlag(SecurityDirective.DISABLE_ENTITY_CHECK) ? false : securityDirective
             .HasFlag(SecurityDirective.ENABLE_ENTITY_CHECK) ? true : default(bool?);
     bool? serviceActive = securityDirective.HasFlag(SecurityDirective.DISABLE_SERVICE_CHECK) ? false : securityDirective
             .HasFlag(SecurityDirective.ENABLE_SERVICE_CHECK) ? true : default(bool?);
     bool? oldSecurityActive = null, oldEntityActive = null, oldServiceActive = null;
     if (securityActive != null)
     {
         oldSecurityActive = securityActiveTL.Value;
         securityActiveTL.Value = securityActive;
     }
     try
     {
         if (entityActive != null)
         {
             oldEntityActive = entityActiveTL.Value;
             entityActiveTL.Value = entityActive;
         }
         try
         {
             if (serviceActive != null)
             {
                 oldServiceActive = serviceActiveTL.Value;
                 serviceActiveTL.Value = serviceActive;
             }
             try
             {
                 return runnable();
             }
             finally
             {
                 if (serviceActive != null)
                 {
                     serviceActiveTL.Value = oldServiceActive;
                 }
             }
         }
         finally
         {
             if (entityActive != null)
             {
                 entityActiveTL.Value = oldEntityActive;
             }
         }
     }
     finally
     {
         if (securityActive != null)
         {
             securityActiveTL.Value = oldSecurityActive;
         }
     }
 }
Esempio n. 13
0
 public IDisposableCache WithParent(ICache parent, IResultingBackgroundWorkerDelegate <IDisposableCache> runnable)
 {
     return(new CacheMock());
 }
Esempio n. 14
0
 public R ExecuteWithCache <R>(IResultingBackgroundWorkerDelegate <R> runnable)
 {
     return(ExecuteWithCache(ThreadLocalCacheProvider, runnable));
 }
Esempio n. 15
0
        protected IOriCollection MergeIntern(ICUDResult cudResultOriginal, IMethodDescription methodDescription)
        {
            IResultingBackgroundWorkerDelegate <IOriCollection> runnable = new IResultingBackgroundWorkerDelegate <IOriCollection>(delegate()
            {
                IDisposableCache childCache = CacheFactory.CreatePrivileged(CacheFactoryDirective.SubscribeTransactionalDCE, false, false,
                                                                            "MergeServiceRegistry.STATE");
                try
                {
                    IncrementalMergeState state = null;
                    ICUDResult cudResultOfCache;
                    if (MergeProcess.IsAddNewlyPersistedEntities() || (Log.DebugEnabled && CudResultPrinter != null))
                    {
                        childCache = CacheFactory.CreatePrivileged(CacheFactoryDirective.SubscribeTransactionalDCE, false, false,
                                                                   "MergeServiceRegistry.STATE");
                        state            = (IncrementalMergeState)CudResultApplier.AcquireNewState(childCache);
                        cudResultOfCache = CudResultApplier.ApplyCUDResultOnEntitiesOfCache(cudResultOriginal, true, state);
                    }
                    else
                    {
                        cudResultOfCache = cudResultOriginal;
                    }
                    if (Log.DebugEnabled)
                    {
                        if (CudResultPrinter != null)
                        {
                            Log.Debug("Initial merge [" + RuntimeHelpers.GetHashCode(state) + "]:\n" + CudResultPrinter.PrintCUDResult(cudResultOfCache, state));
                        }
                        else
                        {
                            Log.Debug("Initial merge [" + RuntimeHelpers.GetHashCode(state) + "]. No Details available");
                        }
                    }
                    List <MergeOperation> mergeOperationSequence = new List <MergeOperation>();
                    ICUDResult extendedCudResult = WhatIfMerged(cudResultOfCache, methodDescription, mergeOperationSequence, state);

                    if (Log.DebugEnabled)
                    {
                        Log.Debug("Merge finished [" + RuntimeHelpers.GetHashCode(state) + "]");
                    }
                    if (MergeSecurityManager != null)
                    {
                        SecurityActive.ExecuteWithSecurityDirective(SecurityDirective.ENABLE_ENTITY_CHECK, delegate()
                        {
                            MergeSecurityManager.CheckMergeAccess(extendedCudResult, methodDescription);
                        });
                    }
                    List <Object> originalRefsOfCache  = new List <Object>(cudResultOfCache.GetOriginalRefs());
                    List <Object> originalRefsExtended = new List <Object>(extendedCudResult.GetOriginalRefs());
                    IOriCollection oriCollExtended     = Intern(extendedCudResult, methodDescription, mergeOperationSequence, state);

                    IList <IChangeContainer> allChangesOriginal = cudResultOriginal.AllChanges;
                    IList <IObjRef> allChangedObjRefsExtended   = oriCollExtended.AllChangeORIs;
                    IObjRef[] allChangedObjRefsResult           = new IObjRef[allChangesOriginal.Count];

                    IdentityHashMap <Object, int?> originalRefOfCacheToIndexMap = new IdentityHashMap <Object, int?>();
                    for (int a = originalRefsOfCache.Count; a-- > 0;)
                    {
                        originalRefOfCacheToIndexMap.Put(originalRefsOfCache[a], a);
                    }
                    for (int a = originalRefsExtended.Count; a-- > 0;)
                    {
                        int?indexOfCache = originalRefOfCacheToIndexMap.Get(originalRefsExtended[a]);
                        if (indexOfCache == null)
                        {
                            // this is a change implied by a rule or an persistence-implicit change
                            // we do not know about it in the outer original CUDResult
                            continue;
                        }
                        IObjRef objRefExtended = allChangedObjRefsExtended[a];
                        IObjRef objRefOriginal = allChangesOriginal[indexOfCache.Value].Reference;
                        if (objRefExtended == null)
                        {
                            // entity has been deleted
                            objRefOriginal.Id      = null;
                            objRefOriginal.Version = null;
                        }
                        else
                        {
                            objRefOriginal.Id      = objRefExtended.Id;
                            objRefOriginal.Version = objRefExtended.Version;
                        }
                        if (objRefOriginal is IDirectObjRef)
                        {
                            ((IDirectObjRef)objRefOriginal).Direct = null;
                        }
                        allChangedObjRefsResult[indexOfCache.Value] = objRefOriginal;
                    }
                    OriCollection oriCollection = new OriCollection(new List <IObjRef>(allChangedObjRefsResult));

                    return(oriCollection);
                }
                finally
                {
                    childCache.Dispose();
                }
            });

            if (SecurityActive == null || !SecurityActive.FilterActivated)
            {
                return(runnable());
            }
            else
            {
                return(SecurityActive.ExecuteWithoutFiltering(runnable));
            }
        }
Esempio n. 16
0
        protected virtual void ProcessCUDResult(Object objectToMerge, ICUDResult cudResult, IList <Object> unpersistedObjectsToDelete,
                                                ProceedWithMergeHook proceedHook, bool addNewEntitiesToCache)
        {
            if (cudResult.AllChanges.Count > 0 || (unpersistedObjectsToDelete != null && unpersistedObjectsToDelete.Count > 0))
            {
                if (proceedHook != null)
                {
                    bool proceed = proceedHook.Invoke(cudResult, unpersistedObjectsToDelete);
                    if (!proceed)
                    {
                        return;
                    }
                }
            }
            if (cudResult.AllChanges.Count == 0)
            {
                if (Log.InfoEnabled)
                {
                    Log.Info("Service call skipped early because there is nothing to merge");
                }
            }
            else
            {
                IOriCollection oriColl;
                EventDispatcher.EnableEventQueue();
                try
                {
                    EventDispatcher.Pause(Cache);
                    try
                    {
                        bool?oldNewlyPersistedEntities = addNewlyPersistedEntitiesTL.Value;
                        addNewlyPersistedEntitiesTL.Value = addNewEntitiesToCache;
                        try
                        {
                            IResultingBackgroundWorkerDelegate <IOriCollection> runnable = new IResultingBackgroundWorkerDelegate <IOriCollection>(delegate()
                            {
                                IOriCollection oriColl2 = MergeService.Merge(cudResult, null);
                                MergeController.ApplyChangesToOriginals(cudResult, oriColl2, null);
                                return(oriColl2);
                            });
                            if (Transaction == null || Transaction.Active)
                            {
                                oriColl = runnable();
                            }
                            else
                            {
                                oriColl = Transaction.RunInLazyTransaction(runnable);
                            }
                        }
                        finally
                        {
                            addNewlyPersistedEntitiesTL.Value = oldNewlyPersistedEntities;
                        }
                    }
                    finally
                    {
                        EventDispatcher.Resume(Cache);
                    }
                }
                finally
                {
                    EventDispatcher.FlushEventQueue();
                }
                DataChangeEvent dataChange = DataChangeEvent.Create(-1, -1, -1);
                // This is intentionally a remote source
                dataChange.IsLocalSource = false;

                if (IsNetworkClientMode)
                {
                    IList <IChangeContainer> allChanges = cudResult.AllChanges;

                    IList <IObjRef> orisInReturn = oriColl.AllChangeORIs;
                    for (int a = allChanges.Count; a-- > 0;)
                    {
                        IChangeContainer changeContainer   = allChanges[a];
                        IObjRef          reference         = changeContainer.Reference;
                        IObjRef          referenceInReturn = orisInReturn[a];
                        if (changeContainer is CreateContainer)
                        {
                            if (referenceInReturn.IdNameIndex != ObjRef.PRIMARY_KEY_INDEX)
                            {
                                throw new ArgumentException("Implementation error: Only PK references are allowed in events");
                            }
                            dataChange.Inserts.Add(new DataChangeEntry(referenceInReturn.RealType, referenceInReturn.IdNameIndex, referenceInReturn.Id, referenceInReturn.Version));
                        }
                        else if (changeContainer is UpdateContainer)
                        {
                            if (referenceInReturn.IdNameIndex != ObjRef.PRIMARY_KEY_INDEX)
                            {
                                throw new ArgumentException("Implementation error: Only PK references are allowed in events");
                            }
                            dataChange.Updates.Add(new DataChangeEntry(referenceInReturn.RealType, referenceInReturn.IdNameIndex, referenceInReturn.Id, referenceInReturn.Version));
                        }
                        else if (changeContainer is DeleteContainer)
                        {
                            if (reference.IdNameIndex != ObjRef.PRIMARY_KEY_INDEX)
                            {
                                throw new ArgumentException("Implementation error: Only PK references are allowed in events");
                            }
                            dataChange.Deletes.Add(new DataChangeEntry(reference.RealType, reference.IdNameIndex, reference.Id, reference.Version));
                        }
                    }
                    //EventDispatcher.DispatchEvent(dataChange, DateTime.Now, -1);
                }
            }
            if (unpersistedObjectsToDelete != null && unpersistedObjectsToDelete.Count > 0)
            {
                // Create a DCE for all objects without an id but which should be deleted...
                // This is the case for newly created objects on client side, which should be
                // "cancelled". The DCE notifies all models which contain identity references to the related
                // objects to erase their existence in all controls. They are not relevant in the previous
                // server merge process
                DataChangeEvent dataChange = DataChangeEvent.Create(0, 0, unpersistedObjectsToDelete.Count);

                for (int a = unpersistedObjectsToDelete.Count; a-- > 0;)
                {
                    Object unpersistedObject = unpersistedObjectsToDelete[a];
                    dataChange.Deletes.Add(new DirectDataChangeEntry(unpersistedObject));
                }
                EventDispatcher.DispatchEvent(dataChange, DateTime.Now, -1);
            }
            RevertChangesHelper.RevertChanges(objectToMerge);
        }
Esempio n. 17
0
 public R ExecuteWithCache <R>(ICache cache, IResultingBackgroundWorkerDelegate <R> runnable)
 {
     ParamChecker.AssertParamNotNull(cache, "cache");
     ParamChecker.AssertParamNotNull(runnable, "runnable");
     return(ExecuteWithCache <R>(new SingleCacheProvider(cache), runnable));
 }