mirror of
https://github.com/correl/mage.git
synced 2024-11-15 11:09:30 +00:00
Fixed a problem that symbols were not redownloaded if a 0 size file was created because the target file to download was not available before.
This commit is contained in:
parent
ecedc360e9
commit
93ac094470
2 changed files with 76 additions and 53 deletions
|
@ -3,10 +3,8 @@
|
|||
*
|
||||
* Created on 25.08.2010
|
||||
*/
|
||||
|
||||
package org.mage.plugins.card.dl;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -21,7 +19,6 @@ import org.mage.plugins.card.dl.beans.properties.Property;
|
|||
import org.mage.plugins.card.dl.lm.AbstractLaternaBean;
|
||||
import org.mage.plugins.card.utils.CardImageUtils;
|
||||
|
||||
|
||||
/**
|
||||
* The class DownloadJob.
|
||||
*
|
||||
|
@ -29,7 +26,9 @@ import org.mage.plugins.card.utils.CardImageUtils;
|
|||
* @author Clemens Koza
|
||||
*/
|
||||
public class DownloadJob extends AbstractLaternaBean {
|
||||
|
||||
public static enum State {
|
||||
|
||||
NEW, WORKING, FINISHED, ABORTED;
|
||||
}
|
||||
|
||||
|
@ -48,7 +47,9 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the job's state. If the state is {@link State#ABORTED}, it instead sets the error to "ABORTED"
|
||||
* Sets the job's state. If the state is {@link State#ABORTED}, it instead
|
||||
* sets the error to "ABORTED"
|
||||
*
|
||||
* @param state
|
||||
*/
|
||||
public void setState(State state) {
|
||||
|
@ -60,8 +61,9 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the job's state to {@link State#ABORTED} and the error message to the given message. Logs a warning
|
||||
* with the given message.
|
||||
* Sets the job's state to {@link State#ABORTED} and the error message to
|
||||
* the given message. Logs a warning with the given message.
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
public void setError(String message) {
|
||||
|
@ -69,8 +71,9 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the job's state to {@link State#ABORTED} and the error to the given exception. Logs a warning with the
|
||||
* given exception.
|
||||
* Sets the job's state to {@link State#ABORTED} and the error to the given
|
||||
* exception. Logs a warning with the given exception.
|
||||
*
|
||||
* @param error
|
||||
*/
|
||||
public void setError(Exception error) {
|
||||
|
@ -78,8 +81,9 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the job's state to {@link State#ABORTED} and the error to the given exception. Logs a warning with the
|
||||
* given message and exception.
|
||||
* Sets the job's state to {@link State#ABORTED} and the error to the given
|
||||
* exception. Logs a warning with the given message and exception.
|
||||
*
|
||||
* @param message
|
||||
* @param error
|
||||
*/
|
||||
|
@ -97,6 +101,7 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
|
||||
/**
|
||||
* Sets the job's message.
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
public void setMessage(String message) {
|
||||
|
@ -119,7 +124,6 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
return message.getValue();
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -163,7 +167,7 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return proxy != null ? proxy.type().toString()+" " :"" + url;
|
||||
return proxy != null ? proxy.type().toString() + " " : "" + url;
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -192,7 +196,7 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return proxy != null ? proxy.type().toString()+" " :"" + url;
|
||||
return proxy != null ? proxy.type().toString() + " " : "" + url;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -213,6 +217,14 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
return new FileOutputStream(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() throws IOException {
|
||||
if (file.isFile()) {
|
||||
return file.length() > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return file.isFile();
|
||||
|
@ -228,16 +240,20 @@ public class DownloadJob extends AbstractLaternaBean {
|
|||
}
|
||||
|
||||
public interface Source {
|
||||
|
||||
InputStream open() throws IOException;
|
||||
|
||||
int length() throws IOException;
|
||||
}
|
||||
|
||||
public interface Destination {
|
||||
|
||||
OutputStream open() throws IOException;
|
||||
|
||||
boolean exists() throws IOException;
|
||||
|
||||
boolean isValid() throws IOException;
|
||||
|
||||
void delete() throws IOException;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
*
|
||||
* Created on 25.08.2010
|
||||
*/
|
||||
|
||||
package org.mage.plugins.card.dl;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
|
@ -29,7 +28,6 @@ import org.mage.plugins.card.dl.DownloadJob.Source;
|
|||
import org.mage.plugins.card.dl.DownloadJob.State;
|
||||
import org.mage.plugins.card.dl.lm.AbstractLaternaBean;
|
||||
|
||||
|
||||
/**
|
||||
* The class Downloader.
|
||||
*
|
||||
|
@ -49,7 +47,7 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
|
|||
public Downloader() {
|
||||
PoolFiberFactory f = new PoolFiberFactory(pool);
|
||||
//subscribe multiple fibers for parallel execution
|
||||
for(int i = 0, numThreads = 10; i < numThreads; i++) {
|
||||
for (int i = 0, numThreads = 10; i < numThreads; i++) {
|
||||
Fiber fiber = f.create();
|
||||
fiber.start();
|
||||
fibers.add(fiber);
|
||||
|
@ -59,15 +57,15 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
|
|||
|
||||
@Override
|
||||
public void dispose() {
|
||||
for(DownloadJob j:getJobs()) {
|
||||
switch(j.getState()) {
|
||||
for (DownloadJob j : getJobs()) {
|
||||
switch (j.getState()) {
|
||||
case NEW:
|
||||
case WORKING:
|
||||
j.setState(State.ABORTED);
|
||||
}
|
||||
}
|
||||
|
||||
for(Fiber f:fibers) {
|
||||
for (Fiber f : fibers) {
|
||||
f.dispose();
|
||||
}
|
||||
pool.shutdown();
|
||||
|
@ -84,10 +82,10 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
|
|||
}
|
||||
|
||||
public void add(DownloadJob job) {
|
||||
if(job.getState() == State.WORKING) {
|
||||
if (job.getState() == State.WORKING) {
|
||||
throw new IllegalArgumentException("Job already running");
|
||||
}
|
||||
if(job.getState() == State.FINISHED) {
|
||||
if (job.getState() == State.FINISHED) {
|
||||
throw new IllegalArgumentException("Job already finished");
|
||||
}
|
||||
job.setState(State.NEW);
|
||||
|
@ -100,15 +98,17 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Performs the download job: Transfers data from {@link Source} to {@link Destination} and updates the
|
||||
* download job's state to reflect the progress.
|
||||
* Performs the download job: Transfers data from {@link Source} to
|
||||
* {@link Destination} and updates the download job's state to reflect the
|
||||
* progress.
|
||||
*/
|
||||
private class DownloadCallback implements Callback<DownloadJob> {
|
||||
|
||||
@Override
|
||||
public void onMessage(DownloadJob job) {
|
||||
//the job won't be processed by multiple threads
|
||||
synchronized(job) {
|
||||
if(job.getState() != State.NEW) {
|
||||
synchronized (job) {
|
||||
if (job.getState() != State.NEW) {
|
||||
return;
|
||||
}
|
||||
job.setState(State.WORKING);
|
||||
|
@ -118,10 +118,17 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
|
|||
Destination dst = job.getDestination();
|
||||
BoundedRangeModel progress = job.getProgress();
|
||||
|
||||
if(dst.exists()) {
|
||||
if (dst.isValid()) {
|
||||
progress.setMaximum(1);
|
||||
progress.setValue(1);
|
||||
} else {
|
||||
if (dst.exists()) {
|
||||
try {
|
||||
dst.delete();
|
||||
} catch (IOException ex1) {
|
||||
logger.warn("While deleting not valid file", ex1);
|
||||
}
|
||||
}
|
||||
progress.setMaximum(src.length());
|
||||
InputStream is = new BufferedInputStream(src.open());
|
||||
try {
|
||||
|
@ -129,45 +136,45 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
|
|||
try {
|
||||
byte[] buf = new byte[8 * 1024];
|
||||
int total = 0;
|
||||
for(int len; (len = is.read(buf)) != -1;) {
|
||||
if(job.getState() == State.ABORTED) {
|
||||
for (int len; (len = is.read(buf)) != -1;) {
|
||||
if (job.getState() == State.ABORTED) {
|
||||
throw new IOException("Job was aborted");
|
||||
}
|
||||
progress.setValue(total += len);
|
||||
os.write(buf, 0, len);
|
||||
}
|
||||
} catch(IOException ex) {
|
||||
} catch (IOException ex) {
|
||||
try {
|
||||
dst.delete();
|
||||
} catch(IOException ex1) {
|
||||
} catch (IOException ex1) {
|
||||
logger.warn("While deleting", ex1);
|
||||
}
|
||||
throw ex;
|
||||
} finally {
|
||||
try {
|
||||
os.close();
|
||||
} catch(IOException ex) {
|
||||
} catch (IOException ex) {
|
||||
logger.warn("While closing", ex);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch(IOException ex) {
|
||||
} catch (IOException ex) {
|
||||
logger.warn("While closing", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
job.setState(State.FINISHED);
|
||||
} catch(ConnectException ex) {
|
||||
} catch (ConnectException ex) {
|
||||
String message;
|
||||
if (ex.getMessage() != null) {
|
||||
message = ex.getMessage();
|
||||
} else {
|
||||
message = "Unknown error";
|
||||
}
|
||||
logger.warn("Error resource download " + job.getName() +" from "+ job.getSource().toString() + ": " + message);
|
||||
} catch(IOException ex) {
|
||||
logger.warn("Error resource download " + job.getName() + " from " + job.getSource().toString() + ": " + message);
|
||||
} catch (IOException ex) {
|
||||
job.setError(ex);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue