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