1 package net.sourceforge.argval.utils.impl;
2
3
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.slf4j.Logger;
10
11 import net.sourceforge.argval.ArgumentValidation;
12 import net.sourceforge.argval.collection.CollectionUtil;
13 import net.sourceforge.argval.impl.ArgumentValidationImpl;
14 import net.sourceforge.argval.lang.SystemConstants;
15 import net.sourceforge.argval.utils.LocateFile;
16 import net.sourceforge.argval.utils.StringUtil;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class PropertyLocatedFile implements LocateFile {
44
45
46
47
48
49
50 private final String propKeyNameFile;
51
52 private final String configDirName;
53
54 private final String defaultFileName;
55
56 private String locationMessage;
57
58 private final Logger logger;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public PropertyLocatedFile(String propKeyNameFile, String configDirName, String defaultFileName, Logger logger) {
81 propKeyNameFile = StringUtil.polish(propKeyNameFile);
82 configDirName = StringUtil.polish(configDirName);
83 defaultFileName = StringUtil.polish(defaultFileName);
84
85 ArgumentValidation argVal = new ArgumentValidationImpl();
86 argVal.isValidStringMinLength("propKeyConfigurationFile", propKeyNameFile, 1);
87 argVal.isValidStringMinLength("defaultFileName", defaultFileName, 1);
88 argVal.isValidWhenNotNull("logger", logger);
89 if (argVal.containsIllegalArgument()) throw argVal.createIllegalArgumentException();
90
91 if (configDirName != null) {
92 File path = new File(System.getProperty("user.home"), configDirName);
93 if (! path.isDirectory()) {
94 configDirName = null;
95 logger.info("The application configuration directory '" + path + "' is not available.");
96 }
97 }
98
99 this.propKeyNameFile = propKeyNameFile;
100 this.configDirName = configDirName;
101 this.defaultFileName = defaultFileName;
102 this.logger = logger;
103 }
104
105
106 public File getFile() throws FileNotFoundException {
107 locationMessage = null;
108
109 final String configFileName = System.getProperty(propKeyNameFile);
110
111 final File theFile;
112 if (configFileName != null) {
113 locationMessage = "Loaded property through specified property-key '" + propKeyNameFile + "'.";
114 theFile = new File(configFileName);
115 if (! theFile.exists()) {
116 locationMessage = "Unable to load the file '" + configFileName
117 + "', specified through the property-key '" + propKeyNameFile + "'.";
118 }
119 }
120 else {
121
122 final List<File> fileLocationDirList = new ArrayList<File>();
123
124 final List<File> fileLocationList = new ArrayList<File>();
125
126
127 final File workDir = new File(System.getProperty(SystemConstants.USER_DIR));
128
129 fileLocationDirList.add(workDir);
130
131
132 final String homeDirName = System.getProperty(SystemConstants.USER_HOME);
133 final File homeDir = new File(homeDirName);
134
135 final File configDir = (configDirName != null) ? new File(homeDir, configDirName) : null;
136
137 if (configDir != null) {
138 fileLocationDirList.add(configDir);
139 }
140
141 for (File dir : fileLocationDirList) {
142 if (dir.exists()) {
143 List<String> fileNameList = getFileNameList();
144 for (String fileName : fileNameList) {
145 File newFile = new File(dir, fileName);
146 if (newFile.exists()) {
147 fileLocationList.add(newFile);
148 }
149 }
150 }
151 }
152
153 if (! fileLocationList.isEmpty()) {
154 logger.warn(CollectionUtil.toString(fileLocationList));
155 theFile = fileLocationList.get(0);
156 locationMessage = "Located file '" + theFile + "'.";
157 }
158 else {
159 theFile = null;
160 locationMessage = "No file with a name " + CollectionUtil.toString(getFileNameList())
161 + " was located in one of the directories " + CollectionUtil.toString(fileLocationDirList);
162 }
163 }
164
165 if (theFile == null || (! theFile.exists())) {
166 throw new FileNotFoundException(locationMessage);
167 }
168 return theFile;
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183 private List<String> getFileNameList() {
184 List<String> fileNameList = new ArrayList<String>();
185
186
187 int indexOfDot = defaultFileName.lastIndexOf('.');
188 if (indexOfDot > 0) {
189 String fileName = defaultFileName.substring(0, indexOfDot);
190 String fileExt = defaultFileName.substring(indexOfDot);
191 String userName = System.getProperty(SystemConstants.USER_NAME);
192 fileNameList.add(fileName + "_" + userName + fileExt);
193 fileNameList.add(fileName + "-" + userName + fileExt);
194 }
195 fileNameList.add(defaultFileName);
196 return fileNameList ;
197 }
198
199
200 public String getFileMessage() {
201 return locationMessage;
202 }
203
204 }