1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package edu.internet2.middleware.grouper.audit;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Set;
25
26 import edu.internet2.middleware.grouperClient.collections.MultiKey;
27
28 import edu.internet2.middleware.grouper.GroupType;
29 import edu.internet2.middleware.grouper.cache.GrouperCache;
30 import edu.internet2.middleware.grouper.misc.GrouperDAOFactory;
31 import edu.internet2.middleware.grouper.util.GrouperUtil;
32
33
34
35
36
37 public class AuditTypeFinder {
38
39
40
41 private static GrouperCache<MultiKey, AuditType> types = null;
42
43
44
45
46 private static Map<String, AuditType> typesById = null;
47
48
49
50
51 public static void clearCache() {
52 types = null;
53 typesById = null;
54 updatedBuiltinTypes = false;
55 AuditTypeBuiltin.internal_clearCache();
56 }
57
58
59
60
61
62
63
64
65
66 public static AuditType find(String auditCategory, String auditAction, boolean exceptionIfNotFound) {
67
68 MultiKey multiKey = new MultiKey(auditCategory, auditAction);
69
70
71 if (types != null && types.containsKey(multiKey)) {
72 return types.get(multiKey);
73 }
74
75 internal_updateKnownTypes();
76 if (types.containsKey(multiKey)) {
77 return types.get(multiKey);
78 }
79 if (exceptionIfNotFound) {
80 String msg = "Invalid audit type: category: " + auditCategory + ", action: " + auditAction;
81 throw new RuntimeException(msg);
82 }
83 return null;
84 }
85
86
87
88
89
90
91
92 public static Set<AuditType> findByCategory(String auditCategory) {
93
94 return GrouperDAOFactory.getFactory().getAuditType().findByCategory(auditCategory);
95 }
96
97
98
99
100
101
102
103
104 public static AuditType find(String auditTypeId, boolean exceptionIfNotFound) {
105
106
107 if (typesById != null && typesById.containsKey(auditTypeId)) {
108 return typesById.get(auditTypeId);
109 }
110
111 internal_updateKnownTypes();
112 if (typesById.containsKey(auditTypeId)) {
113 return typesById.get(auditTypeId);
114 }
115 if (exceptionIfNotFound) {
116 String msg = "Invalid audit type id: " + auditTypeId;
117 throw new RuntimeException(msg);
118 }
119 return null;
120 }
121
122
123
124
125 public synchronized static void internal_updateKnownTypes() {
126 Set<AuditType> auditTypes = GrouperDAOFactory.getFactory().getAuditType().findAll();
127 GrouperCache<MultiKey, AuditType> newTypes = new GrouperCache<MultiKey, AuditType>(
128 AuditTypeFinder.class.getName() + ".typeCache", 10000, false, 60*10, 60*10, false);
129
130 Map<String, AuditType> newTypesById = new HashMap<String, AuditType>();
131
132 for (AuditType auditType : GrouperUtil.nonNull(auditTypes)) {
133 newTypes.put(new MultiKey(auditType.getAuditCategory(), auditType.getActionName()), auditType);
134 newTypesById.put(auditType.getId(), auditType);
135 }
136
137
138 internal_updateBuiltinTypesOnce(newTypes, newTypesById);
139
140 types = newTypes;
141 typesById = newTypesById;
142 }
143
144
145
146
147 private static boolean updatedBuiltinTypes = false;
148
149
150
151
152
153
154 private static void internal_updateBuiltinTypesOnce(GrouperCache<MultiKey, AuditType> newTypes,
155 Map<String, AuditType> newTypesById) {
156 if (updatedBuiltinTypes && newTypes.getCache().getSize() != 0) {
157 return;
158 }
159
160 for (AuditTypeBuiltin auditTypeBuiltin : AuditTypeBuiltin.values()) {
161 internal_findOrReplaceAuditType(newTypes, newTypesById, auditTypeBuiltin.internal_auditTypeDefault());
162 }
163 }
164
165
166
167
168
169
170
171 private static void internal_findOrReplaceAuditType(GrouperCache<MultiKey, AuditType> newTypes,
172 Map<String, AuditType> newTypesById, AuditType auditType) {
173 MultiKey auditKey = new MultiKey(auditType.getAuditCategory(), auditType.getActionName());
174
175
176 if (!newTypes.containsKey(auditKey)) {
177 GrouperDAOFactory.getFactory().getAuditType().saveOrUpdate(auditType);
178 newTypes.put(auditKey, auditType);
179 newTypesById.put(auditType.getId(), auditType);
180 } else {
181
182 AuditType existingType = newTypes.get(auditKey);
183 if (!existingType.equalsDeep(auditType)) {
184
185
186 existingType.copyArgFieldIntoThis(auditType);
187 GrouperDAOFactory.getFactory().getAuditType().saveOrUpdate(existingType);
188 }
189 }
190
191 }
192
193
194 }