1
2
3
4
5
6
7
8
9
10
11
12
13 package net.sf.oval.ogn;
14
15 import java.util.Map;
16
17 import net.sf.oval.Validator;
18 import net.sf.oval.exception.ObjectGraphNavigatorNotAvailableException;
19 import net.sf.oval.internal.Log;
20 import net.sf.oval.internal.util.Assert;
21 import net.sf.oval.internal.util.ReflectionUtils;
22
23
24
25
26
27 public class ObjectGraphNavigatorRegistry
28 {
29 private static final Log LOG = Log.getLog(ObjectGraphNavigatorRegistry.class);
30
31 private final Map<String, ObjectGraphNavigator> cache = Validator.getCollectionFactory().createMap(2);
32
33 private ObjectGraphNavigator _initializeDefaultOGN(final String id)
34 {
35
36 if ("jxpath".equals(id) && ReflectionUtils.isClassPresent("org.apache.commons.jxpath.JXPathContext"))
37 return registerObjectGraphNavigator("jxpath", new ObjectGraphNavigatorJXPathImpl());
38
39 if ("".equals(id)) return registerObjectGraphNavigator("", new ObjectGraphNavigatorDefaultImpl());
40 return null;
41 }
42
43 public ObjectGraphNavigator getObjectGraphNavigator(final String id)
44 {
45 Assert.argumentNotNull("id", id);
46
47 ObjectGraphNavigator ogn = cache.get(id);
48
49 if (ogn == null) ogn = _initializeDefaultOGN(id);
50
51 if (ogn == null) throw new ObjectGraphNavigatorNotAvailableException(id);
52
53 return ogn;
54 }
55
56 public ObjectGraphNavigator registerObjectGraphNavigator(final String id, final ObjectGraphNavigator ogn)
57 throws IllegalArgumentException
58 {
59 Assert.argumentNotNull("id", id);
60 Assert.argumentNotNull("ogn", ogn);
61
62 LOG.info("Object Graph Navigator '{1}' registered: {2}", id, ogn);
63
64 cache.put(id, ogn);
65 return ogn;
66 }
67 }