|
33 | 33 |
|
34 | 34 | import java.io.File; |
35 | 35 | import java.io.IOException; |
| 36 | +import java.io.UnsupportedEncodingException; |
| 37 | +import java.security.MessageDigest; |
| 38 | +import java.security.NoSuchAlgorithmException; |
36 | 39 |
|
37 | 40 | import org.scijava.app.AppService; |
38 | 41 | import org.scijava.command.CommandService; |
@@ -61,6 +64,7 @@ public class DefaultWelcomeService extends AbstractService implements |
61 | 64 | { |
62 | 65 |
|
63 | 66 | private final static String WELCOME_FILE = "WELCOME.md"; |
| 67 | + private final static String CHECKSUM_PREFS_KEY = "checksum"; |
64 | 68 |
|
65 | 69 | @Parameter |
66 | 70 | private LogService log; |
@@ -89,6 +93,10 @@ public void displayWelcome() { |
89 | 93 | try { |
90 | 94 | if (welcomeFile.exists()) { |
91 | 95 | final String welcomeText = textService.asHTML(welcomeFile); |
| 96 | + final String checksum = getChecksum(welcomeText); |
| 97 | + final String previousChecksum = Prefs.get(getClass(), CHECKSUM_PREFS_KEY); |
| 98 | + if (checksum.equals(previousChecksum)) return; |
| 99 | + Prefs.put(getClass(), CHECKSUM_PREFS_KEY, checksum); |
92 | 100 | displayService.createDisplay(welcomeText); |
93 | 101 | } |
94 | 102 | } |
@@ -126,4 +134,32 @@ private String firstRunPrefKey() { |
126 | 134 | return "firstRun-" + appService.getApp().getVersion(); |
127 | 135 | } |
128 | 136 |
|
| 137 | + // get digest of the file as according to fullPath |
| 138 | + private String getChecksum(final String text) |
| 139 | + { |
| 140 | + try { |
| 141 | + final MessageDigest digest = MessageDigest.getInstance("SHA-1"); |
| 142 | + digest.update(text.getBytes("UTF-8")); |
| 143 | + return toHex(digest.digest()); |
| 144 | + } |
| 145 | + catch (NoSuchAlgorithmException e) { |
| 146 | + return "" + text.hashCode(); |
| 147 | + } |
| 148 | + catch (UnsupportedEncodingException e) { |
| 149 | + return "" + text.hashCode(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private final static char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', |
| 154 | + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 155 | + |
| 156 | + private String toHex(final byte[] bytes) { |
| 157 | + final char[] buffer = new char[bytes.length * 2]; |
| 158 | + for (int i = 0; i < bytes.length; i++) { |
| 159 | + buffer[i * 2] = hex[(bytes[i] & 0xf0) >> 4]; |
| 160 | + buffer[i * 2 + 1] = hex[bytes[i] & 0xf]; |
| 161 | + } |
| 162 | + return new String(buffer); |
| 163 | + } |
| 164 | + |
129 | 165 | } |
0 commit comments