This repository was archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathResourceCollection.cs
531 lines (495 loc) · 17.9 KB
/
ResourceCollection.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
* Copyright 2012 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
namespace Splunk
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// Represents the base class of all collections.
/// </summary>
/// <typeparam name="T">The generic parameter derived from Resource
/// </typeparam>
public class ResourceCollection<T> : Resource, IEnumerable<T> where T : Resource
{
/// <summary>
/// The object Type of the class, a derived class of Resource.
/// </summary>
private Type itemClass;
/// <summary>
/// The item signature of the constructors.
/// </summary>
private static Type[] itemSig = new Type[]
{
typeof(Service), typeof(string)
};
/// <summary>
/// Initializes a new instance of the <see cref="ResourceCollection"/>
/// class.
/// </summary>
/// <param name="service">The service</param>
/// <param name="path">The path of the resource</param>
/// <param name="itemClass">The object type</param>
public ResourceCollection(Service service, string path, Type itemClass)
: base(service, path)
{
this.itemClass = itemClass;
this.Items = new Dictionary<string, List<T>>();
}
/// <summary>
/// Initializes a new instance of the <see cref="ResourceCollection"/>
/// class.
/// </summary>
/// <param name="service">The service</param>
/// <param name="path">The path of the resource</param>
/// <param name="args">The variable arguments</param>
/// <param name="itemClass">The object type</param>
public ResourceCollection(
Service service, string path, Args args, Type itemClass)
: base(service, path, args)
{
this.itemClass = itemClass;
this.Items = new Dictionary<string, List<T>>();
}
/// <summary>
/// Gets a value indicating whether the IDictionary has a fixed size.
/// </summary>
public bool IsFixedSize
{
get
{
return false;
}
}
/// <summary>
/// Gets a value indicating whether the IDictionary is read-only.
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
/// <summary>
/// Gets or sets the items in the collection. Because of namespace
/// rules, a key-name may result in multiple Entities, thus the key
/// resolves to a list of one or more Entities.
/// </summary>
protected Dictionary<string, List<T>> Items
{
get;
set;
}
/// <summary>
/// Gets an ICollection of keys in the collection of resources. Note
/// that if the local resource collection is dirty, will refresh an
/// up-to-date copy from the server.
/// </summary>
public ICollection<T> Keys
{
get
{
List<T> collection = new List<T>();
this.Validate();
Dictionary<string, List<T>>.KeyCollection keySet =
this.Items.Keys;
foreach (string key in keySet)
{
List<T> list = this.Items[key];
foreach (T item in list)
{
collection.Add(item);
}
}
return collection;
}
}
/// <summary>
/// Gets the Number of elements in the collection.
/// </summary>
/// <returns>The number of elements in the collection.</returns>
public int Size
{
get
{
return this.Validate().Items.Count;
}
}
/// <summary>
/// Gets an ICollection of values in the collection of resources. Note
/// that if the local resource collection is dirty, will refresh an
/// up-to-date copy from the server.
/// </summary>
public ICollection<T> Values
{
get
{
List<T> collection = new List<T>();
this.Validate();
Dictionary<string, List<T>>.KeyCollection keySet =
this.Items.Keys;
foreach (string key in keySet)
{
List<T> list = this.Items[key];
foreach (T item in list)
{
collection.Add(item);
}
}
return collection;
}
}
/// <summary>
/// Unsupported. Adds a value to the dictionary.
/// </summary>
/// <param name="key">The key</param>
/// <param name="value">The value</param>
public void Add(object key, T value)
{
throw new Exception("Add unsupported");
}
/// <summary>
/// Returns the typed enumerator for this collection.
/// </summary>
/// <returns>The enumerator</returns>
public IEnumerator<T> GetEnumerator()
{
return this.Values.GetEnumerator();
}
/// <summary>
/// Returns the generic enumerator for this collection.
/// </summary>
/// <returns>The enumerator</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
/// <summary>
/// Unsupported. Clear the collection.
/// </summary>
public void Clear()
{
throw new Exception("Clear not supported");
}
/// <summary>
/// Returns a value indicating whether or not the collection of
/// Resources contains the desired key.
/// </summary>
/// <param name="key">The key</param>
/// <returns>True or false</returns>
public virtual bool ContainsKey(object key)
{
return this.Validate().Items.ContainsKey((string)key);
}
/// <summary>
/// Returns a value indicating whether or not the collection of
/// Resources contains the desired key, qualified by namespace.
/// </summary>
/// <param name="key">The key</param>
/// <param name="splunkNamespace">The namespace</param>
/// <returns>True or false</returns>
public virtual bool ContainsKey(object key, Args splunkNamespace)
{
this.Validate();
List<T> entities = this.Items[(string)key];
if (entities.Count == 0)
{
return false;
}
string pathMatcher =
this.Service.Fullpath(string.Empty, splunkNamespace);
foreach (T entity in entities)
{
if (entity.Path.StartsWith(pathMatcher))
{
return true;
}
}
return false;
}
/// <summary>
/// Returns a value indicating whether or not the value exists in the
/// collection.
/// </summary>
/// <param name="value">The value</param>
/// <returns>True or false</returns>
public bool ContainsValue(T value)
{
foreach (object key in this.Keys)
{
if (this.Items[(string)key].Contains(value))
{
return true;
}
}
return false;
}
/// <summary>
/// Creates a collection member.
/// </summary>
/// <param name="itemClass">The object type being created</param>
/// <param name="path">The path to the resource</param>
/// <param name="splunkNamespace">The namespace</param>
/// <returns>The new object, of type T</returns>
public T CreateItem(Type itemClass, string path, Args splunkNamespace)
{
ConstructorInfo ctor = itemClass.GetConstructor(itemSig);
T item = (T)ctor.Invoke(new object[]
{
Service, Service.Fullpath(path, splunkNamespace)
});
return item;
}
/// <summary>
/// Creates a collection member corresponding to a given
/// Atom entry. This base implementation uses the class object that was
/// passed in when the generic ResourceCollection was created.
/// Subclasses may override this method to provide alternative means of
/// instantiating collection items.
/// </summary>
/// <param name="entry">The AtomEntry</param>
/// <returns>The new object, of type T</returns>
protected virtual T CreateItem(AtomEntry entry)
{
return this.CreateItem(
this.itemClass, this.ItemPath(entry), this.SplunkNamespace(entry));
}
/// <summary>
/// Returns a value indicating whether or not the collections are equal.
/// </summary>
/// <param name="o">The object to compare against</param>
/// <returns>True or false</returns>
public override bool Equals(object o)
{
return this.Validate().Items.Equals(o);
}
/// <summary>
/// Gets the object in the collection, given the key. If there
/// are more than one Resource identified by the key, an AMBIGUOUS
/// exception is thrown. In order to disambiguate Resources, a
/// namespace must be supplied.
/// </summary>
/// <param name="key">The key</param>
/// <returns>The object, or default(object) if not found.</returns>
public virtual T Get(object key)
{
this.Validate();
if (!this.Items.ContainsKey((string)key))
{
return default(T);
}
List<T> entities = this.Items[(string)key];
if (entities.Count > 1)
{
throw new SplunkException(
SplunkException.AMBIGUOUS,
"Key has multiple values, specify a namespace");
}
if (entities.Count == 0)
{
return default(T);
}
return entities[0];
}
/// <summary>
/// Gets the object in the collection, given the key, with a scoped
/// name-spaced constraint.
/// </summary>
/// <param name="key">The key</param>
/// <param name="splunkNamespace">The namespace</param>
/// <returns>The object, or default(object) if not found.</returns>
public virtual T Get(object key, Args splunkNamespace)
{
this.Validate();
if (!this.Items.ContainsKey((string)key))
{
return default(T);
}
List<T> entities = this.Items[(string)key];
if (entities.Count == 0)
{
return default(T);
}
string pathMatcher;
pathMatcher = Service.Fullpath(string.Empty, splunkNamespace);
foreach (T entity in entities)
{
if (entity.Path.StartsWith(pathMatcher))
{
return entity;
}
}
return default(T);
}
/// <summary>
/// Returns an up-to-date Hashcode.
/// </summary>
/// <returns>The hash code</returns>
public override int GetHashCode()
{
return this.Validate().Items.GetHashCode();
}
/// <summary>
/// Gets a value indicating whether the up-to-date Resource count is 0.
/// </summary>
/// <returns>True or false</returns>
public bool IsEmpty
{
get
{
return this.Validate().Items.Count == 0;
}
}
/// <summary>
/// Returns the value to use as the item key from a given Atom entry.
/// Subclasses may override this value for collections that use
/// something other than title as the key.
/// </summary>
/// <param name="entry">The AtomEntry</param>
/// <returns>The title</returns>
protected virtual string ItemKey(AtomEntry entry)
{
return entry.Title;
}
/// <summary>
/// Returns the value to use as the item path from a given Atom entry.
/// Subclasses may override this value to support alternative methods of
/// determining a member's path.
/// </summary>
/// <param name="entry">The AtomEntry</param>
/// <returns>The alternate path</returns>
protected virtual string ItemPath(AtomEntry entry)
{
return entry.Links["alternate"];
}
/// <summary>
/// Returns the namesapce of an AtomEntry based on the eai::acl field.
/// </summary>
/// <param name="entry">The AtomEntry</param>
/// <returns>The namespace</returns>
private Args SplunkNamespace(AtomEntry entry)
{
Args splunkNamespace = new Args();
// no content? return an empty namespace.
if (entry.Content == null)
{
return splunkNamespace;
}
Dictionary<string, object> entityMetadata =
(Dictionary<string, object>)entry.Content["eai:acl"];
if (entityMetadata.ContainsKey("owner"))
{
splunkNamespace.AlternateAdd(
"owner", entityMetadata["owner"]);
}
if (entityMetadata.ContainsKey("app"))
{
splunkNamespace.AlternateAdd(
"app", entityMetadata["app"]);
}
if (entityMetadata.ContainsKey("sharing"))
{
splunkNamespace.AlternateAdd(
"sharing", entityMetadata["sharing"]);
}
return splunkNamespace;
}
/// <summary>
/// Issues an HTTP request to list the contents of the collection
/// resource.
/// </summary>
/// <returns>The contents of the collection ResponseMessage format
/// </returns>
public virtual ResponseMessage List()
{
return this.Service.Get(this.Path, this.RefreshArgs);
}
/// <summary>
/// Loads the collection resource from a given Atom feed.
/// </summary>
/// <param name="value">The AtomFeed</param>
/// <returns>The resource collection</returns>
protected ResourceCollection<T> Load(AtomFeed value)
{
base.Load(value);
foreach (AtomEntry entry in value.Entries)
{
string key = this.ItemKey(entry);
T item = this.CreateItem(entry);
if (this.Items.ContainsKey(key))
{
List<T> list = this.Items[key];
list.Add(item);
}
else
{
List<T> list = new List<T>();
list.Add(item);
this.Items.Add(key, list);
}
}
return this;
}
/// <summary>
/// Refresh the resource collection
/// </summary>
/// <returns>The resource</returns>
public override Resource Refresh()
{
this.Items.Clear();
ResponseMessage response = this.List();
AtomFeed feed = AtomFeed.Parse(response.Content);
this.Load(feed);
return this;
}
/// <summary>
/// Unsupported. Remove an element from the resource collection.
/// </summary>
/// <param name="key">The key</param>
/// <returns>Throws an exception</returns>
public T Remove(object key)
{
throw new Exception("Remove unsupported");
}
/// <summary>
/// Validates the collection. If dirty, will refresh.
/// </summary>
/// <returns>The collection</returns>
public new ResourceCollection<T> Validate()
{
base.Validate();
return this;
}
/// <summary>
/// Returns the number of elements in the list of a
/// specific key in the collection.
/// </summary>
/// <param name="key">The key</param>
/// <returns>The number of elements</returns>
public int ValueSize(object key)
{
this.Validate();
if (!this.Items.ContainsKey("key"))
{
return 0;
}
List<T> entities = this.Items[(string)key];
return entities.Count;
}
}
}