|
31 | 31 | * InternalEnforcer = CoreEnforcer + Internal API. |
32 | 32 | */ |
33 | 33 | class InternalEnforcer extends CoreEnforcer { |
| 34 | + |
| 35 | + /** |
| 36 | + * |
| 37 | + * @param sec the section, "p" or "g". |
| 38 | + * @param ptype the policy type, "p", "p2", .. or "g", "g2", .. |
| 39 | + * @param rules the policies |
| 40 | + * @param updateType the UpdateType |
| 41 | + * @return indicate whether the notification to the Watcher is successful or not |
| 42 | + */ |
| 43 | + private boolean notifyWatcher(String sec, String ptype, List<List<String>> rules, WatcherEx.UpdateType updateType) { |
| 44 | + if(watcher == null || !autoNotifyWatcher) return true; |
| 45 | + try { |
| 46 | + if (watcher instanceof WatcherEx) switch (updateType) { |
| 47 | + case UpdateForAddPolicy: |
| 48 | + ((WatcherEx) watcher).updateForAddPolicy(sec, ptype, rules.get(0).toArray(new String[0])); |
| 49 | + break; |
| 50 | + case UpdateForRemovePolicy: |
| 51 | + ((WatcherEx) watcher).updateForRemovePolicy(sec, ptype, rules.get(0).toArray(new String[0])); |
| 52 | + break; |
| 53 | + case UpdateForAddPolicies: |
| 54 | + ((WatcherEx) watcher).updateForAddPolicies(sec, ptype, rules); |
| 55 | + break; |
| 56 | + case UpdateForRemovePolicies: |
| 57 | + ((WatcherEx) watcher).updateForRemovePolicies(sec, ptype, rules); |
| 58 | + break; |
| 59 | + default: |
| 60 | + Util.logPrint("UnsupportedUpdateType for notifyWatcher"); |
| 61 | + break; |
| 62 | + } else { |
| 63 | + watcher.update(); |
| 64 | + } |
| 65 | + } catch (Exception e) { |
| 66 | + Util.logPrint("An exception occurred:" + e.getMessage()); |
| 67 | + return false; |
| 68 | + } |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
34 | 72 | /** |
35 | 73 | * addPolicy adds a rule to the current policy. |
36 | 74 | */ |
@@ -59,17 +97,10 @@ boolean addPolicy(String sec, String ptype, List<String> rule) { |
59 | 97 |
|
60 | 98 | buildIncrementalRoleLinks(sec, ptype, singletonList(rule), Model.PolicyOperations.POLICY_ADD); |
61 | 99 |
|
62 | | - if (watcher != null && autoNotifyWatcher) { |
63 | | - if (watcher instanceof WatcherEx) { |
64 | | - ((WatcherEx) watcher).updateForAddPolicy(rule.toArray(new String[0])); |
65 | | - } else { |
66 | | - watcher.update(); |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - return true; |
| 100 | + return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForAddPolicy); |
71 | 101 | } |
72 | 102 |
|
| 103 | + |
73 | 104 | /** |
74 | 105 | * addPolicies adds rules to the current policy. |
75 | 106 | */ |
@@ -100,11 +131,7 @@ boolean addPolicies(String sec, String ptype, List<List<String>> rules) { |
100 | 131 |
|
101 | 132 | buildIncrementalRoleLinks(sec, ptype, rules, Model.PolicyOperations.POLICY_ADD); |
102 | 133 |
|
103 | | - if (watcher != null && autoNotifyWatcher) { |
104 | | - watcher.update(); |
105 | | - } |
106 | | - |
107 | | - return true; |
| 134 | + return notifyWatcher(sec, ptype, rules, WatcherEx.UpdateType.UpdateForAddPolicies); |
108 | 135 | } |
109 | 136 |
|
110 | 137 | /** |
@@ -145,15 +172,7 @@ boolean removePolicy(String sec, String ptype, List<String> rule) { |
145 | 172 |
|
146 | 173 | buildIncrementalRoleLinks(sec, ptype, singletonList(rule), Model.PolicyOperations.POLICY_REMOVE); |
147 | 174 |
|
148 | | - if (watcher != null && autoNotifyWatcher) { |
149 | | - if (watcher instanceof WatcherEx) { |
150 | | - ((WatcherEx) watcher).updateForRemovePolicy(rule.toArray(new String[0])); |
151 | | - } else { |
152 | | - watcher.update(); |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - return true; |
| 175 | + return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForRemovePolicy); |
157 | 176 | } |
158 | 177 |
|
159 | 178 | /** |
@@ -262,12 +281,7 @@ boolean removePolicies(String sec, String ptype, List<List<String>> rules) { |
262 | 281 |
|
263 | 282 | buildIncrementalRoleLinks(sec, ptype, rules, Model.PolicyOperations.POLICY_REMOVE); |
264 | 283 |
|
265 | | - if (watcher != null && autoNotifyWatcher) { |
266 | | - // error intentionally ignored |
267 | | - watcher.update(); |
268 | | - } |
269 | | - |
270 | | - return true; |
| 284 | + return notifyWatcher(sec, ptype, rules, WatcherEx.UpdateType.UpdateForRemovePolicies); |
271 | 285 | } |
272 | 286 |
|
273 | 287 | /** |
@@ -307,7 +321,7 @@ boolean removeFilteredPolicy(String sec, String ptype, int fieldIndex, String... |
307 | 321 | if (watcher != null && autoNotifyWatcher) { |
308 | 322 | // error intentionally ignored |
309 | 323 | if (watcher instanceof WatcherEx) { |
310 | | - ((WatcherEx) watcher).updateForRemoveFilteredPolicy(fieldIndex, fieldValues); |
| 324 | + ((WatcherEx) watcher).updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, fieldValues); |
311 | 325 | } else { |
312 | 326 | watcher.update(); |
313 | 327 | } |
@@ -339,5 +353,4 @@ private void buildIncrementalRoleLinks( |
339 | 353 | buildIncrementalRoleLinks(operation, ptype, rules); |
340 | 354 | } |
341 | 355 | } |
342 | | - |
343 | 356 | } |
0 commit comments