From 3e6db3cbe457f01a6faae4ed5ad3d6be4920056e Mon Sep 17 00:00:00 2001 From: dzmipt Date: Fri, 13 Sep 2024 17:55:26 +0200 Subject: [PATCH] Migrate to Java 9. From now on, minimum Java 9 is minimum requirement --- .github/workflows/gradle.yml | 4 ++-- build.gradle | 4 ++-- src/studio/core/Studio.java | 44 ++++++++---------------------------- test/kx/KServerMock.java | 35 ++++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 3347eb1c..3e9ff27a 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -18,10 +18,10 @@ jobs: - name: Checkout uses: actions/checkout@v2 - - name: Set up JDK 8 + - name: Set up JDK 9 uses: actions/setup-java@v2 with: - java-version: '8' + java-version: '9' distribution: 'adopt' cache: gradle - name: Grant execute permission for gradlew diff --git a/build.gradle b/build.gradle index 6f5d0e6a..4d832b89 100644 --- a/build.gradle +++ b/build.gradle @@ -13,8 +13,8 @@ archivesBaseName = "kdbStudio" version = "dz4.1" compileJava { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 +` sourceCompatibility = JavaVersion.VERSION_1_9 + targetCompatibility = JavaVersion.VERSION_1_9 } repositories { diff --git a/src/studio/core/Studio.java b/src/studio/core/Studio.java index 4218289c..20faf47a 100755 --- a/src/studio/core/Studio.java +++ b/src/studio/core/Studio.java @@ -21,7 +21,6 @@ import java.io.PrintStream; import java.lang.ref.Reference; import java.lang.reflect.Field; -import java.lang.reflect.Proxy; import java.util.List; import java.util.*; @@ -83,45 +82,22 @@ private static void registerForMacOSMenuJava8() throws Exception { } private static void registerForMacOSMenuJava9() throws Exception { - // Using reflection to be compilable on Java8 - - Class settingsClass = Class.forName("java.awt.desktop.PreferencesHandler"); - Class quitClass = Class.forName("java.awt.desktop.QuitHandler"); - Class aboutClass = Class.forName("java.awt.desktop.AboutHandler"); - - Object handler = Proxy.newProxyInstance(Studio.class.getClassLoader(), new Class[]{settingsClass, quitClass, aboutClass}, - (o, method, objects) -> { - String name = method.getName(); - if (name.equals("handlePreferences")) StudioWindow.settings(); - else if (name.equals("handleAbout")) StudioWindow.about(); - else if (name.equals("handleQuitRequestWith")) { - StudioWindow.quit(); - try { - Class quiteResponseClass = Class.forName("java.awt.desktop.QuitResponse"); - quiteResponseClass.getDeclaredMethod("cancelQuit").invoke(objects[1]); - } catch (Exception e) { - log.error("Error in cancelQuit()", e); - } - } - return null; - } - ); - - Class desktopClass = Class.forName("java.awt.Desktop"); - Object desktop = desktopClass.getMethod("getDesktop").invoke(desktopClass); - - desktopClass.getMethod("setPreferencesHandler", settingsClass).invoke(desktop, handler); - desktopClass.getMethod("setQuitHandler", quitClass).invoke(desktop, handler); - desktopClass.getMethod("setAboutHandler", aboutClass).invoke(desktop, handler); + Desktop desktop = Desktop.getDesktop(); + desktop.setPreferencesHandler(e -> StudioWindow.settings()); + desktop.setAboutHandler(e -> StudioWindow.about()); + desktop.setQuitHandler( (e,response) -> { + StudioWindow.quit(); + response.cancelQuit(); + }); } private static void registerForMacOSMenu() { if (!Util.MAC_OS_X) return; try { - if (Util.Java8Minus) registerForMacOSMenuJava8(); - else registerForMacOSMenuJava9(); - +// if (Util.Java8Minus) registerForMacOSMenuJava8(); +// else registerForMacOSMenuJava9(); + registerForMacOSMenuJava9(); macOSSystemMenu = true; } catch (Exception e) { log.error("Failed to set MacOS handlers", e); diff --git a/test/kx/KServerMock.java b/test/kx/KServerMock.java index 5e31d13a..7f374d93 100644 --- a/test/kx/KServerMock.java +++ b/test/kx/KServerMock.java @@ -4,12 +4,15 @@ import org.apache.logging.log4j.Logger; import studio.kdb.K; +import javax.net.ServerSocketFactory; +import javax.net.ssl.SSLServerSocketFactory; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; @@ -27,7 +30,11 @@ public KServerMock() throws IOException { } public KServerMock(int port) throws IOException { - serverSocket = new ServerSocket(port); + this(ServerSocketFactory.getDefault(), port); + } + + public KServerMock(ServerSocketFactory ssFactory, int port) throws IOException { + serverSocket = ssFactory.createServerSocket(port); this.port = serverSocket.getLocalPort(); new Thread(this, "Server listen port " + port).start(); log.info("Server started on port {}", this.port); @@ -170,7 +177,31 @@ public void shutdown() { } } - public static void main(String... args) throws IOException { + public static void main(String... args) throws IOException, NoSuchAlgorithmException { new KServerMock(2345); + +// SSLContext sslContext = SSLContext.getInstance("SSL"); //getDefault(); + +// new KServerMock(sslContext.getServerSocketFactory(), 2346); + + new KServerMock(SSLServerSocketFactory.getDefault(), 2346); +// +// TrustManager allAcceptTrustManager = new X509TrustManager() { +// @Override +// public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { +// log.info("checkClientTrusted for authType {}; with chain length {}", authType, chain.length); +// } +// +// @Override +// public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { +// log.info("checkServerTrusted for authType {}; with chain length {}", authType, chain.length); +// } +// +// @Override +// public X509Certificate[] getAcceptedIssuers() { +// return null; +// } +// }; + } }