Skip to content

Commit be1afc4

Browse files
committed
Added dependency checker helper
1 parent 2d93b35 commit be1afc4

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package demo;
2+
3+
import lombok.SneakyThrows;
4+
import lombok.val;
5+
import org.simplejavamail.email.EmailBuilder;
6+
import org.simplejavamail.internal.util.MiscUtil;
7+
import testutil.ModuleLoaderTestHelper;
8+
9+
import java.awt.*;
10+
import java.awt.image.BufferedImage;
11+
import java.io.IOException;
12+
import java.net.HttpURLConnection;
13+
import java.net.URL;
14+
15+
import static java.lang.String.format;
16+
import static org.bbottema.genericobjectpool.util.SleepUtil.sleep;
17+
18+
public class DependencyChecker extends DemoAppBase {
19+
20+
static {
21+
// make Simple Java Mail ignore the batch module, so the JVM is never blocked from shutting down (because of the connection pool)
22+
ModuleLoaderTestHelper._forceDisableBatchModule();
23+
}
24+
25+
public static void main(String[] args) throws IOException {
26+
val group = MiscUtil.checkArgumentNotEmpty(args[0], "arg 0 should be the group id");
27+
val artifact = MiscUtil.checkArgumentNotEmpty(args[1], "arg 1 should be the artifact id");
28+
val version = MiscUtil.checkArgumentNotEmpty(args[2], "arg 2 should be the version");
29+
30+
val urlString = format("https://repo1.maven.org/maven2/%s/%s/%s", group.replaceAll("\\W", "/"), artifact, version);
31+
HttpURLConnection connection = null;
32+
33+
System.out.println("Checking URL: " + urlString);
34+
35+
while (true) {
36+
try {
37+
connection = (HttpURLConnection) new URL(urlString).openConnection();
38+
connection.setRequestMethod("HEAD");
39+
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
40+
String dependency = group + ":" + artifact + ":" + version;
41+
mailerTLSBuilder
42+
.withDebugLogging(false)
43+
.buildMailer()
44+
.sendMail(EmailBuilder.startingBlank()
45+
.from("Dependency Checker", "[email protected]")
46+
.to(YOUR_GMAIL_ADDRESS)
47+
.withSubject("Dependency available: " + dependency)
48+
.withPlainText(format("The dependency '%s' is available at %s", dependency, urlString))
49+
.buildEmail());
50+
51+
// Windows 10 notification (lower right corner) - requires Windows 10 and the Windows 10 JDK:
52+
val notificationTitle = showOSNotification("Notification Title", format("The dependency '%s' is available", dependency));
53+
sleep(10_000);
54+
SystemTray.getSystemTray().remove(notificationTitle);
55+
break;
56+
}
57+
} catch (IOException e) {
58+
System.err.println("Error while checking the URL: " + e.getMessage());
59+
} finally {
60+
if (connection != null) {
61+
connection.disconnect();
62+
}
63+
}
64+
65+
sleep(1000);
66+
}
67+
}
68+
69+
@SneakyThrows
70+
public static TrayIcon showOSNotification(String notificationTitle, String msg) {
71+
TrayIcon trayIcon = new TrayIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), "Notification Helper");
72+
SystemTray.getSystemTray().add(trayIcon);
73+
trayIcon.displayMessage(notificationTitle, msg, TrayIcon.MessageType.INFO);
74+
return trayIcon;
75+
}
76+
}

0 commit comments

Comments
 (0)