11package io .beanvortex .bitkip ;
22
33import io .beanvortex .bitkip .api .BatchService ;
4+ import io .beanvortex .bitkip .api .NewInstanceService ;
45import io .beanvortex .bitkip .api .SingleService ;
56import io .beanvortex .bitkip .api .SyncService ;
67import io .beanvortex .bitkip .exceptions .GlobalExceptionHandler ;
4142public class BitKip extends Application {
4243
4344 private static WebServer server ;
44-
45+ private static Stage stage ;
4546 @ Override
4647 public void start (Stage stage ) {
48+ BitKip .stage = stage ;
4749 IOUtils .readConfig ();
4850 AppConfigs .initPaths ();
4951 IOUtils .createSaveLocations ();
@@ -75,15 +77,19 @@ private static void trustAllConnections() {
7577 public X509Certificate [] getAcceptedIssuers () {
7678 return new X509Certificate [0 ];
7779 }
78- public void checkClientTrusted (X509Certificate [] certs , String authType ) {}
79- public void checkServerTrusted (X509Certificate [] certs , String authType ) {}
80+
81+ public void checkClientTrusted (X509Certificate [] certs , String authType ) {
82+ }
83+
84+ public void checkServerTrusted (X509Certificate [] certs , String authType ) {
85+ }
8086 }
8187 };
8288 SSLContext sc = SSLContext .getInstance ("TLS" );
8389 sc .init (null , trustAllCerts , new SecureRandom ());
8490 HttpsURLConnection .setDefaultSSLSocketFactory (sc .getSocketFactory ());
8591
86- HttpsURLConnection .setDefaultHostnameVerifier ((hostname , session ) -> true );
92+ HttpsURLConnection .setDefaultHostnameVerifier ((_ , _ ) -> true );
8793 } catch (Exception e ) {
8894 throw new RuntimeException (e );
8995 }
@@ -110,13 +116,16 @@ private void initTray(Stage stage) {
110116 var popup = new PopupMenu ();
111117 var openItem = new MenuItem ("Open App" );
112118 var exitItem = new MenuItem ("Exit App" );
113- ActionListener openListener = e -> Platform .runLater (() -> {
119+ ActionListener openListener = _ -> Platform .runLater (() -> {
114120 if (stage .isShowing ())
115121 stage .toFront ();
116122 else stage .show ();
117123 });
124+ if (hideOnStart )
125+ stage .close ();
126+
118127 openItem .addActionListener (openListener );
119- exitItem .addActionListener (e -> stop ());
128+ exitItem .addActionListener (_ -> stop ());
120129 popup .add (openItem );
121130 popup .add (exitItem );
122131 var trayIcon = new TrayIcon (image , "BitKip" , popup );
@@ -136,7 +145,8 @@ private void initTray(Stage stage) {
136145 @ Override
137146 public void stop () {
138147 var notObservedDms = new ArrayList <>(currentDownloadings );
139- notObservedDms .forEach (dm -> dm .getDownloadTask ().pause (()->{}));
148+ notObservedDms .forEach (dm -> dm .getDownloadTask ().pause (() -> {
149+ }));
140150 startedQueues .clear ();
141151 currentSchedules .values ().forEach (sm -> {
142152 var startScheduler = sm .getStartScheduler ();
@@ -174,6 +184,7 @@ private static void startServer() {
174184 .register ("/single" , cors , new SingleService ())
175185 .register ("/batch" , cors , new BatchService ())
176186 .register ("/sync" , cors , new SyncService ())
187+ .register ("/new_instance" , cors , new NewInstanceService ())
177188 .build ();
178189 var jacksonSupport = JacksonSupport .create ();
179190 server = WebServer .builder ()
@@ -182,7 +193,12 @@ private static void startServer() {
182193 .addRouting (routing )
183194 .addMediaSupport (jacksonSupport )
184195 .build ();
196+
185197 server .start ().exceptionally (throwable -> {
198+ if (InstanceManager .notifyCurrentInstance ()){
199+ Platform .exit ();
200+ return null ;
201+ }
186202 var header = "Failed to start server. Is there another instance running?\n If not you may need to change application server port and restart" ;
187203 log .error (header );
188204 Platform .runLater (() -> {
@@ -198,4 +214,8 @@ private static void startServer() {
198214 public static URL getResource (String path ) {
199215 return BitKip .class .getResource (path );
200216 }
217+
218+ public static void show (){
219+ stage .show ();
220+ }
201221}
0 commit comments