001: /*
002: * Copyright 2002-2005 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.context.support;
018:
019: import java.io.IOException;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.Properties;
024:
025: import org.springframework.beans.factory.config.PropertiesFactoryBean;
026: import org.springframework.context.ResourceLoaderAware;
027: import org.springframework.core.io.DefaultResourceLoader;
028: import org.springframework.core.io.Resource;
029: import org.springframework.core.io.ResourceLoader;
030:
031: /**
032: * FactoryBean that creates a Map with String keys and Resource values from
033: * properties, interpreting passed-in String values as resource locations.
034: *
035: * <p>Extends PropertiesFactoryBean to inherit the capability of defining
036: * local properties and loading from properties files.
037: *
038: * <p>Implements the ResourceLoaderAware interface to automatically use
039: * the context ResourceLoader if running in an ApplicationContext.
040: * Uses DefaultResourceLoader else.
041: *
042: * @author Juergen Hoeller
043: * @author Keith Donald
044: * @since 1.0.2
045: * @see org.springframework.core.io.DefaultResourceLoader
046: */
047: public class ResourceMapFactoryBean extends PropertiesFactoryBean
048: implements ResourceLoaderAware {
049:
050: private String resourceBasePath = "";
051:
052: private ResourceLoader resourceLoader = new DefaultResourceLoader();
053:
054: /**
055: * Set a base path to prepend to each resource location value
056: * in the properties file.
057: * <p>E.g.: resourceBasePath="/images", value="/test.gif"
058: * -> location="/images/test.gif"
059: */
060: public void setResourceBasePath(String resourceBasePath) {
061: this .resourceBasePath = (resourceBasePath != null ? resourceBasePath
062: : "");
063: }
064:
065: public void setResourceLoader(ResourceLoader resourceLoader) {
066: this .resourceLoader = (resourceLoader != null ? resourceLoader
067: : new DefaultResourceLoader());
068: }
069:
070: public Class getObjectType() {
071: return Map.class;
072: }
073:
074: /**
075: * Create the Map instance, populated with keys and Resource values.
076: */
077: protected Object createInstance() throws IOException {
078: Map resourceMap = new HashMap();
079: Properties props = mergeProperties();
080: for (Enumeration en = props.propertyNames(); en
081: .hasMoreElements();) {
082: String key = (String) en.nextElement();
083: String location = props.getProperty(key);
084: resourceMap.put(key, getResource(location));
085: }
086: return resourceMap;
087: }
088:
089: /**
090: * Fetch the Resource handle for the given location,
091: * prepeding the resource base path.
092: * @param location the resource location
093: * @return the Resource handle
094: * @see org.springframework.core.io.ResourceLoader#getResource(String)
095: */
096: protected Resource getResource(String location) {
097: return this.resourceLoader.getResource(this.resourceBasePath
098: + location);
099: }
100:
101: }
|