-
Notifications
You must be signed in to change notification settings - Fork 13
/
ExtensionHandlerBase.cs
170 lines (151 loc) · 5.49 KB
/
ExtensionHandlerBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
namespace UIShell.OSGi
{
using System;
using System.Collections.Generic;
using System.Xml;
using Collection;
public abstract class ExtensionHandlerBase<T> : IDisposable
{
private bool _disposed;
protected IBundleContext BundleContext { get; private set; }
protected ThreadSafeDictionary<Extension, List<T>> ExtensionDatas { get; private set; }
protected string ExtensionPoint { get; private set; }
public ExtensionHandlerBase(IBundleContext bundleContext, string extensionPoint)
{
BundleContext = bundleContext;
ExtensionPoint = extensionPoint;
ExtensionDatas = new ThreadSafeDictionary<Extension, List<T>>();
HandleExtensions();
BundleContext.ExtensionChanged += new EventHandler<ExtensionEventArgs>(BundleContextExtensionChanged);
}
protected virtual void AddExtensionData(Extension extension, T extensionData)
{
using (var locker = ExtensionDatas.Lock())
{
if (!locker.ContainsKey(extension))
{
locker[extension] = new List<T>();
}
locker[extension].Add(extensionData);
}
}
protected virtual void BundleContextExtensionChanged(object sender, ExtensionEventArgs e)
{
if (e.ExtensionPoint.Equals(ExtensionPoint))
{
if (e.Action == CollectionChangedAction.Add)
{
foreach (T local in ConvertExtensionToExtensionDatas(e.Extension))
{
AddExtensionData(e.Extension, local);
}
}
else if (e.Action == CollectionChangedAction.Remove)
{
RemoveExtensionDatas(e.Extension);
}
else
{
ClearExtensionDatas();
}
}
}
protected virtual void ClearExtensionDatas()
{
ExtensionDatas.ForEach(item => item.Value.Clear());
ExtensionDatas.Clear();
}
protected virtual List<T> ConvertExtensionToExtensionDatas(Extension extension)
{
var list = new List<T>();
T item = default(T);
foreach (var node in extension.Data)
{
if (!(node is XmlComment))
{
item = ConvertXmlNodeToExtensionData(extension, node);
if (item != null)
{
if (!list.Contains(item))
{
list.Add(item);
}
else
{
IgnoreDuplicatedExtensionData(item);
}
}
}
}
return list;
}
protected abstract T ConvertXmlNodeToExtensionData(Extension extension, XmlNode node);
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
BundleContext.ExtensionChanged -= new EventHandler<ExtensionEventArgs>(BundleContextExtensionChanged);
ClearExtensionDatas();
}
_disposed = true;
}
}
~ExtensionHandlerBase()
{
Dispose(false);
}
public List<T> GetExtensionDatas(IBundle bundle)
{
var result = new List<T>();
ExtensionDatas.ForEach(delegate (KeyValuePair<Extension, List<T>> item) {
if (item.Key.Owner.Equals(bundle))
{
result.AddRange(item.Value);
}
});
return result;
}
public T GetFirstOrDefaultExtensionData(IBundle bundle)
{
T data = default(T);
ExtensionDatas.ForEach(delegate (KeyValuePair<Extension, List<T>> item) {
if (item.Key.Owner.Equals(bundle) && (item.Value.Count > 0))
{
data = item.Value[0];
}
});
return data;
}
protected string GetXmlNodeAttributeValue(XmlNode node, string attributeName)
{
var str = string.Empty;
if ((node.Attributes[attributeName] != null) && !string.IsNullOrEmpty(node.Attributes[attributeName].Value.Trim()))
{
str = node.Attributes[attributeName].Value.Trim();
}
return str;
}
protected virtual void HandleExtensions()
{
foreach (var extension in BundleContext.GetExtensions(ExtensionPoint))
{
foreach (T local in ConvertExtensionToExtensionDatas(extension))
{
AddExtensionData(extension, local);
}
}
}
protected abstract void IgnoreDuplicatedExtensionData(T extensionData);
protected virtual void RemoveExtensionDatas(Extension extension)
{
ExtensionDatas.Remove(extension);
}
}
}