public void SetLTCValue() { IObjectFactory of = ObjectFactory; ITestObject testObject = (ITestObject)of.GetObject("testObject"); OrderedLogicalThreadContextCheckAdvisor orderedBeforeLTCSet = (OrderedLogicalThreadContextCheckAdvisor)of.GetObject("orderedBeforeLTCSet"); Assert.AreEqual(0, orderedBeforeLTCSet.CountingBeforeAdvice.GetCalls()); Assert.IsNull(LogicalThreadContext.GetData(LogicalThreadContextAdvice.ORDERING_SLOT)); Assert.AreEqual(4, testObject.Age, "Initial value of age for test object is not correct."); int newAge = 5; testObject.Age = newAge; Assert.AreEqual(1, orderedBeforeLTCSet.CountingBeforeAdvice.GetCalls()); Assert.AreEqual(newAge, testObject.Age, "Assigned value of age for test object is not correct."); Assert.IsNotNull(LogicalThreadContext.GetData(LogicalThreadContextAdvice.ORDERING_SLOT)); }
public override void Before(MethodInfo method, object[] args, object target) { // do check for presence of LTC value.... if (EnclosingInstance.requireLtcHasValue) { if (LogicalThreadContext.GetData(LogicalThreadContextAdvice.ORDERING_SLOT) == null) { throw new SystemException("Expected object in LTC ORDERING_SLOT"); } } else { if (LogicalThreadContext.GetData(LogicalThreadContextAdvice.ORDERING_SLOT) != null) { throw new SystemException("Expected no object in LTC ORDERING_SLOT"); } } base.Before(method, args, target); }
/// <summary> /// Retrieve a resource for the given key that is bound to the current thread. /// </summary> /// <param name="key">key to check</param> /// <returns>a value bound to the current thread, or null if none.</returns> public static object GetResource(Object key) { var resources = LogicalThreadContext.GetData(RESOURCES_DATA_SLOT_NAME) as IDictionary; if (resources == null) { return(null); } //Check for contains since indexer returning null behavior changes in 2.0 if (!resources.Contains(key)) { return(null); } object val = resources[key]; if (val != null && log.IsDebugEnabled) { log.Debug("get value [" + Describe(val) + "] for key [" + Describe(key) + "] bound to thread [" + Thread.CurrentThread.ManagedThreadId + "]"); } return(val); }
/// <summary> /// Bind the given resource for teh given key to the current thread /// </summary> /// <param name="key">key to bind the value to</param> /// <param name="value">value to bind</param> public static void BindResource(Object key, Object value) { var resources = LogicalThreadContext.GetData(RESOURCES_DATA_SLOT_NAME) as IDictionary; //Set thread local resource storage if not found if (resources == null) { resources = new Hashtable(); LogicalThreadContext.SetData(RESOURCES_DATA_SLOT_NAME, resources); } if (resources.Contains(key)) { throw new InvalidOperationException("Already value [" + resources[key] + "] for key [" + key + "] bound to thread [" + Thread.CurrentThread.ManagedThreadId + "]"); } resources.Add(key, value); if (log.IsDebugEnabled) { log.Debug("Bound value [" + Describe(value) + "] for key [" + Describe(key) + "] to thread [" + Thread.CurrentThread.ManagedThreadId + "]"); } }
/// <summary> /// Retrieve a resource for the given key that is bound to the current thread. /// </summary> /// <param name="key">key to check</param> /// <returns>a value bound to the current thread, or null if none.</returns> public static object GetResource(Object key) { AssertUtils.ArgumentNotNull(key, "Key must not be null"); IDictionary resources = LogicalThreadContext.GetData(resourcesDataSlotName) as IDictionary; if (resources == null) { return(null); } //Check for contains since indexer returning null behavior changes in 2.0 if (!resources.Contains(key)) { return(null); } object val = resources[key]; if (val != null && LOG.IsDebugEnabled) { LOG.Debug("Retrieved value [" + Describe(val) + "] for key [" + Describe(key) + "] bound to thread [" + SystemUtils.ThreadId + "]"); } return(val); }
/// <summary> /// Unbind a resource for the given key from the current thread /// </summary> /// <param name="key">key to check</param> /// <returns>the previously bound value</returns> /// <exception cref="InvalidOperationException">if there is no value bound to the thread</exception> public static object UnbindResource(Object key) { var resources = LogicalThreadContext.GetData(RESOURCES_DATA_SLOT_NAME) as IDictionary; if (resources == null || !resources.Contains(key)) { throw new InvalidOperationException("No value for key [" + key + "] bound to thread [" + Thread.CurrentThread.ManagedThreadId + "]"); } Object val = resources[key]; resources.Remove(key); if (resources.Count == 0) { LogicalThreadContext.FreeNamedDataSlot(RESOURCES_DATA_SLOT_NAME); } if (log.IsDebugEnabled) { log.Debug("Removed value [" + Describe(val) + "] for key [" + Describe(key) + "] from thread [" + Thread.CurrentThread.ManagedThreadId + "]"); } return(val); }
/// <summary> /// Process Sessions that have been registered for deferred close /// for the given SessionFactory. /// </summary> /// <param name="sessionFactory">The session factory.</param> /// <exception cref="InvalidOperationException">If there is no session factory associated with the thread.</exception> public static void ProcessDeferredClose(ISessionFactory sessionFactory) { AssertUtils.ArgumentNotNull(sessionFactory, "No SessionFactory specified"); IDictionary holderDictionary = LogicalThreadContext.GetData(DeferredCloseHolderDataSlotName) as IDictionary; if (holderDictionary == null || !holderDictionary.Contains(sessionFactory)) { throw new InvalidOperationException("Deferred close not active for SessionFactory [" + sessionFactory + "]"); } log.Debug("Processing deferred close of Hibernate Sessions"); Set sessions = (Set)holderDictionary[sessionFactory]; holderDictionary.Remove(sessionFactory); foreach (ISession session in sessions) { CloseSession(session); } if (holderDictionary.Count == 0) { LogicalThreadContext.FreeNamedDataSlot(DeferredCloseHolderDataSlotName); } }
/// <summary> /// Schedules a given job and trigger (both wrapped by a <see cref="JobSchedulingBundle" />). /// </summary> /// <param name="job">job wrapper.</param> /// <exception cref="SchedulerException"> /// if the Job or Trigger cannot be added to the Scheduler, or /// there is an internal Scheduler error. /// </exception> public virtual void ScheduleJob(JobSchedulingBundle job) { ScheduleJob(job, (IScheduler)LogicalThreadContext.GetData(ThreadLocalKeyScheduler), OverwriteExistingJobs); }