KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > optional > PropertiesRegisterNut


1 package jfun.yan.xml.nuts.optional;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import jfun.util.Misc;
9 import jfun.yan.util.Utils;
10 import jfun.yan.util.resource.ClassLoader2ResourceLoader;
11 import jfun.yan.util.resource.OrResourceLoader;
12 import jfun.yan.util.resource.ResourceLoaders;
13 /**
14  * This class dynamically registers components using the information
15  * read from a property file.
16  * <p>
17  * @author Ben Yu
18  * Dec 22, 2005 12:32:01 AM
19  */

20 public class PropertiesRegisterNut extends AbstractLoopRegisterNut {
21   private String JavaDoc resource;
22   private File JavaDoc file;
23   private String JavaDoc classpath;
24   
25   public String JavaDoc getClasspath() {
26     return classpath;
27   }
28   public void setClasspath(String JavaDoc classpath) {
29     this.classpath = classpath;
30   }
31   public File JavaDoc getFile() {
32     return file;
33   }
34   public void setFile(File JavaDoc file) {
35     checkDuplicate("resource", resource);
36     this.file = file;
37   }
38   public String JavaDoc getResource() {
39     return resource;
40   }
41   public void setResource(String JavaDoc resource) {
42     checkDuplicate("file", file);
43     this.resource = resource;
44   }
45   protected Properties JavaDoc readProperties()
46   throws IOException JavaDoc{
47     if(resource!=null){
48       return Utils.loadResourceProperties(
49           ResourceLoaders.or(
50               this.getNutEnvironment().getResourceLoader(),
51               getNutClassLoader(classpath)
52           ), resource);
53     }
54     else if(file!=null){
55       return Misc.loadPropertiesFile(file);
56     }
57     else{
58       throw raise("either file or resource has to be specified.");
59     }
60   }
61   public void eval()
62   throws IOException JavaDoc{
63     final Properties JavaDoc props = readProperties();
64     for(Iterator JavaDoc it= props.keySet().iterator(); it.hasNext();){
65       final Object JavaDoc key = it.next();
66       final Object JavaDoc val = props.get(key);
67       loop(key, key, val);
68     }
69   }
70 }
71
Popular Tags