Project

General

Profile

1
package eu.dnetlib.springutils.condbean;
2

    
3
import java.io.FileNotFoundException;
4
import java.io.IOException;
5
import java.util.Properties;
6

    
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.springframework.core.io.Resource;
10

    
11
/**
12
 * Load properties from a spring resource (classpath, filesystem, etc).
13
 * 
14
 * @author marko
15
 * 
16
 */
17
public class ResourcePropertyFinder implements PropertyFinder {
18

    
19
	/**
20
	 * logger. 
21
	 */
22
	private static final Log log = LogFactory.getLog(ResourcePropertyFinder.class); // NOPMD by marko on 11/24/08 5:02 PM
23

    
24
	/**
25
	 * properties are loaded from this resource.
26
	 */
27
	private Resource resource;
28

    
29
	/** 
30
	 * {@inheritDoc}
31
	 * @see eu.dnetlib.springutils.condbean.PropertyFinder#getProperty(java.lang.String)
32
	 */
33
	@Override
34
	public String getProperty(final String name) {
35
		if(resource == null)
36
			return null;
37
		try {
38
			final Properties props = new Properties();
39
			props.load(resource.getInputStream());
40

    
41
			final String res = props.getProperty(name);
42
			if (res != null && !res.isEmpty())
43
				return res;
44
		} catch(final FileNotFoundException e) {
45
				return null;
46
		} catch (final IOException e) {
47
			log.warn("cannot open properties", e);
48
		}
49
		return null;
50
	}
51

    
52
	public Resource getResource() {
53
		return resource;
54
	}
55

    
56
	public void setResource(final Resource resource) {
57
		this.resource = resource;
58
	}
59

    
60
}
(8-8/10)