Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 1 | package aQute.bnd.build; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | import aQute.bnd.service.*; |
| 6 | import aQute.service.reporter.*; |
| 7 | |
| 8 | /** |
| 9 | * This class is intended to be used by the users of a {@link RepositoryPlugin}. |
| 10 | * The |
| 11 | * {@link RepositoryPlugin#get(String, aQute.bnd.version.Version, java.util.Map, aQute.bnd.service.RepositoryPlugin.DownloadListener...)} |
| 12 | * method takes one or more Download Listeners. These are called back with the |
| 13 | * success or failure of a download. This class is a simple implementation of |
| 14 | * this model, just call {@link #getReason()} and it blocks until success or |
| 15 | * failure is called. |
| 16 | */ |
| 17 | public class DownloadBlocker implements RepositoryPlugin.DownloadListener { |
| 18 | public enum Stage { |
| 19 | INIT, SUCCESS, FAILURE |
| 20 | }; |
| 21 | |
| 22 | private volatile Stage stage = Stage.INIT; |
| 23 | private String failure; |
| 24 | private File file; |
| 25 | private final Reporter reporter; |
| 26 | |
| 27 | public DownloadBlocker(Reporter reporter) { |
| 28 | this.reporter = reporter; |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * (non-Javadoc) |
| 33 | * @see |
| 34 | * aQute.bnd.service.RepositoryPlugin.DownloadListener#success(java.io.File) |
| 35 | */ |
| 36 | public void success(File file) throws Exception { |
| 37 | synchronized (this) { |
| 38 | assert stage == Stage.INIT; |
| 39 | stage = Stage.SUCCESS; |
| 40 | this.file = file; |
| 41 | notifyAll(); |
| 42 | } |
| 43 | if (reporter != null) |
| 44 | reporter.trace("successfully downloaded %s", file); |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * (non-Javadoc) |
| 49 | * @see |
| 50 | * aQute.bnd.service.RepositoryPlugin.DownloadListener#failure(java.io.File, |
| 51 | * java.lang.String) |
| 52 | */ |
| 53 | public void failure(File file, String reason) throws Exception { |
| 54 | synchronized (this) { |
| 55 | assert stage == Stage.INIT; |
| 56 | stage = Stage.FAILURE; |
| 57 | this.failure = reason; |
| 58 | this.file = file; |
| 59 | notifyAll(); |
| 60 | } |
| 61 | if (reporter != null) |
| 62 | reporter.error("Download %s %s", reason, file); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * (non-Javadoc) |
| 67 | * @see |
| 68 | * aQute.bnd.service.RepositoryPlugin.DownloadListener#progress(java.io. |
| 69 | * File, int) |
| 70 | */ |
| 71 | public boolean progress(File file, int percentage) throws Exception { |
| 72 | assert stage == Stage.INIT; |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Return a failure reason or null. This method will block until either |
| 78 | * {@link #success(File)} or {@link #failure(File, String)} has been called. |
| 79 | * It can be called many times. |
| 80 | * |
| 81 | * @return null or a reason for a failure |
| 82 | */ |
| 83 | public synchronized String getReason() { |
| 84 | try { |
| 85 | while (stage == Stage.INIT) |
| 86 | wait(); |
| 87 | } |
| 88 | catch (InterruptedException e) { |
| 89 | return "Interrupted"; |
| 90 | } |
| 91 | |
| 92 | return failure; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Return the stage we're in |
| 97 | * |
| 98 | * @return the current stage |
| 99 | */ |
| 100 | public Stage getStage() { |
| 101 | return stage; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * (non-Javadoc) |
| 106 | * @see java.lang.Object#toString() |
| 107 | */ |
| 108 | public String toString() { |
| 109 | return "DownloadBlocker(" + stage + "," + file + ", " + failure + ")"; |
| 110 | } |
| 111 | } |