KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > ResourceMapFactoryBean


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.context.support;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.springframework.beans.factory.config.PropertiesFactoryBean;
26 import org.springframework.context.ResourceLoaderAware;
27 import org.springframework.core.io.DefaultResourceLoader;
28 import org.springframework.core.io.Resource;
29 import org.springframework.core.io.ResourceLoader;
30
31 /**
32  * FactoryBean that creates a Map with String keys and Resource values from
33  * properties, interpreting passed-in String values as resource locations.
34  *
35  * <p>Extends PropertiesFactoryBean to inherit the capability of defining
36  * local properties and loading from properties files.
37  *
38  * <p>Implements the ResourceLoaderAware interface to automatically use
39  * the context ResourceLoader if running in an ApplicationContext.
40  * Uses DefaultResourceLoader else.
41  *
42  * @author Juergen Hoeller
43  * @author Keith Donald
44  * @since 1.0.2
45  * @see org.springframework.core.io.DefaultResourceLoader
46  */

47 public class ResourceMapFactoryBean extends PropertiesFactoryBean implements ResourceLoaderAware {
48
49     private String JavaDoc resourceBasePath = "";
50
51     private ResourceLoader resourceLoader = new DefaultResourceLoader();
52
53
54     /**
55      * Set a base path to prepend to each resource location value
56      * in the properties file.
57      * <p>E.g.: resourceBasePath="/images", value="/test.gif"
58      * -> location="/images/test.gif"
59      */

60     public void setResourceBasePath(String JavaDoc resourceBasePath) {
61         this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : "");
62     }
63
64     public void setResourceLoader(ResourceLoader resourceLoader) {
65         this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
66     }
67
68
69     public Class JavaDoc getObjectType() {
70         return Map JavaDoc.class;
71     }
72
73     /**
74      * Create the Map instance, populated with keys and Resource values.
75      */

76     protected Object JavaDoc createInstance() throws IOException JavaDoc {
77         Map JavaDoc resourceMap = new HashMap JavaDoc();
78         Properties JavaDoc props = mergeProperties();
79         for (Enumeration JavaDoc en = props.propertyNames(); en.hasMoreElements();) {
80             String JavaDoc key = (String JavaDoc) en.nextElement();
81             String JavaDoc location = props.getProperty(key);
82             resourceMap.put(key, getResource(location));
83         }
84         return resourceMap;
85     }
86
87     /**
88      * Fetch the Resource handle for the given location,
89      * prepeding the resource base path.
90      * @param location the resource location
91      * @return the Resource handle
92      * @see org.springframework.core.io.ResourceLoader#getResource(String)
93      */

94     protected Resource getResource(String JavaDoc location) {
95         return this.resourceLoader.getResource(this.resourceBasePath + location);
96     }
97
98 }
99
Popular Tags