Issue #1138 Added null check to base ProtectedFile object since will be null when called recursively first time when constructing the base object. Organized static functions together from member functions.

Change-Id: I5dbef82932c9dc8623061361fad64fd0e8706546

Former-commit-id: ab120dfec6 [formerly fffe220e05] [formerly ab120dfec6 [formerly fffe220e05] [formerly c4398c09a4 [formerly edbff1f2b607d53e8247155a5526a7c21e40ac71]]]
Former-commit-id: c4398c09a4
Former-commit-id: 7d5c82ebfd [formerly a513b5830a]
Former-commit-id: 6c5d3637e1
This commit is contained in:
Max Schenkelberg 2012-09-11 14:04:14 -05:00
parent 346b652757
commit 3a0d62686a

View file

@ -25,13 +25,12 @@ import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import com.raytheon.uf.common.localization.LocalizationContext;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
@ -58,6 +57,7 @@ import com.raytheon.uf.common.status.UFStatus.Priority;
*/
public class ProtectedFiles {
private static transient IUFStatusHandler statusHandler = UFStatus
.getHandler(ProtectedFiles.class);
@ -83,23 +83,6 @@ public class ProtectedFiles {
private static Map<String, ProtectedFiles> sites = new HashMap<String, ProtectedFiles>();
private Set<String> protectedFiles;
private LocalizationLevel level;
private File file;
private long lastModifiedTime = 0;
private ProtectedFiles(LocalizationContext context) {
level = context.getLocalizationLevel();
file = PathManagerFactory.getPathManager()
.getLocalizationFile(context, PROTECTED_FILE).getFile();
protectedFiles = Collections
.synchronizedSortedSet(new TreeSet<String>());
reloadFile();
}
/**
* Add the list of protected files into protectedFiles.txt for the site with
* siteId
@ -131,7 +114,9 @@ public class ProtectedFiles {
ProtectedFiles site = getSiteLevelFiles(localizedSite);
protectedLevel = site.getProtectedLevelInternal(type, path);
}
if (protectedLevel == null) {
// base can be null when constructing the base ProtectedFile object and
// the ProtectedFiles constructor looks up it's localization file
if (protectedLevel == null && base != null) {
protectedLevel = base.getProtectedLevelInternal(type, path);
}
@ -154,27 +139,42 @@ public class ProtectedFiles {
return site;
}
private Set<String> protectedFiles;
private LocalizationLevel level;
private File file;
private long lastModifiedTime = 0;
private ProtectedFiles(LocalizationContext context) {
this.level = context.getLocalizationLevel();
this.protectedFiles = new LinkedHashSet<String>();
this.file = PathManagerFactory.getPathManager()
.getLocalizationFile(context, PROTECTED_FILE).getFile();
reloadFile();
}
/**
* Write the protectedFiles.txt file.
*/
private synchronized void writeProtectedFile() {
String str;
try {
file.createNewFile();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(level == LocalizationLevel.BASE ? BASE_HEADER : String
.format(LEVEL_HEADER, level.toString()));
.format(LEVEL_HEADER, level));
Iterator<String> iter = protectedFiles.iterator();
while (iter.hasNext()) {
str = iter.next();
out.write(str + "\n");
out.write(iter.next() + "\n");
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
statusHandler.handle(Priority.PROBLEM,
"Error writing protected file list", e);
}
}