commit 125d5ededae50f1b563e734fac8abc10ac5a6dbd Author: Karsten Loesing karsten.loesing@gmx.net Date: Wed Feb 1 11:26:40 2012 +0100
Handle exceptions when reading descriptors from disk. --- src/org/torproject/descriptor/DescriptorFile.java | 4 + .../descriptor/impl/DescriptorFileImpl.java | 12 ++++- .../impl/RelayOrBridgeDescriptorReaderImpl.java | 59 +++++++++----------- 3 files changed, 42 insertions(+), 33 deletions(-)
diff --git a/src/org/torproject/descriptor/DescriptorFile.java b/src/org/torproject/descriptor/DescriptorFile.java index 7a70e17..863ce92 100644 --- a/src/org/torproject/descriptor/DescriptorFile.java +++ b/src/org/torproject/descriptor/DescriptorFile.java @@ -21,5 +21,9 @@ public interface DescriptorFile {
/* Return the descriptors contained in the descriptor file. */ public List<Descriptor> getDescriptors(); + + /* Return the first exception that was thrown when reading this file or + * parsing its content, or null if no exception was thrown. */ + public Exception getException(); }
diff --git a/src/org/torproject/descriptor/impl/DescriptorFileImpl.java b/src/org/torproject/descriptor/impl/DescriptorFileImpl.java index 29002a5..787961b 100644 --- a/src/org/torproject/descriptor/impl/DescriptorFileImpl.java +++ b/src/org/torproject/descriptor/impl/DescriptorFileImpl.java @@ -3,6 +3,7 @@ package org.torproject.descriptor.impl;
import java.io.File; +import java.util.ArrayList; import java.util.List;
import org.torproject.descriptor.Descriptor; @@ -39,7 +40,16 @@ public class DescriptorFileImpl implements DescriptorFile { this.descriptors = descriptors; } public List<Descriptor> getDescriptors() { - return this.descriptors; + return this.descriptors == null ? new ArrayList<Descriptor>() : + new ArrayList<Descriptor>(this.descriptors); + } + + private Exception exception; + protected void setException(Exception exception) { + this.exception = exception; + } + public Exception getException() { + return this.exception; } }
diff --git a/src/org/torproject/descriptor/impl/RelayOrBridgeDescriptorReaderImpl.java b/src/org/torproject/descriptor/impl/RelayOrBridgeDescriptorReaderImpl.java index 3cfb18e..60c1e36 100644 --- a/src/org/torproject/descriptor/impl/RelayOrBridgeDescriptorReaderImpl.java +++ b/src/org/torproject/descriptor/impl/RelayOrBridgeDescriptorReaderImpl.java @@ -144,40 +144,35 @@ public class RelayOrBridgeDescriptorReaderImpl } private void readDescriptors() { for (File directory : this.directories) { - try { - Stack<File> files = new Stack<File>(); - files.add(directory); - while (!files.isEmpty()) { - File file = files.pop(); - if (file.isDirectory()) { - files.addAll(Arrays.asList(file.listFiles())); - } else { - String absolutePath = file.getAbsolutePath(); - long lastModifiedMillis = file.lastModified(); - this.newHistory.put(absolutePath, lastModifiedMillis); - if (this.oldHistory.containsKey(absolutePath) && - this.oldHistory.get(absolutePath) == - lastModifiedMillis) { - continue; - } - try { - List<Descriptor> parsedDescriptors = this.readFile(file); - DescriptorFileImpl descriptorFile = - new DescriptorFileImpl(); - descriptorFile.setDirectory(directory); - descriptorFile.setFile(file); - descriptorFile.setLastModified(lastModifiedMillis); - descriptorFile.setDescriptors(parsedDescriptors); - this.descriptorQueue.add(descriptorFile); - } catch (DescriptorParseException e) { - /* TODO Handle me. */ - } + Stack<File> files = new Stack<File>(); + files.add(directory); + boolean abortReading = false; + while (!abortReading && !files.isEmpty()) { + File file = files.pop(); + if (file.isDirectory()) { + files.addAll(Arrays.asList(file.listFiles())); + } else { + String absolutePath = file.getAbsolutePath(); + long lastModifiedMillis = file.lastModified(); + this.newHistory.put(absolutePath, lastModifiedMillis); + if (this.oldHistory.containsKey(absolutePath) && + this.oldHistory.get(absolutePath) == lastModifiedMillis) { + continue; } + DescriptorFileImpl descriptorFile = new DescriptorFileImpl(); + try { + descriptorFile.setDirectory(directory); + descriptorFile.setFile(file); + descriptorFile.setLastModified(lastModifiedMillis); + descriptorFile.setDescriptors(this.readFile(file)); + } catch (DescriptorParseException e) { + descriptorFile.setException(e); + } catch (IOException e) { + descriptorFile.setException(e); + abortReading = true; + } + this.descriptorQueue.add(descriptorFile); } - } catch (IOException e) { - System.err.println("Error while reading descriptors in '" - + directory.getAbsolutePath() + "'."); - /* TODO Handle this exception somehow. */ } } this.descriptorQueue.setOutOfDescriptors();
tor-commits@lists.torproject.org