improved exception handling

This commit is contained in:
Reinhard Pointner 2007-12-27 16:48:32 +00:00
parent 69f549c3b7
commit a8cf61ef33
4 changed files with 47 additions and 29 deletions

View File

@ -20,10 +20,11 @@ public class Checksum {
private Long checksum = null; private Long checksum = null;
private State state = State.PENDING; private State state = State.PENDING;
private ChecksumComputationTask computationTask = null; private ChecksumComputationTask computationTask = null;
private String errorMessage = null;
public static enum State { public static enum State {
PENDING, INPROGRESS, READY; PENDING, INPROGRESS, READY, ERROR;
} }
@ -67,6 +68,21 @@ public class Checksum {
public synchronized void setChecksum(Long checksum) { public synchronized void setChecksum(Long checksum) {
this.checksum = checksum; this.checksum = checksum;
setState(State.READY); setState(State.READY);
computationTask = null;
}
public synchronized void setChecksumError(Exception exception) {
// get root cause
Throwable cause = exception;
while (cause.getCause() != null)
cause = cause.getCause();
errorMessage = cause.getMessage();
setState(State.ERROR);
computationTask = null; computationTask = null;
} }
@ -83,14 +99,15 @@ public class Checksum {
public Integer getProgress() { public Integer getProgress() {
switch (state) { if (state == State.INPROGRESS)
case PENDING: return computationTask.getProgress();
return 0;
case READY: return null;
return 100; }
default:
return computationTask.getProgress();
} public String getErrorMessage() {
return errorMessage;
} }
@ -123,6 +140,7 @@ public class Checksum {
setChecksum(computationTask.get()); setChecksum(computationTask.get());
} }
} catch (Exception e) { } catch (Exception e) {
setChecksumError(e);
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -141,16 +159,13 @@ public class Checksum {
@Override @Override
public String toString() { public String toString() {
switch (state) { if (state == State.READY)
case PENDING: return getChecksumString();
return state.toString();
case INPROGRESS: if (state == State.ERROR)
return state.toString(); return getErrorMessage();
case READY:
return getChecksumString(); return state.toString();
default:
return null;
}
} }
} }

View File

@ -45,13 +45,15 @@ public class ChecksumRow {
public State getState() { public State getState() {
HashSet<Long> checksums = new HashSet<Long>(); HashSet<Long> checksums = new HashSet<Long>();
boolean ready = true;
for (Checksum checksum : checksumMap.values()) { for (Checksum checksum : getChecksums()) {
if (checksum.getState() == Checksum.State.READY) if (checksum.getState() == Checksum.State.READY) {
checksums.add(checksum.getChecksum()); checksums.add(checksum.getChecksum());
else } else if (checksum.getState() == Checksum.State.ERROR) {
ready = false; return State.ERROR;
} else {
return State.UNKNOWN;
}
} }
if (checksums.size() > 1) { if (checksums.size() > 1) {
@ -65,10 +67,6 @@ public class ChecksumRow {
return State.WARNING; return State.WARNING;
} }
if (!ready) {
return State.UNKNOWN;
}
return State.OK; return State.OK;
} }

View File

@ -27,6 +27,8 @@ public class ChecksumTableCellRenderer extends TextTableCellRenderer {
setText(checksum.getChecksumString()); setText(checksum.getChecksumString());
} else if (checksum.getState() == Checksum.State.PENDING) { } else if (checksum.getState() == Checksum.State.PENDING) {
setText("Pending ..."); setText("Pending ...");
} else if (checksum.getState() == Checksum.State.ERROR) {
setText(checksum.getErrorMessage());
} else { } else {
return progressBarRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); return progressBarRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
} }

View File

@ -10,6 +10,7 @@ import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
@ -31,10 +32,12 @@ public abstract class FileTransferablePolicy extends TransferablePolicy {
protected List<File> getFilesFromTransferable(Transferable tr) { protected List<File> getFilesFromTransferable(Transferable tr) {
List<File> files = getFilesFromFileTransferable(tr); List<File> files = getFilesFromFileTransferable(tr);
// if there is no file transferable, look if there is a string transferable // if there is no file transferable, look if there is a string transferable that contains file uris
if (files == null) if (files == null)
files = getFilesFromStringTransferable(tr); files = getFilesFromStringTransferable(tr);
Collections.sort(files);
return files; return files;
} }