1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.security.credential.criteria;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Properties;
26
27 import org.opensaml.xml.Configuration;
28 import org.opensaml.xml.security.Criteria;
29 import org.opensaml.xml.security.SecurityException;
30 import org.opensaml.xml.security.credential.Credential;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41 public final class EvaluableCredentialCriteriaRegistry {
42
43
44
45
46
47 public static final String DEFAULT_MAPPINGS_FILE = "/credential-criteria-registry.properties";
48
49
50 private static Map<Class<? extends Criteria>, Class<? extends EvaluableCredentialCriteria>> registry;
51
52
53 private static boolean initialized;
54
55
56 private EvaluableCredentialCriteriaRegistry() {
57 }
58
59
60
61
62
63
64
65
66
67
68 public static EvaluableCredentialCriteria getEvaluator(Criteria criteria) throws SecurityException {
69 Logger log = getLogger();
70 Class<? extends EvaluableCredentialCriteria> clazz = lookup(criteria.getClass());
71
72 if (clazz != null) {
73 log.debug("Registry located evaluable criteria class {} for criteria class {}", clazz.getName(), criteria
74 .getClass().getName());
75
76 try {
77
78 Constructor<? extends EvaluableCredentialCriteria> constructor = clazz
79 .getConstructor(new Class[] { criteria.getClass() });
80
81 return constructor.newInstance(new Object[] { criteria });
82
83 } catch (java.lang.SecurityException e) {
84 log.error("Error instantiating new EvaluableCredentialCriteria instance", e);
85 throw new SecurityException("Could not create new EvaluableCredentialCriteria", e);
86 } catch (NoSuchMethodException e) {
87 log.error("Error instantiating new EvaluableCredentialCriteria instance", e);
88 throw new SecurityException("Could not create new EvaluableCredentialCriteria", e);
89 } catch (IllegalArgumentException e) {
90 log.error("Error instantiating new EvaluableCredentialCriteria instance", e);
91 throw new SecurityException("Could not create new EvaluableCredentialCriteria", e);
92 } catch (InstantiationException e) {
93 log.error("Error instantiating new EvaluableCredentialCriteria instance", e);
94 throw new SecurityException("Could not create new EvaluableCredentialCriteria", e);
95 } catch (IllegalAccessException e) {
96 log.error("Error instantiating new EvaluableCredentialCriteria instance", e);
97 throw new SecurityException("Could not create new EvaluableCredentialCriteria", e);
98 } catch (InvocationTargetException e) {
99 log.error("Error instantiating new EvaluableCredentialCriteria instance", e);
100 throw new SecurityException("Could not create new EvaluableCredentialCriteria", e);
101 }
102
103 } else {
104 log.debug("Registry could not locate evaluable criteria for criteria class {}", criteria.getClass()
105 .getName());
106 }
107 return null;
108 }
109
110
111
112
113
114
115
116 public static synchronized Class<? extends EvaluableCredentialCriteria> lookup(Class<? extends Criteria> clazz) {
117 return registry.get(clazz);
118 }
119
120
121
122
123
124
125
126 public static synchronized void register(Class<? extends Criteria> criteriaClass,
127 Class<? extends EvaluableCredentialCriteria> evaluableClass) {
128 Logger log = getLogger();
129
130 log.debug("Registering class {} as evaluator for class {}", evaluableClass.getName(), criteriaClass.getName());
131
132 registry.put(criteriaClass, evaluableClass);
133
134 }
135
136
137
138
139
140
141 public static synchronized void deregister(Class<? extends Criteria> criteriaClass) {
142 Logger log = getLogger();
143
144 log.debug("Deregistering evaluator for class {}", criteriaClass.getName());
145 registry.remove(criteriaClass);
146 }
147
148
149
150
151 public static synchronized void clearRegistry() {
152 Logger log = getLogger();
153 log.debug("Clearing evaluable criteria registry");
154
155 registry.clear();
156 }
157
158
159
160
161
162
163 public static synchronized boolean isInitialized() {
164 return initialized;
165 }
166
167
168
169
170 public static synchronized void init() {
171 if (isInitialized()) {
172 return;
173 }
174
175 registry = new HashMap<Class<? extends Criteria>, Class<? extends EvaluableCredentialCriteria>>();
176
177 loadDefaultMappings();
178
179 initialized = true;
180 }
181
182
183
184
185 public static synchronized void loadDefaultMappings() {
186 Logger log = getLogger();
187 log.debug("Loading default evaluable credential criteria mappings");
188 InputStream inStream = EvaluableCredentialCriteriaRegistry.class.getResourceAsStream(DEFAULT_MAPPINGS_FILE);
189 if (inStream == null) {
190 log.error(String.format("Could not open resource stream from default mappings file '%s'",
191 DEFAULT_MAPPINGS_FILE));
192 return;
193 }
194
195 Properties defaultMappings = new Properties();
196 try {
197 defaultMappings.load(inStream);
198 } catch (IOException e) {
199 log.error("Error loading properties file from resource stream", e);
200 return;
201 }
202
203 loadMappings(defaultMappings);
204 }
205
206
207
208
209
210
211 @SuppressWarnings("unchecked")
212 public static synchronized void loadMappings(Properties mappings) {
213 Logger log = getLogger();
214 for (Object key : mappings.keySet()) {
215 if (!(key instanceof String)) {
216 log.error(String.format("Properties key was not an instance of String, was '%s', skipping...", key
217 .getClass().getName()));
218 continue;
219 }
220 String criteriaName = (String) key;
221 String evaluatorName = mappings.getProperty(criteriaName);
222
223 ClassLoader classLoader = Configuration.class.getClassLoader();
224 Class criteriaClass = null;
225 try {
226 criteriaClass = classLoader.loadClass(criteriaName);
227 } catch (ClassNotFoundException e) {
228 log.error(
229 String.format("Could not find criteria class name '%s', skipping registration", criteriaName),
230 e);
231 return;
232 }
233
234 Class evaluableClass = null;
235 try {
236 evaluableClass = classLoader.loadClass(evaluatorName);
237 } catch (ClassNotFoundException e) {
238 log.error(String
239 .format("Could not find evaluator class name '%s', skipping registration", criteriaName), e);
240 return;
241 }
242
243 register(criteriaClass, evaluableClass);
244 }
245
246 }
247
248
249
250
251
252
253 private static Logger getLogger() {
254 return LoggerFactory.getLogger(EvaluableCredentialCriteriaRegistry.class);
255 }
256
257 static {
258 init();
259 }
260 }