001: package org.apache.commons.betwixt.registry;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.commons.betwixt.ElementDescriptor;
026: import org.apache.commons.betwixt.XMLBeanInfo;
027: import org.apache.commons.betwixt.io.read.ElementMapping;
028: import org.apache.commons.betwixt.io.read.ReadContext;
029:
030: /** The default caching implementation.
031: * A hashmap is used.
032: *
033: * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
034: * @version $Id: DefaultXMLBeanInfoRegistry.java 438373 2006-08-30 05:17:21Z bayard $
035: */
036: public class DefaultXMLBeanInfoRegistry implements XMLBeanInfoRegistry,
037: PolymorphicReferenceResolver {
038:
039: /** Used to associated <code>XMLBeanInfo</code>'s to classes */
040: private Map xmlBeanInfos = new HashMap();
041:
042: /**
043: * Get <code>XMLBeanInfo</code> from cache.
044: *
045: * @param forThisClass the class for which to find a <code>XMLBeanInfo</code>
046: * @return cached <code>XMLBeanInfo</code> associated with given class
047: * or <code>null</code> if no <code>XMLBeanInfo</code> has been associated
048: */
049: public XMLBeanInfo get(Class forThisClass) {
050: return (XMLBeanInfo) xmlBeanInfos.get(forThisClass);
051: }
052:
053: /**
054: * Put into cache
055: *
056: * @param forThisClass the class to cache the <code>XMLBeanInfo</code> for
057: * @param beanInfo the <code>XMLBeanInfo</code> to cache
058: */
059: public void put(Class forThisClass, XMLBeanInfo beanInfo) {
060: xmlBeanInfos.put(forThisClass, beanInfo);
061: }
062:
063: /**
064: * Flush existing cached <code>XMLBeanInfo</code>'s.
065: */
066: public void flush() {
067: xmlBeanInfos.clear();
068: }
069:
070: /**
071: * Checks all registered <code>XMLBeanInfo</code>'s for the
072: * first suitable match.
073: * If a suitable one is found, then the class of that info is used.
074: * @see org.apache.commons.betwixt.registry.PolymorphicReferenceResolver#resolveType(org.apache.commons.betwixt.io.read.ElementMapping, org.apache.commons.betwixt.io.read.ReadContext)
075: * @since 0.7
076: */
077: public Class resolveType(ElementMapping mapping, ReadContext context) {
078: Class result = null;
079: Collection cachedClasses = getCachedClasses();
080: ElementDescriptor mappedDescriptor = mapping.getDescriptor();
081: Class mappedType = mappedDescriptor.getSingularPropertyType();
082: if (mappedType == null) {
083: mappedType = mappedDescriptor.getPropertyType();
084: }
085: for (Iterator it = cachedClasses.iterator(); it.hasNext();) {
086: XMLBeanInfo beanInfo = get((Class) it.next());
087: ElementDescriptor typeDescriptor = beanInfo
088: .getElementDescriptor();
089: boolean sameName = mapping.getName().equals(
090: typeDescriptor.getQualifiedName());
091: if (sameName) {
092:
093: boolean compatibleClass = mappedType
094: .isAssignableFrom(beanInfo.getBeanClass());
095: if (compatibleClass) {
096: result = beanInfo.getBeanClass();
097: break;
098: }
099: }
100: }
101: return result;
102: }
103:
104: /**
105: * Gets all classes that are cached in this registry.
106: *
107: * @return The classes
108: */
109: private Collection getCachedClasses() {
110: return xmlBeanInfos.keySet();
111: }
112: }
|