public ServiceContext(String name, IServiceContextIntern parent) { this.name = name; this.parent = parent; ReadWriteLock rwLock = new ReadWriteLock(); readLock = rwLock.ReadLock; writeLock = rwLock.WriteLock; typeToServiceDict.Put(typeof(IServiceContext), this); typeToServiceDict.Put(typeof(IServiceContextIntern), this); }
public void Dispose() { if (disposed || disposing) { return; } ILogger log; IServiceContext[] childrenCopy = null; writeLock.Lock(); try { if (disposed || disposing) { return; } log = GetService <ILoggerCache>().GetCachedLogger(this, typeof(ServiceContext)); if (log.DebugEnabled) { // Safe the toString-method for debugging purpose. Because this is not possible anymore if the context // has been disposed and all bean-references have been cleared toStringBackup = StringBuilderUtil.Concat(delegate(StringBuilder sb) { PrintContent(sb); }); } else { toStringBackup = "n/a"; } disposing = true; if (children != null && children.Count > 0) { childrenCopy = new IServiceContext[children.Count]; children.CopyTo(childrenCopy, 0); children.Clear(); } } finally { writeLock.Unlock(); } if (childrenCopy != null) { foreach (IServiceContext childContext in childrenCopy) { try { childContext.Dispose(); } catch (Exception e) { if (log.ErrorEnabled) { log.Error(e); } } } } writeLock.Lock(); try { if (parent != null) { parent.ChildContextDisposed(this); parent = null; } if (this.linkContainers != null) { IList <ILinkContainer> linkContainers = this.linkContainers; this.linkContainers = null; for (int a = linkContainers.Count; a-- > 0;) { ILinkContainer listenerContainer = linkContainers[a]; try { listenerContainer.Unlink(); } catch (System.Exception e) { if (failOnError) { throw; } if (log.ErrorEnabled) { log.Error(e); } } } } if (this.disposableObjects != null) { IList <Object> disposableObjects = this.disposableObjects; this.disposableObjects = null; for (int a = disposableObjects.Count; a-- > 0;) { Object disposableObject = disposableObjects[a]; if (disposableObject is WeakReference) { disposableObject = ((WeakReference)disposableObject).Target; } if (disposableObject is IDisposableBean) { try { ((IDisposableBean)disposableObject).Destroy(); } catch (System.Exception e) { if (failOnError) { throw; } if (log.ErrorEnabled) { log.Error(e); } } } else if (disposableObject is IDisposable) { try { ((IDisposable)disposableObject).Dispose(); } catch (System.Exception e) { if (failOnError) { throw; } if (log.ErrorEnabled) { log.Error(e); } } } else if (disposableObject is IBackgroundWorkerParamDelegate <IServiceContext> ) { try { ((IBackgroundWorkerParamDelegate <IServiceContext>)disposableObject).Invoke(this); } catch (System.Exception e) { if (failOnError) { throw; } if (log.ErrorEnabled) { log.Error(e); } } } } } if (nameToServiceDict != null) { nameToServiceDict.Clear(); } typeToServiceDict.Clear(); if (postProcessors != null) { postProcessors.Clear(); } if (preProcessors != null) { preProcessors.Clear(); } beanContextFactory.Dispose(); } finally { writeLock.Unlock(); beanContextFactory = null; linkContainers = null; nameToServiceDict = null; postProcessors = null; preProcessors = null; parent = null; disposed = true; running = false; } }