|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.handler; |
|
8 |
| |
|
9 |
| import java.io.File; |
|
10 |
| import java.io.FileInputStream; |
|
11 |
| import java.io.FileWriter; |
|
12 |
| import java.io.InputStream; |
|
13 |
| import java.util.Enumeration; |
|
14 |
| import java.util.Properties; |
|
15 |
| |
|
16 |
| import org.jconfig.Configuration; |
|
17 |
| import org.jconfig.ConfigurationManagerException; |
|
18 |
| import org.jconfig.DefaultConfiguration; |
|
19 |
| import org.jconfig.error.ErrorReporter; |
|
20 |
| import org.jconfig.utils.ResourceLocator; |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public class PropertiesFileHandler extends AbstractHandler { |
|
33 |
| |
|
34 |
| private File file; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
1
| public PropertiesFileHandler() {
|
|
40 |
| } |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
2
| public PropertiesFileHandler(String filename) {
|
|
47 |
2
| this(new File(filename));
|
|
48 |
| } |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
3
| public PropertiesFileHandler(File file) {
|
|
55 |
3
| this.file = file;
|
|
56 |
3
| addFileListener(this);
|
|
57 |
| } |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
1
| public void setFile(File file) {
|
|
65 |
1
| this.file = file;
|
|
66 |
| } |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
3
| public synchronized Configuration load(String configName) throws ConfigurationManagerException {
|
|
71 |
3
| return loadPropertiesFile(file,configName);
|
|
72 |
| } |
|
73 |
| |
|
74 |
3
| private Configuration loadPropertiesFile(File file,String configName) throws ConfigurationManagerException {
|
|
75 |
3
| Configuration config = new DefaultConfiguration(configName);
|
|
76 |
3
| if ( file == null ) {
|
|
77 |
0
| ErrorReporter.getErrorHandler().reportError("The file is NULL");
|
|
78 |
0
| throw new ConfigurationManagerException("The file is NULL");
|
|
79 |
| } |
|
80 |
3
| try {
|
|
81 |
3
| InputStream inputStream = null;
|
|
82 |
3
| if(file.exists()) {
|
|
83 |
1
| inputStream = new FileInputStream(file);
|
|
84 |
| } else { |
|
85 |
2
| ResourceLocator locator = new ResourceLocator(file.getName());
|
|
86 |
2
| inputStream = locator.getInputStream();
|
|
87 |
| } |
|
88 |
3
| if ( inputStream == null ) {
|
|
89 |
0
| ErrorReporter.getErrorHandler().reportError("Cannot find the properties file");
|
|
90 |
0
| throw new ConfigurationManagerException("Cannot find the properties file");
|
|
91 |
| } |
|
92 |
3
| Properties myProperties = new Properties();
|
|
93 |
3
| myProperties.load(inputStream);
|
|
94 |
3
| Enumeration propertyNames = myProperties.propertyNames();
|
|
95 |
3
| while (propertyNames.hasMoreElements()) {
|
|
96 |
29
| String name = (String) propertyNames.nextElement();
|
|
97 |
29
| String value = myProperties.getProperty(name);
|
|
98 |
29
| config.setProperty(name, value);
|
|
99 |
| } |
|
100 |
| } catch (Exception e) { |
|
101 |
0
| ErrorReporter.getErrorHandler().reportError("The file cannot be loaded",e);
|
|
102 |
0
| throw new ConfigurationManagerException("The file cannot be loaded:"+e.getMessage());
|
|
103 |
| } |
|
104 |
3
| config.resetCreated();
|
|
105 |
3
| return config;
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
0
| public void store(Configuration configuration) throws ConfigurationManagerException {
|
|
112 |
| |
|
113 |
0
| String[] categories = configuration.getCategoryNames();
|
|
114 |
0
| try {
|
|
115 |
| |
|
116 |
0
| FileWriter fw = new FileWriter(file);
|
|
117 |
| |
|
118 |
0
| fw.write("#\n");
|
|
119 |
0
| fw.write("# automatically generated properties file\n");
|
|
120 |
0
| fw.write("#\n");
|
|
121 |
| |
|
122 |
0
| for ( int i = 0; i < categories.length;i++) {
|
|
123 |
| |
|
124 |
0
| fw.write("#\n");
|
|
125 |
0
| fw.write("# category: " + categories[i] + "\n");
|
|
126 |
0
| fw.write("#\n");
|
|
127 |
| |
|
128 |
0
| String[] propNames = configuration.getPropertyNames(categories[i]);
|
|
129 |
| |
|
130 |
0
| for ( int j = 0; j < propNames.length;j++) {
|
|
131 |
| |
|
132 |
| |
|
133 |
0
| fw.write(categories[i] + "." + propNames[j] + "=" + configuration.getProperty(propNames[j],"",categories[i]) + "\n");
|
|
134 |
| } |
|
135 |
| } |
|
136 |
| |
|
137 |
0
| fw.close();
|
|
138 |
| } catch (Exception e) { |
|
139 |
0
| ErrorReporter.getErrorHandler().reportError("The file cannot be saved",e);
|
|
140 |
0
| throw new ConfigurationManagerException("The file cannot be saved");
|
|
141 |
| } |
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
6
| public File getFile() {
|
|
148 |
6
| return file;
|
|
149 |
| } |
|
150 |
| } |