1 package org.apache.commons.betwixt.registry;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24
25 import org.apache.commons.betwixt.ElementDescriptor;
26 import org.apache.commons.betwixt.XMLBeanInfo;
27 import org.apache.commons.betwixt.io.read.ElementMapping;
28 import org.apache.commons.betwixt.io.read.ReadContext;
29
30 /*** The default caching implementation.
31 * A hashmap is used.
32 *
33 * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
34 * @version $Id: DefaultXMLBeanInfoRegistry.java 438373 2006-08-30 05:17:21Z bayard $
35 */
36 public class DefaultXMLBeanInfoRegistry implements XMLBeanInfoRegistry, PolymorphicReferenceResolver {
37
38 /*** Used to associated <code>XMLBeanInfo</code>'s to classes */
39 private Map xmlBeanInfos = new HashMap();
40
41 /***
42 * Get <code>XMLBeanInfo</code> from cache.
43 *
44 * @param forThisClass the class for which to find a <code>XMLBeanInfo</code>
45 * @return cached <code>XMLBeanInfo</code> associated with given class
46 * or <code>null</code> if no <code>XMLBeanInfo</code> has been associated
47 */
48 public XMLBeanInfo get(Class forThisClass) {
49 return (XMLBeanInfo) xmlBeanInfos.get(forThisClass);
50 }
51
52 /***
53 * Put into cache
54 *
55 * @param forThisClass the class to cache the <code>XMLBeanInfo</code> for
56 * @param beanInfo the <code>XMLBeanInfo</code> to cache
57 */
58 public void put(Class forThisClass, XMLBeanInfo beanInfo) {
59 xmlBeanInfos.put(forThisClass, beanInfo);
60 }
61
62 /***
63 * Flush existing cached <code>XMLBeanInfo</code>'s.
64 */
65 public void flush() {
66 xmlBeanInfos.clear();
67 }
68
69 /***
70 * Checks all registered <code>XMLBeanInfo</code>'s for the
71 * first suitable match.
72 * If a suitable one is found, then the class of that info is used.
73 * @see org.apache.commons.betwixt.registry.PolymorphicReferenceResolver#resolveType(org.apache.commons.betwixt.io.read.ElementMapping, org.apache.commons.betwixt.io.read.ReadContext)
74 * @since 0.7
75 */
76 public Class resolveType(ElementMapping mapping, ReadContext context) {
77 Class result = null;
78 Collection cachedClasses = getCachedClasses();
79 ElementDescriptor mappedDescriptor = mapping.getDescriptor();
80 Class mappedType = mappedDescriptor.getSingularPropertyType();
81 if (mappedType == null) {
82 mappedType = mappedDescriptor.getPropertyType();
83 }
84 for (Iterator it = cachedClasses.iterator(); it.hasNext();) {
85 XMLBeanInfo beanInfo = get((Class)it.next());
86 ElementDescriptor typeDescriptor = beanInfo.getElementDescriptor();
87 boolean sameName = mapping.getName().equals(typeDescriptor.getQualifiedName());
88 if (sameName)
89 {
90
91 boolean compatibleClass = mappedType.isAssignableFrom(beanInfo.getBeanClass());
92 if (compatibleClass ) {
93 result = beanInfo.getBeanClass();
94 break;
95 }
96 }
97 }
98 return result;
99 }
100
101 /***
102 * Gets all classes that are cached in this registry.
103 *
104 * @return The classes
105 */
106 private Collection getCachedClasses()
107 {
108 return xmlBeanInfos.keySet();
109 }
110 }