| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.Batch |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Tools for managing multiple collections of behaviors. |
| | 12 | | /// </summary> |
| | 13 | | internal class BehaviorManager |
| | 14 | | { |
| 294 | 15 | | internal BehaviorManager( |
| 294 | 16 | | IEnumerable<BatchClientBehavior> baseBehaviors, |
| 294 | 17 | | IEnumerable<BatchClientBehavior> perCallBehaviors, |
| 294 | 18 | | DetailLevel detailLevel = null) |
| | 19 | | { |
| 294 | 20 | | this.BaseBehaviors = new List<BatchClientBehavior>(); |
| | 21 | |
|
| 294 | 22 | | if (null != baseBehaviors) |
| | 23 | | { |
| 294 | 24 | | this.BaseBehaviors.AddRange(baseBehaviors); |
| | 25 | | } |
| | 26 | |
|
| 294 | 27 | | this.PerCallBehaviors = new List<BatchClientBehavior>(); |
| | 28 | |
|
| 294 | 29 | | if (null != perCallBehaviors) |
| | 30 | | { |
| 172 | 31 | | this.PerCallBehaviors.AddRange(perCallBehaviors); |
| | 32 | | } |
| | 33 | |
|
| 294 | 34 | | if (detailLevel != null) |
| | 35 | | { |
| 2 | 36 | | this.AppendDetailLevel(detailLevel); |
| | 37 | | } |
| 294 | 38 | | } |
| | 39 | |
|
| 33 | 40 | | private BehaviorManager(BehaviorManager other) |
| | 41 | | { |
| 33 | 42 | | this.BaseBehaviors = new List<BatchClientBehavior>(other.BaseBehaviors); |
| 33 | 43 | | this.PerCallBehaviors = new List<BatchClientBehavior>(other.PerCallBehaviors); |
| 33 | 44 | | } |
| | 45 | |
|
| 1000 | 46 | | internal List<BatchClientBehavior> BaseBehaviors { get; set; } |
| 857 | 47 | | internal List<BatchClientBehavior> PerCallBehaviors { get; set; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Returns a new list of behaviors that is the concatination of the base + per-call. |
| | 51 | | /// </summary> |
| | 52 | | internal List<BatchClientBehavior> MasterListOfBehaviors |
| | 53 | | { |
| | 54 | | get |
| | 55 | | { |
| 312 | 56 | | List<BatchClientBehavior> ml = new List<BatchClientBehavior>(this.BaseBehaviors); |
| | 57 | |
|
| 312 | 58 | | ml.AddRange(this.PerCallBehaviors); |
| | 59 | |
|
| 312 | 60 | | return ml; |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| | 64 | | /* On "impl" inheritance |
| | 65 | | * |
| | 66 | | * Impls like GetNodeFileAsyncImpl cannot/must-not honor inheritance by propagating behaviors |
| | 67 | | * from the manager or outter class... the new child objects get their base from |
| | 68 | | * BehaviorMangaer.BaseBehaviors. |
| | 69 | | * |
| | 70 | | * When we add features like DetailLevel.. they must not taint the base |
| | 71 | | * collection... or children will get them. |
| | 72 | | * |
| | 73 | | * By corallary, such features should probably not taint the PerCallBeahviors either. |
| | 74 | | * We need an "Inbetween" collection. |
| | 75 | | * |
| | 76 | | * There is a bug on Detaillevel order-of-execution and this work should be part of that bug. |
| | 77 | | * |
| | 78 | | */ |
| | 79 | | internal BehaviorManager CreateBehaviorManagerWithDetailLevel(DetailLevel detailLevel) |
| | 80 | | { |
| | 81 | | //Copy "this" |
| 33 | 82 | | BehaviorManager result = new BehaviorManager(this); |
| | 83 | |
|
| 33 | 84 | | result.AppendDetailLevel(detailLevel); |
| 33 | 85 | | return result; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Gets an ordered list of behaviors of the specific type. |
| | 90 | | /// </summary> |
| | 91 | | /// <typeparam name="T"></typeparam> |
| | 92 | | /// <returns></returns> |
| | 93 | | internal List<T> GetBehaviors<T>() where T : BatchClientBehavior |
| | 94 | | { |
| 82 | 95 | | List<T> result = new List<T>(); |
| | 96 | | //Find all behaviors of type T and get the (in order) |
| 484 | 97 | | foreach (BatchClientBehavior behavior in this.MasterListOfBehaviors) |
| | 98 | | { |
| 160 | 99 | | T typedBehavior = behavior as T; |
| 160 | 100 | | if (typedBehavior != null) |
| | 101 | | { |
| 8 | 102 | | result.Add(typedBehavior); |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| 82 | 106 | | return result; |
| | 107 | | } |
| | 108 | |
|
| | 109 | | private void AppendDetailLevel(DetailLevel detailLevel) |
| | 110 | | { |
| 35 | 111 | | if ((null != detailLevel) && (detailLevel is ODATADetailLevel)) |
| | 112 | | { |
| 13 | 113 | | ODATADetailLevelIntercept intercept = new ODATADetailLevelIntercept(detailLevel as ODATADetailLevel); |
| | 114 | |
|
| 13 | 115 | | this.PerCallBehaviors.Add(intercept); |
| | 116 | | } |
| 35 | 117 | | } |
| | 118 | | } |
| | 119 | | } |