View Javadoc

1   package net.sf.crispy.properties;
2   
3   import java.io.InputStream;
4   import java.util.Properties;
5   
6   import net.sf.crispy.PropertiesLoader;
7   
8   /**
9    * Load file where the class is find.
10   * Example: class is: <code>test.crispy.Run</code> the file is in the same directory 
11   * 			<code>/test/crispy/example.properties</code>
12   * 			use internal: <code>clazz.getResourceAsStream(fileName);</code> 
13   * 
14   * @author Linke
15   *
16   */
17  public class ClassPropertiesLoader implements PropertiesLoader {
18  
19  	private String fileName = null;
20  	private Class clazz = null;
21  	
22  	public ClassPropertiesLoader(Class pvClass, String pvFileName) {
23  		fileName = pvFileName;
24  		clazz = pvClass;
25  	}
26  	
27  	public Properties load() {
28  		if (fileName == null) {
29  			throw new IllegalArgumentException("The file name for the ClassPropertiesLoader is null.");
30  		}
31  		if (clazz == null) {
32  			throw new IllegalArgumentException("The class for the ClassPropertiesLoader is null.");
33  		}
34  		
35  		InputStream lvInputStream = clazz.getResourceAsStream(fileName);
36  		Properties lvProperties = new Properties();
37  		try {
38  			lvProperties.load(lvInputStream);
39  		} catch (Exception e) {
40  			throw new PropertiesLoadException("Error in load-method:", e);
41  		}
42  		return lvProperties;
43  	}
44  
45  }