001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import java.util.Iterator;
020: import java.util.LinkedHashMap;
021: import java.util.Map;
022:
023: import org.springframework.beans.BeanUtils;
024: import org.springframework.beans.TypeConverter;
025: import org.springframework.core.GenericCollectionTypeResolver;
026: import org.springframework.core.JdkVersion;
027:
028: /**
029: * Simple factory for shared Map instances. Allows for central setup
030: * of Maps via the "map" element in XML bean definitions.
031: *
032: * @author Juergen Hoeller
033: * @since 09.12.2003
034: * @see SetFactoryBean
035: * @see ListFactoryBean
036: */
037: public class MapFactoryBean extends AbstractFactoryBean {
038:
039: private Map sourceMap;
040:
041: private Class targetMapClass;
042:
043: /**
044: * Set the source Map, typically populated via XML "map" elements.
045: */
046: public void setSourceMap(Map sourceMap) {
047: this .sourceMap = sourceMap;
048: }
049:
050: /**
051: * Set the class to use for the target Map. Can be populated with a fully
052: * qualified class name when defined in a Spring application context.
053: * <p>Default is a linked HashMap, keeping the registration order.
054: * @see java.util.LinkedHashMap
055: */
056: public void setTargetMapClass(Class targetMapClass) {
057: if (targetMapClass == null) {
058: throw new IllegalArgumentException(
059: "'targetMapClass' must not be null");
060: }
061: if (!Map.class.isAssignableFrom(targetMapClass)) {
062: throw new IllegalArgumentException(
063: "'targetMapClass' must implement [java.util.Map]");
064: }
065: this .targetMapClass = targetMapClass;
066: }
067:
068: public Class getObjectType() {
069: return Map.class;
070: }
071:
072: protected Object createInstance() {
073: if (this .sourceMap == null) {
074: throw new IllegalArgumentException(
075: "'sourceMap' is required");
076: }
077: Map result = null;
078: if (this .targetMapClass != null) {
079: result = (Map) BeanUtils
080: .instantiateClass(this .targetMapClass);
081: } else {
082: result = new LinkedHashMap(this .sourceMap.size());
083: }
084: Class keyType = null;
085: Class valueType = null;
086: if (this .targetMapClass != null && JdkVersion.isAtLeastJava15()) {
087: keyType = GenericCollectionTypeResolver
088: .getMapKeyType(this .targetMapClass);
089: valueType = GenericCollectionTypeResolver
090: .getMapValueType(this .targetMapClass);
091: }
092: if (keyType != null || valueType != null) {
093: TypeConverter converter = getBeanTypeConverter();
094: for (Iterator it = this .sourceMap.entrySet().iterator(); it
095: .hasNext();) {
096: Map.Entry entry = (Map.Entry) it.next();
097: Object convertedKey = converter.convertIfNecessary(
098: entry.getKey(), keyType);
099: Object convertedValue = converter.convertIfNecessary(
100: entry.getValue(), valueType);
101: result.put(convertedKey, convertedValue);
102: }
103: } else {
104: result.putAll(this.sourceMap);
105: }
106: return result;
107: }
108:
109: }
|