Changeset 12 for src/main/java
- Timestamp:
- 04/28/20 12:56:50 (5 years ago)
- Location:
- src/main/java/geniusweb/profilesserver
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/geniusweb/profilesserver/AutoUpdatingProfilesFactory.java
r8 r12 2 2 3 3 import java.io.File; 4 import java.io.FileWriter; 4 5 import java.io.IOException; 6 import java.io.Writer; 5 7 import java.net.URISyntaxException; 6 8 import java.nio.file.Path; … … 63 65 } 64 66 67 @Override 68 public synchronized void putProfile(Profile profile) throws IOException { 69 if (!available.containsKey(profile.getDomain())) { 70 throw new IOException("profile's domain does not exist"); 71 } 72 String domainname = profile.getDomain().getName(); 73 String profilename = profile.getName(); 74 if (!profilename.matches("[a-zA-Z0-9-]+")) 75 throw new IOException("illegal profile name " + profilename 76 + ", only letters, numbers and - allowed"); 77 File file = Paths 78 .get(rootdir.toString(), domainname, profilename + JSON) 79 .toFile(); 80 if (file.exists()) { 81 String msg = fileCheck(file, false, true); 82 if (msg != null) 83 throw new IOException(msg); 84 } 85 86 // CHECK utf-8 compatibility? Do we need that anyway? 87 try (Writer out = new FileWriter(file, false)) { 88 out.write(Jackson.instance().writeValueAsString(profile)); 89 } 90 // trigger the update immediately, avoid wait for FileChecker. 91 super.add(profile); 92 93 } 94 65 95 /** 66 96 * Called when some file that may be relevant has changed. Synchronized to … … 267 297 268 298 /** 269 * check and report basic problems with a file. 299 * Silent check and report basic problems with a file. Silent in the sense 300 * that errors are logged but cause no exception. 270 301 * 271 302 * @param file the file to check … … 275 306 */ 276 307 private boolean basicFileCheck(File file, boolean shouldbeDirectory) { 308 String msg = fileCheck(file, shouldbeDirectory, false); 309 if (msg != null) { 310 log.log(Level.WARNING, msg); 311 return false; 312 } 313 return true; 314 } 315 316 /** 317 * 318 * @param file the file to check 319 * @param shouldbeDirectory true if expected a directory 320 * @param shouldbeWritable true if expected to be writable 321 * @return null if file is there, is file/directory, allows reading and 322 * allows writing (if specified). 323 */ 324 private String fileCheck(File file, boolean shouldbeDirectory, 325 boolean shouldbeWritable) { 277 326 if (!file.exists()) { 278 log.log(Level.WARNING, file + " is missing"); 279 return false; 327 return file + " is missing"; 280 328 } 281 329 if (!file.canRead()) { 282 log.log(Level.WARNING, 283 "directory " + file + "does not allow reading "); 284 return false; 330 return "directory " + file + "does not allow reading "; 285 331 } 286 332 if (shouldbeDirectory) { 287 333 if (!file.isDirectory()) { 288 log.log(Level.WARNING, file + " is not a directory"); 289 return false; 334 return file + " is not a directory"; 290 335 } 291 336 } else { 292 337 if (!file.isFile()) { 293 log.log(Level.WARNING, file + " is not a file"); 294 return false; 295 } 296 } 297 return true; 338 return file + " is not a file"; 339 } 340 } 341 if (shouldbeWritable && !file.canWrite()) { 342 return file + "is read-only"; 343 } 344 return null; 345 298 346 } 299 347 -
src/main/java/geniusweb/profilesserver/DefaultProfilesFactory.java
r7 r12 1 1 package geniusweb.profilesserver; 2 2 3 import java.io.IOException; 3 4 import java.util.Collections; 4 5 import java.util.LinkedList; … … 84 85 } 85 86 87 @Override 88 public synchronized void putProfile(Profile profile) throws IOException { 89 if (!available.containsKey(profile.getDomain())) { 90 throw new IOException("profile's domain does not exist"); 91 } 92 add(profile); 93 } 94 86 95 /**************** support for updating ***************/ 87 96 /** -
src/main/java/geniusweb/profilesserver/ProfilesFactory.java
r1 r12 1 1 package geniusweb.profilesserver; 2 2 3 import java.io.IOException; 3 4 import java.util.List; 4 5 … … 40 41 List<Profile> getProfiles(String domainname); 41 42 43 /** 44 * Permanently (over)write a profile to the repository (not just in memory). 45 * 46 * @param profile the profile to be written. The domain file must exist and 47 * match. 48 * @throws IOException if the profile does not meet the requirements. 49 */ 50 void putProfile(Profile profile) throws IOException; 51 42 52 }
Note:
See TracChangeset
for help on using the changeset viewer.